A Note On Constraint Programming (CP)
A Note On Constraint Programming (CP)
Modeling Languages
For example: A company runs three 8-hour shifts per day and assigns three of its four
employees to different shifts each day, while giving the fourth the day off. The number of
1
possible schedules is: each day, there are 4! = 4 * 3 * 2 * 1 = 24 possible employee assignments,
so the number of possible weekly schedules is 24# , which is over 4.5 billion. Usually there will
be other constraints that reduce the number of feasible solutions — for example, that each
employee works at least a minimum number of days per week. The CP method keeps track of
which solutions remain feasible when you add new constraints, which makes it a powerful tool
for solving large, real-world scheduling problems.
2
stated over this variable. This results in more domain reduction. These domain
reductions are communicated to appropriate constraints. The process continues until no
more various domains can be reduced or until empty solution is reached.
• CP Search: CP searching generates combinations of values for D.V.s by means of
constructive strategies. These strategies are executed and guided towards optimal
solutions in order to converge rapidly.
CP vs MP:
CP MP
Supports logical constraints (&&, ||, !=, ==), Supports only linear constraints, linearized
modulo, integer division, element logical constraints, quadratic convex
expression constraints
Problem. (Map coloring problem) The problem involves choosing colors for the countries on
a map so that at most four colors are used (e.g, blue, white, yellow, green), and no neighboring
countries are the same color. The object is to find a solution for a map coloring problem with
six countries: Belgium, Denmark, France, Germany, Luxembourg, and the Netherlands. Use
CP Optimizer on CPLEX to solve this problem.
3
#Holland and Netherlands are same country. # The colors shown in the map are just for better
visualization. It has nothing to do with the colors used in the given problem.
From the political map of Europe, we have the following information regarding neighbours:
• Denmark: Germany
4
Solution.
using CP;
range r = 0..3;
subject to {
Belgium != France;
Belgium != Germany;
Belgium != Netherlands;
Belgium != Luxembourg;
Denmark != Germany;
France != Germany;
France != Luxembourg;
Germany != Luxembourg;
Germany != Netherlands;
}
execute {
writeln("Belgium: ", Names[Belgium]);
writeln("Denmark: ", Names[Denmark]);
writeln("France: ", Names[France]);
writeln("Germany: ", Names[Germany]);
writeln("Luxembourg: ", Names[Luxembourg]);
writeln("Netherlands: ", Names[Netherlands]);
}
tuple resultT {
string name;
string value;
};
{resultT} solution = {};
execute{
solution.add("Belgium", Names[Belgium]);
solution.add("Denmark", Names[Denmark]);
solution.add("France", Names[France]);
solution.add("Germany", Names[Germany]);
solution.add("Luxembourg", Names[Luxembourg]);
solution.add("Netherlands", Names[Netherlands]);
writeln(solution);
}