Python interface
Once the optimization is complete, we can query the values of the attributes. In particular, we can query the VarName
and X
variable attributes to obtain the name and solution value for each variable:
for v in m.getVars(): print('%s %g' % (v.VarName, v.X))
We can also query the ObjVal
attribute on the model to obtain the objective value for the current solution:
print('Obj: %g' % m.ObjVal)
The names and types of all model, variable, and constraint attributes can be found in the online Python documentation. Type help(GRB.Attr)
in the Gurobi Shell for details.
Python matrix interface
Once the optimization is complete, we can query the values of the attributes. In particular, we can query the X
variable attributes to obtain the solution value for each variable:
print(x.X)
We can also query the ObjVal
attribute on the model to obtain the objective value for the current solution:
print('Obj: %g' % m.ObjVal)
The names and types of all model, variable, and constraint attributes can be found in the online Python documentation. Type help(GRB.Attr)
in the Gurobi Shell for details.
C interface
An important model attribute is the value of the objective function for the computed solution. This is accessed through this call:
error = GRBgetdblattr(model, GRB_DBL_ATTR_OBJVAL, &objval); if (error) goto QUIT;
Note that this call would return a non-zero error result if no solution was found for this model.
Once we know that the model was solved, we can extract the X
attribute of the model, which contains the value for each variable in the computed solution:
error = GRBgetdblattrarray(model, GRB_DBL_ATTR_X, 0, 3, sol); if (error) goto QUIT;
This routine retrieves the values of an array-valued attribute. The third and fourth arguments indicate the index of the first array element to be retrieved, and the number of elements to retrieve, respectively. In this example we retrieve entries 0 through 2 (i.e., all three of them)
C++ interface
Once the optimization is complete, we can query the values of the attributes. In particular, we can query the VarName
and X
attributes to obtain the name and solution value of each variable:
cout << x.get(GRB_StringAttr_VarName) << " " << x.get(GRB_DoubleAttr_X) << endl; cout << y.get(GRB_StringAttr_VarName) << " " << y.get(GRB_DoubleAttr_X) << endl; cout << z.get(GRB_StringAttr_VarName) << " " << z.get(GRB_DoubleAttr_X) << endl;
We can also query the ObjVal
attribute on the model to obtain the objective value for the current solution:
cout << "Obj: " << model.get(GRB_DoubleAttr_ObjVal) << endl;
The names and types of all model, variable, and constraint attributes can be found in the Attributes
section of the Gurobi Reference Manual.
Java interface
Once the optimization is complete, we can query the values of the attributes. In particular, we can query the VarName
and X
attributes to obtain the name and solution value for each variable:
System.out.println(x.get(GRB.StringAttr.VarName) + " " +x.get(GRB.DoubleAttr.X));
We can also query the ObjVal
attribute on the model to obtain the objective value for the current solution:
System.out.println("Obj: " + model.get(GRB.DoubleAttr.ObjVal));
The names and types of all model, variable, and constraint attributes can be found in the Attributes
section of the Gurobi Reference Manual.
.NET interface
Once the optimization is complete, we can query the values of the attributes (which are implemented as .NET properties). In particular, we can query the VarName
and X
attributes to obtain the name and solution value for each variable:
Console.WriteLine(x.VarName + " " + x.X); Console.WriteLine(y.VarName + " " + y.X);
We can also query the ObjVal
attribute on the model to obtain the objective value for the current solution:
Console.WriteLine("Obj: " + model.ObjVal);
The names and types of all model, variable, and constraint attributes can be found in the Attributes section of the Gurobi Reference Manual.
MATLAB interface
The gurobi()
function returns a struct
as its result. This struct contains a number of fields, where each field contains information about the computed solution. The available fields depend on the result of the optimization, the type of model that was solved (LP, QP, QCP, SOCP, or MIP), and the algorithm used to solve the model. The returned struct
will always contain a status
field, which indicates whether Gurobi was able to compute an optimal solution to the model. You should consult the Status Codes section of the Gurobi Reference Manual for a complete list of all possible status codes. If Gurobi was able to find a solution to the model, the return value will also include objval
and x
fields. The former gives the objective value for the computed solution, and the latter is the computed solution vector (one entry per column of the constraint matrix). For continuous models, we will also return dual information (reduced costs and dual multipliers), and possibly an optimal basis. For a list of all possible fields and details about when you will find them populated, refer to the documentation for the gurobi() function in the reference manual.
In our example, we simply print the optimal objective value (result.objval
) and the optimal solution vector (result.x
).
R interface
The gurobi()
function returns a list as its result. This list contains a number of components, where each component contains information about the computed solution. The available components depend on the result of the optimization, the type of model that was solved (LP, QP, SOCP, or MIP), and the algorithm used to solve the model. This result list will always contain an integer status
component, which indicates whether Gurobi was able to compute an optimal solution to the model. You should consult the Status Codes section of the Gurobi Reference Manual for a complete list of all possible status codes. If Gurobi was able to find a solution to the model, the return value will also include objval
and x
components. The former gives the objective value for the computed solution, and the latter is the computed solution vector (one entry per column of the constraint matrix). For continuous models, we will also return dual information (reduced costs and dual multipliers), and possibly an optimal basis. For a list of all possible fields and details about when you will find them populated, refer to the documentation for the gurobi() function in the reference manual.
In our example, we simply print the optimal objective value (result$objval
) and the optimal solution vector (result$x
).
Comments
0 comments
Please sign in to leave a comment.