Separate access to the dual values of various constraints
回答済みHello, I have the following model.
from gurobipy import *
model = Model()
I = [1,2,3]
J = [1,2,3]
D = [1,2,3]
# Variables
x = model.addVars(I, J, D, vtype=GRB.BINARY, name="x")
y = model.addVars(J, D, vtype= GRB.CONTINUOUS, name = "y")
# Model
model.setObjective(quicksum(y[j, d] for j in J for d in D), GRB.MAXIMIZE)
for i in I:
model.addLConstr(quicksum(x[i, j, d] for j in J for d in D) <= 1, name = "x")
for j in J:
for d in D:
model.addLConstr(quicksum(x[i, j, d] for i in I) + y[j, d] == 1, name = "y")
model.optimize()
# Relax
relaxed = model.relax()
relaxed.optimize()
# Duals
duals = relaxed.getAttr("Pi", relaxed.getConstrs())
print(duals)
Unfortunately, the code gives me the dual values for both constraints in a list, but I want to save and access them separately for each constraint.
So, for example, save the dual values for constraint "x" and access them with duals1[i] so that I can then access the 3x1 duals of the first constraint by iterating over I and the other 3x3 dual values with duals2[j,d].
-
正式なコメント
This post is more than two 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 try Gurobot, our chatbot interface offering instant, expert-level support. -
I would consider using addConstrs() for each of the constraint sets. It gives you back a dictionary, e.g. a mapping of i/j/d indices to constraints. Then you can pass that result to getAttr() and you get back a similar dictionary with duals.
ca = model.addConstrs( (quicksum(x[i, j, d] for j in J for d in D) <= 1 for i in I), name = "x")
cb = model.addConstrs( (quicksum(x[i, j, d] for i in I) + y[j, d] == 1 for j in J for d in D), name = "y")
# Relax and solve
duals = relaxed.getAttr("Pi", ca)
print(duals)0 -
Ronald van der Velden Thank you for your help. Sadly it does return this error "gurobipy.GurobiError: Constr not in model". How do i fix that?
0 -
Ah, sorry about that - I should have tested your exact scenario in full and had ignored the fact that calling relax() returns a new model where you cannot query attributes based on constraints from the original model. Unfortunately there is not a clean way to map elements from the original model to their counterpart in the relaxed model. Personally I would relax the model in-place using the second method from this article.
0 -
Ronald van der Velden No worries. I tried your approach and initially it didnt work. But updating the model with "model.update()" after assigning names to the constraints did the trick. Thanks
0
投稿コメントは受け付けていません。
コメント
5件のコメント