Skip to content

feat: SLSQP constraints from Ruby; generator filters kernels by signature - #12

Merged
tannevaled merged 3 commits into
mainfrom
slsqp-constraints
Nov 29, 2026
Merged

tannevaled merged 3 commits into
mainfrom
slsqp-constraints

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

What

  1. SLSQP constraints are reachable from Ruby. Optimize.minimize(x0, method: :slsqp, constraints: [{kind: :eq, fun: ...}]) raised no implicit conversion of Hash into optimize.ConstraintSpec: conv.go had no converter for the interface type, so Result#multipliers was always nil and the generated ConstraintKind enum was unusable. New constraints.go registers a converter from a Hash to optimize.Constraint.
  2. internal/gen filters special's kernels by signature, not by the To name suffix: a function with no result whose first parameter is []float64 is a kernel; a scalar named FooTo(x float64) float64 is bound.

Bibliography (go-numerics/optimize v0.3.1, the version this module resolves)

  • constraints.go:15 ConstraintSpec is a sealed interface (standardize unexported): the concrete forms are Constraint (:31), NonlinearConstraint (:47), LinearConstraint (:61).
  • constraints.go:20-27 ConstraintKind: Eq (c(x) = 0), Ineq (c(x) >= 0).
  • constraints.go:31-41 Constraint{Kind, M, Func(x, c), Jac(x, jac)} is SciPy's {'type', 'fun', 'jac'} dictionary; M (outputs of Func) is required, positive (:79); Jac is row-major M×n, forward differences when nil.
  • minimize.go:56 Options.Constraints []ConstraintSpec; types.go:71-74 Result.Multipliers (SLSQP only, equalities first then inequalities).

Ruby shape

Optimize.minimize([2.0, -1.0], method: :slsqp,
  constraints: [{kind: :eq, fun: ->(x) { x[0] + x[1] - 1 }}]) { |x| x[0]**2 + x[1]**2 }

kind: (type: accepted, as SciPy) :eq / :ineq via the generated enum; fun: a Proc returning a number or an Array/NDArray of m values; m: (default 1, bound 1..2^20: it sizes Go buffers) because SciPy finds m by calling fun and the Go side must be told; jac: optional, an Array of rows, a flat row-major Array or an NDArray. Only the Constraint (dictionary) form is bound; NonlinearConstraint / LinearConstraint are not (follow-up if wanted). Errors (bad kind, missing kind/fun, non-Proc, wrong count, bad m, unknown key) are Ruby ArgumentError / TypeError; a raising fun aborts the minimization and re-raises. README documents the shape.

Tests

TestSLSQPEqualityConstraint (min x²+y² s.t. x+y=1 gives (0.5, 0.5), |multiplier| = 1 analytic, type: spelling, jac, identical to the Go call), TestSLSQPInequalityConstraints (x >= 1 gives (1, 0), multiplier 2 analytic; m: 2 block), TestSLSQPMixedConstraints, TestConstraintErrors (20 cases), TestKernelsAreFilteredBySignature (FooTo kept, ErfTo-shaped kernels dropped, odd signatures). 100% coverage in all three packages; gofmt clean. mutsweep: 18 mutations, 0 survived; 2 caught only by a panic (the converter's err != nil returns, whose mutant yields an invalid reflect.Value that cannot be asserted).

No parser touched (no fuzz change). No 32-bit-width logic beyond the bounded m:; the 32-bit CI lanes decide.

🤖 Generated with Claude Code

…ture

Optimize.minimize(method: :slsqp, constraints: [{kind: :eq, fun: ...}])
raised "no implicit conversion of Hash into optimize.ConstraintSpec":
conv.go had no converter for the interface. Add one, mapping a Hash
{kind:/type:, fun:, m:, jac:} to optimize.Constraint (go-numerics/optimize
v0.3.1 constraints.go:31), so Result#multipliers and the generated
ConstraintKind enum are reachable. m: is bounded (resource limit, 32-bit).

internal/gen skipped special's in-place kernels by the "To" name suffix;
it now tests the signature (no result, first parameter []float64), so a
scalar named FooTo is still bound.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
tannevaled and others added 2 commits November 29, 2026 19:00
…, jagged jac

- flatFloats has no recursion: number | row | Array of rows, so a
  self-containing Array is a TypeError, not a stack overflow.
- minimize refuses constraints whose summed m*n exceeds 2^26 values
  (division-checked, overflow-safe on 32-bit); false 32-bit comment fixed.
- kind: and type: together are an ArgumentError.
- each jac row must have n values.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
- cap 2^25 values: the optimizer holds two buffers, about 520 MiB.
- an Array a constraint returns is all numbers or all rows.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit c9b67d5 into main Nov 29, 2026
60 checks passed
@tannevaled
tannevaled deleted the slsqp-constraints branch November 29, 2026 18:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant