1 problem.Problem
problem.Problem(name=None)An optimization problem with objective and constraints.
1.1 Example
x = Variable(“x”, lb=0) y = Variable(“y”, lb=0) prob = Problem() prob.minimize(x2 + y2) prob.subject_to(x + y >= 1) solution = prob.solve() print(solution.values) # {‘x’: 0.5, ‘y’: 0.5}
1.2 Note
The Problem class is not thread-safe. Compiled callables are cached per instance and reused across multiple solve() calls for performance. Structural mutations invalidate the relevant caches. Mutable bounds, parameters, and linear objective coefficients are re-read or versioned so their current values are used on the next solve.
1.3 Attributes
| Name | Description |
|---|---|
| constraints | List of constraints. |
| n_constraints | Number of constraints. |
| n_variables | Number of decision variables. |
| objective | The objective function expression. |
| sense | The optimization sense (minimize or maximize). |
| variables | All decision variables in the problem. |
1.4 Methods
| Name | Description |
|---|---|
| get_bounds | Get variable bounds as a list of (lb, ub) tuples. |
| maximize | Set the objective function to maximize. |
| minimize | Set the objective function to minimize. |
| remove_constraint | Remove a constraint by index or name. |
| reset | Reset the problem solver state (clears caches and warm start). |
| solve | Solve the optimization problem. |
| subject_to | Add a constraint or list of constraints to the problem. |
| summary | Return a human-readable summary of the optimization problem. |
| to_lp | Return the LP format string representation of the problem. |
| write | Export the problem to LP file format. |
1.4.1 get_bounds
problem.Problem.get_bounds()Get variable bounds as a list of (lb, ub) tuples.
1.4.1.1 Returns
| Name | Type | Description |
|---|---|---|
| list[tuple[float | None, float | None]] | List of bounds in variable order. |
1.4.2 maximize
problem.Problem.maximize(expr)Set the objective function to maximize.
1.4.2.1 Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| expr | Expression | float | int | Expression to maximize. Must be an optyx Expression, Variable, or numeric constant (int/float). | required |
1.4.2.2 Returns
| Name | Type | Description |
|---|---|---|
| Problem | Self for method chaining. |
1.4.2.3 Raises
| Name | Type | Description |
|---|---|---|
| InvalidOperationError | If expr is not a valid expression type. |
1.4.2.4 Example
prob.maximize(revenue - cost)
1.4.3 minimize
problem.Problem.minimize(expr)Set the objective function to minimize.
1.4.3.1 Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| expr | Expression | float | int | Expression to minimize. Must be an optyx Expression, Variable, or numeric constant (int/float). | required |
1.4.3.2 Returns
| Name | Type | Description |
|---|---|---|
| Problem | Self for method chaining. |
1.4.3.3 Raises
| Name | Type | Description |
|---|---|---|
| InvalidOperationError | If expr is not a valid expression type. |
1.4.3.4 Example
prob.minimize(x2 + y2) prob.minimize(x + 2*y - 5)
1.4.4 remove_constraint
problem.Problem.remove_constraint(index_or_name)Remove a constraint by index or name.
1.4.4.1 Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| index_or_name | int | str | If int, removes the constraint at that index. If str, removes the first constraint with that name. | required |
1.4.4.2 Returns
| Name | Type | Description |
|---|---|---|
| Problem | Self for method chaining. |
1.4.4.3 Raises
| Name | Type | Description |
|---|---|---|
| IndexError | If integer index is out of range. | |
| KeyError | If no constraint with the given name is found. |
1.4.4.4 Example
from optyx import Constraint capacity = x + y <= 10 prob.subject_to(Constraint(capacity.expr, capacity.sense, name=“cap”)) prob.remove_constraint(“cap”)
1.4.5 reset
problem.Problem.reset()Reset the problem solver state (clears caches and warm start).
Forces a complete re-analysis and re-compilation of the problem on the next solve() call. Also clears any stored warm start state, forcing a cold start on the next solve.
1.4.6 solve
problem.Problem.solve(
method='auto',
strict=False,
warm_start=True,
callback=None,
time_limit=None,
**kwargs,
)Solve the optimization problem.
1.4.6.1 Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| method | str | Solver method. Options: - “auto” (default): Automatically select the best method: - Linear continuous models → linprog (HiGHS) - Linear discrete models → milp (HiGHS) - Unconstrained or bounds-only NLPs → L-BFGS-B - Large sparse constrained NLPs → trust-constr - Higher-degree or transcendental constrained NLPs → trust-constr - Linear/quadratic constrained NLPs → SLSQP, with a feasibility/stationarity-based trust-constr retry - “linprog”: Force LP solver (scipy.optimize.linprog) - “highs”: HiGHS LP solver (auto method selection) - “highs-ds”: HiGHS dual simplex - “highs-ipm”: HiGHS interior point method - “SLSQP”: Sequential Least Squares Programming - “trust-constr”: Trust-region constrained optimization - “L-BFGS-B”: Limited-memory BFGS with bounds - “BFGS”: Broyden-Fletcher-Goldfarb-Shanno - “Nelder-Mead”: Derivative-free simplex method | 'auto' |
| strict | bool | Retained for API compatibility. Linear discrete models are solved as MILPs, and nonlinear discrete models are rejected regardless of this value. | False |
| warm_start | bool | If True (default), use the previous solution as the initial point for re-solving. Only applies to NLP methods. Call reset() to clear warm start state. | True |
| callback | Callable[[SolverProgress], bool | None] | None | Optional function called at each solver iteration with a SolverProgress object. Return True to terminate early (solution will have SolverStatus.TERMINATED). Only applies to NLP methods (SciPy). | None |
| time_limit | float | None | Maximum wall-clock time in seconds. If exceeded, the solver terminates early with SolverStatus.TERMINATED. Only applies to NLP methods (SciPy). | None |
| **kwargs | Any | Additional arguments passed to the solver. | {} |
1.4.6.2 Returns
| Name | Type | Description |
|---|---|---|
| Solution | Solution object with results. |
1.4.6.3 Raises
| Name | Type | Description |
|---|---|---|
| NoObjectiveError | If no objective has been set. | |
| UnsupportedOperationError | If the problem is a nonlinear discrete model (MIQP/MINLP), which the current solver stack does not support. |
1.4.7 subject_to
problem.Problem.subject_to(constraint)Add a constraint or list of constraints to the problem.
1.4.7.1 Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| constraint | Constraint | MatrixConstraintBlock | Iterable[Constraint | MatrixConstraintBlock] | Constraint or iterable of constraints to add. Accepts lists, tuples, generators, etc. | required |
1.4.7.2 Returns
| Name | Type | Description |
|---|---|---|
| Problem | Self for method chaining. |
1.4.7.3 Raises
| Name | Type | Description |
|---|---|---|
| ConstraintError | If constraint is not a valid Constraint type. |
1.4.7.4 Example
x = VectorVariable(“x”, 100) prob.subject_to(x >= 0) # Adds 100 constraints prob.subject_to(x[i] >= 0 for i in range(10)) # Generator
1.4.8 summary
problem.Problem.summary()Return a human-readable summary of the optimization problem.
Provides an overview including problem name, variable counts (with breakdown by type), constraint counts, and objective sense.
1.4.8.1 Returns
| Name | Type | Description |
|---|---|---|
| str | Multi-line string describing the problem structure. |
1.4.8.2 Example
x = VectorVariable(“x”, 100, lb=0) prob = Problem(“portfolio”) prob.minimize(x.dot(x)) prob.subject_to(x.sum().eq(1)) print(prob.summary()) Optyx Problem: portfolio Variables: 100 Constraints: 1 (1 equality, 0 inequality) Objective: minimize
1.4.9 to_lp
problem.Problem.to_lp()Return the LP format string representation of the problem.
Like write(), but returns the string instead of writing to a file.
1.4.9.1 Returns
| Name | Type | Description |
|---|---|---|
| str | The LP format string. |
1.4.9.2 Raises
| Name | Type | Description |
|---|---|---|
| InvalidOperationError | If the problem contains nonlinear expressions. |
1.4.10 write
problem.Problem.write(filename)Export the problem to LP file format.
Writes the problem formulation to a human-readable .lp file, compatible with solvers like CPLEX, Gurobi, GLPK, and HiGHS.
Supports linear and quadratic objectives, linear constraints, variable bounds, and integer/binary variable types.
1.4.10.1 Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| filename | str | Path to the output .lp file. | required |
1.4.10.2 Raises
| Name | Type | Description |
|---|---|---|
| InvalidOperationError | If the problem contains nonlinear expressions that cannot be represented in LP format. | |
| NoObjectiveError | If no objective has been set. |
1.4.10.3 Example
x = Variable(“x”, lb=0) y = Variable(“y”, lb=0) prob = Problem(“example”) prob.minimize(2 * x + 3 * y) prob.subject_to(x + y >= 1) prob.write(“example.lp”)