IBM ILOG Solver User's Manual > Local Search > Basic Local Search > Example: Map coloring > Finding a first solution

To find a first solution, we create an instance of IloSolver from the model, create a goal which instantiates the color of each country, and then have the solver execute it:

    IloSolver solver(model);
    IloGoal generate = IloGenerate(env, AllVars);
    if (!solver.solve(generate)) {
      throw "Could not find an initial solution";
    }

We now store this initial solution in an IloSolution object which we create as follows:

    IloSolution soln(env, "Colors");

We specify the variables whose values should be stored in the solution with the add member function:

    soln.add(AllVars);

Since it is useful in a local search process to know the quality of current solution, we add the objective:

    soln.add(obj);

This ensures that the value of the objective is stored together with the color of each country. Notice that we do not add the objective to the model, which would result in a search for the optimal solution when we call solver.solve(). We now store our first solution and display its quality:

    soln.store(solver);
    solver.out() << "1st solution: Objective value = "
                 << soln.getObjectiveValue() << endl;

When storing a solution, the algorithm is passed as a parameter. The solution uses this algorithm to retrieve the current values of the variables added to the solution.