Filter Content By
Version
Text Search
${sidebar_list_label} - Back
Filter by Language
Adding constraints to the model
The next step in the example is to add the linear constraints. The first constraint is added here:
# Add constraint: x + 2 y + 3 z <= 4 m.addConstr(x + 2 * y + 3 * z <= 4, "c0")As with variables, constraints are always associated with a specific model. They are created using the addConstr() method on the model object.
We again use overloaded arithmetic operators to build linear expressions. The comparison operators are also overloaded to make it easier to build constraints.
The second argument to addConstr
gives the (optional)
constraint name.
Again, this simple example builds the linear expression for the constraint in a single statement using an explicit list of terms. More complex programs will typically build the expression incrementally.
The second constraint is created in a similar manner:
# Add constraint: x + y >= 1 m.addConstr(x + y >= 1, "c1")