Quantcast
Channel: Common structures in Gurobi - Python - Operations Research Stack Exchange
Viewing all articles
Browse latest Browse all 3

Answer by dhasson for Common structures in Gurobi - Python

$
0
0

The Python syntax could be like this:

  1. model.addConstr(sum(x[i,j] for i in A for j in A if i != j) <= 10, "firstConstraint")

  2. model.addConstrs(sum(x[i,j] for i in A if i != j) <= 10 for j in B, "secondConstraintClass")

  3. model.addConstr(x[i,j] <= 10 for i in A for j in B if i != j, "thirdConstraintClass")

where model is the gurobi API object in your code.

Note that addConstrs is for the case when you add many similar constraints indexed by a set, while addConstr is for when you add only one constraint at the time (note the for j in B after the inequality and before the comma in the second constraint where addConstrs is used).

You can check more about the addConstrs method in its documentation, including some examples and use cases.

For sums over large arrays or linear products, or when adding a linear product where the coefficients could be sparse, I'd recommend reading about the quicksum and LinExpr methods which can yield further efficiency.


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>