How can I get or print the LHS constraint values?
AnsweredHello, I'm getting started in using gurobi-python and need help. This is my first model and I want to print or get the values of the LHS_Constr (see below):
from gurobipy import *
pp3=Model()
vrs=["Original","Cheddar","Wasabi"]
procs=["X-Ray","Checkweigher"]
RestrProc2={"X-Ray":{"Original":8.6, "Cheddar":2.2, "Wasabi":5.1},
"Checkweigher":{"Original":7, "Cheddar":6.8, "Wasabi":9.8}}
MCapcity={"X-Ray":480,"Checkweigher":540}
PV={"Original": 20, "Cheddar":15, "Wasabi":17}
CostProc={"X-Ray":0.11,"Checkweigher":0.22}
Vrbl=pp3.addVars(vrs,name="Cheese")
#Objective function
FnOb=quicksum(PV[vr]*Vrbl[vr] for vr in vrs)-quicksum(CostProc[proc]*RestrProc2[proc][vr]*Vrbl[vr] for vr in vrs for proc in procs)
pp3.setObjective(FnOb,GRB.MAXIMIZE)
#Constraints
LHS_Constr=(quicksum(RestrProc2[proc][vr]*Vrbl[vr] for vr in vrs)<=MCapcity[proc] for proc in procs)
pp3.addConstrs(LHS_Constr, name="Capacity")
pp3.optimize()
pp3.printAttr('X')
I appreciate your support.
-
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?. -
Hi Marcela,
Do you mean you want to extract the values of the LHS of those constraints at the optimal solution? If so, you can do this as follows:
- Construct a LinExpr object for each of the LHS expressions
- After optimizing, evaluate each LHS expression with the LinExpr.getValue() method
In your case:
LHS = {proc : quicksum(RestrProc2[proc][vr]*Vrbl[vr] for vr in vrs) for proc in procs}
pp3.addConstrs(LHS[proc] <= MCapcity[proc] for proc in procs)
pp3.optimize()
for proc in procs:
print(LHS[proc].getValue())I hope this helps!
Eli
1 -
Hi Eli, it worked! Thank you very much.
0 -
Just add a note to Eli's answer:
If you don't want to create a new object LHS, below can give you the values too.
for proc in procs:
quicksum(RestrProc2[proc][vr]*Vrbl[vr] for vr in vrs).getValue()0
Post is closed for comments.
Comments
4 comments