langford-number.cpp
Go to the documentation of this file.
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Patrick Pekczynski <pekczynski@ps.uni-sb.de>
* Mikael Lagerkvist <lagerkvist@gecode.org>
* Christian Schulte <schulte@gecode.org>
*
* Copyright:
* Patrick Pekczynski, 2004
* Mikael Lagerkvist, 2006
* Christian Schulte, 2007
*
* 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 LangfordNumberOptions : public Options {
public:
int k, n;
LangfordNumberOptions(const char* s, int k0, int n0)
: Options(s), k(k0), n(n0) {}
void parse(int& argc, char* argv[]) {
Options::parse(argc,argv);
if (argc < 3)
return;
n = atoi(argv[1]);
k = atoi(argv[2]);
}
virtual void help(void) {
Options::help();
std::cerr << "\t(unsigned int) default: " << n << std::endl
<< "\t\tparameter n" << std::endl
<< "\t(unsigned int) default: " << k << std::endl
<< "\t\tparameter k" << std::endl;
}
};
class LangfordNumber : public Script {
protected:
int k, n;
IntVarArray y;
public:
enum {
PROP_REIFIED,
PROP_EXTENSIONAL,
PROP_EXTENSIONAL_CHANNEL
};
LangfordNumber(const LangfordNumberOptions& opt)
: Script(opt), k(opt.k), n(opt.n), y(*this,k*n,1,n) {
switch (opt.propagation()) {
case PROP_REIFIED:
{
// Position of values in sequence
IntVarArgs pv(*this,k*n,0,k*n-1);
Matrix<IntVarArgs> p(pv,n,k);
/*
* The occurences of v in the Langford sequence are v numbers apart.
*
* Let \#(i, v) denote the position of the i-th occurence of
* value v in the Langford Sequence. Then
*
* \f$ \forall i, j \in \{1, \dots, k\}, i \neq j:
* \forall v \in \{1, \dots, n\}: \#(i, v) + (v + 1) = \#(j, v)\f$
*
*/
for (int i=0; i<n; i++)
for (int j=0; j<k-1; j++)
rel(*this, p(i,j)+i+2 == p(i,j+1));
distinct(*this, pv, opt.ipl());
// Channel positions <-> values
for (int i=0; i<n; i++)
for (int j=0; j<k; j++)
element(*this, y, p(i,j), i+1);
}
break;
case PROP_EXTENSIONAL:
{
IntArgs a(n-1);
for (int v=2; v<=n; v++)
a[v-2]=v;
for (int v=1; v<=n; v++) {
// Construct regular expression for all symbols but v
if (v > 1)
a[v-2]=v-1;
REG ra(a), rv(v);
extensional(*this, y, *ra+rv+(ra(v,v)+rv)(k-1,k-1)+*ra);
}
}
break;
case PROP_EXTENSIONAL_CHANNEL:
{
// Boolean variables for channeling
BoolVarArgs bv(*this,k*n*n,0,1);
Matrix<BoolVarArgs> b(bv,k*n,n);
// Post channel constraints
for (int i=0; i<n*k; i++)
channel(*this, b.col(i), y[i], 1);
// For placing two numbers three steps apart, we construct the
// regular expression 0*100010*, and apply it to the projection of
// the sequence on the value.
REG r0(0), r1(1);
for (int v=1; v<=n; v++)
extensional(*this, b.row(v-1),
*r0 + r1 + (r0(v,v) + r1)(k-1,k-1) + *r0);
}
break;
}
// Symmetry breaking
rel(*this, y[0], IRT_LE, y[n*k-1]);
// Branching
branch(*this, y, INT_VAR_SIZE_MIN(), INT_VAL_MAX());
}
virtual void print(std::ostream& os) const {
os << "\t" << y << std::endl;
}
LangfordNumber(LangfordNumber& l)
: Script(l), k(l.k), n(l.n) {
y.update(*this, l.y);
}
virtual Space*
copy(void) {
return new LangfordNumber(*this);
}
};
int
main(int argc, char* argv[]) {
LangfordNumberOptions opt("Langford Numbers",3,9);
opt.ipl(IPL_DOM);
opt.propagation(LangfordNumber::PROP_EXTENSIONAL_CHANNEL);
opt.propagation(LangfordNumber::PROP_REIFIED,
"reified");
opt.propagation(LangfordNumber::PROP_EXTENSIONAL,
"extensional");
opt.propagation(LangfordNumber::PROP_EXTENSIONAL_CHANNEL,
"extensional-channel");
opt.parse(argc, argv);
if (opt.k < 1) {
std::cerr << "k must be at least 1!" << std::endl;
return 1;
}
if (opt.k > opt.n) {
std::cerr << "n must be at least k!" << std::endl;
return 1;
}
Script::run<LangfordNumber,DFS,LangfordNumberOptions>(opt);
return 0;
}
// STATISTICS: example-any
Gecode::BaseOptions::help
virtual void help(void)
Print help text.
Definition options.cpp:494
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::Driver::ScriptBase< Driver::IgnoreStepOption< Space > >::run
static void run(const Options &opt, Script *s=NULL)
Gecode::IntVarArray
Integer variable array.
Definition int.hh:763
Gecode::Matrix
Matrix-interface for arrays.
Definition minimodel.hh:2161
Gecode::Options::Options
Options(const char *s)
Initialize options for script with name s.
Definition options.cpp:576
Gecode::REG
Regular expressions over integer values.
Definition minimodel.hh:1625
Gecode::Space
Computation spaces.
Definition core.hpp:1742
LangfordNumberOptions
Options taking two additional parameters.
Definition langford-number.cpp:49
LangfordNumberOptions::n
int n
Definition langford-number.cpp:51
LangfordNumberOptions::k
int k
Definition langford-number.cpp:51
LangfordNumberOptions::LangfordNumberOptions
LangfordNumberOptions(const char *s, int k0, int n0)
Parameters to be given on the command line Initialize options for example with name s.
Definition langford-number.cpp:53
LangfordNumberOptions::parse
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc).
Definition langford-number.cpp:56
LangfordNumberOptions::help
virtual void help(void)
Print help message.
Definition langford-number.cpp:64
LangfordNumber::y
IntVarArray y
Sequence variables.
Definition langford-number.cpp:83
LangfordNumber::main
int main(int argc, char *argv[])
Main-function.
Definition langford-number.cpp:190
LangfordNumber::LangfordNumber
LangfordNumber(const LangfordNumberOptions &opt)
Construct model.
Definition langford-number.cpp:93
LangfordNumber::n
int n
Problem parameters.
Definition langford-number.cpp:82
LangfordNumber::print
virtual void print(std::ostream &os) const
Print solution.
Definition langford-number.cpp:168
LangfordNumber::copy
virtual Space * copy(void)
Copy during cloning.
Definition langford-number.cpp:180
LangfordNumber::k
int k
Definition langford-number.cpp:82
LangfordNumber::LangfordNumber
LangfordNumber(LangfordNumber &l)
Constructor for cloning l.
Definition langford-number.cpp:173
LangfordNumber::PROP_REIFIED
@ PROP_REIFIED
Use reified constraints.
Definition langford-number.cpp:88
LangfordNumber::PROP_EXTENSIONAL
@ PROP_EXTENSIONAL
Use extensional constraints.
Definition langford-number.cpp:89
LangfordNumber::PROP_EXTENSIONAL_CHANNEL
@ PROP_EXTENSIONAL_CHANNEL
Use extensional and channel constraints.
Definition langford-number.cpp:90
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::rel
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVar x1)
Post propagator for .
Definition rel.cpp:68
Gecode::extensional
void extensional(Home home, const IntVarArgs &x, DFA d, IntPropLevel ipl=IPL_DEF)
Post domain consistent propagator for extensional constraint described by a DFA.
Definition extensional-regular.cpp:39
Gecode::IRT_LE
@ IRT_LE
Less ( ).
Definition int.hh:929
Gecode::IPL_DOM
@ IPL_DOM
Domain propagation Options: basic versus advanced propagation.
Definition int.hh:979
minimodel.hh
Gecode
Gecode toplevel namespace
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_VAL_MAX
IntValBranch INT_VAL_MAX(void)
Select largest value.
Definition val.hpp:65
Gecode::INT_VAR_SIZE_MIN
IntVarBranch INT_VAR_SIZE_MIN(BranchTbl tbl=nullptr)
Select variable with smallest domain size.
Definition var.hpp:206