Accessing indexed variables systematically after a run?
回答済みMy optimization problem has a large number of variables. A lot of them are indexed, for example, of the form x[i, j]. My optimization problem solves just fine. But I run into problems while accessing the variables. The m.getVars() method gets me a list of all variables and I can access their name and values using the v.varName and v.x methods. But I cannot find a way of doing a systematic search in the variable space. Particularly for indexed variables. I am using the Python interface and I need to access specific variables for specific post-processing.
P.S: I don't want to find the length of m.getVars() and go back and check how many variables of each kind I have to figure out their ranges. That seems too tedious.
Any help is appreciated.
-
正式なコメント
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?. -
To clarify, you are looking for a way to iterate over certain groups of variables in your model?
Model.addVars() returns a tupledict object containing the newly added variables. If you create a Python variable to store this tupledict, you can use it to iterate over the group of variables you are interested in. For example:
import gurobipy as gp
m = gp.Model()
x = m.addVars(3, name='x')
y = m.addVars(4, name='y')
z = m.addVars(5, 5, name='z')
m.optimize()
for v in x.values():
print(v.VarName, v.X)prints only the names and optimal values of the three \( \texttt{x} \) variables:
x[0] 0.0
x[1] 0.0
x[2] 0.00
投稿コメントは受け付けていません。
コメント
2件のコメント