Constraint(expression, lb=None, ub=None, indicator_variable=None, active_when=1, *args, **kwargs)¶
Constraint objects represent the mathematical (in-)equalities that constrain an optimization problem. A constraint is formulated by a symbolic expression of variables and a lower and/or upper bound. Equality constraints can be formulated by setting the upper and lower bounds to the same value.
Some solvers support indicator variables. This lets a binary variable act as a switch that decides whether the constraint should be active (cannot be violated) or inactive (can be violated).
The constraint expression can be an arbitrary combination of variables, however the individual solvers have limits to the forms of constraints they allow. Most solvers only allow linear constraints, meaning that the expression should be of the form
a * var1 + b * var2 + c * var3 ...
sympy – The mathematical expression defining the constraint.
str, optional – The constraint’s name.
float or None, optional – The lower bound, if None then -inf.
float or None, optional – The upper bound, if None then inf.
Variable – The indicator variable (needs to be binary).
0 or 1 (default 0) – When the constraint should
Model or None, optional – A reference to the optimization model the variable belongs to.
>>> expr = 2.4 * var1 - 3.8 * var2 >>> c1 = Constraint(expr, lb=0, ub=10)
>>> indicator_var = Variable("var3", type="binary") # Only possible with some solvers >>> c2 = Constraint(var2, lb=0, ub=0, indicator_variable=indicator_var, active_when=1) # When the indicator is 1, var2 is constrained to be 0
Activity relation of constraint to indicator variable (if supported).
clone(constraint, model=None, **kwargs)¶
Make a copy of another constraint. The constraint being copied can be of the same type or belong to a different solver interface.
>>> const_copy = Constraint.clone(old_constraint)
Dual of constraint (None if no solution exists).
Constructs a Variable from the provided json-object.
>>> import json >>> with open("path_to_file.json") as infile: >>> constraint = Constraint.from_json(json.load(infile))
The indicator variable of constraint (if available).
Lower bound of constraint.
Primal of constraint (None if no solution exists).
Returns a json-compatible object from the constraint that can be saved using the json module.
>>> import json >>> with open("path_to_file.json", "w") as outfile: >>> json.dump(constraint.to_json(), outfile)
Upper bound of constraint.