Map Coloring

You need to use no more than 4 colors (blue, red, green, or yellow) to color six European countries: Belgium, Denmark, France, Germany, Luxembourg, and the Netherlands. These is one condition: neighboring countries cannot use the same color. Source

To define and solve this problem we created Java class MapColoring that extends JavaSolver:

The method define() creates constrained integer variables for each country with possible values from 0 to (number of colors – 1). Then we posted constraints csp.post(country1, “!=”, country2) for all neighboring countries.  To solve the problem we called the standard JavaSolver’s method solve(). We added an optional method saveSolution(Solution solution) to print a found solution in a nicer way:

If you replace solve() to solveAll(), Java Solver will generate 144 different solutions.