mm-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, 2008
*
* 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"
#include <gecode/minimodel.hh>
namespace Test { namespace Int {
namespace MiniModelCount {
std::string expand(Gecode::IntRelType irt) {
switch (irt) {
case Gecode::IRT_EQ: return "Exactly";
case Gecode::IRT_LQ: return "AtMost";
case Gecode::IRT_GQ: return "AtLeast";
default: GECODE_NEVER;
}
GECODE_NEVER;
return "";
}
class IntInt : public Test {
protected:
Gecode::IntRelType irt;
public:
IntInt(Gecode::IntRelType irt0)
: Test("MiniModel::"+expand(irt0)+"::Int::Int",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) {
switch (irt) {
case Gecode::IRT_EQ:
Gecode::exactly(home,x,0,2); break;
case Gecode::IRT_LQ:
Gecode::atmost(home,x,0,2); break;
case Gecode::IRT_GQ:
Gecode::atleast(home,x,0,2); break;
default: GECODE_NEVER;
}
}
};
class IntVar : public Test {
protected:
Gecode::IntRelType irt;
public:
IntVar(Gecode::IntRelType irt0)
: Test("MiniModel::"+expand(irt0)+"::Int::Var",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];
switch (irt) {
case Gecode::IRT_EQ:
Gecode::exactly(home,y,0,x[4]); break;
case Gecode::IRT_LQ:
Gecode::atmost(home,y,0,x[4]); break;
case Gecode::IRT_GQ:
Gecode::atleast(home,y,0,x[4]); break;
default: GECODE_NEVER;
}
}
};
class VarVar : public Test {
protected:
Gecode::IntRelType irt;
public:
VarVar(Gecode::IntRelType irt0)
: Test("MiniModel::"+expand(irt0)+"::Var::Var",5,-2,2), irt(irt0) {}
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];
switch (irt) {
case Gecode::IRT_EQ:
Gecode::exactly(home,y,x[3],x[4]); break;
case Gecode::IRT_LQ:
Gecode::atmost(home,y,x[3],x[4]); break;
case Gecode::IRT_GQ:
Gecode::atleast(home,y,x[3],x[4]); break;
default: GECODE_NEVER;
}
}
};
class VarInt : public Test {
protected:
Gecode::IntRelType irt;
public:
VarInt(Gecode::IntRelType irt0)
: Test("MiniModel::"+expand(irt0)+"::Var::Int",4,-2,2), irt(irt0) {}
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];
switch (irt) {
case Gecode::IRT_EQ:
Gecode::exactly(home,y,x[3],2); break;
case Gecode::IRT_LQ:
Gecode::atmost(home,y,x[3],2); break;
case Gecode::IRT_GQ:
Gecode::atleast(home,y,x[3],2); break;
default: GECODE_NEVER;
}
}
};
Gecode::IntArgs ints({1,0,3,2});
class IntArrayInt : public Test {
protected:
Gecode::IntRelType irt;
public:
IntArrayInt(Gecode::IntRelType irt0)
: Test("MiniModel::"+expand(irt0)+"::IntArray::Int",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];
switch (irt) {
case Gecode::IRT_EQ:
Gecode::exactly(home,y,ints,2); break;
case Gecode::IRT_LQ:
Gecode::atmost(home,y,ints,2); break;
case Gecode::IRT_GQ:
Gecode::atleast(home,y,ints,2); break;
default: GECODE_NEVER;
}
}
};
class IntArrayVar : public Test {
protected:
Gecode::IntRelType irt;
public:
IntArrayVar(Gecode::IntRelType irt0)
: Test("MiniModel::"+expand(irt0)+"::IntArray::Var",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];
switch (irt) {
case Gecode::IRT_EQ:
Gecode::exactly(home,y,ints,x[4]); break;
case Gecode::IRT_LQ:
Gecode::atmost(home,y,ints,x[4]); break;
case Gecode::IRT_GQ:
Gecode::atleast(home,y,ints,x[4]); break;
default: GECODE_NEVER;
}
}
};
class Create {
public:
Create(void) {
for (IntRelTypes irts; irts(); ++irts)
if ((irts.irt() == Gecode::IRT_EQ) ||
(irts.irt() == Gecode::IRT_LQ) ||
(irts.irt() == Gecode::IRT_GQ)) {
(void) new IntInt(irts.irt());
(void) new IntVar(irts.irt());
(void) new VarVar(irts.irt());
(void) new VarInt(irts.irt());
(void) new IntArrayInt(irts.irt());
(void) new IntArrayVar(irts.irt());
}
}
};
Create c;
}
}}
// STATISTICS: test-minimodel
Gecode::IntArgs
Passing integer arguments.
Definition int.hh:628
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
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::IntRelTypes
Iterator for integer relation types.
Definition int.hh:368
Test::Int::MiniModelCount::Create
Help class to create and register tests.
Definition mm-count.cpp:266
Test::Int::MiniModelCount::Create::Create
Create(void)
Perform creation and registration.
Definition mm-count.cpp:269
Test::Int::MiniModelCount::IntArrayInt
Test number of several equal integers equal to integer
Definition mm-count.cpp:196
Test::Int::MiniModelCount::IntArrayInt::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition mm-count.cpp:214
Test::Int::MiniModelCount::IntArrayInt::solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition mm-count.cpp:206
Test::Int::MiniModelCount::IntArrayInt::irt
Gecode::IntRelType irt
Integer relation type to propagate.
Definition mm-count.cpp:199
Test::Int::MiniModelCount::IntArrayInt::IntArrayInt
IntArrayInt(Gecode::IntRelType irt0)
Create and register test.
Definition mm-count.cpp:202
Test::Int::MiniModelCount::IntArrayVar
Test number of several equal integers equal to integer variable
Definition mm-count.cpp:231
Test::Int::MiniModelCount::IntArrayVar::irt
Gecode::IntRelType irt
Integer relation type to propagate.
Definition mm-count.cpp:234
Test::Int::MiniModelCount::IntArrayVar::solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition mm-count.cpp:241
Test::Int::MiniModelCount::IntArrayVar::IntArrayVar
IntArrayVar(Gecode::IntRelType irt0)
Create and register test.
Definition mm-count.cpp:237
Test::Int::MiniModelCount::IntArrayVar::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition mm-count.cpp:249
Test::Int::MiniModelCount::IntInt
Test number of equal integers equal to integer
Definition mm-count.cpp:61
Test::Int::MiniModelCount::IntInt::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition mm-count.cpp:78
Test::Int::MiniModelCount::IntInt::IntInt
IntInt(Gecode::IntRelType irt0)
Create and register test.
Definition mm-count.cpp:67
Test::Int::MiniModelCount::IntInt::solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition mm-count.cpp:70
Test::Int::MiniModelCount::IntInt::irt
Gecode::IntRelType irt
Integer relation type to propagate.
Definition mm-count.cpp:64
Test::Int::MiniModelCount::IntVar
Test number of equal integers equal to integer variable
Definition mm-count.cpp:92
Test::Int::MiniModelCount::IntVar::irt
Gecode::IntRelType irt
Integer relation type to propagate.
Definition mm-count.cpp:95
Test::Int::MiniModelCount::IntVar::IntVar
IntVar(Gecode::IntRelType irt0)
Create and register test.
Definition mm-count.cpp:98
Test::Int::MiniModelCount::IntVar::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition mm-count.cpp:109
Test::Int::MiniModelCount::IntVar::solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition mm-count.cpp:101
Test::Int::MiniModelCount::VarInt
Test number of equal variables equal to integer
Definition mm-count.cpp:160
Test::Int::MiniModelCount::VarInt::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition mm-count.cpp:177
Test::Int::MiniModelCount::VarInt::VarInt
VarInt(Gecode::IntRelType irt0)
Create and register test.
Definition mm-count.cpp:166
Test::Int::MiniModelCount::VarInt::solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition mm-count.cpp:169
Test::Int::MiniModelCount::VarInt::irt
Gecode::IntRelType irt
Integer relation type to propagate.
Definition mm-count.cpp:163
Test::Int::MiniModelCount::VarVar
Test number of equal variables equal to integer variable
Definition mm-count.cpp:126
Test::Int::MiniModelCount::VarVar::VarVar
VarVar(Gecode::IntRelType irt0)
Create and register test.
Definition mm-count.cpp:132
Test::Int::MiniModelCount::VarVar::solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition mm-count.cpp:135
Test::Int::MiniModelCount::VarVar::irt
Gecode::IntRelType irt
Integer relation type to propagate.
Definition mm-count.cpp:129
Test::Int::MiniModelCount::VarVar::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition mm-count.cpp:143
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
Gecode::IntRelType
IntRelType
Relation types for integers.
Definition int.hh:925
Gecode::IRT_EQ
@ IRT_EQ
Equality ( ).
Definition int.hh:926
Gecode::IRT_GQ
@ IRT_GQ
Greater or equal ( ).
Definition int.hh:930
Gecode::IRT_LQ
@ IRT_LQ
Less or equal ( ).
Definition int.hh:928
minimodel.hh
Gecode::atmost
void atmost(Home home, const IntVarArgs &x, int n, int m, IntPropLevel ipl=IPL_DEF)
Post constraint .
Definition aliases.hpp:39
Gecode::atleast
void atleast(Home home, const IntVarArgs &x, int n, int m, IntPropLevel ipl=IPL_DEF)
Post constraint .
Definition aliases.hpp:70
Gecode::exactly
void exactly(Home home, const IntVarArgs &x, int n, int m, IntPropLevel ipl=IPL_DEF)
Post constraint .
Definition aliases.hpp:101
Test::Int::MiniModelCount
Tests for minimal modelling constraints (counting)
Definition mm-count.cpp:41
Test::Int::MiniModelCount::expand
std::string expand(Gecode::IntRelType irt)
Expand relation to abbreviation.
Definition mm-count.cpp:44
Test::Int::MiniModelCount::ints
Gecode::IntArgs ints({1, 0, 3, 2})
Test::Int::MiniModelCount::c
Create c
Definition mm-count.cpp:284
Test::Int
Testing finite domain integers.
Definition int.cpp:40
Test
General test support.
Definition afc.cpp:39
GECODE_NEVER
#define GECODE_NEVER
Assert that this command is never executed.
Definition macros.hpp:56
int.hh