Package de.labathome.trustconstr
Class LinearConstraint
java.lang.Object
de.labathome.trustconstr.LinearConstraint
- All Implemented Interfaces:
Constraint,Jacobian
Linear constraint of the form
lb <= A x <= ub
Counterpart to scipy.optimize.LinearConstraint
(scipy/optimize/_constraints.py:139). Stores the constraint matrix
A, the bounds, and the per-row keepFeasible flag.
The Java port distinguishes equality and inequality at the constraint
level -- this class implements both Constraint (constraint values)
and Jacobian (analytic Jacobian, which for a linear constraint is
just A).
Each row i of A is classified as one of four categories:
equal:lb[i] == ub[i](finite). Contributes one row toconstrEq/jacEq:A[i,:] x - lb[i].less:lb[i] == -inf,ub[i] < inf(upper-only). Contributes one ineq rowA[i,:] x - ub[i](sign +1).greater:ub[i] == +inf,lb[i] > -inf(lower-only). Contributes one ineq rowlb[i] - A[i,:] x(sign -1, target lb).interval: both finite,lb[i] < ub[i]. Contributes two ineq rows: an upper-side row and a lower-side row.
The constrIneq / jacIneq output is grouped scipy-style
(mirrors canonical_constraint.py:_interval_to_canonical): all
less rows first, then all greater rows, then all
interval-upper rows, then all interval-lower rows. Within
each block the original row order is preserved.
-
Constructor Summary
ConstructorsConstructorDescriptionLinearConstraint(Matrix a, double[] lb, double[] ub) Convenience overload: equivalent toLinearConstraint(Matrix, double[], double[], boolean[])withkeepFeasible = null.LinearConstraint(Matrix a, double[] lb, double[] ub, boolean[] keepFeasible) Build a linear constraintlb ≤ A x ≤ ub. -
Method Summary
Modifier and TypeMethodDescriptionbounds()constrIneq(Matrix x) boolean[]Per-canonical-inequality-rowenforceFeasibilityflags derived from the user-suppliedkeep_feasible.static LinearConstraintfromBounds(Bounds bounds) Build aLinearConstraintfrom variableBounds, i.e. the trivial linear constraintlb <= x <= ubwithA = I.getA()jacEqCSR()Sparse Jacobian for equality rows: aCSRMatrixbuilt directly from the row-selected non-zeros ofA, without going through a dense intermediate.Sparse Jacobian for inequality rows: aCSRMatrix(with the appropriate per-row sign flips for one-sided lower-bound rows).boolean[]double[]lb()intnEq()intnIneq()Lower and upper residuals atx:sl = A x - lb(positive iff lower bound satisfied),sb = ub - A x(positive iff upper bound satisfied).double[]ub()voidThrowsIllegalArgumentExceptionif any row markedkeepFeasible[i] == trueis violated at the supplied starting point.
-
Constructor Details
-
LinearConstraint
Build a linear constraintlb ≤ A x ≤ ub.Rows where
lb[i] == ub[i](and both are finite) become equality rows; other rows become canonical inequality rows in scipy's 4-block order:less | greater | interval-upper | interval-lower.- Parameters:
a- constraint matrix (m x n)lb- lower bounds, lengthm(use-infto disable per row)ub- upper bounds, lengthm(use+infto disable per row)keepFeasible- per-row strict-feasibility flag, lengthm;null= all-false
-
LinearConstraint
Convenience overload: equivalent toLinearConstraint(Matrix, double[], double[], boolean[])withkeepFeasible = null.- Parameters:
a- constraint matrix (m x n)lb- lower bounds, lengthmub- upper bounds, lengthm
-
-
Method Details
-
fromBounds
Build aLinearConstraintfrom variableBounds, i.e. the trivial linear constraintlb <= x <= ubwithA = I. Mirrors scipy's promotion ofBoundsto a canonical constraint viaPreparedConstraint(bounds, ...).- Parameters:
bounds- variable bounds --lbandubaren x 1column matrices- Returns:
- identity-Jacobian constraint that enforces
bounds
-
getA
- Returns:
- the constraint matrix
A
-
lb
public double[] lb()- Returns:
- a defensive copy of the lower bounds array
-
ub
public double[] ub()- Returns:
- a defensive copy of the upper bounds array
-
keepFeasible
public boolean[] keepFeasible()- Returns:
- a defensive copy of the per-row keep-feasible flags
-
bounds
- Returns:
- the bounds repackaged as a
Boundsrecord
-
nEq
public int nEq()- Returns:
- number of canonical equality rows
-
nIneq
public int nIneq()- Returns:
- number of canonical inequality rows
-
enforceFeasibilityIneq
public boolean[] enforceFeasibilityIneq()Per-canonical-inequality-rowenforceFeasibilityflags derived from the user-suppliedkeep_feasible. Each canonical ineq row inherits the kf flag of the original row it came from. Length matchesnIneq()and is suitable to pass directly toTrustRegionInteriorPoint'senforceFeasibilityparameter.- Returns:
- per-canonical-row strict-feasibility flags, length
nIneq()
-
validateKeepFeasibleAtStart
ThrowsIllegalArgumentExceptionif any row markedkeepFeasible[i] == trueis violated at the supplied starting point. Mirrors scipy's strict-feasibility precondition forkeep_feasible=Truerows: the algorithm will not enforce intermediate-iterate feasibility for these rows if the start is already infeasible there.- Parameters:
x0- starting iterate (n x 1)
-
residual
Lower and upper residuals atx:sl = A x - lb(positive iff lower bound satisfied),sb = ub - A x(positive iff upper bound satisfied). Mirrors scipy'sLinearConstraint.residual(x).- Parameters:
x- current iterate (n x 1)- Returns:
(sl, sb)as aResidual
-
constrEq
- Specified by:
constrEqin interfaceConstraint- Parameters:
x- current iterate (n × 1)- Returns:
- equality-constraint residual (
nEq × 1)
-
constrIneq
- Specified by:
constrIneqin interfaceConstraint- Parameters:
x- current iterate (n × 1)- Returns:
- inequality-constraint residual (
nIneq × 1); feasible iff every entry is≤ 0
-
jacEq
-
jacIneq
-
jacEqCSR
Sparse Jacobian for equality rows: aCSRMatrixbuilt directly from the row-selected non-zeros ofA, without going through a dense intermediate.- Returns:
- CSR representation of the equality-row Jacobian
-
jacIneqCSR
Sparse Jacobian for inequality rows: aCSRMatrix(with the appropriate per-row sign flips for one-sided lower-bound rows).- Returns:
- CSR representation of the inequality-row Jacobian
-