如何获取我之前在 gurobi python 中定义的变量值(使用 addVar)?我需要比较 gurobi 变量的值,然后执行计算以达到我的目标变量。在优化之前也必须这样做。
最佳答案
您有两个选择。最直接的方法是保存对 Model.addVar
返回的 Var 对象的引用。另一种方法是在 addVar 中使用名称参数为变量命名,然后使用 Model.getVarByName 检索变量。
from gurobipy import *
a_var = m.addVar(name="variable.0")
# ...
a_var_reference = m.getVarByName("variable.0")
# a_var and a_var_reference refer to the same object
m.optimize()
#obtain the value of a_var in the optimal solution
if m.Status == GRB.OPTIMAL:
print a_var.X
关于python - Gurobi python获取定义变量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16436834/