Internally, maximization is converted to minimization by negating the objective.
3.3.subject_to(constraint)
Add one or more constraints to the problem. Accepts scalar constraints, matrix-style constraints like A @ x <= b, a list of constraints, or a generator expression.
prob.subject_to(constraint)prob.subject_to([c1, c2, c3])prob.subject_to(x[i] >=0for i inrange(n)) # generatorprob.subject_to(A @ x <= b)prob.subject_to((A @ x).eq(b))
Parameter
Type
Description
constraint
Constraint \| Iterable[Constraint]
Constraint(s) to add, including matrix blocks produced by A @ x <= b
Returns:self (for chaining)
Note
For dense matrices, prob.subject_to(A @ x <= b) works directly. For raw scipy.sparse matrices, wrap the matrix first with as_matrix(...). SciPy owns the left-hand @ operator for sparse matrices and tries to do a numeric multiplication before Optyx can build a symbolic MatrixVectorProduct.
as_matrix() also accepts storage="auto" | "dense" | "sparse" when you want to force or auto-select the internal storage format for large matrix blocks.
3.4.remove_constraint(index_or_name)
Remove a constraint by index or name.
prob.remove_constraint(0) # remove first constraintprob.remove_constraint("cap") # remove by name
Parameter
Type
Description
index_or_name
int \| str
Index position or constraint name
Returns:self (for chaining)
Raises:IndexError if index is out of range; KeyError if name not found.
3.5.reset()
Clear solver caches and warm-start state, forcing cold re-analysis on the next solve().
prob.reset()
3.6.write(filename)
Export the problem to LP file format. Supports linear and quadratic objectives, constraints, variable bounds, and integer/binary sections.
prob.write("model.lp")
Parameter
Type
Description
filename
str
Output .lp file path
Raises:InvalidOperationError for nonlinear expressions.
3.7.to_lp()
Return the LP format string (same as write() but returns the string instead of writing to a file).
lp_string = prob.to_lp()
Returns:str — the LP format representation.
3.8 Context Manager
Problem supports with statements:
with Problem() as p: p.minimize(x**2+ y**2) p.subject_to(x + y >=1) solution = p.solve()
When method="auto" (default), Optyx automatically selects the best solver based on problem structure:
Problem Type
Selected Method
Linear with integer/binary variables
milp (HiGHS MILP solver)
Linear objective and constraints
linprog (HiGHS LP solver)
Unconstrained or bounds-only nonlinear problem
L-BFGS-B
Large sparse constrained nonlinear problem
trust-constr
Higher-degree or transcendental constrained problem
trust-constr
Linear/quadratic constrained nonlinear problem
SLSQP, with validation and a trust-constr retry when needed
Available methods:
Method
Bounds
Constraints
Gradient
Hessian
Description
"auto"
✅
✅
✅
✅
Automatic selection (default)
"milp"
✅
✅ (linear)
N/A
N/A
HiGHS MILP solver (integer/binary)
"linprog"
✅
✅ (linear)
N/A
N/A
HiGHS LP solver (for linear problems)
"SLSQP"
✅
✅
✅
❌
Sequential Least Squares Programming
"trust-constr"
✅
✅
✅
✅
Trust-region constrained optimization
"L-BFGS-B"
✅
❌
✅
❌
Limited-memory BFGS with bounds
"COBYLA"
❌
✅ (ineq)
❌
❌
Constrained Optimization BY Linear Approx
"TNC"
✅
❌
✅
❌
Truncated Newton Conjugate-Gradient
"Powell"
✅
❌
❌
❌
Powell’s conjugate direction method
"Nelder-Mead"
✅
❌
❌
❌
Simplex algorithm (derivative-free)
"CG"
❌
❌
✅
❌
Conjugate gradient
"BFGS"
❌
❌
✅
❌
Broyden-Fletcher-Goldfarb-Shanno
"Newton-CG"
❌
❌
✅
✅
Newton conjugate gradient
"dogleg"
❌
❌
✅
✅
Dog-leg trust-region
"trust-ncg"
❌
❌
✅
✅
Newton conjugate gradient trust-region
"trust-exact"
❌
❌
✅
✅
Nearly exact trust-region
"trust-krylov"
❌
❌
✅
✅
Krylov subspace trust-region
TipRecommended Methods
Linear problems: Use "auto" or "linprog" for best performance
With constraints: Use "SLSQP" or "trust-constr"
Bounds only: Use "L-BFGS-B" for large-scale problems
Unconstrained: Use "BFGS" or "trust-ncg" for smooth problems
NotePerformance
LP and MILP: warm solves approach raw SciPy at larger benchmark sizes
CQP: exact Jacobians keep warm solves near raw SciPy in the bundled suite
Repeated solves: cached structure avoids recompilation, but solver work remains
Results depend on model structure, size, solver iterations, and hardware. See the Benchmarks page for versioned measurements.
4 Strict Compatibility Parameter
strict remains in the public signature for compatibility. Linear discrete models are solved as MILPs, while nonlinear discrete models (MIQP/MINLP) raise UnsupportedOperationError regardless of the value of strict. Continuous models are unaffected by it.
5 Properties
Property
Type
Description
.name
str | None
Problem name
.objective
Expression \| None
Objective function, or None before one is set
.sense
str
"minimize" or "maximize"
.constraints
list[Constraint]
A copy of scalar constraints; structured matrix blocks are not included
.variables
list[Variable]
All decision variables
.n_constraints
int
Scalar constraints plus all structured matrix-block rows
n_constraints and summary() count both scalar constraints and every row in structured matrix constraint blocks. remove_constraint() currently operates only on scalar constraints, by scalar-list index or name.
5.1 Examples
from optyx import Variable, Problemx = Variable("x", lb=0)y = Variable("y", lb=0)prob = ( Problem("demo") .minimize(x**2+ y**2) .subject_to(x + y >=1))print(f"Name: {prob.name}")print(f"Sense: {prob.sense}")print(f"Variables: {[v.name for v in prob.variables]}")print(f"Num constraints: {len(prob.constraints)}")
Name: demo
Sense: minimize
Variables: ['x', 'y']
Num constraints: 1