how to get the variable values by indices
回答済みI have a list containing the values of the variable as follows:
[<gurobi.Var x1,2,>, <gurobi.Var x2,1,>, <gurobi.Var x10,16,>, <gurobi.Var x16,10,>, <gurobi.Var x18,24,>, <gurobi.Var x24,18,>]
where I have used getVars() to get these values :
x = model.getVars()
there is any way to access the variables by the indices, what I mean I want the value of the first x and use it as follows :
x[1,2].X
0
-
正式なコメント
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?. -
The best way to do this is to add the variables to the model using Model.addVars(). The return value is a tupledict that maps the variable indices to the Var objects, just like you want. For example:
K = [(1, 2),
(2, 1),
(16, 10),
(18, 24),
(24, 18)]
# x is a tupledict mapping tuples in K to Var objects
# Access Var objects with x[1, 2], x[2, 1], etc.
x = model.addVars(K, name='x')
model.optimize()
print(x[1, 2].X)0
投稿コメントは受け付けていません。
コメント
2件のコメント