
This is an advanced version of “Map Coloring” problem that could use 4 colors. Now we have only 3 colors (blue, red, green). It is certainly not enough to color six European countries: Belgium, Denmark, France, Germany, Luxembourg, and the Netherlands with no neighboring countries using the same color. So, some neighboring counties may have the same colors but there is a relative cost for such violations:
- France – Luxembourg: $257
- Luxembourg – Germany: $904
- Luxembourg – Belgium: $568
We need to find a solution that satisfies as many constraints as possible while keeping the total violation cost as low as possible.
This time we will use actual colors as the problem input:

Here is the corresponding Glossary:

The following table defines unknown decision variables for country colors:

We will split all constraints into Hard (must be satisfied) and Soft (could be violated). First we will post hard constraints

Then we will define soft constraint without posting them:

Now we can post these constraints as “SOFT” we their violation costs:

These three constraints were added to the array “Constraint Violations”. Now we can define the objective variable “Total Constraint Violation”:

Finally, we can use the above table to Define and Solve the problem by using the predefined method “DefineAndSolve” with these two tables:

Here are the execution results:

Only the “cheapest” constraint “France != Luxembourg[257]” has been violated.
The proper standard project is available in openrules.solver/MapColoringPreferences.


