count.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, 2005
*
* 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 "test/int.hh"
namespace Test { namespace Int {
namespace Count {
class IntInt : public Test {
protected:
Gecode::IntRelType irt;
public:
IntInt(Gecode::IntRelType irt0)
: Test("Count::Int::Int::"+str(irt0),4,-2,2), irt(irt0) {}
virtual bool solution(const Assignment& x) const {
int m = 0;
for (int i=x.size(); i--; )
if (x[i] == 0)
m++;
return cmp(m,irt,2);
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
Gecode::count(home, x, 0, irt, 2);
}
};
class SetInt : public Test {
protected:
Gecode::IntRelType irt;
public:
SetInt(Gecode::IntRelType irt0)
: Test("Count::Set::Int::"+str(irt0),4,-2,2), irt(irt0) {}
virtual bool solution(const Assignment& x) const {
int m = 0;
for (int i=x.size(); i--; )
if ((x[i] >= -1) && (x[i] <= 1))
m++;
return cmp(m,irt,2);
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
Gecode::IntSet s(-1,1);
Gecode::count(home, x, s, irt, 2);
}
};
class IntIntDup : public Test {
protected:
Gecode::IntRelType irt;
public:
IntIntDup(Gecode::IntRelType irt0)
: Test("Count::Int::Int::Dup::"+str(irt0),4,-2,2), irt(irt0) {}
virtual bool solution(const Assignment& x) const {
int m = 0;
for (int i=x.size(); i--; )
if (x[i] == 0)
m += 2;
return cmp(m,irt,4);
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
Gecode::IntVarArgs y(8);
for (int i=x.size(); i--; )
y[i]=y[4+i]=x[i];
Gecode::count(home, y, 0, irt, 4);
}
};
class IntVar : public Test {
protected:
Gecode::IntRelType irt;
public:
IntVar(Gecode::IntRelType irt0)
: Test("Count::Int::Var::"+str(irt0),5,-2,2), irt(irt0) {}
virtual bool solution(const Assignment& x) const {
int m = 0;
for (int i=0; i<4; i++)
if (x[i] == 0)
m++;
return cmp(m,irt,x[4]);
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
Gecode::IntVarArgs y(4);
for (int i=0; i<4; i++)
y[i]=x[i];
Gecode::count(home, y, 0, irt, x[4]);
}
};
class SetVar : public Test {
protected:
Gecode::IntRelType irt;
public:
SetVar(Gecode::IntRelType irt0)
: Test("Count::Set::Var::"+str(irt0),5,-2,2), irt(irt0) {}
virtual bool solution(const Assignment& x) const {
int m = 0;
for (int i=0; i<4; i++)
if ((x[i] >= -1) && (x[i] <= 1))
m++;
return cmp(m,irt,x[4]);
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
Gecode::IntVarArgs y(4);
for (int i=0; i<4; i++)
y[i]=x[i];
Gecode::IntSet s(-1,1);
Gecode::count(home, y, s, irt, x[4]);
}
};
Gecode::IntArgs ints({1,0,3,2});
class IntArrayInt : public Test {
protected:
Gecode::IntRelType irt;
public:
IntArrayInt(Gecode::IntRelType irt0)
: Test("Count::IntArray::Int::"+str(irt0),5,-2,2), irt(irt0) {}
virtual bool solution(const Assignment& x) const {
int m = 0;
for (int i=0; i<4; i++)
if (x[i] == ints[i])
m++;
return cmp(m,irt,2);
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
Gecode::IntVarArgs y(4);
for (int i=0; i<4; i++)
y[i]=x[i];
Gecode::count(home, y, ints, irt, 2);
}
};
class IntArrayVar : public Test {
protected:
Gecode::IntRelType irt;
public:
IntArrayVar(Gecode::IntRelType irt0)
: Test("Count::IntArray::Var::"+str(irt0),5,-2,2), irt(irt0) {}
virtual bool solution(const Assignment& x) const {
int m = 0;
for (int i=0; i<4; i++)
if (x[i] == ints[i])
m++;
return cmp(m,irt,x[4]);
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
Gecode::IntVarArgs y(4);
for (int i=0; i<4; i++)
y[i]=x[i];
Gecode::count(home, y, ints, irt, x[4]);
}
};
class IntVarShared : public Test {
protected:
Gecode::IntRelType irt;
public:
IntVarShared(Gecode::IntRelType irt0)
: Test("Count::Int::Var::Shared::"+str(irt0),4,-2,2), irt(irt0) {}
virtual bool solution(const Assignment& x) const {
int m = 0;
for (int i=0; i<4; i++)
if (x[i] == 0)
m++;
return cmp(m,irt,x[2]);
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
Gecode::count(home, x, 0, irt, x[2]);
}
};
class VarVar : public Test {
protected:
Gecode::IntRelType irt;
public:
VarVar(Gecode::IntRelType irt0, Gecode::IntPropLevel ipl)
: Test("Count::Var::Var::"+str(irt0)+"::"+str(ipl),5,-2,2,false,ipl),
irt(irt0) {
contest = CTL_NONE;
}
virtual bool solution(const Assignment& x) const {
int m = 0;
for (int i=0; i<3; i++)
if (x[i] == x[3])
m++;
return cmp(m,irt,x[4]);
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
Gecode::IntVarArgs y(3);
for (int i=0; i<3; i++)
y[i]=x[i];
Gecode::count(home, y, x[3], irt, x[4], ipl);
}
};
class VarInt : public Test {
protected:
Gecode::IntRelType irt;
public:
VarInt(Gecode::IntRelType irt0, Gecode::IntPropLevel ipl)
: Test("Count::Var::Int::"+str(irt0)+"::"+str(ipl),4,-2,2,false,ipl),
irt(irt0) {
contest = CTL_NONE;
}
virtual bool solution(const Assignment& x) const {
int m = 0;
for (int i=0; i<3; i++)
if (x[i] == x[3])
m++;
return cmp(m,irt,2);
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
Gecode::IntVarArgs y(3);
for (int i=0; i<3; i++)
y[i]=x[i];
Gecode::count(home, y, x[3], irt, 2, ipl);
}
};
class VarVarSharedA : public Test {
protected:
Gecode::IntRelType irt;
public:
VarVarSharedA(Gecode::IntRelType irt0)
: Test("Count::Var::Var::Shared::A::"+str(irt0),5,-2,2), irt(irt0) {}
virtual bool solution(const Assignment& x) const {
int m = 0;
for (int i=0; i<4; i++)
if (x[i] == x[1])
m++;
return cmp(m,irt,x[4]);
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
Gecode::IntVarArgs y(4);
for (int i=0; i<4; i++)
y[i]=x[i];
Gecode::count(home, y, x[1], irt, x[4]);
}
};
class VarVarSharedB : public Test {
protected:
Gecode::IntRelType irt;
public:
VarVarSharedB(Gecode::IntRelType irt0)
: Test("Count::Var::Var::Shared::B::"+str(irt0),5,-2,2), irt(irt0) {}
virtual bool solution(const Assignment& x) const {
int m = 0;
for (int i=0; i<4; i++)
if (x[i] == x[4])
m++;
return cmp(m,irt,x[3]);
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
Gecode::IntVarArgs y(4);
for (int i=0; i<4; i++)
y[i]=x[i];
Gecode::count(home, y, x[4], irt, x[3]);
}
};
class VarVarSharedC : public Test {
protected:
Gecode::IntRelType irt;
public:
VarVarSharedC(Gecode::IntRelType irt0)
: Test("Count::Var::Var::Shared::C::"+str(irt0),4,-2,2), irt(irt0) {}
virtual bool solution(const Assignment& x) const {
int m = 0;
for (int i=0; i<4; i++)
if (x[i] == x[1])
m++;
return cmp(m,irt,x[3]);
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
Gecode::count(home, x, x[1], irt, x[3]);
}
};
class Create {
public:
Create(void) {
for (IntRelTypes irts; irts(); ++irts) {
(void) new IntInt(irts.irt());
(void) new SetInt(irts.irt());
(void) new IntIntDup(irts.irt());
(void) new IntVar(irts.irt());
(void) new SetVar(irts.irt());
(void) new IntArrayInt(irts.irt());
(void) new IntArrayVar(irts.irt());
(void) new IntVarShared(irts.irt());
(void) new VarVar(irts.irt(), Gecode::IPL_BND);
(void) new VarVar(irts.irt(), Gecode::IPL_DOM);
(void) new VarInt(irts.irt(), Gecode::IPL_BND);
(void) new VarInt(irts.irt(), Gecode::IPL_DOM);
(void) new VarVarSharedA(irts.irt());
(void) new VarVarSharedB(irts.irt());
(void) new VarVarSharedC(irts.irt());
}
}
};
Create c;
}
}}
// STATISTICS: test-int
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::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::Count::Create
Help class to create and register tests.
Definition count.cpp:383
Test::Int::Count::Create::Create
Create(void)
Perform creation and registration.
Definition count.cpp:386
Test::Int::Count::IntArrayInt
Test number of several equal integers equal to integer
Definition count.cpp:175
Test::Int::Count::IntArrayInt::IntArrayInt
IntArrayInt(Gecode::IntRelType irt0)
Create and register test.
Definition count.cpp:181
Test::Int::Count::IntArrayInt::solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition count.cpp:184
Test::Int::Count::IntArrayInt::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition count.cpp:192
Test::Int::Count::IntArrayInt::irt
Gecode::IntRelType irt
Integer relation type to propagate.
Definition count.cpp:178
Test::Int::Count::IntArrayVar
Test number of several equal integers equal to integer variable
Definition count.cpp:201
Test::Int::Count::IntArrayVar::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition count.cpp:218
Test::Int::Count::IntArrayVar::IntArrayVar
IntArrayVar(Gecode::IntRelType irt0)
Create and register test.
Definition count.cpp:207
Test::Int::Count::IntArrayVar::irt
Gecode::IntRelType irt
Integer relation type to propagate.
Definition count.cpp:204
Test::Int::Count::IntArrayVar::solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition count.cpp:210
Test::Int::Count::IntIntDup
Test number of equal integers equal to integer with duplicate variables
Definition count.cpp:94
Test::Int::Count::IntIntDup::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition count.cpp:111
Test::Int::Count::IntIntDup::irt
Gecode::IntRelType irt
Integer relation type to propagate.
Definition count.cpp:97
Test::Int::Count::IntIntDup::solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition count.cpp:103
Test::Int::Count::IntIntDup::IntIntDup
IntIntDup(Gecode::IntRelType irt0)
Create and register test.
Definition count.cpp:100
Test::Int::Count::IntInt
Test number of equal integers equal to integer
Definition count.cpp:47
Test::Int::Count::IntInt::IntInt
IntInt(Gecode::IntRelType irt0)
Create and register test.
Definition count.cpp:53
Test::Int::Count::IntInt::irt
Gecode::IntRelType irt
Integer relation type to propagate.
Definition count.cpp:50
Test::Int::Count::IntInt::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition count.cpp:64
Test::Int::Count::IntInt::solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition count.cpp:56
Test::Int::Count::IntVarShared
Test number of equal integers equal to integer variable with sharing
Definition count.cpp:227
Test::Int::Count::IntVarShared::irt
Gecode::IntRelType irt
Integer relation type to propagate.
Definition count.cpp:230
Test::Int::Count::IntVarShared::IntVarShared
IntVarShared(Gecode::IntRelType irt0)
Create and register test.
Definition count.cpp:233
Test::Int::Count::IntVarShared::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition count.cpp:244
Test::Int::Count::IntVarShared::solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition count.cpp:236
Test::Int::Count::IntVar
Test number of equal integers equal to integer variable
Definition count.cpp:120
Test::Int::Count::IntVar::IntVar
IntVar(Gecode::IntRelType irt0)
Create and register test.
Definition count.cpp:126
Test::Int::Count::IntVar::solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition count.cpp:129
Test::Int::Count::IntVar::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition count.cpp:137
Test::Int::Count::IntVar::irt
Gecode::IntRelType irt
Integer relation type to propagate.
Definition count.cpp:123
Test::Int::Count::SetInt
Test number of integers contained in an integer set equal to integer
Definition count.cpp:70
Test::Int::Count::SetInt::solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition count.cpp:79
Test::Int::Count::SetInt::SetInt
SetInt(Gecode::IntRelType irt0)
Create and register test.
Definition count.cpp:76
Test::Int::Count::SetInt::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition count.cpp:87
Test::Int::Count::SetInt::irt
Gecode::IntRelType irt
Integer relation type to propagate.
Definition count.cpp:73
Test::Int::Count::SetVar
Test number of integers contained in an integer set equal to integer variable
Definition count.cpp:146
Test::Int::Count::SetVar::irt
Gecode::IntRelType irt
Integer relation type to propagate.
Definition count.cpp:149
Test::Int::Count::SetVar::solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition count.cpp:155
Test::Int::Count::SetVar::SetVar
SetVar(Gecode::IntRelType irt0)
Create and register test.
Definition count.cpp:152
Test::Int::Count::SetVar::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition count.cpp:163
Test::Int::Count::VarInt
Test number of equal variables equal to integer
Definition count.cpp:279
Test::Int::Count::VarInt::irt
Gecode::IntRelType irt
Integer relation type to propagate.
Definition count.cpp:282
Test::Int::Count::VarInt::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition count.cpp:299
Test::Int::Count::VarInt::solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition count.cpp:291
Test::Int::Count::VarInt::VarInt
VarInt(Gecode::IntRelType irt0, Gecode::IntPropLevel ipl)
Create and register test.
Definition count.cpp:285
Test::Int::Count::VarVarSharedA
Test number of equal variables equal to integer variable with sharing
Definition count.cpp:308
Test::Int::Count::VarVarSharedA::irt
Gecode::IntRelType irt
Integer relation type to propagate.
Definition count.cpp:311
Test::Int::Count::VarVarSharedA::solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition count.cpp:317
Test::Int::Count::VarVarSharedA::VarVarSharedA
VarVarSharedA(Gecode::IntRelType irt0)
Create and register test.
Definition count.cpp:314
Test::Int::Count::VarVarSharedA::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition count.cpp:325
Test::Int::Count::VarVarSharedB
Test number of equal variables equal to integer variable with sharing
Definition count.cpp:334
Test::Int::Count::VarVarSharedB::irt
Gecode::IntRelType irt
Integer relation type to propagate.
Definition count.cpp:337
Test::Int::Count::VarVarSharedB::solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition count.cpp:343
Test::Int::Count::VarVarSharedB::VarVarSharedB
VarVarSharedB(Gecode::IntRelType irt0)
Create and register test.
Definition count.cpp:340
Test::Int::Count::VarVarSharedB::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition count.cpp:351
Test::Int::Count::VarVarSharedC
Test number of equal variables equal to integer variable with sharing
Definition count.cpp:360
Test::Int::Count::VarVarSharedC::irt
Gecode::IntRelType irt
Integer relation type to propagate.
Definition count.cpp:363
Test::Int::Count::VarVarSharedC::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition count.cpp:377
Test::Int::Count::VarVarSharedC::VarVarSharedC
VarVarSharedC(Gecode::IntRelType irt0)
Create and register test.
Definition count.cpp:366
Test::Int::Count::VarVarSharedC::solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition count.cpp:369
Test::Int::Count::VarVar
Test number of equal variables equal to integer variable
Definition count.cpp:250
Test::Int::Count::VarVar::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition count.cpp:270
Test::Int::Count::VarVar::VarVar
VarVar(Gecode::IntRelType irt0, Gecode::IntPropLevel ipl)
Create and register test.
Definition count.cpp:256
Test::Int::Count::VarVar::solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition count.cpp:262
Test::Int::Count::VarVar::irt
Gecode::IntRelType irt
Integer relation type to propagate.
Definition count.cpp:253
Test::Int::IntRelTypes
Iterator for integer relation types.
Definition int.hh:368
Test::Int::Test::ipl
Gecode::IntPropLevel ipl
Propagation level.
Definition int.hh:234
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::contest
ConTestLevel contest
Whether to test for certain consistency.
Definition int.hh:236
Gecode::IntRelType
IntRelType
Relation types for integers.
Definition int.hh:925
Gecode::IntPropLevel
IntPropLevel
Propagation levels for integer propagators.
Definition int.hh:974
Gecode::IPL_DOM
@ IPL_DOM
Domain propagation Options: basic versus advanced propagation.
Definition int.hh:979
Gecode::IPL_BND
@ IPL_BND
Bounds propagation.
Definition int.hh:978
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
Test::Int::Count
Tests for count constraints
Definition count.cpp:39
Test::Int::Count::ints
Gecode::IntArgs ints({1, 0, 3, 2})
Test::Int::Count::c
Create c
Definition count.cpp:407
Test::Int
Testing finite domain integers.
Definition int.cpp:40
Test::Int::CTL_NONE
@ CTL_NONE
No consistency-test.
Definition int.hh:140
Test
General test support.
Definition afc.cpp:39
int.hh