Generated on Thu Jan 16 2025 00:00:00 for Gecode by doxygen 1.14.0
channel.cpp
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Christian Schulte <schulte@gecode.org>
5 *
6 * Copyright:
7 * Christian Schulte, 2006
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.org
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files (the
15 * "Software"), to deal in the Software without restriction, including
16 * without limitation the rights to use, copy, modify, merge, publish,
17 * distribute, sublicense, and/or sell copies of the Software, and to
18 * permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 *
32 */
33
34#include "test/int.hh"
35
36#include <gecode/minimodel.hh>
37
38namespace Test { namespace Int {
39
41 namespace Channel {
42
48
49 class ChannelFull : public Test {
50 private:
51 int xoff; //< Offset for the x variables
52 int yoff; //< Offset for the y variables
53 public:
55 ChannelFull(int xoff0, int yoff0, Gecode::IntPropLevel ipl)
56 : Test("Channel::Full::"+str(xoff0)+"::"+str(yoff0)+"::"+str(ipl),
57 8,0,3,false,ipl),
58 xoff(xoff0), yoff(yoff0) {
60 }
61
62 virtual bool solution(const Assignment& x) const {
63 for (int i=0; i<4; i++)
64 if (x[4+x[i]] != i)
65 return false;
66 return true;
67 }
68
69 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
70 using namespace Gecode;
71 IntVarArgs xa(4); IntVarArgs ya(4);
72 for (int i=4; i--; ) {
73 if (xoff != 0) {
74 IntVar xo(home, xoff, 3+xoff);
75 Gecode::rel(home, x[i] == xo-xoff);
76 xa[i] = xo;
77 } else {
78 xa[i] = x[i];
79 }
80 if (yoff != 0) {
81 IntVar yo(home, yoff, 3+yoff);
82 Gecode::rel(home, x[4+i] == yo-yoff);
83 ya[i] = yo;
84 } else {
85 ya[i] = x[4+i];
86 }
87 }
88 channel(home, xa, xoff, ya, yoff, ipl);
89 }
90 };
91
93 class ChannelHalf : public Test {
94 public:
97 : Test("Channel::Half::"+str(ipl),6,0,5,false,ipl) {
99 }
100
101 virtual bool solution(const Assignment& x) const {
102 for (int i=0; i<6; i++)
103 for (int j=i+1; j<6; j++)
104 if (x[i] == x[j])
105 return false;
106 return true;
107 }
108
109 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
110 using namespace Gecode;
111 Gecode::IntVarArgs y(home,6,dom);
112 for (int i=0; i<6; i++)
113 for (int j=0; j<6; j++) {
114 Gecode::BoolVar b(home,0,1);
115 rel(home, x[i], Gecode::IRT_EQ, j, b);
116 rel(home, y[j], Gecode::IRT_EQ, i, b);
117 }
118 channel(home, x, y, ipl);
119 }
120 };
121
123 class ChannelShared : public Test {
124 public:
127 : Test("Channel::Shared::"+str(ipl),6,0,5,false,ipl) {
129 }
130
131 virtual bool solution(const Assignment& x) const {
132 for (int i=0; i<6; i++)
133 if (x[x[i]] != i)
134 return false;
135 return true;
136 }
137
138 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
139 using namespace Gecode;
140 channel(home, x, x, ipl);
141 }
142 };
143
145 class ChannelLinkSingle : public Test {
146 public:
149 : Test("Channel::Bool::Single",2,-1,2) {
151 }
152
153 virtual bool solution(const Assignment& x) const {
154 return ((x[0]==0) || (x[0]==1)) && (x[0]==x[1]);
155 }
156
157 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
158 using namespace Gecode;
159 Gecode::BoolVar b(home,0,1);
160 channel(home, x[0], b);
161 channel(home, x[1], b);
162 }
163 };
164
166 class ChannelLinkMulti : public Test {
167 private:
168 int o;
169 public:
171 ChannelLinkMulti(const std::string& s, int min, int max, int o0)
172 : Test("Channel::Bool::Multi::"+s,7,min,max), o(o0) {
173 }
174
175 virtual bool solution(const Assignment& x) const {
176 int n = x.size()-1;
177 for (int i=n; i--; )
178 if ((x[i] != 0) && (x[i] != 1))
179 return false;
180 int k=x[n]-o;
181 if ((k<0) || (k>=n))
182 return false;
183 for (int i=0; i<k; i++)
184 if (x[i] != 0)
185 return false;
186 for (int i=k+1; i<n; i++)
187 if (x[i] != 0)
188 return false;
189 return x[k] == 1;
190 }
191
192 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
193 using namespace Gecode;
194 int n=x.size()-1;
196 for (int i=n; i--; )
197 b[i]=channel(home,x[i]);
198 channel(home, b, x[n], o);
199 }
200 };
201
202
203
206
209
212
215
218
220
221 ChannelLinkMulti clma("A", 0, 5, 0);
222 ChannelLinkMulti clmb("B", 1, 6, 1);
223 ChannelLinkMulti clmc("C",-1, 4,-1);
225
226 }
227}}
228
229// STATISTICS: test-int
230
Passing Boolean variables.
Definition int.hh:721
Boolean integer variables.
Definition int.hh:515
Passing integer variables.
Definition int.hh:662
Integer variable array.
Definition int.hh:772
Integer variables.
Definition int.hh:371
Computation spaces.
Definition core.hpp:1744
Base class for assignments
Definition int.hh:59
Simple test for channel (testing all variables)
Definition channel.cpp:49
ChannelFull(int xoff0, int yoff0, Gecode::IntPropLevel ipl)
Construct and register test.
Definition channel.cpp:55
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition channel.cpp:69
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition channel.cpp:62
Simple test for channel (testing single set of variables)
Definition channel.cpp:93
ChannelHalf(Gecode::IntPropLevel ipl)
Construct and register test.
Definition channel.cpp:96
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition channel.cpp:109
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition channel.cpp:101
Test channel between integer variable and array of Boolean variables
Definition channel.cpp:166
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition channel.cpp:192
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition channel.cpp:175
ChannelLinkMulti(const std::string &s, int min, int max, int o0)
Construct and register test.
Definition channel.cpp:171
Test channel between integer and Boolean variable
Definition channel.cpp:145
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition channel.cpp:153
ChannelLinkSingle(void)
Construct and register test.
Definition channel.cpp:148
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition channel.cpp:157
Test channel with shared variables
Definition channel.cpp:123
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition channel.cpp:138
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition channel.cpp:131
ChannelShared(Gecode::IntPropLevel ipl)
Construct and register test.
Definition channel.cpp:126
Gecode::IntPropLevel ipl
Propagation level.
Definition int.hh:234
static std::string str(Gecode::IntPropLevel ipl)
Map integer propagation level to string.
Definition int.hpp:209
Gecode::IntSet dom
Domain of variables.
Definition int.hh:228
ConTestLevel contest
Whether to test for certain consistency.
Definition int.hh:236
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVar x1)
Post propagator for .
Definition rel.cpp:68
IntPropLevel
Propagation levels for integer propagators.
Definition int.hh:989
@ IRT_EQ
Equality ( )
Definition int.hh:941
@ IPL_DOM
Domain propagation Options: basic versus advanced propagation.
Definition int.hh:994
@ IPL_VAL
Value propagation.
Definition int.hh:992
Gecode toplevel namespace
void channel(Home home, FloatVar x0, IntVar x1)
Post propagator for channeling a float and an integer variable .
Definition channel.cpp:41
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Post propagator for SetVar SetOpType SetVar y
Definition set.hh:773
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Post propagator for SetVar x
Definition set.hh:773
Tests for channel constraints
Definition channel.cpp:41
ChannelFull cfd11(1, 1, Gecode::IPL_DOM)
ChannelFull cfd35(3, 5, Gecode::IPL_DOM)
ChannelLinkSingle cls
Definition channel.cpp:219
ChannelFull cfv(0, 0, Gecode::IPL_VAL)
ChannelFull cfd(0, 0, Gecode::IPL_DOM)
ChannelLinkMulti clma("A", 0, 5, 0)
ChannelLinkMulti clmb("B", 1, 6, 1)
ChannelFull cfv35(3, 5, Gecode::IPL_VAL)
ChannelLinkMulti clmc("C",-1, 4,-1)
ChannelFull cfv11(1, 1, Gecode::IPL_VAL)
ChannelShared csd(Gecode::IPL_DOM)
ChannelHalf chd(Gecode::IPL_DOM)
ChannelHalf chv(Gecode::IPL_VAL)
ChannelShared csv(Gecode::IPL_VAL)
Testing finite domain integers.
Definition int.cpp:40
@ CTL_NONE
No consistency-test.
Definition int.hh:140
General test support.
Definition afc.cpp:39