IBM ILOG Solver User's Manual > The Basics > Using Objectives: Map Coloring with Minimum Colors > Complete program

The complete map coloring with minimum colors program follows. You can also view it online in the file YourSolverHome/examples/src/colormin.cpp.

#include <ilsolver/ilosolverany.h>
ILOSTLBEGIN
 
char blue[]="blue";
char white[]="white";
char yellow[]="yellow";
 
void print(IloSolver solver, char* name, IloAnyVar color) {
        solver.out() << name << (char*)solver.getAnyValue(color) << endl;
}
 
 
int main(){
  IloEnv env;
  try {
    IloModel model(env);
    IloAnyArray Colors(env, 3, blue, white, yellow);
    IloAnyVar Belgium(env, Colors), Denmark(env, Colors),
              France(env, Colors), Germany(env, Colors),
              Luxembourg(env, Colors), Netherlands(env, Colors);
    model.add(France != Belgium);
    model.add(France != Germany);
    model.add(Belgium != Netherlands);
    model.add(Germany != Netherlands);
    model.add(Germany != Denmark);
    model.add(Germany != Belgium);
    IloObjective obj = IloMaximize(env, 9043 * (Luxembourg != Germany)
                                      +  568 * (Luxembourg != Belgium)
                                      +  257 * (Luxembourg != France));
    model.add(obj);
    IloSolver solver(model);
    if (solver.solve())
      {
      solver.out() << solver.getStatus() << " Solution" << endl;
      solver.out() << "Objective = " << solver.getValue(obj) << endl;
      print(solver, "Belgium:     ", Belgium);
      print(solver, "Denmark:     ", Denmark);
      print(solver, "France:      ", France);
      print(solver, "Germany:     ", Germany);
      print(solver, "Netherlands: ", Netherlands);
      print(solver, "Luxembourg:  ", Luxembourg);
    }
    else
      solver.out() << "No Solution" << endl;
  }
  catch (IloException& ex) {
    cerr << "Error: " << ex << endl;
  }
  env.end();
  return 0;
}
 
 

Results

Optimal Solution
Objective = 9611
Belgium:     white
Denmark:     blue
France:      blue
Germany:     yellow
Netherlands: blue
Luxembourg:  blue