General Constraints can be used to model many types of logical expressions, like `AND` and `OR` relationships. More complex logical expressions can be created via combinations of general constraints.

For example, to model an `AND` expression in Python, you can use the Model.addGenConstrAnd() method:

# x5 = and(x1, x3, x4)

model.addGenConstrAnd(x5, [x1, x3, x4], "and_constr")

or, using the overloaded form via the and_() helper function:

model.addConstr(x5 == and_([x1, x3, x4]), "and_constr")

To work with general expressions within constraints, you may sometimes need to add intermediate decision variables. For example, the abs_() function is used to set one variable equal to the absolute value of another. To model the constraint \( v = | x - y | \), define a free auxiliary variable \( u \) and set it equal to \( x − y \). Then, set \( v \) equal to \( | u | \). For example, in Python:

import gurobipy as gp

model = gp.Model()

x = model.addVar(name="x")

y = model.addVar(name="y")

v = model.addVar(name="v")

# v = |x - y|

u = model.addVar(lb=-gp.GRB.INFINITY, name="u")

model.addConstr(u == x - y)

model.addConstr(v == gp.abs_(u))

### Further information

## Comments

0 comments

Article is closed for comments.