How to use the optimal solution (Gurobi output) to calculate another equation
AnsweredHello,
I need to use the output of the gurobi solver in python to calculate another equation, or simply I need a way to be able to find the value of the left hand side (LHS) of one of the constraints but I cannot find a way to do that. I have defined a variable X[i,j,k] and have a parameter dist[i,j].
for i in N:
for j in N:
for k in B:
x[(i,j,k)] = m.addVar(lb=0, vtype=GRB.BINARY, name="x%d,%d,%d" % (i,j,k))
My objective function is:
Here is a constraint that I added:
I just need a way to use the values of variable x and calculate the LHS of constraint 18 and also want to define other equations like the LHS of this constraint but do the sum over different sets for i and j and k. I appreciate if you can help on this.
Just to clarify, after solving the model in python, when I try to print x, I see the picture below, but I need a way to use its value (0 here) in an equation.
-
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 get the value of a variable in the final solution via the variable's X attribute, e.g.:
x[(0,0,1)].X
In order to get the value of a linear expression at the current solution, you can use the LinExpr.getValue method.
You can either save the LHS of your constraints to LinExpr objects first and then use them in constraints or use the Model.getRow method to retrieve the expression from the model. E.g.:
# assume c points to the constraint object whose LHS you want to evaluate
expr = m.getRow(c)
expr.getValue()You can also define other linear expressions as linear combinations of your variables that are not constraints and then evaluate them at the current solution. E.g.:
expr = quicksum(x[(i,j,k)] for i in N for j in N for k in B)
expr.getValue()0 -
Thank you so much. This was a big help.
0 -
This is very useful information, Thanks for Sharing.
0
Post is closed for comments.
Comments
4 comments