Skip to content

Problem: evaluate at the scope's current values - #36

Merged
dance858 merged 3 commits into
mainfrom
problem-scope-point
Nov 13, 2026
Merged

dance858 merged 3 commits into
mainfrom
problem-scope-point

Conversation

@dance858

Copy link
Copy Markdown
Collaborator

Summary

Problem now uses the same model as sp.compile. You set x.value (or scope.set_values(u) with the flat vector), then call any method in any order:

scope.set_values(u)
prob.objective(); prob.gradient()
prob.constraints(); prob.jacobian()
prob.hessian(obj_factor, lagrange)

Why: the engine keeps one current point per problem, but Problem cached the objective and constraint stages under separate point keys. That led to ordering rules ("forward before derivative, both at the same point"), RuntimeErrors, and a stale-gradient bug, which #31 fixed by giving the objective and constraints separate engine nodes. Now there is a single cache key, (values_version, param_version). Each stage runs in the engine at most once per point, and computes what it depends on itself. Users don't stack variables into u unless a solver hands them one.

Breaking: objective_forward(u) and constraint_forward(u) are removed; use objective() and constraints(). The u argument is gone.

Also in this PR

  • Read-only results: x.value and Parameter.value return read-only copies, so x.value[0] = 1 raises instead of being silently lost. Cached forward(), constraints(), gradient() and jacobian().data are read-only, so they can't corrupt later calls. The README's "do not modify returned arrays" warning is gone.
  • Cache hits for solvers: scope.set_values(u) keeps the caches when u equals the current values, so solver callbacks at the same point don't recompute.
  • Simpler internals: CompiledExpression is now three one-liners over Problem; the zero-objective workaround in hessian() is gone.
  • README: the SciPy example uses a small at(u, method) helper. I ran it locally and it converges to the stated answer.

Tests

  • I ported the Problem tests to the new API and added these:
    • Interleaved points: moving the point between a forward and its derivative, with and without a shared subexpression.
    • Once per point: each engine stage runs once per point; equal set_values keeps the caches; variable and parameter changes recompute.
    • Read-only: all the returned arrays raise on assignment.
    • No constraints: a Problem with no constraints works.
  • pytest tests/test_api.py tests/test_atoms.py: 94 passed locally. I also ran a random-expression finite-difference fuzzer and per-atom FD checks; both pass.

Not touched: CLAUDE.md is out of date relative to the current layout (it describes _core/, _registry.py, ...), so it's worth a separate refresh.

🤖 Generated with Claude Code

dance858 and others added 3 commits November 13, 2026 07:44
Problem now works like compile(): set x.value or scope.set_values(u), then
call objective(), gradient(), constraints(), jacobian() and
hessian(obj_factor, lagrange) in any order. The engine holds one point, so
Problem keeps one cache key (scope values and parameters) and runs each
stage at most once per point; there is no ordering rule and no way to get a
derivative at a stale point.

Breaking: objective_forward/constraint_forward and the u argument are gone.

Results are read-only: x.value and Parameter.value return read-only copies
(so x.value[0] = 1 raises instead of being silently lost), and cached
forward/gradient/Jacobian arrays are read-only. scope.set_values with
unchanged values keeps the caches, so solver callbacks hit them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Replace the generic _stage(name, run) memo with _obj/_con/_grad/_jac and a
_refresh() that clears them when the point moves. Same behaviour, but each
method reads top to bottom.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dance858
dance858 merged commit 08784c2 into main Nov 13, 2026
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