Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gurobi python get value of the defined variable

How do i get the value of the variable that i have define previously(using addVar) in gurobi python? I need to compare the value of the gurobi variable and then perform calculations to reach to my objective variable. The same has to be done before optimization.

like image 678
Abhinav Yashkar Avatar asked May 08 '13 09:05

Abhinav Yashkar


1 Answers

You have two options. The most straightforward is to save a reference to the Var object returned by Model.addVar. Another way is to give a name your variables with the name parameter in addVar, then retrieve the variable with 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
like image 130
David Nehme Avatar answered Sep 20 '22 17:09

David Nehme