Information associated with the model or specific variables/constraints can be accessed via attributes. Variable values at the current solution are accessed via the X attribute. Note that the X attribute is stored as a "double". This is independent of the variable type (binary, integer, continuous, etc.).
Before retrieving variable values, you may want to query the model status to ensure the model is solved as expected.
The examples below show how to retrieve variable values in various programming languages.
Python
To retrieve the value of a single variable, query the X attribute of the corresponding Var object. For example:
model.optimize()
# Print the values of all variables
for v in model.getVars():
print(f"{v.VarName} = {v.X}")
To retrieve the values of multiple variables at once, use Model.getAttr(). For example:
model.optimize()
all_vars = model.getVars()
values = model.getAttr("X", all_vars)
names = model.getAttr("VarName", all_vars)
for name, val in zip(names, values):
print(f"{name} = {val}")
For more information, see:
- Reporting the results in Tutorial: Getting Started with the Gurobi Python API
- Python examples (particularly mip1.py)
- Python API Overview
C
To retrieve the value of a single variable, query the X attribute of the variable using GRBgetdblattrelement(). For example:
error = GRBoptimize(model);
if (error) goto QUIT;
int numvars, i;
double value;
char* varname;
error = GRBgetintattr(model, GRB_INT_ATTR_NUMVARS, &numvars);
if (error) goto QUIT;
/* Print the values of all variables */
for (i = 0; i < numvars; i++) {
error = GRBgetdblattrelement(model, GRB_DBL_ATTR_X, i, &value);
if (error) goto QUIT;
error = GRBgetstrattrelement(model, GRB_STR_ATTR_VARNAME, i, &varname);
if (error) goto QUIT;
printf("%s = %.0f\n", varname, value);
}
To retrieve the values of multiple variables at once, use GRBgetdblattrarray(). For example:
error = GRBoptimize(model);
if (error) goto QUIT;
int numvars, i;
double* values = NULL;
char** varnames = NULL;
error = GRBgetintattr(model, GRB_INT_ATTR_NUMVARS, &numvars);
if (error) goto QUIT;
values = (double *) malloc(numvars * sizeof(double));
varnames = (char **) malloc(numvars * sizeof(char *));
error = GRBgetdblattrarray(model, GRB_DBL_ATTR_X, 0, numvars, values);
if (error) goto QUIT;
error = GRBgetstrattrarray(model, GRB_STR_ATTR_VARNAME, 0, numvars, varnames);
if (error) goto QUIT;
/* Print the values of all variables */
for (i = 0; i < numvars; i++) {
printf("%s = %.0f\n", varnames[i], values[i]);
}
free(values);
free(varnames);
For more information, see:
- Reporting the results in Tutorial: Getting Started with the Gurobi C API
- C examples (particularly mip1_c.c)
- C API Overview
C++
To retrieve the value of a single variable, query the X attribute of the corresponding GRBVar object via GRBVar::get(). For example:
model.optimize();
GRBVar* vars = NULL;
vars = model.getVars();
// Print the values of all variables
for (int i = 0; i < model.get(GRB_IntAttr_NumVars); i++) {
cout << vars[i].get(GRB_StringAttr_VarName) << " = " << vars[i].get(GRB_DoubleAttr_X) << endl;
}
To retrieve the values of multiple variables at once, use GRBModel::get(). For example:
model.optimize();
GRBVar* vars = NULL;
double* values = NULL;
string* names = NULL;
int numVars = model.get(GRB_IntAttr_NumVars);
vars = model.getVars();
values = model.get(GRB_DoubleAttr_X, vars, numVars);
names = model.get(GRB_StringAttr_VarName, vars, numVars);
// Print the values of all variables
for (int i = 0; i < numVars; i++) {
cout << names[i] << " = " << values[i] << endl;
}
For more information, see:
- Reporting the results in Tutorial: Getting Started with the Gurobi C++ API
- C++ examples (particularly mip1_c++.cpp)
- C++ API Overview
Java
To retrieve the value of a single variable, query the X attribute of the corresponding GRBVar object via GRBVar.get(). For example:
model.optimize();
// Print the values of all variables
for (GRBVar v : model.getVars()) {
System.out.println(v.get(GRB.StringAttr.VarName) + " = " + v.get(GRB.DoubleAttr.X));
}
To retrieve the values of multiple variables at once, use GRBModel.get(). For example:
model.optimize();
GRBVar vars[] = model.getVars();
double values[] = model.get(GRB.DoubleAttr.X, vars);
String names[] = model.get(GRB.StringAttr.VarName, vars);
// Print the values of all variables
for (int i = 0; i < model.get(GRB.IntAttr.NumVars); i++) {
System.out.println(names[i] + " = " + values[i]);
}
For more information, see:
- Reporting the results in Tutorial: Getting Started with the Gurobi Java API
- Java examples (particularly Mip1.java)
- Java API Overview
C#
To retrieve the value of a single variable, query the X property of the corresponding GRBVar object. For example:
model.Optimize();
GRBVar[] vars = model.GetVars();
// Print the values of all variables
for (int i = 0; i < model.NumVars; i++) {
Console.WriteLine(vars[i].VarName + " = " + vars[i].X);
}
To retrieve the values of multiple variables at once, use GRBModel.Get(). For example:
model.Optimize();
GRBVar[] vars = model.GetVars();
double[] values = model.Get(GRB.DoubleAttr.X, vars);
string[] names = model.Get(GRB.StringAttr.VarName, vars);
// Print the values of all variables
for (int i = 0; i < model.NumVars; i++) {
Console.WriteLine(names[i] + " = " + values[i]);
}
For more information, see:
- Reporting the results in Tutorial: Getting Started with the Gurobi .NET API
- C# examples (particularly mip1_cs.cs)
- .NET API Overview
VB.NET
To retrieve the value of a single variable, query the X property of the corresponding GRBVar object. For example:
model.Optimize();
Dim vars() As GRBVar = model.GetVars()
' Print the values of all variables
For i As Integer = 0 To model.NumVars - 1
Console.WriteLine(vars(i).VarName & " = " & vars(i).X)
Next
To retrieve the values of multiple variables at once, use GRBModel.Get(). For example:
model.Optimize();
Dim vars() As GRBVar = model.GetVars()
Dim values() As Double = model.Get(GRB.DoubleAttr.X, vars)
Dim names() As String = model.Get(GRB.StringAttr.VarName, vars)
' Print the values of all variables
For i As Integer = 0 To model.NumVars - 1
Console.WriteLine(names(i) & " = " & values(i))
Next
For more information, see:
- Reporting the results in Tutorial: Getting Started with the Gurobi .NET API
- Visual Basic examples (particularly mip1_vb.vb)
- .NET API Overview
MATLAB
To retrieve the array of solution values, query the x
field of the struct returned by the gurobi() function. For example:
result = gurobi(model, params);
% Print the values of all variables
disp(result.x)
For more information, see:
- Reporting the results in Tutorial: Getting Started with the Gurobi MATLAB API
- MATLAB examples (particularly mip1.m)
- MATLAB API Overview
R
To retrieve the vector of solution values, query the x-named component of the list returned by the gurobi() function. For example:
result <- gurobi(model, params)
# Print the values of all variables
print(result$x)
For more information, see:
- Reporting the results in Tutorial: Getting Started with the Gurobi R API
- R examples (particularly mip.R)
- R API Overview
Comments
0 comments
Article is closed for comments.