dom.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 <gecode/minimodel.hh>
#include "test/int.hh"
namespace Test { namespace Int {
namespace Dom {
class DomInt : public Test {
public:
DomInt(int n)
: Test("Dom::Int::"+str(n),n,-4,4,n == 1,
Gecode::IPL_DOM) {}
virtual bool solution(const Assignment& x) const {
for (int i=x.size(); i--; )
if (x[i] != -2)
return false;
return true;
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
if (x.size() == 1)
Gecode::dom(home, x[0], -2);
else
Gecode::dom(home, x, -2);
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
Gecode::Reify r) {
assert(x.size() == 1);
if (Base::rand(2) != 0) {
Gecode::dom(home, x[0], -2, r);
} else {
switch (r.mode()) {
case Gecode::RM_EQV:
Gecode::rel(home, Gecode::dom(x[0], -2) == r.var()); break;
case Gecode::RM_IMP:
Gecode::rel(home, Gecode::dom(x[0], -2) << r.var()); break;
case Gecode::RM_PMI:
Gecode::rel(home, Gecode::dom(x[0], -2) >> r.var()); break;
default: GECODE_NEVER;
}
}
}
};
class DomRange : public Test {
public:
DomRange(int n)
: Test("Dom::Range::"+str(n),n,-4,4,n == 1,
Gecode::IPL_DOM) {}
virtual bool solution(const Assignment& x) const {
for (int i=x.size(); i--; )
if ((x[i] < -2) || (x[i] > 2))
return false;
return true;
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
if (x.size() == 1)
Gecode::dom(home, x[0], -2, 2);
else
Gecode::dom(home, x, -2, 2);
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
Gecode::Reify r) {
assert(x.size() == 1);
if (Base::rand(2) != 0) {
Gecode::dom(home, x[0], -2, 2, r);
} else {
switch (r.mode()) {
case Gecode::RM_EQV:
Gecode::rel(home, Gecode::dom(x[0], -2, 2) == r.var()); break;
case Gecode::RM_IMP:
Gecode::rel(home, Gecode::dom(x[0], -2, 2) << r.var()); break;
case Gecode::RM_PMI:
Gecode::rel(home, Gecode::dom(x[0], -2, 2) >> r.var()); break;
default: GECODE_NEVER;
}
}
}
};
class DomRangeEmpty : public Test {
public:
DomRangeEmpty(void) : Test("Dom::Range::Empty",1,-4,4,true) {}
virtual bool solution(const Assignment&) const {
return false;
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray&) {
home.fail();
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
Gecode::Reify r) {
Gecode::dom(home, x[0], 3, 2, r);
}
};
const int r[4][2] = {
{-4,-3},{-1,-1},{1,1},{3,5}
};
Gecode::IntSet d(r,4);
class DomDom : public Test {
public:
DomDom(int n)
: Test("Dom::Dom::"+str(n),n,-6,6,n == 1,
Gecode::IPL_DOM) {}
virtual bool solution(const Assignment& x) const {
for (int i=x.size(); i--; )
if (!(((x[i] >= -4) && (x[i] <= -3)) ||
((x[i] >= -1) && (x[i] <= -1)) ||
((x[i] >= 1) && (x[i] <= 1)) ||
((x[i] >= 3) && (x[i] <= 5))))
return false;
return true;
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
if (x.size() == 1)
Gecode::dom(home, x[0], d);
else
Gecode::dom(home, x, d);
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
Gecode::Reify r) {
assert(x.size() == 1);
if (Base::rand(2) != 0) {
Gecode::dom(home, x[0], d, r);
} else {
switch (r.mode()) {
case Gecode::RM_EQV:
Gecode::rel(home, Gecode::dom(x[0], d) == r.var()); break;
case Gecode::RM_IMP:
Gecode::rel(home, Gecode::dom(x[0], d) << r.var()); break;
case Gecode::RM_PMI:
Gecode::rel(home, Gecode::dom(x[0], d) >> r.var()); break;
default: GECODE_NEVER;
}
}
}
};
DomInt di1(1);
DomInt di3(3);
DomRange dr1(1);
DomRange dr3(3);
DomDom dd1(1);
DomDom dd3(3);
DomRangeEmpty dre;
}
}}
// STATISTICS: test-int
Gecode::IntSet
Integer sets.
Definition int.hh:174
Gecode::IntVarArray
Integer variable array.
Definition int.hh:763
Gecode::Reify
Reification specification.
Definition int.hh:876
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::Base::rand
static Gecode::Support::RandomGenerator rand
Random number generator.
Definition test.hh:134
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::Dom::DomDom
Test for domain constraint (full integer set)
Definition dom.cpp:158
Test::Int::Dom::DomDom::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition dom.cpp:175
Test::Int::Dom::DomDom::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x, Gecode::Reify r)
Post reified constraint on x for r.
Definition dom.cpp:182
Test::Int::Dom::DomDom::solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition dom.cpp:165
Test::Int::Dom::DomDom::DomDom
DomDom(int n)
Create and register test.
Definition dom.cpp:161
Test::Int::Dom::DomInt
Test for domain constraint (integer)
Definition dom.cpp:49
Test::Int::Dom::DomInt::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x, Gecode::Reify r)
Post reified constraint on x for r.
Definition dom.cpp:70
Test::Int::Dom::DomInt::DomInt
DomInt(int n)
Create and register test.
Definition dom.cpp:52
Test::Int::Dom::DomInt::solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition dom.cpp:56
Test::Int::Dom::DomInt::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition dom.cpp:63
Test::Int::Dom::DomRangeEmpty
Test for domain constraint (empty range)
Definition dom.cpp:132
Test::Int::Dom::DomRangeEmpty::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &)
Post constraint on x.
Definition dom.cpp:141
Test::Int::Dom::DomRangeEmpty::solution
virtual bool solution(const Assignment &) const
Test whether x is solution
Definition dom.cpp:137
Test::Int::Dom::DomRangeEmpty::DomRangeEmpty
DomRangeEmpty(void)
Create and register test.
Definition dom.cpp:135
Test::Int::Dom::DomRangeEmpty::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x, Gecode::Reify r)
Post reified constraint on x for r.
Definition dom.cpp:145
Test::Int::Dom::DomRange
Test for domain constraint (range)
Definition dom.cpp:91
Test::Int::Dom::DomRange::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x, Gecode::Reify r)
Post reified constraint on x for r.
Definition dom.cpp:112
Test::Int::Dom::DomRange::solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition dom.cpp:98
Test::Int::Dom::DomRange::DomRange
DomRange(int n)
Create and register test.
Definition dom.cpp:94
Test::Int::Dom::DomRange::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition dom.cpp:105
Test::Int::Test::str
static std::string str(Gecode::IntPropLevel ipl)
Map integer propagation level to string.
Definition int.hpp:209
Gecode::Space::fail
void fail(void)
Fail space.
Definition core.hpp:4030
Gecode::rel
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVar x1)
Post propagator for .
Definition rel.cpp:68
Gecode::RM_IMP
@ RM_IMP
Implication for reification.
Definition int.hh:862
Gecode::RM_PMI
@ RM_PMI
Inverse implication for reification.
Definition int.hh:869
Gecode::RM_EQV
@ RM_EQV
Equivalence for reification (default).
Definition int.hh:855
minimodel.hh
Gecode
Gecode toplevel namespace
Gecode::dom
void dom(Home home, FloatVar x, FloatVal n)
Propagates .
Definition dom.cpp:40
Test::Int::Dom
Tests for domain constraints
Definition dom.cpp:41
Test::Int::Dom::dre
DomRangeEmpty dre
Definition dom.cpp:207
Test::Int::Dom::dr1
DomRange dr1(1)
Test::Int::Dom::dd1
DomDom dd1(1)
Test::Int::Dom::dd3
DomDom dd3(3)
Test::Int::Dom::dr3
DomRange dr3(3)
Test::Int::Dom::r
const int r[4][2]
Definition dom.cpp:152
Test::Int::Dom::di1
DomInt di1(1)
Test::Int::Dom::d
Gecode::IntSet d(r, 4)
Test::Int::Dom::di3
DomInt di3(3)
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