efpa.cpp
Go to the documentation of this file.
/* -*- 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;
class EFPAOptions : public Options {
private:
Driver::UnsignedIntOption _v;
Driver::UnsignedIntOption _q;
Driver::UnsignedIntOption _l;
Driver::UnsignedIntOption _d;
Driver::StringOption _permutation;
public:
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");
}
void parse(int& argc, char* argv[]) {
Options::parse(argc,argv);
}
int v(void) const { return _v.value(); }
int q(void) const { return _q.value(); }
int l(void) const { return _l.value(); }
int d(void) const { return _d.value(); }
bool permutation(void) const { return d() == 4 && _permutation.value(); }
bool symmetry(void) const { return _symmetry.value(); }
};
class EFPA : public Script {
protected:
int v;
int q;
int l;
int d;
int n;
int nseqpair;
IntVarArray c;
BoolVarArray diff;
public:
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());
}
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;
}
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);
}
virtual Space*
copy(void) {
return new EFPA(*this);
}
};
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
EFPAOptions
Options for EFPA problems
Definition efpa.cpp:45
EFPAOptions::v
int v(void) const
Get v, number of sequences.
Definition efpa.cpp:85
EFPAOptions::parse
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc).
Definition efpa.cpp:81
EFPAOptions::symmetry
bool symmetry(void) const
Whether to use symmetry breaking.
Definition efpa.cpp:96
EFPAOptions::q
int q(void) const
Get q, number of symbols.
Definition efpa.cpp:87
EFPAOptions::l
int l(void) const
Get lambda, sets of symbols per sequence.
Definition efpa.cpp:89
EFPAOptions::d
int d(void) const
Get d, Hamming distance between sequences.
Definition efpa.cpp:91
EFPAOptions::EFPAOptions
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
EFPAOptions::permutation
bool permutation(void) const
Whether to use permutation constraints. Only active if d=4.
Definition efpa.cpp:94
EFPA::v
int v
Number of sequences.
Definition efpa.cpp:116
EFPA::c
IntVarArray c
Variables for sequences.
Definition efpa.cpp:122
EFPA::d
int d
Hamming distance between any pair of sequences.
Definition efpa.cpp:119
EFPA::main
int main(int argc, char *argv[])
Main-function.
Definition efpa.cpp:311
EFPA::nseqpair
int nseqpair
Number of sequence pairs ( ).
Definition efpa.cpp:121
EFPA::n
int n
Length of sequence ( ).
Definition efpa.cpp:120
EFPA::EFPA
EFPA(const EFPAOptions &opt)
Actual model.
Definition efpa.cpp:127
EFPA::q
int q
Number of symbols.
Definition efpa.cpp:117
EFPA::l
int l
Number of sets of symbols for a sequence ( ).
Definition efpa.cpp:118
EFPA::diff
BoolVarArray diff
Differences between sequences.
Definition efpa.cpp:123
EFPA::print
virtual void print(std::ostream &os) const
Print instance and solution.
Definition efpa.cpp:278
EFPA::copy
virtual Space * copy(void)
Copy during cloning.
Definition efpa.cpp:302
EFPA::EFPA
EFPA(EFPA &s)
Constructor for cloning s.
Definition efpa.cpp:288
Gecode::BaseOptions::add
void add(Driver::BaseOption &o)
Add new option o.
Definition options.cpp:474
Gecode::BaseOptions::parse
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc).
Definition options.cpp:548
Gecode::BoolVarArgs
Passing Boolean variables.
Definition int.hh:712
Gecode::BoolVarArray
Boolean variable array.
Definition int.hh:808
Gecode::BoolVar
Boolean integer variables.
Definition int.hh:512
Gecode::Driver::ScriptBase< Driver::IgnoreStepOption< Space > >::run
static void run(const Options &opt, Script *s=NULL)
Gecode::Driver::StringOption
String-valued option (integer value defined by strings).
Definition driver.hh:174
Gecode::Driver::UnsignedIntOption
Unsigned integer option.
Definition driver.hh:229
Gecode::IntArgs
Passing integer arguments.
Definition int.hh:628
Gecode::IntSet
Integer sets.
Definition int.hh:174
Gecode::IntVarArgs
Passing integer variables.
Definition int.hh:656
Gecode::IntVarArray
Integer variable array.
Definition int.hh:763
Gecode::IntVar
Integer variables.
Definition int.hh:371
Gecode::Matrix
Matrix-interface for arrays.
Definition minimodel.hh:2161
Gecode::Matrix::col
Slice< A > col(int c) const
Access column c.
Definition matrix.hpp:183
Gecode::Matrix::row
Slice< A > row(int r) const
Access row r.
Definition matrix.hpp:177
Gecode::Options::Options
Options(const char *s)
Initialize options for script with name s.
Definition options.cpp:576
Gecode::Options::_symmetry
Driver::StringOption _symmetry
General symmetry options.
Definition driver.hh:371
Gecode::Space
Computation spaces.
Definition core.hpp:1742
Test::Options::parse
void parse(int argc, char *argv[])
Parse commandline arguments.
Definition test.cpp:120
driver.hh
int.hh
Gecode::Script
Driver::ScriptBase< Driver::IgnoreStepOption< Space > > Script
Base-class for scripts.
Definition driver.hh:801
Gecode::branch
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
Gecode::linear
void linear(Home home, const FloatVarArgs &x, FloatRelType frt, FloatVal c)
Post propagator for .
Definition linear.cpp:41
Gecode::rel
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVar x1)
Post propagator for .
Definition rel.cpp:68
Gecode::IntRelType
IntRelType
Relation types for integers.
Definition int.hh:925
Gecode::IRT_EQ
@ IRT_EQ
Equality ( ).
Definition int.hh:926
Gecode::IRT_NQ
@ IRT_NQ
Disequality ( ).
Definition int.hh:927
Gecode::IRT_LE
@ IRT_LE
Less ( ).
Definition int.hh:929
Gecode::IRT_LQ
@ IRT_LQ
Less or equal ( ).
Definition int.hh:928
Gecode::IPL_DOM
@ IPL_DOM
Domain propagation Options: basic versus advanced propagation.
Definition int.hh:979
minimodel.hh
Gecode
Gecode toplevel namespace
Gecode::count
void count(Home home, const IntVarArgs &x, int n, IntRelType irt, int m, IntPropLevel ipl=IPL_DEF)
Post propagator for .
Definition count.cpp:40
Gecode::channel
void channel(Home home, FloatVar x0, IntVar x1)
Post propagator for channeling a float and an integer variable .
Definition channel.cpp:41
Gecode::INT_VAR_NONE
IntVarBranch INT_VAR_NONE(void)
Select first unassigned variable.
Definition var.hpp:96
Gecode::element
void element(Home home, IntSharedArray n, IntVar x0, IntVar x1, IntPropLevel ipl=IPL_DEF)
Post domain consistent propagator for .
Definition element.cpp:39
Gecode::values
void values(Home home, const IntVarArgs &x, IntSet y, IntPropLevel ipl=IPL_DEF)
Post constraint .
Definition aliases.hpp:143
Gecode::INT_VAL_MIN
IntValBranch INT_VAL_MIN(void)
Select smallest value.
Definition val.hpp:55
Gecode::cardinality
LinIntExpr cardinality(const SetExpr &)
Cardinality of set expression.
Definition set-expr.cpp:817