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.

Try Gurobi for Free

Choose the evaluation license that fits you best, and start working with our Expert Team for technical guidance and support.

Evaluation License
Get a free, full-featured license of the Gurobi Optimizer to experience the performance, support, benchmarking and tuning services we provide as part of our product offering.
Academic License
Gurobi supports the teaching and use of optimization within academic institutions. We offer free, full-featured copies of Gurobi for use in class, and for research.
Cloud Trial

Request free trial hours, so you can see how quickly and easily a model can be solved on the cloud.