Solve
Once the warehouse location problem has been described and modeled, you use CP Optimizer classes and functions to solve the constraint programming problem.
Solving the problem consists of finding values for all of the decision variables in such a way that the values simultaneously satisfy the constraints and minimize the objective representing the cost of the solution.
Step 11: Create an instance of IloCP
Add the following code after the comment //Create
an instance of IloCP
IloCP cp(model);
You now use the member function IloCP::solve,
which solves a problem by using constructive search and constraint
propagation.
Step 12: Search for a solution
Add the following code after the comment //Search
for a solution
if (cp.solve())
The member function IloCP::solve will
return a value of IloTrue if the optimizer
is able to find an optimal solution. To display the solution, you
use the member functions and streams IloAlgorithm::getStatus, IloAlgortihm::getValue and IloAlgorithm::out.
The code for displaying the solution is provided for you:
cp.out() << std::endl << "Optimal value: " << cp.getValue(obj) << std::endl;
for (j = 0; j < nbLocations; j++){
if (cp.getValue(open[j]) == 1){
cp.out() << "Facility " << j << " is open, it serves stores ";
for (i = 0; i < nbStores; i++){
if (cp.getValue(supplier[i]) == j)
cp.out() << i << " ";
}
cp.out() << std::endl;
}
}
Step 13: Compile and run the program
Compile and run the program. You should get the following results:
Optimal value: 1383
Facility 0 is open, it serves stores 2 5 7
Facility 1 is open, it serves stores 3
Facility 3 is open, it serves stores 0 1 4 6
As you can see, the optimal solution uses three warehouse sites: Bonn, Bordeaux, Paris. All stores are supplied by a supplier warehouse and the total cost is 1383.
The complete program can be viewed online in the <Install_dir>/cpoptimizer/examples/src/cpp/facility.cpp file.