Skip to content

Commit 1a80be1

Browse files
A set Settings.Budget caps lids and arguments, and replaces allowances (#2475)
Each BudgetLedger.For call site now declares a BudgetRole, and a set MathS.Settings.Budget does to it what that role calls for: - Allowance (SolveSystem's 10,000 steps / 60 s, Gröbner's 5 s): replaced, as before, so "raise MathS.Settings.Budget to allow more" stays true. - Lid (the perfect-square proof's 2,000 steps / 50 ms): the smaller limit on each axis, so a set budget, Unlimited included, never lifts it. - Argument (EqualitySaturation, CanonicalizationOverGraph, AreEqual): the argument is the allowance and the set budget caps it, so an explicit budget inside an Unlimited scope is spent as passed. The lid is tightened on both axes rather than the clock alone: a lid is never raised, so the incomparability of steps between algorithms does not bite, and a caller who set steps is not ignored. The proof completes in 15 to 1,203 steps over the unit suite (median 212) and a stopped proof falls back to the symbolic test; Factorize and Simplify answers were measured unchanged under unset, Unlimited, {Steps = 1} and {Steps = 1_000_000}. What moves: EqualitySaturation and CanonicalizationOverGraph with an explicit budget inside a scope set to Unlimited, measured against the 2.7.0 package; BREAKING-CHANGES.md has the rows. The four test-only ledgers pass their own budget, so they are Argument. Part of #2454 Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 423d3aa commit 1a80be1

17 files changed

Lines changed: 318 additions & 36 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,32 @@ returned as all of them. `MathS.Equations("y^2 - 2", "x^2 + (P - Q y) * x - 3").
252252

253253
---
254254

255+
### A set `MathS.Settings.Budget` caps a budget passed as an argument instead of replacing it
256+
257+
**Silent: the same call stops sooner.** A set `MathS.Settings.Budget` replaced every budget in the library, including one
258+
a caller passed by hand, so `EqualitySaturation(budget, ...)` and `CanonicalizationOverGraph(budget)` inside a scope set
259+
to `WorkBudget.Unlimited` ran unlimited. Now what a set budget does depends on what the budget it meets is for
260+
([#2454](https://github.com/asc-community/AngouriMath/issues/2454)): a budget passed as an argument runs under the smaller
261+
limit on each axis of it and the setting, so `Unlimited` leaves it as passed; the perfect-square proof that `Factorize`
262+
runs and `Simplify` offers keeps its own lid of 2,000 steps and 50 ms under the same rule, so a set budget only tightens
263+
it; and the system solver and its Gröbner path are replaced by the setting as before, so raising it still allows more.
264+
With `budget = new WorkBudget { Steps = 5, Time = TimeSpan.FromSeconds(30) }`, inside
265+
`MathS.Settings.Budget.Set(WorkBudget.Unlimited)`:
266+
267+
| Input | Was (2.7.0) | Now |
268+
|---|---|---|
269+
| `Transformation.EqualitySaturation(budget, CostModel.Default).ApplyOrKeep("x * 2 * 3 + x")` | `x * 7` | `x * 2 * 3 + x` |
270+
| `Transformation.EqualitySaturation(budget, CostModel.Default).ApplyOrKeep("2 * sqrt(2) * sqrt(3)")` | `2 * sqrt(6)` | `2 * sqrt(2) * sqrt(3)` |
271+
| `Transformation.CanonicalizationOverGraph(budget).ApplyOrKeep("a * b + b * a")` | `2 * a * b` | `a * b + a * b` |
272+
| `Transformation.CanonicalizationOverGraph(budget).ApplyOrKeep("x + 0 + 2 * x")` | `3 * x` | `2 * x + x` |
273+
274+
Unset, nothing moves. `Factorize` and `Simplify` give the same answers on 2.7.0 and now under an unset,
275+
an `Unlimited`, a `{ Steps = 1 }` and a `{ Steps = 1_000_000 }` budget, measured on `2 + 2 * sqrt(2) * sqrt(3) + 3`,
276+
`a + 2 * sqrt(a) * sqrt(b) + b` and `2 + 2 * sqrt(6) + 3`: a proof the lid stops is "not proved", and the symbolic test
277+
behind it still answers.
278+
279+
---
280+
255281
### An indexed union or intersection binds its index
256282

257283
**Silent: a bound name is no longer reported free.** `FreeVariables` of `union(A_k, k in I)` and `intersection(A_k, k in I)`

‎Sources/AngouriMath/Convenience/MathS.Equality.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ partial class MathS
2525
/// tried, a rule's match attempt, a node the graph grows by -- so the same question under
2626
/// the same steps gets the same answer on any machine; a <see cref="WorkBudget.Time"/>
2727
/// makes it depend on the machine, and <see cref="EqualityAnswer.Budget"/> says when it
28-
/// did. <see cref="Settings.Budget"/>, where set, overrides this.
28+
/// did. <see cref="Settings.Budget"/>, where set, caps this: the smaller limit on each axis
29+
/// applies, so an ambient <see cref="WorkBudget.Unlimited"/> leaves it as passed.
2930
/// </param>
3031
/// <param name="weakest">
3132
/// The weakest soundness a proof may rest on. <see cref="Soundness.Sound"/>, the default,

‎Sources/AngouriMath/Convenience/MathS.cs‎

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6732,11 +6732,40 @@ internal static EDecimal DowncastingTolerance
67326732
/// <para>
67336733
/// <b>Leaving this alone is not the same as setting it to
67346734
/// <see cref="WorkBudget.Unlimited"/>.</b> Unset, each algorithm keeps the budget
6735-
/// it chose for itself -- the Gr&#246;bner path bounds itself by five seconds and
6736-
/// by five structural ceilings, and did so before this setting existed. Set, this
6737-
/// replaces those defaults wherever a budget is honoured, so
6735+
/// it chose for itself. Set, what it does depends on what that budget is for
6736+
/// (<a href="https://github.com/asc-community/AngouriMath/issues/2454">#2454</a>):
6737+
/// </para>
6738+
/// <list type="bullet">
6739+
/// <item><b>A working allowance is replaced.</b> The system solver
6740+
/// (<see cref="Core.EquationSystem.Solve(Variable[])"/>: 10,000 steps and 60 seconds
6741+
/// over the whole call) and its Gr&#246;bner path (5 seconds) run under this budget
6742+
/// instead of their own, so this is how a caller raises them -- and
67386743
/// <see cref="WorkBudget.Unlimited"/> here means "do not stop", which for a
6739-
/// doubly-exponential algorithm means what it says.
6744+
/// doubly-exponential algorithm means what it says.</item>
6745+
/// <item><b>A lid can only be tightened.</b> The proof that a written-out perfect
6746+
/// square is one, which <see cref="Entity.Factorize(int)"/> runs and
6747+
/// <see cref="Entity.Simplify(int)"/> offers as a candidate, bounds itself by 2,000
6748+
/// steps and 50 milliseconds against a stall. It runs under the smaller limit on
6749+
/// each axis of that and this, so setting this never makes <c>Simplify</c> or
6750+
/// <c>Factorize</c> run longer. A proof stopped early is "not proved" and falls back
6751+
/// to the symbolic test, so tightening it costs time, not answers.</item>
6752+
/// <item><b>A budget passed as an argument is capped.</b>
6753+
/// <see cref="Core.Transformations.Transformation.EqualitySaturation(WorkBudget, Core.CostModel)"/>,
6754+
/// <see cref="Core.Transformations.Transformation.CanonicalizationOverGraph(WorkBudget)"/> and
6755+
/// <see cref="AreEqual(Entity, Entity, WorkBudget, Core.Transformations.Soundness, Core.Domain)"/>
6756+
/// spend the budget they were given, under the smaller limit on each axis of it and
6757+
/// this: the outer scope bounds the inner one, and
6758+
/// <see cref="WorkBudget.Unlimited"/> here leaves the argument as passed.</item>
6759+
/// </list>
6760+
/// <para>
6761+
/// <b>A budget names both axes.</b> <see cref="WorkBudget"/> cannot say "no opinion"
6762+
/// about one of them, so a <see cref="WorkBudget.Time"/> left <see langword="null"/>
6763+
/// is "no clock": <c>new WorkBudget { Steps = 1_000_000 }</c> raises the solver's
6764+
/// steps and also removes its 60 seconds. Against a lid or an argument the missing
6765+
/// clock is simply the looser limit, and theirs applies -- the lid's 50 milliseconds
6766+
/// included, which decide how soon the proof gives up and not the answer. Saying
6767+
/// "no opinion" per axis is a change to <see cref="WorkBudget"/> itself, left to v3
6768+
/// (<a href="https://github.com/asc-community/AngouriMath/issues/746">#746</a>).
67406769
/// </para>
67416770
/// <para>
67426771
/// A budget in <see cref="WorkBudget.Steps"/> is reproducible and one in
@@ -6747,17 +6776,10 @@ internal static EDecimal DowncastingTolerance
67476776
/// (<a href="https://github.com/asc-community/AngouriMath/issues/373">#373</a>)
67486777
/// </para>
67496778
/// <para>
6750-
/// It is honoured only where an algorithm asks. Today that is the system solver --
6751-
/// its Gr&#246;bner path, and the ledger <see cref="Core.EquationSystem.Solve(Variable[])"/>
6752-
/// keeps over the whole call --, the rewrite graph wherever it runs:
6753-
/// <see cref="Core.Transformations.Transformation.EqualitySaturation(WorkBudget, Core.CostModel)"/>,
6754-
/// <see cref="Core.Transformations.Transformation.CanonicalizationOverGraph(WorkBudget)"/> and
6755-
/// <see cref="AreEqual(Entity, Entity, WorkBudget, Core.Transformations.Soundness, Core.Domain)"/>, and the proof that a written-out perfect
6756-
/// square is one, which <see cref="Entity.Factorize(int)"/> runs and
6757-
/// <see cref="Entity.Simplify(int)"/> offers as a candidate. <c>Simplify</c> itself
6758-
/// is not bounded by it, and nor is anything else in the library, each of which
6759-
/// bounds itself by ceilings of its own that this does not reach; so setting it does
6760-
/// not make an arbitrary call bounded.
6779+
/// It is honoured only at the sites listed above. <c>Simplify</c> itself is not
6780+
/// bounded by it, and nor is anything else in the library, each of which bounds
6781+
/// itself by ceilings of its own that this does not reach; so setting it does not
6782+
/// make an arbitrary call bounded.
67616783
/// </para>
67626784
/// </remarks>
67636785
/// <example>

‎Sources/AngouriMath/Core/Budgets/BudgetLedger.cs‎

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,48 @@ private BudgetLedger(string where, WorkBudget limits)
6969

7070
/// <summary>
7171
/// A ledger for one computation. <paramref name="ownDefault"/> is what the algorithm
72-
/// bounds itself by when nobody has said otherwise; a caller who sets
73-
/// <see cref="MathS.Settings.Budget"/> replaces it.
72+
/// bounds itself by when nobody has set <see cref="MathS.Settings.Budget"/>;
73+
/// <paramref name="role"/> says what a set one does to it.
74+
/// https://github.com/asc-community/AngouriMath/issues/2454
7475
/// </summary>
75-
internal static BudgetLedger For(string where, WorkBudget ownDefault)
76-
=> new(where, MathS.Settings.Budget.IsOverriden ? MathS.Settings.Budget.Value : ownDefault);
76+
internal static BudgetLedger For(string where, WorkBudget ownDefault, BudgetRole role)
77+
=> new(where, Limits(ownDefault, role));
78+
79+
/// <summary>The limits a computation of <paramref name="role"/> runs under.</summary>
80+
internal static WorkBudget Limits(WorkBudget ownDefault, BudgetRole role)
81+
{
82+
var setting = MathS.Settings.Budget;
83+
if (!setting.IsOverriden)
84+
return ownDefault;
85+
var set = setting.Value;
86+
// A lid is tightened on both axes, not on the clock alone, although a step means
87+
// something different to each algorithm. The incomparability is a reason not to
88+
// *raise* one algorithm's ceiling by a number chosen for another, and a lid is never
89+
// raised. Taking only the clock would ignore a caller who set steps -- the
90+
// reproducible axis -- at exactly the sites where they asked for less. And it is
91+
// cheap: the one lid, the perfect-square proof, completes in 15 to 1,203 steps over
92+
// the unit suite (median 212), and a proof stopped short is "not proved", which falls
93+
// back to the symbolic test -- so a budget below that costs time, and no answer of
94+
// Factorize or Simplify was measured to move under one.
95+
return role switch
96+
{
97+
BudgetRole.Allowance => set,
98+
BudgetRole.Lid or BudgetRole.Argument => Tighter(ownDefault, set),
99+
_ => throw new ArgumentOutOfRangeException(nameof(role), role, null),
100+
};
101+
}
102+
103+
/// <summary>
104+
/// The smaller limit on each axis: what both <paramref name="a"/> and
105+
/// <paramref name="b"/> allow. A missing <see cref="WorkBudget.Time"/> is no ceiling,
106+
/// so it yields to any clock.
107+
/// </summary>
108+
private static WorkBudget Tighter(WorkBudget a, WorkBudget b)
109+
=> new()
110+
{
111+
Steps = Math.Min(a.Steps, b.Steps),
112+
Time = a.Time is { } x && b.Time is { } y ? (x < y ? x : y) : a.Time ?? b.Time,
113+
};
77114

78115
/// <summary>
79116
/// Charges <paramref name="units"/> of work and answers whether the computation may
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// Copyright (c) 2019-2026 Angouri.
3+
// AngouriMath is licensed under MIT.
4+
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
5+
// Website: https://am.angouri.org.
6+
//
7+
8+
namespace AngouriMath.Core.Budgets
9+
{
10+
/// <summary>
11+
/// What a computation's own budget is for, which decides what a set
12+
/// <see cref="MathS.Settings.Budget"/> does to it. Declared where the ledger is made,
13+
/// <see cref="BudgetLedger.For"/>, so that each site says which it is.
14+
/// https://github.com/asc-community/AngouriMath/issues/2454
15+
/// </summary>
16+
internal enum BudgetRole
17+
{
18+
/// <summary>
19+
/// A guard against a stall inside an operation that is not about this computation
20+
/// at all -- the perfect-square proof <c>Factorize</c> runs. A set budget can only
21+
/// tighten it, so setting one never makes <c>Simplify</c> or <c>Factorize</c> run
22+
/// longer than they would unset.
23+
/// </summary>
24+
Lid,
25+
26+
/// <summary>
27+
/// The working allowance of an algorithm a caller asks for by name -- the system
28+
/// solver, the Gr&#246;bner path. A set budget replaces it, which is what lets a caller
29+
/// raise it; capping it instead would make raising it do nothing.
30+
/// </summary>
31+
Allowance,
32+
33+
/// <summary>
34+
/// A budget the caller passed as an argument. It is the allowance, and a set budget,
35+
/// being the outer scope, caps it: the smaller limit on each axis.
36+
/// </summary>
37+
Argument,
38+
}
39+
}

‎Sources/AngouriMath/Core/Budgets/WorkBudget.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ public sealed record WorkBudget
6262
/// <summary>
6363
/// No ceiling on either axis. This is what <see cref="MathS.Settings.Budget"/>
6464
/// reads as its default — but see that property: leaving it alone means each
65-
/// algorithm keeps its own default, and it is only a caller who sets it who gets
66-
/// this.
65+
/// algorithm keeps its own default, and setting it to this lifts only the working
66+
/// allowances, never a lid.
6767
/// </summary>
6868
public static WorkBudget Unlimited { get; } = new();
6969
}

‎Sources/AngouriMath/Core/Transformations/EqualityOracle.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ internal static EqualityAnswer Ask(Entity left, Entity right, WorkBudget budget,
203203
throw new ArgumentOutOfRangeException(nameof(domain), domain,
204204
"Equality is asked over the complex or the real numbers.");
205205

206-
var ledger = BudgetLedger.For(Where, budget);
206+
// The caller's argument, capped by a set MathS.Settings.Budget.
207+
var ledger = BudgetLedger.For(Where, budget, BudgetRole.Argument);
207208
try
208209
{
209210
if (left.Equals(right))

‎Sources/AngouriMath/Core/Transformations/Saturation.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,9 @@ internal static bool ProvesEqual(Entity left, Entity right,
270270
var b = graph.AddEntity(right);
271271
graph.Rebuild();
272272

273-
var ledger = BudgetLedger.For(nameof(ProvesEqual), budget);
273+
// A lid: the one caller passes a guard against stalls, not an allowance a user
274+
// chose, so a set MathS.Settings.Budget can only tighten it.
275+
var ledger = BudgetLedger.For(nameof(ProvesEqual), budget, BudgetRole.Lid);
274276
Run(graph, rules, ledger, CostModel.Default.Cost);
275277
ledger.Report();
276278
return graph.Find(a) == graph.Find(b);

‎Sources/AngouriMath/Core/Transformations/Transformation.Catalogue.cs‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -441,9 +441,9 @@ private static class PolynomialFactorizationHolder
441441
/// <param name="budget">
442442
/// What this call may spend before it settles for the best it has found so far.
443443
/// <see cref="WorkBudget.Steps"/> is charged once per e-node the graph actually creates;
444-
/// <see cref="WorkBudget.Time"/> is the wall-clock backstop. A caller who sets
445-
/// <see cref="MathS.Settings.Budget"/> overrides this, the same as every other bounded
446-
/// computation in the library.
444+
/// <see cref="WorkBudget.Time"/> is the wall-clock backstop. A set
445+
/// <see cref="MathS.Settings.Budget"/> caps this, the smaller limit on each axis
446+
/// applying, and does not replace it.
447447
/// </param>
448448
/// <param name="costModel">Which candidate counts as cheapest once exploration stops.</param>
449449
/// <remarks>
@@ -770,7 +770,9 @@ internal EqualitySaturationTransformation(WorkBudget budget, CostModel costModel
770770
var root = graph.AddEntity(input);
771771
graph.Rebuild();
772772

773-
var ledger = BudgetLedger.For(Name, budget);
773+
// The caller passed this budget, so it is theirs to spend and a set
774+
// MathS.Settings.Budget, the outer scope, caps it.
775+
var ledger = BudgetLedger.For(Name, budget, BudgetRole.Argument);
774776
// Stop once the answer has: after two passes in which the cheapest member of the
775777
// root's class did not change, further merging can only add members this
776778
// extraction would not choose. That is the fixed point a caller who extracts
@@ -829,7 +831,8 @@ internal GraphCanonicalizationTransformation(WorkBudget budget, RewriteRuleGrowt
829831
var root = graph.AddEntity(input);
830832
graph.Rebuild();
831833

832-
var ledger = BudgetLedger.For(Name, budget);
834+
// An argument, as in EqualitySaturationTransformation.
835+
var ledger = BudgetLedger.For(Name, budget, BudgetRole.Argument);
833836
// The same stop as EqualitySaturationTransformation's, on the least member
834837
// rather than the cheapest, since that is what this extracts.
835838
Entity? last = null;

‎Sources/AngouriMath/Functions/Algebra/Groebner/Buchberger.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ internal sealed class GroebnerBudget
5151
[ConstantField]
5252
internal static readonly WorkBudget Default = new() { Time = TimeSpan.FromSeconds(5) };
5353

54-
private readonly BudgetLedger ledger = BudgetLedger.For("Gröbner", Default);
54+
// An allowance: a set MathS.Settings.Budget replaces Default, so a caller can raise it.
55+
private readonly BudgetLedger ledger = BudgetLedger.For("Gröbner", Default, BudgetRole.Allowance);
5556

5657
/// <summary>
5758
/// Charges one unit of work and answers whether to carry on. Called where the

0 commit comments

Comments
 (0)