Solve
Once the house building with worker allocation problem has been described and modeled, you use CP Optimizer classes and functions to solve the constraint programming problem.
You use an instance of the class IloCP to
solve a problem expressed in a model. The constructor for IloCP takes
an IloModel as its argument.
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 the problem contained in the model by using constructive
search and constraint propagation. The search for an optimal solution
in this problem could potentiality take a long time, so you place
a fail limit on the solve process. The search will stop when the fail
limit is reached, even if optimality of the current best solution
is not guaranteed.
Step 12: Search for a solution
Add the following code after the comment //Search
for a solution
cp.setParameter(IloCP::FailLimit, 10000);
if (cp.solve()) {
The member function IloCP::solve returns
a Boolean value of type IloBool. If a solution
is found, the value IloTrue is returned.
After a solution has been found, you can use the member
functions IloCP::getObjValue and IloCP::domain to
examine the solution. The stream IloAlgorithm::out is
the communication stream for general output. The code for displaying
the solution has been provided for you:
cp.out() << "Solution with objective " << cp.getObjValue() << ":" << std::endl;
for (IloInt i=0; i<allTasks.getSize(); ++i) {
if (cp.isPresent(allTasks[i]))
cp.out() << cp.domain(allTasks[i]) << std::endl;
}
Step 13: Compile and run the program
Compile and run the program. You should get the following results:
Solution with objective 357:
H0-masonry-Joe [1: 0 -- 35 --> 35]
H0-carpentry-Joe [1: 170 -- 15 --> 185]
H0-plumbing-Jack [1: 65 -- 40 --> 105]
H0-ceiling-Jack [1: 50 -- 15 --> 65]
H0-roofing-Joe [1: 215 -- 5 --> 220]
H0-painting-Jim [1: 65 -- 10 --> 75]
H0-windows-Joe [1: 250 -- 5 --> 255]
H0-facade-Joe [1: 265 -- 10 --> 275]
H0-garden-Jim [1: 220 -- 5 --> 225]
H0-moving-Jim [1: 275 -- 5 --> 280]
H1-masonry-Joe [1: 35 -- 35 --> 70]
H1-carpentry-Joe [1: 140 -- 15 --> 155]
H1-plumbing-Jack [1: 215 -- 40 --> 255]
H1-ceiling-Jack [1: 105 -- 15 --> 120]
H1-roofing-Joe [1: 225 -- 5 --> 230]
H1-painting-Jim [1: 120 -- 10 --> 130]
H1-windows-Joe [1: 240 -- 5 --> 245]
H1-facade-Joe [1: 275 -- 10 --> 285]
H1-garden-Jim [1: 255 -- 5 --> 260]
H1-moving-Jim [1: 285 -- 5 --> 290]
H2-masonry-Joe [1: 105 -- 35 --> 140]
H2-carpentry-Joe [1: 155 -- 15 --> 170]
H2-plumbing-Jack [1: 175 -- 40 --> 215]
H2-ceiling-Joe [1: 185 -- 15 --> 200]
H2-roofing-Joe [1: 230 -- 5 --> 235]
H2-painting-Jim [1: 200 -- 10 --> 210]
H2-windows-Joe [1: 245 -- 5 --> 250]
H2-facade-Joe [1: 255 -- 10 --> 265]
H2-garden-Jim [1: 235 -- 5 --> 240]
H2-moving-Jim [1: 265 -- 5 --> 270]
H3-masonry-Joe [1: 70 -- 35 --> 105]
H3-carpentry-Joe [1: 200 -- 15 --> 215]
H3-plumbing-Jack [1: 255 -- 40 --> 295]
H3-ceiling-Jack [1: 120 -- 15 --> 135]
H3-roofing-Joe [1: 220 -- 5 --> 225]
H3-painting-Jim [1: 135 -- 10 --> 145]
H3-windows-Joe [1: 235 -- 5 --> 240]
H3-facade-Joe [1: 295 -- 10 --> 305]
H3-garden-Jim [1: 295 -- 5 --> 300]
H3-moving-Jim [1: 305 -- 5 --> 310]
H4-masonry-Jack [1: 0 -- 35 --> 35]
H4-carpentry-Jim [1: 103 -- 15 --> 118]
H4-plumbing-Jack [1: 135 -- 40 --> 175]
H4-ceiling-Jack [1: 35 -- 15 --> 50]
H4-roofing-Jack [1: 295 -- 5 --> 300]
H4-painting-Jim [1: 93 -- 10 --> 103]
H4-windows-Joe [1: 305 -- 5 --> 310]
H4-facade-Jack [1: 300 -- 10 --> 310]
H4-garden-Jim [1: 300 -- 5 --> 305]
H4-moving-Jim [1: 310 -- 5 --> 315]
The complete program can be viewed online in the <Install_dir>/cpoptimizer/examples/src/cpp/sched_optional.cpp file.