callback with variables based on the edges of a graph
回答済みHi
I have a callback to solve a graph problem, the variable in my model is x[i,j] where (i,j) is an edge in the input graph, now the edges of the graph are defined as follow:
E = gp.tuplelist([(2, 1), (17, 6), (33, 23), (1, 5), (1, 22), (10, 34), (7, 35), (21, 35)])
my problem that if I want the values of the variables after solving the model every time I am using :
vars=model.cbGetSolution(model._vars)
I am getting the values of the variables but not in the right order, if I just print out the variables I will get :
[ <gurobi.Var x1,5,>, <gurobi.Var x5,1,>, <gurobi.Var x1,22,>, <gurobi.Var x22,1,>]
which is not in the same order as the edges define, moreover I cant use the option :
var[0].X
-
正式なコメント
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 Ahmad,
How are you creating the variables based on this tuplelist E? From the code snippets you shared, it's hard to see what might cause the different ordering.
Cheers,
Matthias0 -
Hi Matthias
I simply do the following :
x = {}
for (i, j) in E:
x[i, j] = model.addVar(vtype=GRB.BINARY, name="x%d,%d," % (i, j))or
x = model.addVars(E, vtype=GRB.BINARY, name='x')
0 -
Hi Ahmad,
dictionaries in Python do not necessarily preserve the order of their elements. You might want to use an OrderedDict, instead:
fromcollectionsimportOrderedDict
[...]
x = OrderedDict()
for (i, j) in E:
x[i, j] = m.addVar(vtype=GRB.BINARY, name="x%d,%d," % (i, j))Cheers,
Matthias0
投稿コメントは受け付けていません。
コメント
4件のコメント