IBM ILOG Solver User's Manual > The Basics > Modeling and Solving a Simple Problem: Map Coloring > Solve

Solving a problem using constraint programming consists of finding a value for each variable so that all constraints are satisfied. 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.

How Solver finds a solution

Solver uses two techniques to find a solution: search strategies and constraint propagation.

For more information, see the section "Solve".

You use an instance of the class IloSolver to solve a problem expressed in a model. The constructor for IloSolver takes an instance of IloModel as a parameter.

Step 7   -  

Create an instance of IloSolver

Add the following code after the comment //Create an instance of IloSolver

    IloSolver solver(model);

You now use the member function IloSolver::solve, which solves a problem by using a default goal to launch the search. Goals are the mechanism by which Solver implements search strategies. But how do you decide what search strategy to use? The default goal defines the search strategy when you do not specify a goal. Using a default goal allows you to concentrate on modeling the problem, without knowing the details of the search strategy. The default goal is one of a set of predefined goals that Solver offers (more on this in Chapter 4, Searching with Predefined Goals: Magic Square). You can also write your own goals (more on this in Part IV, Extending the Library).

Step 8   -  

Search for a solution

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

    if (solver.solve()) 

The member function IloSolver::solve returns a Boolean value of type IloBool. If a solution is found, an IloTrue value is returned and the program displays the solution.

IloBool

IloBool is a type definition that represents Boolean values in Concert Technology.

Those values are IloTrue and IloFalse. Booleans are, in fact, integers of type IloInt. IloFalse is 0 (zero), and IloTrue is 1 (one).

The member function IloSolver::getValue returns the value Solver found for the variable. When you print the solution, you associate each value with the name of a color using the array Names[]. The value 0 is associated with Names[0], which is the first array element "blue"; the value 1 is associated with Names[1], which is the second array element "white"; and so on. The array Names[] is already created for you in the exercise code.

The member function IloAlgorithm::getStatus returns a status indicating information about the solution that Solver has found. IloAlgorithm::out is the communication stream for general output. These general purpose functions and streams are members of IloAlgorithm, the base class for algorithms in Concert Technology. You can use IloAlgorithm to employ different algorithms in solving problems represented with Concert Technology modeling classes. IloSolver is a subclass of IloAlgorithm.

      solver.out() << solver.getStatus() << " Solution" << endl;
      solver.out() << "Belgium:     " 
                   << Names[(IloInt)solver.getValue(Belgium)]     << endl;
      solver.out() << "Denmark:     " 
                   << Names[(IloInt)solver.getValue(Denmark)]     << endl;
      solver.out() << "France:      " 
                   << Names[(IloInt)solver.getValue(France)]      << endl;
      solver.out() << "Germany:     " 
                   << Names[(IloInt)solver.getValue(Germany)]     << endl;
      solver.out() << "Luxembourg:  " 
                   << Names[(IloInt)solver.getValue(Luxembourg)]  << endl;
      solver.out() << "Netherlands: " 
                   << Names[(IloInt)solver.getValue(Netherlands)] << endl;

Note
If there is more than one set of values for the variables that satisfy the constraints of the problem, there is more than one solution. Within your problem, there may be certain criteria that make one such set of values more appropriate than another as a solution. This appropriateness is usually measured in terms of a cost function that can be optimized. You can read more on this in Chapter 5, Using Objectives: Map Coloring with Minimum Colors.

Step 9   -  

Compile and run the program

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

FEASIBLE Solution
Belgium:     blue
Denmark:     blue
France:      white
Germany:     yellow
Luxembourg:  green
Netherlands: white

As you can see, all four colors are used.

The complete program is listed in "Complete program". You can also view it online in the YourSolverHome/examples/src/color.cpp file.