Adding and referencing constraints
AnsweredHello,
I am using Gurobipy to solve LP and I want to refer to a constraint to call the .pi attribute.
For a single constraint such as
constr = sub.addConstr(y1 - y2 == 0);
I can refer to the pi attribute by the statement dual = constraints.pi
However, how can I use the same if I add the constraints in following manner?
for i in range(1,N+1):
m.addConstr(quicksum(xBar[(i,t)] for t in range(1,T+1)) == 1)
I created a list of duals
duals = [0 for a in range(1,N+1)]
Now I need to access each of the N constraints' pi attribute and save it.
How can this be done? How can I reference each of these constraints?
Amogh
-
Official comment
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
You can use Model.addConstrs() to add multiple constraints at once. The returned value is a tupledict that maps the generator expression indices to the corresponding Constr objects.
Below is an example you can modify to fit your needs. It also takes advantage of Model.addVars() and tupledict.sum().
xBar = m.addVars(N, T, vtype=GRB.BINARY, name='xBar')
# c is a tupledict mapping indices 0..N-1 to Constr objects
c = m.addConstrs((xBar.sum(i, '*') == 1 for i in range(N)), name='sum_to_one')
m.optimize()
for i in range(N):
print(c[i].ConstrName, c[i].Pi)Note that you can use Model.getAttr() to obtain a tupledict mapping the constraint indices to their corresponding Pi values:
# pi is a tupledict mapping 0..N-1 to the corresponding constraint's dual value
pi = m.getAttr('Pi', c)0
Post is closed for comments.
Comments
2 comments