Constraints define conditions the solution must satisfy.
3.1 Inequality Constraints
from optyx import Variablex = Variable("x")y = Variable("y")# Greater than or equalc1 = x + y >=1# x + y ≥ 1# Less than or equal c2 = x - y <=5# x - y ≤ 5# Can use expressions on either sidec3 = x**2+ y**2<=100
3.2 Equality Constraints
from optyx import Variablex = Variable("x")y = Variable("y")# Use eq() for equalityc = (x + y).eq(10) # x + y = 10
Note
We use .eq() instead of == because Python requires __eq__ to return a boolean for hash table operations. This is a common pattern in symbolic math libraries.
3.3 Constraint Internals
Every constraint has:
expr — The expression (normalized so rhs = 0)
sense — One of ">=", "<=", or "=="
from optyx import Variablex = Variable("x")c = x +5>=10print(f"Sense: {c.sense}")# Expression is: (x + 5) - 10 = x - 5 >= 0
Sense: >=
4 Problems
A Problem brings together the objective and constraints:
from optyx import Variable, Problemx = Variable("x", lb=0)y = Variable("y", lb=0)# Build problem with fluent APIprob = ( Problem(name="example") .minimize(x**2+ y**2) .subject_to(x + y >=1) .subject_to(x <=5))# Or build step by stepprob2 = Problem()prob2.minimize(x**2+ y**2)prob2.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"Constraints: {len(prob.constraints)}")
Name: example
Sense: minimize
Variables: ['x', 'y']
Constraints: 2