Generated on Thu May 7 2015 05:43:35 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: 2015-03-17 16:09:39 +0100 (Tue, 17 Mar 2015) $ by $Author: schulte $
11  * $Revision: 14447 $
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  : Script(opt),
133  v(opt.v()),
134  q(opt.q()),
135  l(opt.l()),
136  d(opt.d()),
137  n(q*l),
138  nseqpair((v*(v-1))/2),
139  c(*this, n*v, 1,q),
140  diff(*this, n*nseqpair, 0, 1)
141  {
142  // Matrix access
143  // q*lambda=n columns, and v rows
144  Matrix<IntVarArray> cm(c, n, v);
145  // q*lambda=n columns, and nseqpair rows
146  Matrix<BoolVarArray> diffm(diff, n, nseqpair);
147 
148  // Counting symbols in rows
149  {
150  IntArgs values(q);
151  for (int i = q; i--; ) values[i] = i+1;
152  IntSet cardinality(l, l);
153  for (int i = v; i--; )
154  count(*this, cm.row(i), cardinality, values, opt.icl());
155  }
156 
157  // Difference variables
158  {
159  int nseqi = 0;
160  for (int a = 0; a < v; ++a) {
161  for (int b = a+1; b < v; ++b) {
162  for (int i = n; i--; ) {
163  rel(*this, cm(i, a), IRT_NQ, cm(i, b), diffm(i, nseqi));
164  }
165  ++nseqi;
166  }
167  }
168  assert(nseqi == nseqpair);
169  }
170 
171  // Counting the Hamming difference
172  {
173  for (int i = nseqpair; i--; ) {
174  linear(*this, diffm.row(i), IRT_EQ, d);
175  }
176  }
177 
178  // Symmetry breaking
179  if (opt.symmetry()) {
180  IntRelType row_less = d==0 ? IRT_EQ : IRT_LE;
181  // order rows
182  for (int r = 0; r<v-1; ++r) {
183  rel(*this, cm.row(r), row_less, cm.row(r+1));
184  }
185  // order columns
186  for (int c = 0; c<n-1; ++c) {
187  rel(*this, cm.col(c), IRT_LQ, cm.col(c+1));
188  }
189  // Set first row according to symmetry breaking
190  int color = 1;
191  int ncolor = 0;
192  for (int c = 0; c < n; ++c) {
193  rel(*this, cm(c, 0), IRT_EQ, color);
194  if (++ncolor == l) {
195  ncolor = 0;
196  ++color;
197  }
198  }
199  }
200 
201  // Permutation constraints
202  if (opt.permutation()) {
203  const int k[][4] = { // inverse indexing of the permutation
204  {0, 1, 3, 2}, // cform == 0, ((1, 2)(3, 4))
205  {1, 2, 3, 0}, // cform == 1, ((1, 2, 3, 4))
206  };
207  assert(d == 4);
208  // Constraint on each pair of rows
209  for (int r1 = 0; r1 < v; ++r1) {
210  for (int r2 = r1+1; r2 < v; ++r2) {
211  IntVarArgs row1 = cm.row(r1);
212  IntVarArgs row2 = cm.row(r2);
213  // Perm is the
214  IntVarArgs perm(d);
215  for (int i = d; i--; ) perm[i] = IntVar(*this, 0, n-1);
216  // cform is the cycle-form of the permutation
217  IntVar cform(*this, 0, 1);
218  BoolVar cformb = channel(*this, cform);
219 
220  /* Permutation mapping*/
221  // Values from row1...
222  IntVarArgs _p(2*d);
223  for (int i = 2*d; i--; ) _p[i] = IntVar(*this, 1, q);
224  Matrix<IntVarArgs> p(_p, d, 2);
225  for (int i = 0; i < 2; ++i) {
226  for (int j = 0; j < d; ++j) {
227  element(*this, row1, perm[k[i][j]], p(j, i));
228  }
229  }
230 
231  // ...into values in row2
232  for (int i = 0; i < d; ++i) {
233  IntVar index(*this, 0, 2*d);
234  rel(*this, cform*d + i == index);
235  IntVar value(*this, 1, q);
236  element(*this, _p, index, value);
237  element(*this, row2, perm[i], value);
238  }
239 
240  /* Rows r1 and r2 are equal at indices not in perm */
241  // uses Boolean representations pib for perm[i]
242  BoolVarArgs p1b(*this, n, 0, 1);
243  channel(*this, p1b, perm[0]);
244  BoolVarArgs p2b(*this, n, 0, 1);
245  channel(*this, p2b, perm[1]);
246  BoolVarArgs p3b(*this, n, 0, 1);
247  channel(*this, p3b, perm[2]);
248  BoolVarArgs p4b(*this, n, 0, 1);
249  channel(*this, p4b, perm[3]);
250  for (int i = n; i--; ) {
251  // No perm-variable uses i is equivalent to the reows
252  // being equal at i
253  rel(*this, (!p1b[i] && !p2b[i] && !p3b[i] && !p4b[i]) ==
254  (row1[i] == row2[i]));
255  }
256 
257  /* Constraints for fixing the permutation */
258  // Common non-equality constraints - derangements
259  rel(*this, perm[0], IRT_NQ, perm[1]);
260  rel(*this, perm[2], IRT_NQ, perm[3]);
261  // Conditional non-equality constraints - derangment of cform 1
262  // Implements distinct(*this, perm, cformb);
263  rel(*this, perm[0], IRT_NQ, perm[2], cformb);
264  rel(*this, perm[0], IRT_NQ, perm[3], cformb);
265  rel(*this, perm[1], IRT_NQ, perm[2], cformb);
266  rel(*this, perm[1], IRT_NQ, perm[3], cformb);
267  // Common ordering-constraints - symmetry breaking
268  rel(*this, perm[0], IRT_LE, perm[1]);
269  rel(*this, perm[0], IRT_LE, perm[2]);
270  rel(*this, perm[0], IRT_LE, perm[3]);
271  // Conditional ordering constraint - symmetry breaking for cform 0
272  rel(*this, (!cformb) >> (perm[2] < perm[3]));
273  }
274  }
275  }
276 
277  branch(*this, c, INT_VAR_NONE(), INT_VAL_MIN());
278  }
279 
281  virtual void
282  print(std::ostream& os) const {
283  Matrix<IntVarArray> cm(c, n, v);
284  for (int i = 0; i < v; ++i) {
285  IntVarArgs r = cm.row(i);
286  os << r << std::endl;
287  }
288  os << std::endl;
289  }
290 
292  EFPA(bool share, EFPA& s)
293  : Script(share,s),
294  v(s.v),
295  q(s.q),
296  l(s.l),
297  d(s.d),
298  n(s.n),
299  nseqpair(s.nseqpair)
300  {
301  c.update(*this, share, s.c);
302  diff.update(*this, share, s.diff);
303  }
305  virtual Space*
306  copy(bool share) {
307  return new EFPA(share,*this);
308  }
309 };
310 
314 int
315 main(int argc, char* argv[]) {
316  EFPAOptions opt("Equidistant Frequency Permutation Arrays");
317  opt.icl(ICL_DOM);
318  opt.parse(argc,argv);
319 
320  Script::run<EFPA,DFS,EFPAOptions>(opt);
321  return 0;
322 }
323 
324 // 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:292
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:633
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
struct Gecode::@519::NNF::@60::@62 a
For atomic nodes.
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:282
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:818
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
struct Gecode::@519::NNF::@60::@61 b
For binary nodes (and, or, eqv)
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:306
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:315
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
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
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