-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathc-photo.tex.in
executable file
·258 lines (236 loc) · 8.01 KB
/
c-photo.tex.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
% -*- mode: LaTeX; -*-
\chapter{Photo alignment}
\label{chap:c:photo}
%% FILES: CHAPTERONLY
This chapter shows how to use reified constraints for solving
an overconstrained problem.
\section{Problem}
Betty, Chris, Donald, Fred, Gary, Mary, Paul, Peter, and Susan
want to align in a row for taking a photo. They have the
following preferences:
\begin{enumerate}
\item Betty wants to stand next to Donald, Gary, and Peter.
\item Chris wants to stand next to Gary and Susan.
\item Donald wants to stand next to Fred and Gary.
\item Fred wants to stand next to Betty and Gary.
\item Gary wants to stand next to Mary and Betty.
\item Mary wants to stand next to Betty and Susan.
\item Paul wants to stand next to Donald and Peter.
\item Peter wants to stand next to Susan and Paul.
\end{enumerate}
These preferences are obviously not satisfiable all at once (e.g., Betty
cannot possibly stand next to three people at once). The
problem is \emph{overconstrained}. To solve an overconstrained
problem, we turn it into an optimization problem: The task is to
find an alignment that violates as few preferences as possible.
\section{Model}
We model the photo alignment as an array of integer variables
\?pos? such that \?pos[p]? represents the position of person \?p?
in the final left-to-right order. The outline of a script for this problem is
shown in \autoref{fig:c:photo:script}.
The \?cost()? function as required by the class \?MinimizeScript?
(see \autoref{sec:m:driver:script}) just returns the number of
violations.
\begin{figure}
\insertlitcode{photo}
\caption{A script for the photo alignment problem}
\label{fig:c:photo:script}
\end{figure}
There are only two hard constraints for this model: no person can be in more
than one place, and no two persons can stand in the same place. The first
constraint is enforced automatically by the choice of variables, as each
\?pos? variable represents the unique position of a person (see
also \autoref{tip:c:warehouses:varchoice}). For the second
constraint, the variables in the \?pos? array
must be pairwise distinct (see \autoref{sec:m:integer:distinct}):
\insertlitcode{photo:constrain positions}
We choose the bounds consistent variant of \?distinct? (by giving
the extra argument \?IPL_BND?, see \autoref{sec:m:integer:ipl})
as also the other propagators perform only bounds reasoning.
The remaining constraints implement the preferences and turn them
into a measure of \emph{violation}, which expresses how many
preferences are not fulfilled in a solution. A preference $(i,j)$
is not fulfilled if the distance between the positions of person
$i$ and person $j$ is greater than one. This can be implemented
using a linear constraint, an absolute value constraint, and a
reified constraint for each preference, as well as one linear
constraint that constrains the sum of the violations:
\insertlitcode{photo without modeling support}
Using the MiniModel library (see \autoref{fig:m:minimodel:bool}
and \autoref{sec:m:minimodel:exprrel}) yields more
compact and readable code:
\insertlitcode{photo:compute violations}
We can observe that this problem has a symmetry, as reversing a
solution yields again a solution. Symmetric solutions like this
can be ruled out by arbitrarily picking two persons, and always placing one somewhere to the left of the other. For example, let us always place Betty somewhere to the left of Chris:
\insertlitcode{photo:symmetry breaking}
\section{More information}
This case study is also available as a Gecode example, see
\gecoderef[example]{photo}.
\begin{litcode}{photo}{tack}
\begin{litblock}{anonymous}
#include <gecode/driver.hh>
#include <gecode/int.hh>
#include <gecode/minimodel.hh>
using namespace Gecode;
const char* names[] =
{"Betty","Chris","Donald","Fred","Gary",
"Mary","Paul","Peter","Susan"};
\end{litblock}
enum {
Betty, Chris, Donald, Fred, Gary,
Mary, Paul, Peter, Susan
};
const int n = 9;
const int n_prefs = 17;
int spec[n_prefs][2] = {
{Betty,Donald}, {Betty,Gary}, {Betty,Peter},
{Chris,Gary}, {Chris,Susan},
{Donald,Fred}, {Donald,Gary},
{Fred,Betty}, {Fred,Gary},
{Gary,Mary}, {Gary,Betty},
{Mary,Betty}, {Mary,Susan},
{Paul,Donald}, {Paul,Peter},
{Peter,Susan}, {Peter,Paul}
};
class Photo : public IntMinimizeScript {
IntVarArray pos;
IntVar violations;
public:
Photo(const Options& opt)
: IntMinimizeScript(opt),
pos(*this,n,0,n-1), violations(*this,0,n_prefs) {
\begin{litblock}{constrain positions}
distinct(*this, pos, IPL_BND);
\end{litblock}
\begin{litblock}{compute violations}
BoolVarArgs viol(n_prefs);
for (int i=0; i<n_prefs; i++) {
viol[i] = expr(*this, abs(pos[spec[i][0]]-pos[spec[i][1]]) > 1);
}
rel(*this, violations == sum(viol));
\end{litblock}
\begin{litblock}{symmetry breaking}
rel(*this, pos[Betty] < pos[Chris]);
\end{litblock}
\begin{litblock}{anonymous}
branch(*this, pos, INT_VAR_NONE(), INT_VAL_MIN());
\end{litblock}
}
virtual IntVar cost(void) const {
return violations;
}
\begin{litblock}{anonymous}
Photo(Photo& p) : IntMinimizeScript(p) {
pos.update(*this, p.pos);
violations.update(*this, p.violations);
}
virtual Space* copy(void) {
return new Photo(*this);
}
virtual void print(std::ostream& os) const {
if (pos.assigned()) {
os << "\tOrder: ";
int order[n];
for (int i=0; i<n; i++)
order[pos[i].val()] = i;
for (int i=0; i<n; i++)
os << names[order[i]] << " ";
} else {
os << "\tPositions: " << pos;
}
os << std::endl << "\tViolations: " << violations << std::endl;
}
\end{litblock}
};
\begin{litblock}{anonymous}
int main(int argc, char* argv[]) {
Options opt("Photo");
opt.solutions(0);
opt.parse(argc,argv);
IntMinimizeScript::run<Photo,BAB,Options>(opt);
return 0;
}
\end{litblock}
\end{litcode}
\begin{litcode}{photo without modeling support}{tack}
\begin{litblock}{anonymous}
#include <gecode/driver.hh>
#include <gecode/int.hh>
#include <gecode/minimodel.hh>
using namespace Gecode;
const char* names[] =
{"Betty","Chris","Donald","Fred","Gary",
"Mary","Paul","Peter","Susan"};
enum {
Betty, Chris, Donald, Fred, Gary,
Mary, Paul, Peter, Susan
};
const int n = 9;
const int n_prefs = 17;
int spec[n_prefs][2] = {
{Betty,Donald}, {Betty,Gary}, {Betty,Peter},
{Chris,Gary}, {Chris,Susan},
{Donald,Fred}, {Donald,Gary},
{Fred,Betty}, {Fred,Gary},
{Gary,Mary}, {Gary,Betty},
{Mary,Betty}, {Mary,Susan},
{Paul,Donald}, {Paul,Peter},
{Peter,Susan}, {Peter,Paul}
};
class Photo : public IntMinimizeScript {
IntVarArray pos;
IntVar violations;
public:
Photo(const Options& opt)
: IntMinimizeScript(opt),
pos(*this,n,0,n-1), violations(*this,0,n_prefs) {
distinct(*this, pos, IPL_BND);
\end{litblock}
BoolVarArgs viol(*this,n_prefs,0,1);
for (int i=0; i<n_prefs; i++) {
IntVar distance(*this,0,n), diff(*this,-n,n);
linear(*this, {1,-1},
IntVarArgs({pos[spec[i][0]],pos[spec[i][1]]}),
IRT_EQ, diff);
abs(*this, diff, distance);
rel(*this, distance, IRT_GR, 1, viol[i]);
}
linear(*this, viol, IRT_EQ, violations);
\begin{litblock}{anonymous}
rel(*this, pos[Betty] < pos[Chris]);
branch(*this, pos, INT_VAR_NONE(), INT_VAL_MIN());
}
virtual IntVar cost(void) const {
return violations;
}
Photo(Photo& p) : IntMinimizeScript(p) {
pos.update(*this, p.pos);
violations.update(*this, p.violations);
}
virtual Space* copy(void) {
return new Photo(*this);
}
virtual void print(std::ostream& os) const {
if (pos.assigned()) {
os << "\tOrder: ";
int order[n];
for (int i=0; i<n; i++)
order[pos[i].val()] = i;
for (int i=0; i<n; i++)
os << names[order[i]] << " ";
} else {
os << "\tPositions: " << pos;
}
os << std::endl << "\tViolations: " << violations << std::endl;
}
};
int main(int argc, char* argv[]) {
Options opt("Photo");
opt.solutions(0);
opt.parse(argc,argv);
IntMinimizeScript::run<Photo,BAB,Options>(opt);
return 0;
}
\end{litblock}
\end{litcode}