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()
See the Benchmarks page for detailed performance analysis.
4 Strict Mode
Use strict=True to enforce that the solver can handle all problem features exactly:
# Strict: fail early if problem can't be solved exactlysolution = prob.solve(strict=True) # Raises ValueError for unsupported configurations
This is useful for production code where you want to catch configurations that the solver cannot handle (e.g., nonlinear objectives with integer variables / MIQP).
5 Properties
Property
Type
Description
.name
str | None
Problem name
.objective
Expression
Objective function
.sense
str
"minimize" or "maximize"
.constraints
list[Constraint]
All constraints
.variables
list[Variable]
All decision variables
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