Repository navigation
Problem: evaluate at the scope's current values - #36
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Problemnow uses the same model assp.compile. You setx.value(orscope.set_values(u)with the flat vector), then call any method in any order:Why: the engine keeps one current point per problem, but
Problemcached 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 intouunless a solver hands them one.Breaking:
objective_forward(u)andconstraint_forward(u)are removed; useobjective()andconstraints(). Theuargument is gone.Also in this PR
x.valueandParameter.valuereturn read-only copies, sox.value[0] = 1raises instead of being silently lost. Cachedforward(),constraints(),gradient()andjacobian().dataare read-only, so they can't corrupt later calls. The README's "do not modify returned arrays" warning is gone.scope.set_values(u)keeps the caches whenuequals the current values, so solver callbacks at the same point don't recompute.CompiledExpressionis now three one-liners overProblem; the zero-objective workaround inhessian()is gone.at(u, method)helper. I ran it locally and it converges to the stated answer.Tests
set_valueskeeps the caches; variable and parameter changes recompute.Problemwith 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.mdis out of date relative to the current layout (it describes_core/,_registry.py, ...), so it's worth a separate refresh.🤖 Generated with Claude Code