Generated on Thu Jan 16 2025 00:00:00 for Gecode by doxygen 1.14.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 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
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);
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
81 void parse(int& argc, char* argv[]) {
82 Options::parse(argc,argv);
83 }
84
85 int v(void) const { return _v.value(); }
87 int q(void) const { return _q.value(); }
89 int l(void) const { return _l.value(); }
91 int d(void) const { return _d.value(); }
92
94 bool permutation(void) const { return d() == 4 && _permutation.value(); }
96 bool symmetry(void) const { return _symmetry.value(); }
97};
98
99
114class EFPA : public Script {
115protected:
116 int v;
117 int q;
118 int l;
119 int d;
120 int n;
124
125public:
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
143
144 // Counting symbols in rows
145 {
147 for (int i = q; i--; ) values[i] = i+1;
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
277 virtual void
278 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
289 : Script(s),
290 v(s.v),
291 q(s.q),
292 l(s.l),
293 d(s.d),
294 n(s.n),
296 {
297 c.update(*this, s.c);
298 diff.update(*this, s.diff);
299 }
300
301 virtual Space*
302 copy(void) {
303 return new EFPA(*this);
304 }
305};
306
310int
311main(int argc, char* argv[]) {
312 EFPAOptions opt("Equidistant Frequency Permutation Arrays");
313 opt.ipl(IPL_DOM);
314 opt.parse(argc,argv);
315
317 return 0;
318}
319
320// STATISTICS: example-any
Options for EFPA problems
Definition efpa.cpp:45
int v(void) const
Get v, number of sequences.
Definition efpa.cpp:85
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition efpa.cpp:81
bool symmetry(void) const
Whether to use symmetry breaking.
Definition efpa.cpp:96
int q(void) const
Get q, number of symbols.
Definition efpa.cpp:87
int l(void) const
Get lambda, sets of symbols per sequence.
Definition efpa.cpp:89
int d(void) const
Get d, Hamming distance between sequences.
Definition efpa.cpp:91
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:94
int v
Number of sequences.
Definition efpa.cpp:116
IntVarArray c
Variables for sequences.
Definition efpa.cpp:122
int d
Hamming distance between any pair of sequences.
Definition efpa.cpp:119
int main(int argc, char *argv[])
Main-function.
Definition efpa.cpp:311
int nseqpair
Number of sequence pairs ( )
Definition efpa.cpp:121
int n
Length of sequence ( )
Definition efpa.cpp:120
EFPA(const EFPAOptions &opt)
Actual model.
Definition efpa.cpp:127
int q
Number of symbols.
Definition efpa.cpp:117
int l
Number of sets of symbols for a sequence ( )
Definition efpa.cpp:118
BoolVarArray diff
Differences between sequences.
Definition efpa.cpp:123
virtual void print(std::ostream &os) const
Print instance and solution.
Definition efpa.cpp:278
virtual Space * copy(void)
Copy during cloning.
Definition efpa.cpp:302
EFPA(EFPA &s)
Constructor for cloning s.
Definition efpa.cpp:288
void add(Driver::BaseOption &o)
Add new option o.
Definition options.cpp:474
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition options.cpp:548
Passing Boolean variables.
Definition int.hh:721
Boolean variable array.
Definition int.hh:820
Boolean integer variables.
Definition int.hh:515
static void run(const Options &opt, Script *s=NULL)
String-valued option (integer value defined by strings)
Definition driver.hh:174
Unsigned integer option.
Definition driver.hh:229
Passing integer arguments.
Definition int.hh:634
Integer sets.
Definition int.hh:174
Passing integer variables.
Definition int.hh:662
Integer variable array.
Definition int.hh:772
Integer variables.
Definition int.hh:371
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:576
Driver::StringOption _symmetry
General symmetry options.
Definition driver.hh:371
Computation spaces.
Definition core.hpp:1744
void parse(int argc, char *argv[])
Parse commandline arguments.
Definition test.cpp:120
Driver::ScriptBase< Driver::IgnoreStepOption< Space > > Script
Base-class for scripts.
Definition driver.hh:801
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:940
@ IRT_EQ
Equality ( )
Definition int.hh:941
@ IRT_NQ
Disequality ( )
Definition int.hh:942
@ IRT_LE
Less ( )
Definition int.hh:944
@ IRT_LQ
Less or equal ( )
Definition int.hh:943
@ IPL_DOM
Domain propagation Options: basic versus advanced propagation.
Definition int.hh:994
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
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition set.hh:773
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