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