archimedes.nlp_solverΒΆ
- archimedes.nlp_solver(obj: Callable, constr: Callable | None = None, static_argnames: str | Sequence[str] | None = None, constrain_x: bool = False, name: str | None = None, method: str = 'ipopt', **options) FunctionCache ΒΆ
Create a reusable solver for a nonlinear optimization problem.
This function transforms an objective function and optional constraint function into an efficient solver for nonlinear programming problems of the form:
minimize f(x, p) subject to lbx <= x <= ubx lbg <= g(x, p) <= ubg
where
x
represents decision variables andp
represents parameters.- Parameters:
obj (callable) β Objective function to minimize with signature
obj(x, *args)
. Must return a scalar value.constr (callable, optional) β Constraint function with signature
constr(x, *args)
. Must return a vector of constraint values where the constraints are interpreted aslbg <= constr(x, *args) <= ubg
.static_argnames (tuple of str, optional) β Names of arguments in
obj
andconstr
that should be treated as static parameters rather than symbolic variables. Static arguments are not differentiated through and the solver will be recompiled when their values change.constrain_x (bool, default=False) β If True, the solver will accept bounds on decision variables
(lbx, ubx)
. If False, no bounds onx
will be applied (equivalent to-β <= x <= β
).name (str, optional) β Name for the resulting solver function. If None, a name will be generated based on the objective function name.
method (str, optional) β The optimization method to use. Default is βipoptβ. See CasADi documentation for available methods.
**options (dict) β Additional options passed to the underlying optimization solver. See
minimize()
and the [CasADi documentation](https://web.casadi.org/python-api/#nlp) for available options.
- Returns:
solver β A callable function that solves the nonlinear optimization problem. The signature of this function depends on the values of
constrain_x
and whether a constraint function was provided:With constraints and x bounds:
solver(x0, lbx, ubx, lbg, ubg, *args)
With constraints, no x bounds:
solver(x0, lbg, ubg, *args)
With x bounds, no constraints:
solver(x0, lbx, ubx, *args)
No constraints or x bounds:
solver(x0, *args)
The returned solver can be evaluated both numerically and symbolically.
- Return type:
FunctionCache
Notes
By default the NLP solver uses the IPOPT interior point method which is suitable for large-scale nonlinear problems. See the
minimize()
documentation for additional solvers and configuration options.The function leverages automatic differentiation to compute exact derivatives of the objective and constraints, unless this behavior is overridden via configuration (e.g. by passing a custom evaluation function or using an L-BFGS approximation).
Both
obj` and `constr
must accept the same arguments, and ifstatic_argnames
is specified, the static arguments must be the same for both functions.Examples
>>> import numpy as np >>> import archimedes as arc >>> >>> # Define the Rosenbrock function >>> def f(x): ... return 100 * (x[1] - x[0]**2)**2 + (1 - x[0])**2 >>> >>> # Define a constraint function >>> def g(x): ... g1 = (x[0] - 1)**3 - x[1] + 1 ... g2 = x[0] + x[1] - 2 ... return np.array([g1, g2], like=x) >>> >>> # Create the solver >>> solver = arc.nlp_solver(f, constr=g) >>> >>> # Initial guess >>> x0 = np.array([2.0, 0.0]) >>> >>> # Constraint bounds: g <= 0 >>> lbg = -np.inf * np.ones(2) >>> ubg = np.zeros(2) >>> >>> # Solve the problem >>> x_opt = solver(x0, lbg, ubg) >>> print(x_opt) [0.99998266 1.00000688]