Lecture CP 3
Lecture CP 3
Optimization
Constraint Programming: Part III
Goals of the Lecture
‣ Illustrating the rich modeling language
of constraint programming
‣ Key aspect of constraint programming
– ability to state complex, idiosyncratic
constraints
2
Magic Series
‣ A series S = (S0,...,Sn) is magic if Si represents
the number of occurrences of i in S
0 1 2 3 4
Occurrences ?
2 ?
1 ?
2 ?
0 ?
0
3
Magic Series and Reification
int n = 5;
range D = 0..n-1;
var{int} series[D] in D;
solve {
forall(k in D)
series[k] = sum(i in D) (series[i]=k);
}
‣ Reification
– the ability to transform a constraint
into a 0/1 variable
– the variable has the value 1 if the
constraint is true and 0 otherwise
4
Magic Series and Reification
series[0] = (series[0]=0)+(series[1]=0)+(series[2]=0)+(series[3]=0)+(series[4]=0);
series[1] = (series[0]=1)+(series[1]=1)+(series[2]=1)+(series[3]=1)+(series[4]=1);
series[2] = (series[0]=2)+(series[1]=2)+(series[2]=2)+(series[3]=2)+(series[4]=2);
series[3] = (series[0]=3)+(series[1]=3)+(series[2]=3)+(series[3]=3)+(series[4]=3);
series[4] = (series[0]=4)+(series[1]=4)+(series[2]=4)+(series[3]=4)+(series[4]=4);
0 1 2 3 4
Occurrences ? ? ? ? ?
5
Magic Series and Reification
series[0] = (series[0]=0)+(series[1]=0)+(series[2]=0)+(series[3]=0)+(series[4]=0);
series[1] = (series[0]=1)+(series[1]=1)+(series[2]=1)+(series[3]=1)+(series[4]=1);
series[2] = (series[0]=2)+(series[1]=2)+(series[2]=2)+(series[3]=2)+(series[4]=2);
series[3] = (series[0]=3)+(series[1]=3)+(series[2]=3)+(series[3]=3)+(series[4]=3);
series[4] = (series[0]=4)+(series[1]=4)+(series[2]=4)+(series[3]=4)+(series[4]=4);
‣ But
Whatnow
if series[0]=1?
series[1] > 0?
1 = 0 + (series[1]=0)+(series[2]=0)+(series[3]=0)+(series[4]=0);
(series[2]=0)+(series[3]=0)+(series[4]=0);
series[1] = 1 + (series[1]=1)+(series[2]=1)+(series[3]=1)+(series[4]=1);
series[2] = 0 + (series[1]=2)+(series[2]=2)+(series[3]=2)+(series[4]=2);
series[3] = 0 + (series[1]=3)+(series[2]=3)+(series[3]=3)+(series[4]=3);
series[4] = 0 + (series[1]=4)+(series[2]=4)+(series[3]=4)+(series[4]=4);
6
Reification
‣ What is happening behind the scene?
int n = 5;
range D = 0..n-1;
var{int} series[D] in D;
solve {
forall(k in D) {
var{int} b[D] in 0..1;
forall(i in D)
booleq(b[i],series[i],k);
series[k] = sum(i in D) b[i];
}
}
7
Stable Marriages
‣ Basic assumptions
– Every woman should
provide a ranking of the
men
– Every man provides a
ranking of the women
8
Stable Marriages
‣ A marriage between Hugh and Angelina is
stable provided that
– If Angelina prefers another man, say George,
over Hugh, then George must prefer his
spouse over Angelina
– If Hugh prefers another woman, say Julia,
over Angelina, then Julia must prefer her
spouse over Hugh
9
Stable Marriages
‣ What are the decision variables?
10
Stable Marriages
‣ Data and decision variables
wrank[Hugh,Julia] is the ranking
of Julia in Hugh’s preferences
int wrank[Men,Women];
int mrank[Women,Men];
...
var{Women} wife[Men];
var{Men} husband[Women];
11
Stable Marriages
solve {
forall(m in Men)
husband[wife[m]] = m;
forall(w in Women)
wife[husband[w]] = w;
...
}
12
Stable Marriages
solve {
forall(m in Men)
husband[wife[m]] = m;
m prefers w over his wife
forall(w in Women)
wife[husband[w]] = w;
13
Stable Marriages
solve {
forall(m in Men)
husband[wife[m]] = m;
forall(w in Women) w prefers her husband over m
wife[husband[w]] = w;
14
Stable Marriages
15
Stable Marriages
‣ Two interesting features
– Element constraint
• useful in many applications
– Logical combination of constraints
‣ The element constraint
– ability to index an array/matrix with a
variable or an expression containing
variables
‣ Logical combination of constraints
– can be handled by reification for
instance
16
The Basic Element Constraint
‣ x, y: variables Y 2 {0, 1, 2, 3, 4, 5}
‣ c is an array of integers
‣ constraint x = c[y] X 2 {3, 4, 5}
Array c
i 0 1 2 3 4 5
c[i] 3 4 5 5 4 3
X 6= 3
Y 6= 4
Y 6= 1
17
Citations
66ème Festival de Venise (Mostra)(http://www.flickr.com/photos/nicogenin/3902889281/) by Nicolas Genin (http://www.flickr.com/photos/nicogenin/) CC BY-SA
2.0 (http://creativecommons.org/licenses/by-sa/2.0/deed.en)
Clive Owen, (http://www.flickr.com/photos/oneras/251106343/) by Mario Antonio Pena Zapateira (http://www.flickr.com/photos/oneras/) CC BY-SA 2.0 (http://
creativecommons.org/licenses/by-sa/2.0/deed.en)
Halle Berry 10 by German Marin [CC BY 3.0 (http://creativecommons.org/licenses/by/3.0)], via Wikimedia Commons (http://upload.wikimedia.org/wikipedia/
commons/f/f8/Halle_Berry_10.jpg)
18