classify decision variables according their value in the user callback
AnsweredHi,
1) For my callback function, I need to get the value of some decision variables rather than all of them and pass them into model.cbGetSolution. The getVars() method return all the values, but I need some of them. How can I solve this?
2) when I get the solution of these specific decision values, I need to classify them according to their values and index and put them into a new list
To illustrate my problem: I have these decision variables which are binary:
x[0,0], x[0,1], x[0,2],x[1,0],x[1,1],x[1,2]
y[0,0,0],y[0,0,1], y[0,1,1],y[0,2,0],y[1,0,0],y[1,0,1], y[1,1,1],y[1,2,0]
I need the solution value of y variables, and after getting these variables I want to classify them according to the first index, meaning
C[0,0]: y[0,0,0], y[0,2,0],
C[0,1]:y[0,0,1], y[0,1,1]
C[1,0]:y[1,0,0], y[1,2,0]
C[1,1]:y[1,1,1]
-
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 Amir,
You can access only the \(y\) variables via the cbGetSolution function. To achieve this, you will have to define them as a private model variable
import gurobipy as gp
from gurobipy import GRB
def myCallback(model, where):
if where == GRB.Callback.MIPSOL:
# get values of the y variables only
yvals = model.cbGetSolution(model._yvars)
print(yvals)
# do something with the values of y variables
m = gp.Model("myModel")
x = m.addVars(3, 3, vtype = GRB.BINARY, name="x")
y = m.addVars(3, 3, 3, vtype = GRB.BINARY, name="y")
# construct model
# [...]
# define the y variables as a private model variable
# to get access to them in the callback
m._yvars = y
m.optimize(myCallback)Best regards,
Jaromił0
Post is closed for comments.
Comments
2 comments