Generated on Sat Feb 7 2015 02:01:11 for Gecode by doxygen 1.8.9.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: 2013-07-08 14:22:40 +0200 (Mon, 08 Jul 2013) $ by $Author: schulte $
11  * $Revision: 13820 $
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
void value(int v)
Set default value to v.
Definition: options.hpp:62
EFPAOptions(const char *s, int v0=5, int q0=3, int lambda0=2, int d0=4)
Initialize options for example with name s.
Definition: efpa.cpp:59
IntVarBranch INT_VAR_NONE(void)
Select first unassigned variable.
Definition: var.hpp:108
void linear(Home home, const FloatVarArgs &x, FloatRelType frt, FloatNum c)
Post propagator for .
Definition: linear.cpp:45
int v
Number of sequences.
Definition: efpa.cpp:120
void channel(Home home, FloatVar x0, IntVar x1)
Post propagator for channeling a float and an integer variable .
Definition: arithmetic.cpp:218
int d(void) const
Get d, Hamming distance between sequences.
Definition: efpa.cpp:95
EFPA(bool share, EFPA &s)
Constructor for cloning s.
Definition: efpa.cpp:291
Multi _d(Gecode::IntArgs(3, 3, 2, 1))
void add(int v, const char *o, const char *h=NULL)
Add option value for value v, string o, and help text h.
Definition: options.cpp:121
Less or equal ( )
Definition: int.hh:906
int v(void) const
Get v, number of sequences.
Definition: efpa.cpp:89
Integer variable array.
Definition: int.hh:741
int d
Hamming distance between any pair of sequences.
Definition: efpa.cpp:123
EFPA(const EFPAOptions &opt)
Actual model.
Definition: efpa.cpp:131
Computation spaces.
Definition: core.hpp:1362
Parametric base-class for scripts.
Definition: driver.hh:622
void value(unsigned int v)
Set default value to v.
Definition: options.hpp:95
Gecode::IntSet d(v, 7)
void update(Space &, bool share, VarArray< Var > &a)
Update array to be a clone of array a.
Definition: array.hpp:1072
IntVarArray c
Variables for sequences.
Definition: efpa.cpp:126
Gecode::FloatVal c(-8, 8)
int q
Number of symbols.
Definition: efpa.cpp:121
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
Gecode::IntArgs i(4, 1, 2, 3, 4)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
Equality ( )
Definition: int.hh:904
Options opt
The options.
Definition: test.cpp:101
int n
Length of sequence ( )
Definition: efpa.cpp:124
IntRelType
Relation types for integers.
Definition: int.hh:903
NNF * r
Right subtree.
Definition: bool-expr.cpp:246
bool symmetry(void) const
Whether to use symmetry breaking.
Definition: efpa.cpp:100
IntValBranch INT_VAL_MIN(void)
Select smallest value.
Definition: val.hpp:68
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: efpa.cpp:85
Unsigned integer option.
Definition: driver.hh:229
Slice< A > row(int r) const
Access row r.
Definition: matrix.hpp:181
Less ( )
Definition: int.hh:907
Integer sets.
Definition: int.hh:171
void element(Home home, IntSharedArray c, IntVar x0, IntVar x1, IntConLevel)
Post domain consistent propagator for .
Definition: element.cpp:43
virtual void print(std::ostream &os) const
Print instance and solution.
Definition: efpa.cpp:281
Passing integer variables.
Definition: int.hh:636
Passing integer arguments.
Definition: int.hh:607
Passing Boolean variables.
Definition: int.hh:690
Boolean variable array.
Definition: int.hh:786
Boolean integer variables.
Definition: int.hh:491
LinIntExpr cardinality(const SetExpr &e)
Cardinality of set expression.
Definition: set-expr.cpp:815
BoolVarArray diff
Differences between sequences.
Definition: efpa.cpp:127
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:331
const int v[7]
Definition: distinct.cpp:207
String-valued option (integer value defined by strings)
Definition: driver.hh:174
void values(Home home, const IntVarArgs &x, IntSet y, IntConLevel icl=ICL_DEF)
Post constraint .
Definition: minimodel.hh:1869
void count(Home home, const IntVarArgs &x, int n, IntRelType irt, int m, IntConLevel)
Post propagator for .
Definition: count.cpp:44
Example: Equidistant Frequency Permutation Arrays
Definition: efpa.cpp:118
Integer variables.
Definition: int.hh:350
virtual Space * copy(bool share)
Copy during cloning.
Definition: efpa.cpp:305
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:47
Matrix-interface for arrays.
Definition: minimodel.hh:1924
int main(int argc, char *argv[])
Main-function.
Definition: efpa.cpp:314
int q(void) const
Get q, number of symbols.
Definition: efpa.cpp:91
bool permutation(void) const
Whether to use permutation constraints. Only active if d=4.
Definition: efpa.cpp:98
struct Gecode::@518::NNF::@57::@58 b
For binary nodes (and, or, eqv)
Gecode toplevel namespace
Disequality ( )
Definition: int.hh:905
Slice< A > col(int c) const
Access column c.
Definition: matrix.hpp:187
BrancherHandle branch(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatValBranch vals, FloatBranchFilter bf, FloatVarValPrint vvp)
Branch over x with variable selection vars and value selection vals.
Definition: branch.cpp:43
int nseqpair
Number of sequence pairs ( )
Definition: efpa.cpp:125
int l(void) const
Get lambda, sets of symbols per sequence.
Definition: efpa.cpp:93
Options for scripts
Definition: driver.hh:326
void icl(IntConLevel i)
Set default integer consistency level.
Definition: options.hpp:194
struct Gecode::@518::NNF::@57::@59 a
For atomic nodes.
Domain propagation or consistency.
Definition: int.hh:940
Options for EFPA problems
Definition: efpa.cpp:49
int l
Number of sets of symbols for a sequence ( )
Definition: efpa.cpp:122