Solve

Once the house building with workers 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 16: 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 17: Search for a solution

Add the following code after the comment //Search for a solution


    cp.setParameter(IloCP::FailLimit, 30000);
    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 (i=0; i<allTasks.getSize(); ++i) {
        cp.out() << cp.domain(allTasks[i]) << std::endl;
      }

Step 18: Compile and run the program

Compile and run the program. You should get the following results:


Solution with objective 13852:
H0-masonry  [1: 1 -- 35 --> 36]
H0-carpentry[1: 36 -- 15 --> 51]
H0-plumbing [1: 36 -- 40 --> 76]
H0-ceiling  [1: 76 -- 15 --> 91]
H0-roofing  [1: 51 -- 5 --> 56]
H0-painting [1: 91 -- 10 --> 101]
H0-windows  [1: 101 -- 5 --> 106]
H0-facade   [1: 97 -- 10 --> 107]
H0-garden   [1: 107 -- 5 --> 112]
H0-moving   [1: 112 -- 5 --> 117]
H1-masonry  [1: 138 -- 35 --> 173]
H1-carpentry[1: 192 -- 15 --> 207]
H1-plumbing [1: 197 -- 40 --> 237]
H1-ceiling  [1: 237 -- 15 --> 252]
H1-roofing  [1: 207 -- 5 --> 212]
H1-painting [1: 252 -- 10 --> 262]
H1-windows  [1: 262 -- 5 --> 267]
H1-facade   [1: 252 -- 10 --> 262]
H1-garden   [1: 262 -- 5 --> 267]
H1-moving   [1: 267 -- 5 --> 272]
H2-masonry  [1: 216 -- 35 --> 251]
H2-carpentry[1: 268 -- 15 --> 283]
H2-plumbing [1: 273 -- 40 --> 313]
H2-ceiling  [1: 313 -- 15 --> 328]
H2-roofing  [1: 283 -- 5 --> 288]
H2-painting [1: 328 -- 10 --> 338]
H2-windows  [1: 338 -- 5 --> 343]
H2-facade   [1: 333 -- 10 --> 343]
H2-garden   [1: 328 -- 5 --> 333]
H2-moving   [1: 343 -- 5 --> 348]
H3-masonry  [1: 59 -- 35 --> 94]
H3-carpentry[1: 115 -- 15 --> 130]
H3-plumbing [1: 120 -- 40 --> 160]
H3-ceiling  [1: 160 -- 15 --> 175]
H3-roofing  [1: 130 -- 5 --> 135]
H3-painting [1: 175 -- 10 --> 185]
H3-windows  [1: 185 -- 5 --> 190]
H3-facade   [1: 180 -- 10 --> 190]
H3-garden   [1: 175 -- 5 --> 180]
H3-moving   [1: 190 -- 5 --> 195]
H4-masonry  [1: 291 -- 35 --> 326]
H4-carpentry[1: 345 -- 15 --> 360]
H4-plumbing [1: 350 -- 40 --> 390]
H4-ceiling  [1: 390 -- 15 --> 405]
H4-roofing  [1: 360 -- 5 --> 365]
H4-painting [1: 405 -- 10 --> 415]
H4-windows  [1: 415 -- 5 --> 420]
H4-facade   [1: 390 -- 10 --> 400]
H4-garden   [1: 400 -- 5 --> 405]
H4-moving   [1: 420 -- 5 --> 425]

The complete program can be viewed in the <Install_dir>/cpoptimizer/examples/src/cpp/sched_sequence.cpp file.