nvalues.cpp
Go to the documentation of this file.
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christian Schulte <schulte@gecode.org>
*
* Copyright:
* Christian Schulte, 2011
*
* 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/minimodel.hh>
#include "test/int.hh"
namespace Test { namespace Int {
namespace NValues {
class IntInt : public Test {
protected:
Gecode::IntRelType irt;
int m;
public:
IntInt(int n, int m0, Gecode::IntRelType irt0)
: Test("NValues::Int::Int::"+str(irt0)+"::"+str(n)+"::"+str(m0),
n,0,n),
irt(irt0), m(m0) {
testfix = false;
if (arity > 5)
testsearch = false;
}
virtual Assignment* assignment(void) const {
if (arity > 5)
return new RandomAssignment(arity,dom,500);
else
return new CpltAssignment(arity,dom);
}
virtual bool solution(const Assignment& x) const {
int n = x.size();
bool* v = new bool[n+1];
for (int i=n+1; i--; )
v[i] = false;
int k = 0;
for (int i=n; i--; )
if (!v[x[i]]) {
k++;
v[x[i]] = true;
}
delete [] v;
return cmp(k,irt,m);
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
Gecode::nvalues(home, x, irt, m);
}
};
class IntVar : public Test {
protected:
Gecode::IntRelType irt;
public:
IntVar(int n, Gecode::IntRelType irt0)
: Test("NValues::Int::Var::"+str(irt0)+"::"+str(n),n+1,0,n),
irt(irt0) {
testfix = false;
}
virtual bool solution(const Assignment& x) const {
int n = x.size() - 1;
bool* v = new bool[n+1];
for (int i=n+1; i--; )
v[i] = false;
int k = 0;
for (int i=n; i--; )
if (!v[x[i]]) {
k++;
v[x[i]] = true;
}
delete [] v;
return cmp(k,irt,x[n]);
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& xy) {
int n = xy.size() - 1;
Gecode::IntVarArgs x(n);
for (int i=n; i--; )
x[i] = xy[i];
Gecode::nvalues(home, x, irt, xy[n]);
}
};
class BoolInt : public Test {
protected:
Gecode::IntRelType irt;
int m;
public:
BoolInt(int n, int m0, Gecode::IntRelType irt0)
: Test("NValues::Bool::Int::"+str(irt0)+"::"+str(n)+"::"+str(m0),
n,0,2),
irt(irt0), m(m0) {}
virtual bool solution(const Assignment& x) const {
int n = x.size();
for (int i=n; i--; )
if (x[i] > 1)
return false;
bool* v = new bool[n+1];
for (int i=n+1; i--; )
v[i] = false;
int k = 0;
for (int i=n; i--; )
if (!v[x[i]]) {
k++;
v[x[i]] = true;
}
delete [] v;
return cmp(k,irt,m);
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
using namespace Gecode;
BoolVarArgs y(x.size());
for (int i=x.size(); i--; )
y[i] = channel(home, x[i]);
nvalues(home, y, irt, m);
}
};
class BoolVar : public Test {
protected:
Gecode::IntRelType irt;
public:
BoolVar(int n, Gecode::IntRelType irt0)
: Test("NValues::Bool::Var::"+str(irt0)+"::"+str(n),n+1,0,2),
irt(irt0) {}
virtual bool solution(const Assignment& x) const {
int n = x.size() - 1;
for (int i=n; i--; )
if (x[i] > 1)
return false;
bool* v = new bool[n+1];
for (int i=n+1; i--; )
v[i] = false;
int k = 0;
for (int i=n; i--; )
if (!v[x[i]]) {
k++;
v[x[i]] = true;
}
delete [] v;
return cmp(k,irt,x[n]);
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& xy) {
using namespace Gecode;
int n = xy.size() - 1;
BoolVarArgs x(n);
for (int i=n; i--; )
x[i] = channel(home, xy[i]);
nvalues(home, x, irt, xy[n]);
}
};
class Create {
public:
Create(void) {
for (IntRelTypes irts; irts(); ++irts) {
for (int i=1; i<=7; i += 3) {
for (int m=0; m<=3; m++)
(void) new BoolInt(i, m, irts.irt());
(void) new BoolVar(i, irts.irt());
}
for (int i=1; i<=7; i += 2) {
for (int m=0; m<=i+1; m++)
(void) new IntInt(i, m, irts.irt());
if (i <= 5)
(void) new IntVar(i, irts.irt());
}
}
}
};
Create c;
}
}}
// STATISTICS: test-int
Gecode::BoolVarArgs
Passing Boolean variables.
Definition int.hh:712
Gecode::IntVarArgs
Passing integer variables.
Definition int.hh:656
Gecode::IntVarArray
Integer variable array.
Definition int.hh:763
Gecode::Space
Computation spaces.
Definition core.hpp:1742
Gecode::VarArray::size
int size(void) const
Return size of array (number of elements).
Definition array.hpp:926
Test::Int::Assignment
Base class for assignments
Definition int.hh:59
Test::Int::Assignment::size
int size(void) const
Return number of variables.
Definition int.hpp:46
Test::Int::CpltAssignment
Generate all assignments.
Definition int.hh:79
Test::Int::IntRelTypes
Iterator for integer relation types.
Definition int.hh:368
Test::Int::NValues::BoolInt
Test number of values of Boolean variables equal to integer
Definition nvalues.cpp:130
Test::Int::NValues::BoolInt::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition nvalues.cpp:161
Test::Int::NValues::BoolInt::solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition nvalues.cpp:143
Test::Int::NValues::BoolInt::BoolInt
BoolInt(int n, int m0, Gecode::IntRelType irt0)
Create and register test.
Definition nvalues.cpp:138
Test::Int::NValues::BoolInt::m
int m
Number of values.
Definition nvalues.cpp:135
Test::Int::NValues::BoolInt::irt
Gecode::IntRelType irt
Integer relation type to propagate.
Definition nvalues.cpp:133
Test::Int::NValues::BoolVar
Test number of values of Boolean variables equal to integer variable
Definition nvalues.cpp:171
Test::Int::NValues::BoolVar::irt
Gecode::IntRelType irt
Integer relation type to propagate.
Definition nvalues.cpp:174
Test::Int::NValues::BoolVar::solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition nvalues.cpp:181
Test::Int::NValues::BoolVar::BoolVar
BoolVar(int n, Gecode::IntRelType irt0)
Create and register test.
Definition nvalues.cpp:177
Test::Int::NValues::BoolVar::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &xy)
Post constraint on xy.
Definition nvalues.cpp:199
Test::Int::NValues::Create
Help class to create and register tests.
Definition nvalues.cpp:210
Test::Int::NValues::Create::Create
Create(void)
Perform creation and registration.
Definition nvalues.cpp:213
Test::Int::NValues::IntInt
Test number of values of integer variables equal to integer
Definition nvalues.cpp:48
Test::Int::NValues::IntInt::solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition nvalues.cpp:72
Test::Int::NValues::IntInt::m
int m
Number of values.
Definition nvalues.cpp:53
Test::Int::NValues::IntInt::irt
Gecode::IntRelType irt
Integer relation type to propagate.
Definition nvalues.cpp:51
Test::Int::NValues::IntInt::IntInt
IntInt(int n, int m0, Gecode::IntRelType irt0)
Create and register test.
Definition nvalues.cpp:56
Test::Int::NValues::IntInt::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition nvalues.cpp:87
Test::Int::NValues::IntInt::assignment
virtual Assignment * assignment(void) const
Create and register initial assignment.
Definition nvalues.cpp:65
Test::Int::NValues::IntVar
Test number of values of integer variables equal to integer variable
Definition nvalues.cpp:93
Test::Int::NValues::IntVar::irt
Gecode::IntRelType irt
Integer relation type to propagate.
Definition nvalues.cpp:96
Test::Int::NValues::IntVar::solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition nvalues.cpp:105
Test::Int::NValues::IntVar::IntVar
IntVar(int n, Gecode::IntRelType irt0)
Create and register test.
Definition nvalues.cpp:99
Test::Int::NValues::IntVar::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &xy)
Post constraint on xy.
Definition nvalues.cpp:120
Test::Int::RandomAssignment
Generate random selection of assignments.
Definition int.hh:96
Test::Int::Test::testsearch
bool testsearch
Whether to perform search test.
Definition int.hh:238
Test::Int::Test::testfix
bool testfix
Whether to perform fixpoint test.
Definition int.hh:240
Test::Int::Test::arity
int arity
Number of variables.
Definition int.hh:226
Test::Int::Test::str
static std::string str(Gecode::IntPropLevel ipl)
Map integer propagation level to string.
Definition int.hpp:209
Test::Int::Test::cmp
static bool cmp(T x, Gecode::IntRelType r, T y)
Compare x and y with respect to r.
Definition int.hpp:285
Test::Int::Test::dom
Gecode::IntSet dom
Domain of variables.
Definition int.hh:228
Gecode::IntRelType
IntRelType
Relation types for integers.
Definition int.hh:925
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::nvalues
void nvalues(Home home, const IntVarArgs &x, IntRelType irt, int y, IntPropLevel ipl=IPL_DEF)
Post propagator for .
Definition nvalues.cpp:40
Test::Int::NValues
Tests for number of values constraints
Definition nvalues.cpp:40
Test::Int::NValues::c
Create c
Definition nvalues.cpp:230
Test::Int
Testing finite domain integers.
Definition int.cpp:40
Test
General test support.
Definition afc.cpp:39
int.hh