Inspecting the solution

Inspecting the solution

Once a model has been solved, you can inspect the values of the model variables in the optimal solution with the printAttr() method on the Model object:

gurobi> m.printAttr('X')
    Variable            X
       Dimes            2
    Quarters           53
     Dollars          100
          Cu        999.8
          Ni         46.9
          Zi           50
          Mn           30
This routine prints all non-zero values of the specified attribute X. Attributes play a major role in the Gurobi optimizer. We'll discuss them in more detail shortly.

You can also inspect the results of the optimization at a finer grain by retrieving a list of all the variables in the model using the getVars() method on the Model object (m.getVars() in our example):

gurobi> v = m.getVars()
gurobi> print(len(v))
The first command assigns the list of all Var objects in model m to variable v. The Python len() command gives the length of the array (our example model coins has 9 variables). You can then query various attributes of the individual variables in the list. For example, to obtain the variable name and solution value for the first variable in list v, you would issue the following command:
gurobi> print(v[0].varName, v[0].x)
Pennies 0.0
You can type help(Var) or help(v[0]) to get a list of all methods on a Var object. You can type help(GRB.Attr) to get a list of all attributes.