Generated on for Gecode by doxygen 1.17.0
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 Zayenz Lagerkvist <lagerkvist@gecode.dev>
5 *
6 * Copyright:
7 * Mikael Zayenz Lagerkvist, 2009
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.dev
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
38using namespace Gecode;
39
45class EFPAOptions : public Options {
46private:
51 Driver::StringOption _permutation;
52
53public:
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
72 // Add permutation options
73 _permutation.add(true, "full" );
74 _permutation.add(false, "none");
75 // Add symmetry options
76 _symmetry.add(true, "true" );
77 _symmetry.add(false, "false");
78 }
79
80 void parse(int& argc, char* argv[]) {
81 Options::parse_strict(argc,argv);
82 }
83
84 int v(void) const { return _v.value(); }
86 int q(void) const { return _q.value(); }
88 int l(void) const { return _l.value(); }
90 int d(void) const { return _d.value(); }
91
93 bool permutation(void) const { return d() == 4 && _permutation.value(); }
95 bool symmetry(void) const { return _symmetry.value(); }
96};
97
98
113class EFPA : public Script {
114protected:
115 int v;
116 int q;
117 int l;
118 int d;
119 int n;
123
124public:
126 EFPA(const EFPAOptions& opt)
127 : Script(opt),
128 v(opt.v()),
129 q(opt.q()),
130 l(opt.l()),
131 d(opt.d()),
132 n(q*l),
133 nseqpair((v*(v-1))/2),
134 c(*this, n*v, 1,q),
135 diff(*this, n*nseqpair, 0, 1)
136 {
137 // Matrix access
138 // q*lambda=n columns, and v rows
139 Matrix<IntVarArray> cm(c, n, v);
140 // q*lambda=n columns, and nseqpair rows
142
143 // Counting symbols in rows
144 {
146 for (int i = q; i--; ) values[i] = i+1;
148 for (int i = v; i--; )
149 count(*this, cm.row(i), cardinality, values, opt.ipl());
150 }
151
152 // Difference variables
153 {
154 int nseqi = 0;
155 for (int a = 0; a < v; ++a) {
156 for (int b = a+1; b < v; ++b) {
157 for (int i = n; i--; ) {
158 rel(*this, cm(i, a), IRT_NQ, cm(i, b), diffm(i, nseqi));
159 }
160 ++nseqi;
161 }
162 }
163 assert(nseqi == nseqpair);
164 }
165
166 // Counting the Hamming difference
167 {
168 for (int i = nseqpair; i--; ) {
169 linear(*this, diffm.row(i), IRT_EQ, d);
170 }
171 }
172
173 // Symmetry breaking
174 if (opt.symmetry()) {
175 IntRelType row_less = d==0 ? IRT_EQ : IRT_LE;
176 // order rows
177 for (int r = 0; r<v-1; ++r) {
178 rel(*this, cm.row(r), row_less, cm.row(r+1));
179 }
180 // order columns
181 for (int c = 0; c<n-1; ++c) {
182 rel(*this, cm.col(c), IRT_LQ, cm.col(c+1));
183 }
184 // Set first row according to symmetry breaking
185 int color = 1;
186 int ncolor = 0;
187 for (int c = 0; c < n; ++c) {
188 rel(*this, cm(c, 0), IRT_EQ, color);
189 if (++ncolor == l) {
190 ncolor = 0;
191 ++color;
192 }
193 }
194 }
195
196 // Permutation constraints
197 if (opt.permutation()) {
198 const int k[][4] = { // inverse indexing of the permutation
199 {0, 1, 3, 2}, // cform == 0, ((1, 2)(3, 4))
200 {1, 2, 3, 0}, // cform == 1, ((1, 2, 3, 4))
201 };
202 assert(d == 4);
203 // Constraint on each pair of rows
204 for (int r1 = 0; r1 < v; ++r1) {
205 for (int r2 = r1+1; r2 < v; ++r2) {
206 IntVarArgs row1 = cm.row(r1);
207 IntVarArgs row2 = cm.row(r2);
208 // Perm is the
209 IntVarArgs perm(d);
210 for (int i = d; i--; ) perm[i] = IntVar(*this, 0, n-1);
211 // cform is the cycle-form of the permutation
212 IntVar cform(*this, 0, 1);
213 BoolVar cformb = channel(*this, cform);
214
215 /* Permutation mapping*/
216 // Values from row1...
217 IntVarArgs _p(2*d);
218 for (int i = 2*d; i--; ) _p[i] = IntVar(*this, 1, q);
219 Matrix<IntVarArgs> p(_p, d, 2);
220 for (int i = 0; i < 2; ++i) {
221 for (int j = 0; j < d; ++j) {
222 element(*this, row1, perm[k[i][j]], p(j, i));
223 }
224 }
225
226 // ...into values in row2
227 for (int i = 0; i < d; ++i) {
228 IntVar index(*this, 0, 2*d);
229 rel(*this, cform*d + i == index);
230 IntVar value(*this, 1, q);
231 element(*this, _p, index, value);
232 element(*this, row2, perm[i], value);
233 }
234
235 /* Rows r1 and r2 are equal at indices not in perm */
236 // uses Boolean representations pib for perm[i]
237 BoolVarArgs p1b(*this, n, 0, 1);
238 channel(*this, p1b, perm[0]);
239 BoolVarArgs p2b(*this, n, 0, 1);
240 channel(*this, p2b, perm[1]);
241 BoolVarArgs p3b(*this, n, 0, 1);
242 channel(*this, p3b, perm[2]);
243 BoolVarArgs p4b(*this, n, 0, 1);
244 channel(*this, p4b, perm[3]);
245 for (int i = n; i--; ) {
246 // No perm-variable uses i is equivalent to the reows
247 // being equal at i
248 rel(*this, (!p1b[i] && !p2b[i] && !p3b[i] && !p4b[i]) ==
249 (row1[i] == row2[i]));
250 }
251
252 /* Constraints for fixing the permutation */
253 // Common non-equality constraints - derangements
254 rel(*this, perm[0], IRT_NQ, perm[1]);
255 rel(*this, perm[2], IRT_NQ, perm[3]);
256 // Conditional non-equality constraints - derangment of cform 1
257 // Implements distinct(*this, perm, cformb);
258 rel(*this, perm[0], IRT_NQ, perm[2], cformb);
259 rel(*this, perm[0], IRT_NQ, perm[3], cformb);
260 rel(*this, perm[1], IRT_NQ, perm[2], cformb);
261 rel(*this, perm[1], IRT_NQ, perm[3], cformb);
262 // Common ordering-constraints - symmetry breaking
263 rel(*this, perm[0], IRT_LE, perm[1]);
264 rel(*this, perm[0], IRT_LE, perm[2]);
265 rel(*this, perm[0], IRT_LE, perm[3]);
266 // Conditional ordering constraint - symmetry breaking for cform 0
267 rel(*this, (!cformb) >> (perm[2] < perm[3]));
268 }
269 }
270 }
271
272 branch(*this, c, INT_VAR_NONE(), INT_VAL_MIN());
273 }
274
276 virtual void
277 print(std::ostream& os) const {
278 Matrix<IntVarArray> cm(c, n, v);
279 for (int i = 0; i < v; ++i) {
280 IntVarArgs r = cm.row(i);
281 os << r << std::endl;
282 }
283 os << std::endl;
284 }
285
288 : Script(s),
289 v(s.v),
290 q(s.q),
291 l(s.l),
292 d(s.d),
293 n(s.n),
295 {
296 c.update(*this, s.c);
297 diff.update(*this, s.diff);
298 }
299
300 virtual Space*
301 copy(void) {
302 return new EFPA(*this);
303 }
304};
305
309int
310main(int argc, char* argv[]) {
311 EFPAOptions opt("Equidistant Frequency Permutation Arrays");
312 opt.ipl(IPL_DOM);
313 opt.parse(argc,argv);
314
316 return 0;
317}
318
319// STATISTICS: example-any
Options for EFPA problems
Definition efpa.cpp:45
int v(void) const
Get v, number of sequences.
Definition efpa.cpp:84
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc).
Definition efpa.cpp:80
bool symmetry(void) const
Whether to use symmetry breaking.
Definition efpa.cpp:95
int q(void) const
Get q, number of symbols.
Definition efpa.cpp:86
int l(void) const
Get lambda, sets of symbols per sequence.
Definition efpa.cpp:88
int d(void) const
Get d, Hamming distance between sequences.
Definition efpa.cpp:90
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:55
bool permutation(void) const
Whether to use permutation constraints. Only active if d=4.
Definition efpa.cpp:93
int v
Number of sequences.
Definition efpa.cpp:115
IntVarArray c
Variables for sequences.
Definition efpa.cpp:121
int d
Hamming distance between any pair of sequences.
Definition efpa.cpp:118
int main(int argc, char *argv[])
Main-function.
Definition efpa.cpp:310
int nseqpair
Number of sequence pairs ( ).
Definition efpa.cpp:120
int n
Length of sequence ( ).
Definition efpa.cpp:119
EFPA(const EFPAOptions &opt)
Actual model.
Definition efpa.cpp:126
int q
Number of symbols.
Definition efpa.cpp:116
int l
Number of sets of symbols for a sequence ( ).
Definition efpa.cpp:117
BoolVarArray diff
Differences between sequences.
Definition efpa.cpp:122
virtual void print(std::ostream &os) const
Print instance and solution.
Definition efpa.cpp:277
virtual Space * copy(void)
Copy during cloning.
Definition efpa.cpp:301
EFPA(EFPA &s)
Constructor for cloning s.
Definition efpa.cpp:287
void add(Driver::BaseOption &o)
Add new option o.
Definition options.cpp:537
void parse_strict(int &argc, char *argv[])
Parse options with strict checking (equivalent to parse() with true).
Definition options.hpp:195
Passing Boolean variables.
Definition int.hh:739
Boolean variable array.
Definition int.hh:840
Boolean integer variables.
Definition int.hh:534
static void run(const Options &opt, Script *s=nullptr)
String-valued option (integer value defined by strings).
Definition driver.hh:189
Unsigned integer option.
Definition driver.hh:244
Passing integer arguments.
Definition int.hh:653
Integer sets.
Definition int.hh:179
Passing integer variables.
Definition int.hh:681
Integer variable array.
Definition int.hh:792
Integer variables.
Definition int.hh:390
Matrix-interface for arrays.
Slice< A > col(int c) const
Access column c.
Definition matrix.hpp:183
Slice< A > row(int r) const
Access row r.
Definition matrix.hpp:177
Options(const char *s)
Initialize options for script with name s.
Definition options.cpp:656
Driver::StringOption _symmetry
General symmetry options.
Definition driver.hh:452
Computation spaces.
Definition core.hpp:1787
void parse(int argc, char *argv[])
Parse commandline arguments.
Definition test.cpp:177
Driver::ScriptBase< Driver::IgnoreStepOption< Space > > Script
Base-class for scripts.
Definition driver.hh:1002
void branch(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatValBranch vals, FloatBranchFilter bf=nullptr, FloatVarValPrint vvp=nullptr)
Branch over x with variable selection vars and value selection vals.
Definition branch.cpp:39
void linear(Home home, const FloatVarArgs &x, FloatRelType frt, FloatVal c)
Post propagator for .
Definition linear.cpp:41
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVar x1)
Post propagator for .
Definition rel.cpp:68
IntRelType
Relation types for integers.
Definition int.hh:1225
@ IRT_EQ
Equality ( ).
Definition int.hh:1226
@ IRT_NQ
Disequality ( ).
Definition int.hh:1227
@ IRT_LE
Less ( ).
Definition int.hh:1229
@ IRT_LQ
Less or equal ( ).
Definition int.hh:1228
@ IPL_DOM
Domain propagation Options: basic versus advanced propagation.
Definition int.hh:1279
Gecode toplevel namespace
void count(Home home, const IntVarArgs &x, int n, IntRelType irt, int m, IntPropLevel ipl=IPL_DEF)
Post propagator for .
Definition count.cpp:40
void channel(Home home, FloatVar x0, IntVar x1)
Post propagator for channeling a float and an integer variable .
Definition channel.cpp:41
IntVarBranch INT_VAR_NONE(void)
Select first unassigned variable.
Definition var.hpp:96
void element(Home home, IntSharedArray n, IntVar x0, IntVar x1, IntPropLevel ipl=IPL_DEF)
Post domain consistent propagator for .
Definition element.cpp:39
void values(Home home, const IntVarArgs &x, IntSet y, IntPropLevel ipl=IPL_DEF)
Post constraint .
Definition aliases.hpp:143
IntValBranch INT_VAL_MIN(void)
Select smallest value.
Definition val.hpp:55
LinIntExpr cardinality(const SetExpr &)
Cardinality of set expression.
Definition set-expr.cpp:817