Class CanonicalConstraint
f_eq(x) = 0
f_ineq(x) ≤ 0
Mirrors scipy's CanonicalConstraint
(scipy/optimize/_trustregion_constr/canonical_constraint.py).
The Java port already canonicalises bounds-form constraints inline inside
LinearConstraint and NonlinearConstraint (see those classes'
4-block ineq ordering: less | greater | interval-upper |
interval-lower). This class wraps that machinery into the scipy-shape API
for parity with test_canonical_constraint.py.
The class itself is immutable; all factory methods produce a new instance carrying lambdas that delegate to the underlying constraint objects.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceFunctional interface for the constraint Hessian-of-Lagrangian:Sum_i v_eq[i] * H_{eq,i}(x) + Sum_j v_ineq[j] * H_{ineq,j}(x).static final recordOutput shape ofinitialConstraintsAsCanonical(int, java.util.List<de.labathome.trustconstr.CanonicalConstraint>, de.labathome.trustconstr.matrix.Matrix, boolean): the four arrays(cEq, cIneq, JEq, JIneq)returned as a record. -
Field Summary
FieldsModifier and TypeFieldDescriptionfinal boolean[]Per-canonical-inequality-row strict-feasibility flags (lengthnIneq).final intNumber of canonical equality rows.final intNumber of canonical inequality rows. -
Method Summary
Modifier and TypeMethodDescriptionstatic CanonicalConstraintconcatenate(List<CanonicalConstraint> sources, boolean sparseJacobian) Concatenate a list of canonical constraints into one.static CanonicalConstraintempty(int n) Construct an empty canonical constraint -- zero rows of each kind.static CanonicalConstraintfromBounds(Bounds bounds) PromoteBoundsinto a canonical constraint viaLinearConstraint.fromBounds(Bounds).static CanonicalConstraintWrap aLinearConstraintas a canonical constraint.static CanonicalConstraintfromNonlinearConstraint(NonlinearConstraint nc, int n) Wrap aNonlinearConstraintas a canonical constraint.static CanonicalConstraintBuild a canonical constraint from aPreparedConstraint.Evaluate the canonical equality and inequality residuals atx.Evaluate the constraint Hessian-of-Lagrangian atx.initialConstraintsAsCanonical(int n, List<CanonicalConstraint> canonicals, Matrix x0, boolean sparseJacobian) Stack the canonical residuals and Jacobians of every constraint incanonicalsat the starting iteratex0.initialConstraintsAsCanonical(int n, List<PreparedConstraint> prepared, boolean sparseJacobian) scipy-shape overload taking a list ofPreparedConstraints.Evaluate the canonical equality and inequality Jacobians atx.
-
Field Details
-
nEq
public final int nEqNumber of canonical equality rows. -
nIneq
public final int nIneqNumber of canonical inequality rows. -
keepFeasible
public final boolean[] keepFeasiblePer-canonical-inequality-row strict-feasibility flags (lengthnIneq). Mirrors scipy'skeep_feasibleattribute.
-
-
Method Details
-
fun
Evaluate the canonical equality and inequality residuals atx.- Parameters:
x- current iterate (n x 1)- Returns:
(cEq, cIneq)wherecEqisnEq x 1andcIneqisnIneq x 1
-
jac
Evaluate the canonical equality and inequality Jacobians atx.- Parameters:
x- current iterate (n x 1)- Returns:
(JEq, JIneq)whereJEqisnEq x nandJIneqisnIneq x n
-
hess
Evaluate the constraint Hessian-of-Lagrangian atx. -
empty
Construct an empty canonical constraint -- zero rows of each kind. The Jacobians are returned as0 x nmatrices, the Hessian as ann x nzero matrix. Mirrors scipy'sCanonicalConstraint.empty(n).- Parameters:
n- number of decision variables- Returns:
- an empty canonical constraint sized for
nvariables
-
fromLinearConstraint
Wrap aLinearConstraintas a canonical constraint. The underlying row classification (equal / less / greater / interval-upper / interval-lower) is already done byLinearConstraint; this factory just exposes itsconstrEq/constrIneq/jacEq/jacIneqvia the scipy-shape API.Linear constraints have zero constraint Hessian, so
hess(...)returns ann x nzero matrix.- Parameters:
lc- underlying linear constraint- Returns:
- canonical wrapper over
lc
-
fromNonlinearConstraint
Wrap aNonlinearConstraintas a canonical constraint. Mirrors the dispatch done by scipy'sfrom_PreparedConstrainton a constraint that came from a user-suppliedNonlinearConstraint.If the underlying constraint was constructed without an analytic
hess,hess(x, vEq, vIneq)returns ann x nzero matrix (no Hessian contribution).- Parameters:
nc- underlying nonlinear constraintn- number of decision variables (cannot be inferred fromNonlinearConstraint, which only knows the constraint-output dimensionm)- Returns:
- canonical wrapper over
nc
-
fromBounds
PromoteBoundsinto a canonical constraint viaLinearConstraint.fromBounds(Bounds). Mirrors scipy'sPreparedConstraint(bounds, ...)->from_PreparedConstraintshortcut.- Parameters:
bounds- box bounds on the decision variables- Returns:
- canonical constraint enforcing
lb ≤ x ≤ ub
-
fromPreparedConstraint
Build a canonical constraint from aPreparedConstraint. Mirrors scipy'sCanonicalConstraint.from_PreparedConstraint: dispatches on the original source constraint type carried byPreparedConstraint.source().- Parameters:
pc- prepared constraint with cachedf,Jat x0- Returns:
- canonical wrapper over the source constraint
- Throws:
IllegalArgumentException- if the source is not one ofLinearConstraint,NonlinearConstraint, orBounds
-
concatenate
public static CanonicalConstraint concatenate(List<CanonicalConstraint> sources, boolean sparseJacobian) Concatenate a list of canonical constraints into one. Equality blocks are stacked first across all sources, then inequality blocks across all sources. Mirrors scipy'sCanonicalConstraint.concatenate(canonical_constraints, sparse_jacobian).The resulting Jacobian's storage layout follows
sparseJacobian:trueproduces aSparseMatrix,falseproduces a denseMatrix. The summed Hessian is the (matrix) sum of each source's Hessian applied to its slice of the multipliers.- Parameters:
sources- canonical constraints to concatenate; row order follows the list ordersparseJacobian- iftrue, the combined Jacobian is built as aSparseMatrix; otherwise dense- Returns:
- concatenated canonical constraint
-
initialConstraintsAsCanonical
public static CanonicalConstraint.InitialCanonical initialConstraintsAsCanonical(int n, List<CanonicalConstraint> canonicals, Matrix x0, boolean sparseJacobian) Stack the canonical residuals and Jacobians of every constraint incanonicalsat the starting iteratex0. Mirrors scipy's top-levelinitial_constraints_as_canonical(n, prepared_constraints, sparse_jacobian)function.scipy avoids re-evaluating each constraint at
x0by reusing the cachedf,Jstored inPreparedConstraint; the Java port currently re-evaluates becausePreparedConstraintdoes not yet cache (Phase 2). Numerically the result is identical.- Parameters:
n- number of decision variables (used to size the empty Jacobians whencanonicalsis empty)canonicals- canonical constraints to evaluate and stackx0- starting iterate (n x 1)sparseJacobian- iftrue, the combined Jacobian is built as aSparseMatrix; otherwise dense- Returns:
- stacked residuals and Jacobians
-
initialConstraintsAsCanonical
public static CanonicalConstraint.InitialCanonical initialConstraintsAsCanonical(int n, List<PreparedConstraint> prepared, boolean sparseJacobian) scipy-shape overload taking a list ofPreparedConstraints. Mirrors scipy'sinitial_constraints_as_canonical(n, prepared_constraints, sparse_jacobian).Each prepared constraint is converted to a
CanonicalConstraintviafromPreparedConstraint(de.labathome.trustconstr.records.PreparedConstraint), and the resulting canonicals are stacked. The cached iteratex0from the first prepared constraint is used to evaluate the stacked residuals and Jacobians.- Parameters:
n- number of decision variablesprepared- prepared constraintssparseJacobian- iftrue, the combined Jacobian is built sparse; otherwise dense- Returns:
- stacked residuals and Jacobians at
x0
-