IBM ILOG Solver User's Manual > Local Search > Basic Local Search > Constructing moves > The map coloring example revisited

The map coloring example can also be greatly simplified by the use of IloSingleMove and IloImprove. The local search part of the code can now be written as follows:

    IloNHood nhood = IloFlip(env, AllVars);
    IloGoal move = IloSingleMove(env, soln, nhood, IloImprove(env));
    while (solver.solve(move)) {
      solver.out() << "Move made:    Objective value = "
                   << soln.getObjectiveValue() << endl;
    }

For reference, the entire example using IloSingleMove is given below:

#include <ilsolver/iimls.h>
 
ILOSTLBEGIN
 
typedef enum { COLOR_DIRECT, COLOR_DELTAS,
               COLOR_NHOOD, COLOR_SINGLEMOVE } ColorType;
 
const char* Names[] = {"blue", "white"}; 
 
void print(IloSolver solver, const char* name, IloIntVar var) { 
  solver.out() << name << Names[(IloInt)solver.getValue(var)] << endl;
}
 
int main() {
  IloEnv env;
  try {
    IloModel model(env);
    
    IloIntVar Belgium(env, 0, 1), Denmark(env, 0, 1), 
              France(env, 0, 1), Germany(env, 0, 1), 
              Netherlands(env, 0, 1), Luxembourg(env, 0, 1);
    IloIntVarArray AllVars(env);
    AllVars.add(Belgium);
    AllVars.add(Denmark);
    AllVars.add(France);
    AllVars.add(Germany);
    AllVars.add(Netherlands);
    AllVars.add(Luxembourg);
    model.add(AllVars);
    model.add(France != Belgium); 
    model.add(France != Germany); 
    model.add(Belgium != Netherlands); 
    model.add(Germany != Netherlands); 
    model.add(Germany != Denmark); 
    IloIntVar quality(env, 0, 20000);
    model.add(quality == 257 * (France != Luxembourg)
                      + 9043 * (Luxembourg != Germany)
                      +  568 * (Luxembourg != Belgium));
    IloObjective obj = IloMaximize(env, quality);
    IloSolver solver(model);
    IloGoal generate = IloGenerate(env, AllVars);
    if (!solver.solve(generate)) {
      throw "Could not find an initial solution";
    }
    IloSolution soln(env, "Colors");
    soln.add(AllVars);
    soln.add(obj);
    soln.store(solver);
    solver.out() << "1st solution: Objective value = "
                 << soln.getObjectiveValue() << endl;
    IloNHood nhood = IloFlip(env, AllVars);
    IloGoal move = IloSingleMove(env, soln, nhood, IloImprove(env));
    while (solver.solve(move)) {
      solver.out() << "Move made:    Objective value = "
                   << soln.getObjectiveValue() << endl;
    }
    solver.solve(IloRestoreSolution(env, soln));
    print(solver, "Belgium:     ", Belgium); 
    print(solver, "Denmark:     ", Denmark); 
    print(solver, "France:      ", France); 
    print(solver, "Germany:     ", Germany); 
    print(solver, "Netherlands: ", Netherlands); 
    print(solver, "Luxembourg:  ", Luxembourg); 
  }
  catch(IloException &ex) {
    cout << "Caught : " << ex << endl;
  }
  catch(const char *ex) {
    cout << "Caught : " << ex << endl;
  }
  env.end();
  return 0;
}
 
 

Privacy Policy