channel.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, 2006
*
* 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 Channel {
class ChannelFull : public Test {
private:
int xoff; //< Offset for the x variables
int yoff; //< Offset for the y variables
public:
ChannelFull(int xoff0, int yoff0, Gecode::IntPropLevel ipl)
: Test("Channel::Full::"+str(xoff0)+"::"+str(yoff0)+"::"+str(ipl),
8,0,3,false,ipl),
xoff(xoff0), yoff(yoff0) {
contest = CTL_NONE;
}
virtual bool solution(const Assignment& x) const {
for (int i=0; i<4; i++)
if (x[4+x[i]] != i)
return false;
return true;
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
using namespace Gecode;
IntVarArgs xa(4); IntVarArgs ya(4);
for (int i=4; i--; ) {
if (xoff != 0) {
IntVar xo(home, xoff, 3+xoff);
Gecode::rel(home, x[i] == xo-xoff);
xa[i] = xo;
} else {
xa[i] = x[i];
}
if (yoff != 0) {
IntVar yo(home, yoff, 3+yoff);
Gecode::rel(home, x[4+i] == yo-yoff);
ya[i] = yo;
} else {
ya[i] = x[4+i];
}
}
channel(home, xa, xoff, ya, yoff, ipl);
}
};
class ChannelHalf : public Test {
public:
ChannelHalf(Gecode::IntPropLevel ipl)
: Test("Channel::Half::"+str(ipl),6,0,5,false,ipl) {
contest = CTL_NONE;
}
virtual bool solution(const Assignment& x) const {
for (int i=0; i<6; i++)
for (int j=i+1; j<6; j++)
if (x[i] == x[j])
return false;
return true;
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
using namespace Gecode;
Gecode::IntVarArgs y(home,6,dom);
for (int i=0; i<6; i++)
for (int j=0; j<6; j++) {
Gecode::BoolVar b(home,0,1);
rel(home, x[i], Gecode::IRT_EQ, j, b);
rel(home, y[j], Gecode::IRT_EQ, i, b);
}
channel(home, x, y, ipl);
}
};
class ChannelShared : public Test {
public:
ChannelShared(Gecode::IntPropLevel ipl)
: Test("Channel::Shared::"+str(ipl),6,0,5,false,ipl) {
contest = CTL_NONE;
}
virtual bool solution(const Assignment& x) const {
for (int i=0; i<6; i++)
if (x[x[i]] != i)
return false;
return true;
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
using namespace Gecode;
channel(home, x, x, ipl);
}
};
class ChannelLinkSingle : public Test {
public:
ChannelLinkSingle(void)
: Test("Channel::Bool::Single",2,-1,2) {
contest = CTL_NONE;
}
virtual bool solution(const Assignment& x) const {
return ((x[0]==0) || (x[0]==1)) && (x[0]==x[1]);
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
using namespace Gecode;
Gecode::BoolVar b(home,0,1);
channel(home, x[0], b);
channel(home, x[1], b);
}
};
class ChannelLinkMulti : public Test {
private:
int o;
public:
ChannelLinkMulti(const std::string& s, int min, int max, int o0)
: Test("Channel::Bool::Multi::"+s,7,min,max), o(o0) {
}
virtual bool solution(const Assignment& x) const {
int n = x.size()-1;
for (int i=n; i--; )
if ((x[i] != 0) && (x[i] != 1))
return false;
int k=x[n]-o;
if ((k<0) || (k>=n))
return false;
for (int i=0; i<k; i++)
if (x[i] != 0)
return false;
for (int i=k+1; i<n; i++)
if (x[i] != 0)
return false;
return x[k] == 1;
}
virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
using namespace Gecode;
int n=x.size()-1;
Gecode::BoolVarArgs b(n);
for (int i=n; i--; )
b[i]=channel(home,x[i]);
channel(home, b, x[n], o);
}
};
ChannelFull cfd(0,0,Gecode::IPL_DOM);
ChannelFull cfv(0,0,Gecode::IPL_VAL);
ChannelFull cfd11(1,1,Gecode::IPL_DOM);
ChannelFull cfv11(1,1,Gecode::IPL_VAL);
ChannelFull cfd35(3,5,Gecode::IPL_DOM);
ChannelFull cfv35(3,5,Gecode::IPL_VAL);
ChannelHalf chd(Gecode::IPL_DOM);
ChannelHalf chv(Gecode::IPL_VAL);
ChannelShared csd(Gecode::IPL_DOM);
ChannelShared csv(Gecode::IPL_VAL);
ChannelLinkSingle cls;
ChannelLinkMulti clma("A", 0, 5, 0);
ChannelLinkMulti clmb("B", 1, 6, 1);
ChannelLinkMulti clmc("C",-1, 4,-1);
}
}}
// STATISTICS: test-int
Gecode::BoolVarArgs
Passing Boolean variables.
Definition int.hh:712
Gecode::BoolVar
Boolean integer variables.
Definition int.hh:512
Gecode::IntVarArgs
Passing integer variables.
Definition int.hh:656
Gecode::IntVarArray
Integer variable array.
Definition int.hh:763
Gecode::IntVar
Integer variables.
Definition int.hh:371
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::Channel::ChannelFull
Simple test for channel (testing all variables).
Definition channel.cpp:49
Test::Int::Channel::ChannelFull::ChannelFull
ChannelFull(int xoff0, int yoff0, Gecode::IntPropLevel ipl)
Construct and register test.
Definition channel.cpp:55
Test::Int::Channel::ChannelFull::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition channel.cpp:69
Test::Int::Channel::ChannelFull::solution
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition channel.cpp:62
Test::Int::Channel::ChannelHalf
Simple test for channel (testing single set of variables).
Definition channel.cpp:93
Test::Int::Channel::ChannelHalf::ChannelHalf
ChannelHalf(Gecode::IntPropLevel ipl)
Construct and register test.
Definition channel.cpp:96
Test::Int::Channel::ChannelHalf::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition channel.cpp:109
Test::Int::Channel::ChannelHalf::solution
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition channel.cpp:101
Test::Int::Channel::ChannelLinkMulti
Test channel between integer variable and array of Boolean variables
Definition channel.cpp:166
Test::Int::Channel::ChannelLinkMulti::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition channel.cpp:192
Test::Int::Channel::ChannelLinkMulti::solution
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition channel.cpp:175
Test::Int::Channel::ChannelLinkMulti::ChannelLinkMulti
ChannelLinkMulti(const std::string &s, int min, int max, int o0)
Construct and register test.
Definition channel.cpp:171
Test::Int::Channel::ChannelLinkSingle
Test channel between integer and Boolean variable
Definition channel.cpp:145
Test::Int::Channel::ChannelLinkSingle::solution
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition channel.cpp:153
Test::Int::Channel::ChannelLinkSingle::ChannelLinkSingle
ChannelLinkSingle(void)
Construct and register test.
Definition channel.cpp:148
Test::Int::Channel::ChannelLinkSingle::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition channel.cpp:157
Test::Int::Channel::ChannelShared
Test channel with shared variables
Definition channel.cpp:123
Test::Int::Channel::ChannelShared::post
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition channel.cpp:138
Test::Int::Channel::ChannelShared::solution
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition channel.cpp:131
Test::Int::Channel::ChannelShared::ChannelShared
ChannelShared(Gecode::IntPropLevel ipl)
Construct and register test.
Definition channel.cpp:126
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::dom
Gecode::IntSet dom
Domain of variables.
Definition int.hh:228
Test::Int::Test::contest
ConTestLevel contest
Whether to test for certain consistency.
Definition int.hh:236
Gecode::rel
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVar x1)
Post propagator for .
Definition rel.cpp:68
Gecode::IntPropLevel
IntPropLevel
Propagation levels for integer propagators.
Definition int.hh:974
Gecode::IRT_EQ
@ IRT_EQ
Equality ( ).
Definition int.hh:926
Gecode::IPL_DOM
@ IPL_DOM
Domain propagation Options: basic versus advanced propagation.
Definition int.hh:979
Gecode::IPL_VAL
@ IPL_VAL
Value propagation.
Definition int.hh:977
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::min
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition arithmetic.cpp:67
Gecode::max
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition arithmetic.cpp:49
Test::Int::Channel
Tests for channel constraints
Definition channel.cpp:41
Test::Int::Channel::cfd11
ChannelFull cfd11(1, 1, Gecode::IPL_DOM)
Test::Int::Channel::cfd35
ChannelFull cfd35(3, 5, Gecode::IPL_DOM)
Test::Int::Channel::cls
ChannelLinkSingle cls
Definition channel.cpp:219
Test::Int::Channel::cfv
ChannelFull cfv(0, 0, Gecode::IPL_VAL)
Test::Int::Channel::cfd
ChannelFull cfd(0, 0, Gecode::IPL_DOM)
Test::Int::Channel::clma
ChannelLinkMulti clma("A", 0, 5, 0)
Test::Int::Channel::clmb
ChannelLinkMulti clmb("B", 1, 6, 1)
Test::Int::Channel::cfv35
ChannelFull cfv35(3, 5, Gecode::IPL_VAL)
Test::Int::Channel::clmc
ChannelLinkMulti clmc("C",-1, 4,-1)
Test::Int::Channel::cfv11
ChannelFull cfv11(1, 1, Gecode::IPL_VAL)
Test::Int::Channel::csd
ChannelShared csd(Gecode::IPL_DOM)
Test::Int::Channel::chd
ChannelHalf chd(Gecode::IPL_DOM)
Test::Int::Channel::chv
ChannelHalf chv(Gecode::IPL_VAL)
Test::Int::Channel::csv
ChannelShared csv(Gecode::IPL_VAL)
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