Gurobi python获取定义变量的值

Abh*_*kar 6 python mathematical-optimization linear-programming gurobi

如何在gurobi python中获取我之前定义的变量(使用addVar)的值?我需要比较gurobi变量的值,然后执行计算以达到我的目标变量.在优化之前必须完成同样的工作.

Dav*_*hme 8

你有两个选择.最直接的方法是保存对返回的Var对象的引用Model.addVar.另一种方法是使用addVar中的name参数为变量命名,然后使用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
Run Code Online (Sandbox Code Playgroud)