diff --git a/packages/cultmath/src/CultMath/BoundedLeastSquares.cs b/packages/cultmath/src/CultMath/BoundedLeastSquares.cs index 4cfbad74..343ad60e 100644 --- a/packages/cultmath/src/CultMath/BoundedLeastSquares.cs +++ b/packages/cultmath/src/CultMath/BoundedLeastSquares.cs @@ -31,9 +31,11 @@ public enum BoundedLeastSquaresStatus /// inside the box. /// /// -/// Stopping. KKT termination uses a tolerance of 1e-6 times the problem's gradient scale (the largest gradient -/// component at the box point nearest the origin), so scaling A, b or the bounds, and the choice of warm start, do -/// not change it. The tolerance is only a resolution: once a stationarity miss or a +/// Stopping. KKT termination uses a tolerance of (1e-6) times the problem's +/// gradient scale (the largest gradient component at the box point nearest the origin), so scaling A, b or the +/// bounds, and the choice of warm start, do not change it. A caller whose rows span many decades of weight can pass +/// a smaller kktRelativeTolerance, which changes only this tolerance: the progress rule below and the pivot +/// threshold are fixed. The tolerance is only a resolution: once a stationarity miss or a /// bound release has been followed by a pass that lowers the cost by no more than rounding, the solver reports /// , so an optimal point whose double gradient sits above the /// tolerance costs one confirming iteration rather than the iteration cap. @@ -48,7 +50,7 @@ public enum BoundedLeastSquaresStatus /// /// The solver allocates nothing and is deterministic. /// -/// Input contract: a and b must be finite; lo and hi must not be NaN and need +/// Input contract: kktRelativeTolerance must be finite and greater than zero; a and b must be finite; lo and hi must not be NaN and need /// lo <= hi. Infinite bounds are allowed and mean unbounded on that side (lo = -Inf, /// hi = +Inf); lo = +Inf or hi = -Inf is invalid. Any violation returns /// without throwing, with iterations 0 and @@ -62,8 +64,10 @@ public static class BoundedLeastSquares /// Default iteration cap for . public const int DefaultMaxIterations = 100; + /// Default KKT tolerance, relative to the gradient scale; see Stopping in the type remarks. + public const double DefaultKktRelativeTolerance = 1e-6; + private const double PivotRelativeTolerance = 1e-12; - private const double KktRelativeTolerance = 1e-6; private const double StallRelativeTolerance = 1e-15; /// Floats of caller-supplied workspace needed for columns. @@ -90,6 +94,33 @@ public static class BoundedLeastSquares Span x, Span workspace, out int iterations, int maxIterations = DefaultMaxIterations) + => Solve(m, n, a, b, lo, hi, x, workspace, out iterations, DefaultKktRelativeTolerance, maxIterations); + + /// + /// Minimises ||A x - b||^2 subject to lo <= x <= hi. is the warm start + /// on entry (clamped to the box first) and the result on exit. is the + /// stationarity resolution relative to the gradient scale defined under Stopping; the overload without it passes + /// and gives the same result. + /// + /// Rows of . + /// Columns of , and length of , and . + /// Row-major m * n matrix. + /// Length m target. + /// Lower bounds; each must be <= hi; -Inf means unbounded below. + /// Upper bounds; +Inf means unbounded above. + /// Warm start in, solution out. + /// At least floats; contents on entry are ignored. + /// Active-set iterations used. + /// Finite and greater than zero, else . Tighten it when rows span many decades of weight; a value below the gradient's rounding noise is harmless because the progress rule ends the solve. + /// Iteration cap. + public static BoundedLeastSquaresStatus Solve( + int m, int n, + ReadOnlySpan a, ReadOnlySpan b, + ReadOnlySpan lo, ReadOnlySpan hi, + Span x, Span workspace, + out int iterations, + double kktRelativeTolerance, + int maxIterations = DefaultMaxIterations) { if (m < 0 || n < 0 || a.Length < m * n || b.Length < m || lo.Length < n || hi.Length < n || x.Length < n || workspace.Length < WorkspaceLength(n)) @@ -113,6 +144,8 @@ public static class BoundedLeastSquares var dependent = Take(ref ints, n); iterations = 0; + if (!(kktRelativeTolerance > 0.0) || double.IsPositiveInfinity(kktRelativeTolerance)) + return BoundedLeastSquaresStatus.InvalidInput; for (var j = 0; j < n; j++) if (!(lo[j] <= hi[j]) || float.IsPositiveInfinity(lo[j]) || float.IsNegativeInfinity(hi[j])) return BoundedLeastSquaresStatus.InvalidInput; @@ -165,7 +198,7 @@ public static class BoundedLeastSquares s += ata[j * n + k] * Math.Clamp(0.0, lo[k], hi[k]); gScale = Math.Max(gScale, Math.Abs(s)); } - var kktTol = KktRelativeTolerance * gScale; + var kktTol = kktRelativeTolerance * gScale; // Cost at the last point from which the solver chose to continue past a stationarity miss or a // release. A pass that ends without lowering it has nothing left to gain: the tolerance is a