Once you run the code MyModel.optimize(); you will be able to solve the problem, and the objective value will be displayed in the command window automatically. However, the values of the variables will not be displayed automatically, and you need to write some code to get the results.
There are two ways that you could do this:
- Display the results in the command window. This works when there are not a lot of variables.
std::cout << "The value of the MyVariable: " << MyVariable.get(GRB_DoubleAttr_X) << endl;
- Save the results into a .txt file. This works well when there are a lot of variables. Actually, I suggest you save everything in this file, including the objective value, the solution time, and the values of all variables. This is because when the optimization problem is large, it would take very long to solve the problem. If you save everything, you don’t need to re-run the optimization problem each time when you want to analyze something else about this problem.
- First, we need to include a head file:
#include<fstream>
- Second, add the following after the problem is solved:
ofstream results;
results.open ("results.txt");
results << "The value of the MyVariable: " << MyVariable.get(GRB_DoubleAttr_X) << endl;
results.close();