pysolver_view.optimization.problems.root_adapter module

pysolver_view.optimization.problems.root_adapter module#

Present existing residual equations as a zero-objective feasibility problem.

pysolver_view.optimization.problems.root_adapter.as_optimization_problem(problem, *, n_variables, n_equations=None, bounds=None)[source]#

Adapt a nonlinear equation system to equality-constrained optimization.

Return a new OptimizationProblem representing minimize 0 subject to problem.residual(x) == 0. Every residual becomes one equality constraint, in its original order and units. This lets an existing root-finding model use an equality-capable OptimizationSolver backend without changing the model class or the root/optimization contracts. It does not minimize the sum of squared residuals.

Parameters:
problemNonlinearSystemProblem or object with a callable residual

Existing model exposing residual(x) with shape (n_equations,). A scalar residual is also accepted for a single equation. An optional callable gradient(x) supplies the dense residual Jacobian with shape (n_equations, n_variables). For a single equation, a gradient of shape (n_variables,) is accepted, as is a scalar for one variable. The adapter keeps a reference to this model; it does not copy the model or assign optimization attributes to it.

n_variablesint

Positive number of decision variables. Explicit dimensions avoid a preliminary model evaluation and allow conversion before choosing an initial guess.

n_equationsint, optional

Positive number of residual equations. Defaults to n_variables for square systems. Specify it explicitly for rectangular systems. Shapes are checked whenever the residual or analytic Jacobian is requested.

boundstuple of array_like, optional

(lower, upper) variable bounds in the original model coordinates, following the package’s optimization convention. Each side may be a scalar or a vector of length n_variables. Omission gives unbounded variables. Bounds are copied and must satisfy lower <= upper; NaN, positive-infinite lower, and negative-infinite upper bounds are rejected. No bounds are inferred from the wrapped model.

Returns:
OptimizationProblem

Wrapper whose fitness(x) returns [0, *problem.residual(x)], get_nec() returns n_equations, and get_nic() returns zero. If the model provides an analytic Jacobian, gradient(x) returns that Jacobian with a leading zero objective-gradient row. Otherwise the wrapper has no gradient method, allowing the optimizer’s existing numerical differentiation and probe accounting to operate.

Raises:
TypeError

If problem.residual is not callable.

ValueError

If dimensions or bounds are invalid, or a later model evaluation has an incompatible shape.

Notes

Conversion performs no residual or Jacobian evaluations. Each subsequent fitness request makes exactly one residual call, and each analytic gradient request makes exactly one Jacobian call, without evaluating residuals. There is no adapter cache or hidden numerical differentiation. Consequently optimizer model calls correspond directly to wrapped residual calls, including numerical probes. Work inside the model’s own Jacobian is still outside the optimizer’s observable residual-call accounting.

Input and returned arrays are detached. Complex perturbations are preserved for complex-step differentiation. DomainError and other model exceptions propagate unchanged; the selected optimizer determines whether recovery is supported. Mutable internal model state remains owned by the caller.

No residual weighting or variable rescaling is added. Optional variable_names are copied, and optimizer scaling belongs to the wrapper, leaving attributes on the original model unchanged. The wrapper initially uses physical coordinates (problem_scale=None). If bound-based scaling is enabled on OptimizationSolver, its existing scaling rules still apply.

A zero objective gives no preference between feasible roots. Its zero gradient is not evidence that the equations are solved: check the final original residual against an explicit root tolerance independently of the optimizer’s status. Residual norms and equality-feasibility criteria can use different norms/tolerances. An inconsistent system remains infeasible.

Backend restrictions still apply. In particular, SLSQP cannot handle more equalities than variables and can fail on dependent equality Jacobian rows. The custom SLSQP backend also requires fixed variables to be eliminated. The adapter represents rectangular systems but does not remove redundant equations or select a numerical solver for them.

Examples

Convert a two-equation model without evaluating it during construction:

>>> class Equations:
...     def residual(self, x):
...         return np.array([x[0] ** 2 - 1, x[1] - 2])
...     def gradient(self, x):
...         return np.array([[2 * x[0], 0], [0, 1]])
>>> adapted = as_optimization_problem(Equations(), n_variables=2)
>>> adapted.fitness(np.array([1.0, 2.0]))
array([0., 0., 0.])

Pass adapted to OptimizationSolver with method="slsqp" and an appropriate backend, then independently evaluate the original residual at the returned point. Such a post-solve audit is outside solve call counters.