Generated on Tue Mar 5 2013 22:37:12 for Gecode by doxygen 1.8.3.1
efpa.cpp
Go to the documentation of this file.
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  * Last modified:
10  * $Date: 2010-10-07 20:52:01 +1100 (Thu, 07 Oct 2010) $ by $Author: schulte $
11  * $Revision: 11473 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 #include <gecode/driver.hh>
39 #include <gecode/int.hh>
40 #include <gecode/minimodel.hh>
41 
42 using namespace Gecode;
43 
49 class EFPAOptions : public Options {
50 private:
55  Driver::StringOption _permutation;
56 
57 public:
59  EFPAOptions(const char* s,
60  int v0 = 5, int q0 = 3, int lambda0 = 2, int d0 = 4)
61  : Options(s),
62  _v("-v", "number of sequences", v0 ),
63  _q("-q", "number of symbols", q0 ),
64  _l("-l", "sets of symbols per sequence (lambda)", lambda0),
65  _d("-d", "Hamming distance between sequences", d0 ),
66  _permutation("-permutation", "use permutation constraints if d=4",
67  false)
68  {
69  // Add options
70  add(_d);
71  add(_l);
72  add(_q);
73  add(_v);
74  add(_permutation);
75  add(_symmetry);
76 
77  // Add permutation options
78  _permutation.add(true, "full" );
79  _permutation.add(false, "none");
80  // Add symmetry options
81  _symmetry.add(true, "true" );
82  _symmetry.add(false, "false");
83  }
85  void parse(int& argc, char* argv[]) {
86  Options::parse(argc,argv);
87  }
89  int v(void) const { return _v.value(); }
91  int q(void) const { return _q.value(); }
93  int l(void) const { return _l.value(); }
95  int d(void) const { return _d.value(); }
96 
98  bool permutation(void) const { return d() == 4 && _permutation.value(); }
100  bool symmetry(void) const { return _symmetry.value(); }
101 };
102 
103 
118 class EFPA : public Script {
119 protected:
120  int v;
121  int q;
122  int l;
123  int d;
124  int n;
125  int nseqpair;
128 
129 public:
132  : v(opt.v()),
133  q(opt.q()),
134  l(opt.l()),
135  d(opt.d()),
136  n(q*l),
137  nseqpair((v*(v-1))/2),
138  c(*this, n*v, 1,q),
139  diff(*this, n*nseqpair, 0, 1)
140  {
141  // Matrix access
142  // q*lambda=n columns, and v rows
143  Matrix<IntVarArray> cm(c, n, v);
144  // q*lambda=n columns, and nseqpair rows
145  Matrix<BoolVarArray> diffm(diff, n, nseqpair);
146 
147  // Counting symbols in rows
148  {
149  IntArgs values(q);
150  for (int i = q; i--; ) values[i] = i+1;
151  IntSet cardinality(l, l);
152  for (int i = v; i--; )
153  count(*this, cm.row(i), cardinality, values, opt.icl());
154  }
155 
156  // Difference variables
157  {
158  int nseqi = 0;
159  for (int a = 0; a < v; ++a) {
160  for (int b = a+1; b < v; ++b) {
161  for (int i = n; i--; ) {
162  rel(*this, cm(i, a), IRT_NQ, cm(i, b), diffm(i, nseqi));
163  }
164  ++nseqi;
165  }
166  }
167  assert(nseqi == nseqpair);
168  }
169 
170  // Counting the Hamming difference
171  {
172  for (int i = nseqpair; i--; ) {
173  linear(*this, diffm.row(i), IRT_EQ, d);
174  }
175  }
176 
177  // Symmetry breaking
178  if (opt.symmetry()) {
179  IntRelType row_less = d==0 ? IRT_EQ : IRT_LE;
180  // order rows
181  for (int r = 0; r<v-1; ++r) {
182  rel(*this, cm.row(r), row_less, cm.row(r+1));
183  }
184  // order columns
185  for (int c = 0; c<n-1; ++c) {
186  rel(*this, cm.col(c), IRT_LQ, cm.col(c+1));
187  }
188  // Set first row according to symmetry breaking
189  int color = 1;
190  int ncolor = 0;
191  for (int c = 0; c < n; ++c) {
192  rel(*this, cm(c, 0), IRT_EQ, color);
193  if (++ncolor == l) {
194  ncolor = 0;
195  ++color;
196  }
197  }
198  }
199 
200  // Permutation constraints
201  if (opt.permutation()) {
202  const int k[][4] = { // inverse indexing of the permutation
203  {0, 1, 3, 2}, // cform == 0, ((1, 2)(3, 4))
204  {1, 2, 3, 0}, // cform == 1, ((1, 2, 3, 4))
205  };
206  assert(d == 4);
207  // Constraint on each pair of rows
208  for (int r1 = 0; r1 < v; ++r1) {
209  for (int r2 = r1+1; r2 < v; ++r2) {
210  IntVarArgs row1 = cm.row(r1);
211  IntVarArgs row2 = cm.row(r2);
212  // Perm is the
213  IntVarArgs perm(d);
214  for (int i = d; i--; ) perm[i] = IntVar(*this, 0, n-1);
215  // cform is the cycle-form of the permutation
216  IntVar cform(*this, 0, 1);
217  BoolVar cformb = channel(*this, cform);
218 
219  /* Permutation mapping*/
220  // Values from row1...
221  IntVarArgs _p(2*d);
222  for (int i = 2*d; i--; ) _p[i] = IntVar(*this, 1, q);
223  Matrix<IntVarArgs> p(_p, d, 2);
224  for (int i = 0; i < 2; ++i) {
225  for (int j = 0; j < d; ++j) {
226  element(*this, row1, perm[k[i][j]], p(j, i));
227  }
228  }
229 
230  // ...into values in row2
231  for (int i = 0; i < d; ++i) {
232  IntVar index(*this, 0, 2*d);
233  rel(*this, cform*d + i == index);
234  IntVar value(*this, 1, q);
235  element(*this, _p, index, value);
236  element(*this, row2, perm[i], value);
237  }
238 
239  /* Rows r1 and r2 are equal at indices not in perm */
240  // uses Boolean representations pib for perm[i]
241  BoolVarArgs p1b(*this, n, 0, 1);
242  channel(*this, p1b, perm[0]);
243  BoolVarArgs p2b(*this, n, 0, 1);
244  channel(*this, p2b, perm[1]);
245  BoolVarArgs p3b(*this, n, 0, 1);
246  channel(*this, p3b, perm[2]);
247  BoolVarArgs p4b(*this, n, 0, 1);
248  channel(*this, p4b, perm[3]);
249  for (int i = n; i--; ) {
250  // No perm-variable uses i is equivalent to the reows
251  // being equal at i
252  rel(*this, (!p1b[i] && !p2b[i] && !p3b[i] && !p4b[i]) ==
253  (row1[i] == row2[i]));
254  }
255 
256  /* Constraints for fixing the permutation */
257  // Common non-equality constraints - derangements
258  rel(*this, perm[0], IRT_NQ, perm[1]);
259  rel(*this, perm[2], IRT_NQ, perm[3]);
260  // Conditional non-equality constraints - derangment of cform 1
261  // Implements distinct(*this, perm, cformb);
262  rel(*this, perm[0], IRT_NQ, perm[2], cformb);
263  rel(*this, perm[0], IRT_NQ, perm[3], cformb);
264  rel(*this, perm[1], IRT_NQ, perm[2], cformb);
265  rel(*this, perm[1], IRT_NQ, perm[3], cformb);
266  // Common ordering-constraints - symmetry breaking
267  rel(*this, perm[0], IRT_LE, perm[1]);
268  rel(*this, perm[0], IRT_LE, perm[2]);
269  rel(*this, perm[0], IRT_LE, perm[3]);
270  // Conditional ordering constraint - symmetry breaking for cform 0
271  rel(*this, (!cformb) >> (perm[2] < perm[3]));
272  }
273  }
274  }
275 
276  branch(*this, c, INT_VAR_NONE, INT_VAL_MIN);
277  }
278 
280  virtual void
281  print(std::ostream& os) const {
282  Matrix<IntVarArray> cm(c, n, v);
283  for (int i = 0; i < v; ++i) {
284  IntVarArgs r = cm.row(i);
285  os << r << std::endl;
286  }
287  os << std::endl;
288  }
289 
291  EFPA(bool share, EFPA& s)
292  : Script(share,s),
293  v(s.v),
294  q(s.q),
295  l(s.l),
296  d(s.d),
297  n(s.n),
298  nseqpair(s.nseqpair)
299  {
300  c.update(*this, share, s.c);
301  diff.update(*this, share, s.diff);
302  }
304  virtual Space*
305  copy(bool share) {
306  return new EFPA(share,*this);
307  }
308 };
309 
313 int
314 main(int argc, char* argv[]) {
315  EFPAOptions opt("Equidistant Frequency Permutation Arrays");
316  opt.icl(ICL_DOM);
317  opt.parse(argc,argv);
318 
319  Script::run<EFPA,DFS,EFPAOptions>(opt);
320  return 0;
321 }
322 
323 // STATISTICS: example-any