Solve
Once the steel mill problem has been described and modeled, you use CP Optimizer classes and functions to solve the constraint programming problem.
Solving a problem using constraint programming consists of assigning a value to each decision variable so that all constraints are satisfied and minimize the objective representing the cost of the solution. You may not always know beforehand whether there is a solution that satisfies all the constraints of the problem. In some cases, there may be no solution. In other cases, there may be many solutions to a problem.
You use an instance of the class IloCP to
solve a problem expressed in a model. The constructor for IloCP takes
an instance of IloModel as an argument.
Step 10: Create an instance of IloCP
Add the following code after the comment //Create
an instance of IloCP
IloCP cp(model);
Since you know information about the structure of this
problem, you can tune the optimizer to help it perform better. Once
all the decision variables in the array where have
been assigned values, the values for all of the other variables in
the model will have been determined through constraint propagation.
A search strategy that assigns values to the variables in this array
first works well in this problem.
To tune the search to concentrate on the where variables,
you create an instance of the class IloSearchPhase which
takes the environment and the array of where variables
as arguments. This tuning object is then passed as an argument to
the IloCP::setSearchPhases method.
Step 11: Search for a solution
Add the following code after the comment //Search
for a solution
cp.setSearchPhases(IloSearchPhase(env, where));
if (cp.solve()){
cp.out() << "Optimal value: " << cp.getValue(obj) << std::endl;
for (m = 0; m < nbSlabs; m++) {
IloInt p = 0;
for (o = 0; o < nbOrders; o++)
p += cp.getValue(where[o]) == m;
if (p == 0) continue;
cp.out() << "Slab " << m << " is used for order";
if (p > 1) cp.out() << "s";
cp.out() << " :";
for (o = 0; o < nbOrders; o++) {
if (cp.getValue(where[o]) == m)
cp.out() << " " << o;
}
cp.out() << std::endl;
}
}
Step 12: Compile and run the program
Compile and run the program. You should get the following results:
Optimal value: 0
Slab 0 is used for orders : 0 8
Slab 1 is used for orders : 2 11
Slab 2 is used for orders : 5 6
Slab 3 is used for orders : 1 10
Slab 4 is used for orders : 3 9
Slab 5 is used for orders : 4 7
As you can see, sixslabs are used and the orders have been distributed such that there is no loss.
The complete program can be viewed online in the <Install_dir>/cpoptimizer/examples/src/cpp/steelmill.cpp file.