1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3 * Main authors:
4 * Mikael Lagerkvist <lagerkvist@gecode.org>
5 *
6 * Copyright:
7 * Mikael Lagerkvist, 2009
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.org
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files (the
15 * "Software"), to deal in the Software without restriction, including
16 * without limitation the rights to use, copy, modify, merge, publish,
17 * distribute, sublicense, and/or sell copies of the Software, and to
18 * permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 *
32 */
33
34 #include <gecode/driver.hh>
35 #include <gecode/int.hh>
36 #include <gecode/minimodel.hh>
37
38 using namespace Gecode;
39
40 /**
41 * \brief %Options for %EFPA problems
42 *
43 * \relates EFPA
44 */
45 class EFPAOptions : public Options {
46 private:
47 Driver::UnsignedIntOption _v; ///< Parameter v
48 Driver::UnsignedIntOption _q; ///< Parameter q
49 Driver::UnsignedIntOption _l; ///< Parameter lambda
50 Driver::UnsignedIntOption _d; ///< Parameter d
51 Driver::StringOption _permutation; ///< Use permutation constraints if d=4
52
53 public:
54 /// Initialize options for example with name \a s
EFPAOptions(const char * s,int v0=5,int q0=3,int lambda0=2,int d0=4)55 EFPAOptions(const char* s,
56 int v0 = 5, int q0 = 3, int lambda0 = 2, int d0 = 4)
57 : Options(s),
58 _v("v", "number of sequences", v0 ),
59 _q("q", "number of symbols", q0 ),
60 _l("l", "sets of symbols per sequence (lambda)", lambda0),
61 _d("d", "Hamming distance between sequences", d0 ),
62 _permutation("permutation", "use permutation constraints if d=4",
63 false)
64 {
65 // Add options
66 add(_d);
67 add(_l);
68 add(_q);
69 add(_v);
70 add(_permutation);
71 add(_symmetry);
72
73 // Add permutation options
74 _permutation.add(true, "full" );
75 _permutation.add(false, "none");
76 // Add symmetry options
77 _symmetry.add(true, "true" );
78 _symmetry.add(false, "false");
79 }
80 /// Parse options from arguments \a argv (number is \a argc)
parse(int & argc,char * argv[])81 void parse(int& argc, char* argv[]) {
82 Options::parse(argc,argv);
83 }
84 /// Get v, number of sequences
v(void) const85 int v(void) const { return _v.value(); }
86 /// Get q, number of symbols
q(void) const87 int q(void) const { return _q.value(); }
88 /// Get lambda, sets of symbols per sequence
l(void) const89 int l(void) const { return _l.value(); }
90 /// Get d, Hamming distance between sequences
d(void) const91 int d(void) const { return _d.value(); }
92
93 /// Whether to use permutation constraints. Only active if d=4
permutation(void) const94 bool permutation(void) const { return d() == 4 && _permutation.value(); }
95 /// Whether to use symmetry breaking.
symmetry(void) const96 bool symmetry(void) const { return _symmetry.value(); }
97 };
98
99
100 /**
101 * \brief %Example: Equidistant Frequency Permutation Arrays
102 *
103 * This example solves instances of the equidistant frequency
104 * permutation arrays problem.
105 *
106 * The model of the problem is mostly taken from "Modelling
107 * Equidistant Frequency Permutation Arrays in Constraints", by Ian
108 * P. Gent, Paul McKay, Peter Nightingale, and Sophie Huczynska. It
109 * implements the non-Boolean model without SAC.
110 *
111 * \ingroup Example
112 *
113 */
114 class EFPA : public Script {
115 protected:
116 int v; ///< Number of sequences
117 int q; ///< Number of symbols
118 int l; ///< Number of sets of symbols for a sequence (\f$\lambda\f$)
119 int d; ///< Hamming distance between any pair of sequences
120 int n; ///< Length of sequence (\f$q\cdot\lambda\f$)
121 int nseqpair; ///< Number of sequence pairs (\f$\frac{v(v-1)}{2}\f$)
122 IntVarArray c; ///< Variables for sequences
123 BoolVarArray diff; ///< Differences between sequences
124
125 public:
126 /// Actual model
EFPA(const EFPAOptions & opt)127 EFPA(const EFPAOptions& opt)
128 : Script(opt),
129 v(opt.v()),
130 q(opt.q()),
131 l(opt.l()),
132 d(opt.d()),
133 n(q*l),
134 nseqpair((v*(v-1))/2),
135 c(*this, n*v, 1,q),
136 diff(*this, n*nseqpair, 0, 1)
137 {
138 // Matrix access
139 // q*lambda=n columns, and v rows
140 Matrix<IntVarArray> cm(c, n, v);
141 // q*lambda=n columns, and nseqpair rows
142 Matrix<BoolVarArray> diffm(diff, n, nseqpair);
143
144 // Counting symbols in rows
145 {
146 IntArgs values(q);
147 for (int i = q; i--; ) values[i] = i+1;
148 IntSet cardinality(l, l);
149 for (int i = v; i--; )
150 count(*this, cm.row(i), cardinality, values, opt.ipl());
151 }
152
153 // Difference variables
154 {
155 int nseqi = 0;
156 for (int a = 0; a < v; ++a) {
157 for (int b = a+1; b < v; ++b) {
158 for (int i = n; i--; ) {
159 rel(*this, cm(i, a), IRT_NQ, cm(i, b), diffm(i, nseqi));
160 }
161 ++nseqi;
162 }
163 }
164 assert(nseqi == nseqpair);
165 }
166
167 // Counting the Hamming difference
168 {
169 for (int i = nseqpair; i--; ) {
170 linear(*this, diffm.row(i), IRT_EQ, d);
171 }
172 }
173
174 // Symmetry breaking
175 if (opt.symmetry()) {
176 IntRelType row_less = d==0 ? IRT_EQ : IRT_LE;
177 // order rows
178 for (int r = 0; r<v-1; ++r) {
179 rel(*this, cm.row(r), row_less, cm.row(r+1));
180 }
181 // order columns
182 for (int c = 0; c<n-1; ++c) {
183 rel(*this, cm.col(c), IRT_LQ, cm.col(c+1));
184 }
185 // Set first row according to symmetry breaking
186 int color = 1;
187 int ncolor = 0;
188 for (int c = 0; c < n; ++c) {
189 rel(*this, cm(c, 0), IRT_EQ, color);
190 if (++ncolor == l) {
191 ncolor = 0;
192 ++color;
193 }
194 }
195 }
196
197 // Permutation constraints
198 if (opt.permutation()) {
199 const int k[][4] = { // inverse indexing of the permutation
200 {0, 1, 3, 2}, // cform == 0, ((1, 2)(3, 4))
201 {1, 2, 3, 0}, // cform == 1, ((1, 2, 3, 4))
202 };
203 assert(d == 4);
204 // Constraint on each pair of rows
205 for (int r1 = 0; r1 < v; ++r1) {
206 for (int r2 = r1+1; r2 < v; ++r2) {
207 IntVarArgs row1 = cm.row(r1);
208 IntVarArgs row2 = cm.row(r2);
209 // Perm is the
210 IntVarArgs perm(d);
211 for (int i = d; i--; ) perm[i] = IntVar(*this, 0, n-1);
212 // cform is the cycle-form of the permutation
213 IntVar cform(*this, 0, 1);
214 BoolVar cformb = channel(*this, cform);
215
216 /* Permutation mapping*/
217 // Values from row1...
218 IntVarArgs _p(2*d);
219 for (int i = 2*d; i--; ) _p[i] = IntVar(*this, 1, q);
220 Matrix<IntVarArgs> p(_p, d, 2);
221 for (int i = 0; i < 2; ++i) {
222 for (int j = 0; j < d; ++j) {
223 element(*this, row1, perm[k[i][j]], p(j, i));
224 }
225 }
226
227 // ...into values in row2
228 for (int i = 0; i < d; ++i) {
229 IntVar index(*this, 0, 2*d);
230 rel(*this, cform*d + i == index);
231 IntVar value(*this, 1, q);
232 element(*this, _p, index, value);
233 element(*this, row2, perm[i], value);
234 }
235
236 /* Rows r1 and r2 are equal at indices not in perm */
237 // uses Boolean representations pib for perm[i]
238 BoolVarArgs p1b(*this, n, 0, 1);
239 channel(*this, p1b, perm[0]);
240 BoolVarArgs p2b(*this, n, 0, 1);
241 channel(*this, p2b, perm[1]);
242 BoolVarArgs p3b(*this, n, 0, 1);
243 channel(*this, p3b, perm[2]);
244 BoolVarArgs p4b(*this, n, 0, 1);
245 channel(*this, p4b, perm[3]);
246 for (int i = n; i--; ) {
247 // No perm-variable uses i is equivalent to the reows
248 // being equal at i
249 rel(*this, (!p1b[i] && !p2b[i] && !p3b[i] && !p4b[i]) ==
250 (row1[i] == row2[i]));
251 }
252
253 /* Constraints for fixing the permutation */
254 // Common non-equality constraints - derangements
255 rel(*this, perm[0], IRT_NQ, perm[1]);
256 rel(*this, perm[2], IRT_NQ, perm[3]);
257 // Conditional non-equality constraints - derangment of cform 1
258 // Implements distinct(*this, perm, cformb);
259 rel(*this, perm[0], IRT_NQ, perm[2], cformb);
260 rel(*this, perm[0], IRT_NQ, perm[3], cformb);
261 rel(*this, perm[1], IRT_NQ, perm[2], cformb);
262 rel(*this, perm[1], IRT_NQ, perm[3], cformb);
263 // Common ordering-constraints - symmetry breaking
264 rel(*this, perm[0], IRT_LE, perm[1]);
265 rel(*this, perm[0], IRT_LE, perm[2]);
266 rel(*this, perm[0], IRT_LE, perm[3]);
267 // Conditional ordering constraint - symmetry breaking for cform 0
268 rel(*this, (!cformb) >> (perm[2] < perm[3]));
269 }
270 }
271 }
272
273 branch(*this, c, INT_VAR_NONE(), INT_VAL_MIN());
274 }
275
276 /// Print instance and solution
277 virtual void
print(std::ostream & os) const278 print(std::ostream& os) const {
279 Matrix<IntVarArray> cm(c, n, v);
280 for (int i = 0; i < v; ++i) {
281 IntVarArgs r = cm.row(i);
282 os << r << std::endl;
283 }
284 os << std::endl;
285 }
286
287 /// Constructor for cloning \a s
EFPA(EFPA & s)288 EFPA(EFPA& s)
289 : Script(s),
290 v(s.v),
291 q(s.q),
292 l(s.l),
293 d(s.d),
294 n(s.n),
295 nseqpair(s.nseqpair)
296 {
297 c.update(*this, s.c);
298 diff.update(*this, s.diff);
299 }
300 /// Copy during cloning
301 virtual Space*
copy(void)302 copy(void) {
303 return new EFPA(*this);
304 }
305 };
306
307 /** \brief Main-function
308 * \relates EFPA
309 */
310 int
main(int argc,char * argv[])311 main(int argc, char* argv[]) {
312 EFPAOptions opt("Equidistant Frequency Permutation Arrays");
313 opt.ipl(IPL_DOM);
314 opt.parse(argc,argv);
315
316 Script::run<EFPA,DFS,EFPAOptions>(opt);
317 return 0;
318 }
319
320 // STATISTICS: example-any
321