Generated on for Gecode by doxygen 1.15.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 * Guido Tack <tack@gecode.dev>
5 *
6 * Copyright:
7 * Guido Tack, 2005
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.dev
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/set.hh"
35#include "test/int.hh"
36#include <gecode/minimodel.hh>
37
38using namespace Gecode;
39
40namespace Test { namespace Set {
41
43 namespace Channel {
44
50
51 static IntSet d1(0,2);
52 static IntSet d_12(-1,2);
53
54 static IntSet d2(-1,3);
55 static IntSet d3(0,3);
56
57 static IntSet d4(0,4);
58
59 static IntSet ds_33(-3,3);
60
62 class ChannelSorted : public SetTest {
63 public:
65 ChannelSorted(const char* t)
66 : SetTest(t,1,ds_33,false,3) {}
67
68 virtual bool solution(const SetAssignment& x) const {
69 if (x.ints()[0]>=x.ints()[1] ||
70 x.ints()[1]>=x.ints()[2])
71 return false;
72 CountableSetValues xr(x.lub, x[0]);
73 if (!xr())
74 return false;
75 if (xr.val() != x.ints()[0])
76 return false;
77 ++xr;
78 if (!xr())
79 return false;
80 if (xr.val() != x.ints()[1])
81 return false;
82 ++xr;
83 if (!xr())
84 return false;
85 if (xr.val() != x.ints()[2])
86 return false;
87 ++xr;
88 if (xr())
89 return false;
90 return true;
91 }
92
93 virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
94 Gecode::channelSorted(home, y, x[0]);
95 }
96 };
97 ChannelSorted _channelSorted("Channel::Sorted");
98
100 class ChannelInt : public SetTest {
101 private:
102 int ssize, isize;
103 public:
105 ChannelInt(const char* t, const IntSet& d, int _ssize, int _isize)
106 : SetTest(t,_ssize,d,false,_isize), ssize(_ssize), isize(_isize) {}
107
108 virtual bool solution(const SetAssignment& x) const {
109 for (int i=0; i<isize; i++) {
110 if (x.ints()[i] < 0 || x.ints()[i] >= ssize)
111 return false;
112 Iter::Ranges::Singleton single(i,i);
113 CountableSetRanges csr(x.lub, x[x.ints()[i]]);
114 if (!Iter::Ranges::subset(single, csr))
115 return false;
116 }
117 for (int i=0; i<ssize; i++) {
118 for (CountableSetValues csv(x.lub, x[i]); csv(); ++csv) {
119 if (csv.val() < 0 || csv.val() >= isize) return false;
120 if (x.ints()[csv.val()] != i) return false;
121 }
122 }
123 return true;
124 }
125
126 virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
127 Gecode::channel(home, y, x);
128 }
129 };
130
131 ChannelInt _channelint1("Channel::Int::1", d2, 2, 3);
132 ChannelInt _channelint2("Channel::Int::2", d3, 3, 3);
133
135 class ChannelBool : public SetTest {
136 private:
137 int isize;
138 public:
140 ChannelBool(const char* t, const IntSet& d, int _isize)
141 : SetTest(t,1,d,false,_isize), isize(_isize) {}
142
143 virtual bool solution(const SetAssignment& x) const {
144 for (int i=0; i<isize; i++) {
145 if (x.ints()[i] < 0 || x.ints()[i] > 1)
146 return false;
147 }
148 int cur = 0;
149 for (CountableSetValues csv(x.lub, x[0]); csv(); ++csv) {
150 if (csv.val() < 0 || csv.val() >= isize) return false;
151 if (x.ints()[csv.val()] != 1) return false;
152 for (; cur<csv.val(); cur++)
153 if (x.ints()[cur] != 0) return false;
154 cur = csv.val() + 1;
155 }
156 for (; cur<isize; cur++)
157 if (x.ints()[cur] != 0) return false;
158 return true;
159 }
160
161 virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
162 BoolVarArgs b(y.size());
163 for (int i=y.size(); i--;)
164 b[i] = channel(home, y[i]);
165 Gecode::channel(home, b, x[0]);
166 }
167 };
168
169 ChannelBool _channelbool1("Channel::Bool::1", d2, 3);
170 ChannelBool _channelbool2("Channel::Bool::2", d3, 3);
171 ChannelBool _channelbool3("Channel::Bool::3", d4, 5);
172
174 class ChannelSet : public SetTest {
175 private:
176 int _x0size, _x1size;
177 public:
179 ChannelSet(const char* t, const IntSet& d, int x0size, int x1size)
180 : SetTest(t,x0size+x1size,d,false), _x0size(x0size), _x1size(x1size) {}
181
182 virtual bool solution(const SetAssignment& x) const {
183 for (int i=0; i<_x0size; i++) {
184 CountableSetRanges x0ir(x.lub, x[i]);
185 IntSet x0is(x0ir);
186 if (x0is.min() < 0 || x0is.max() >= _x1size)
187 return false;
188 for (int j=0; j<_x1size; j++) {
189 CountableSetRanges x1ir(x.lub, x[_x0size+j]);
190 IntSet x1is(x1ir);
191 if (x1is.min() < 0 || x1is.max() >= _x0size)
192 return false;
193 bool jInI = x0is.in(j);
194 bool iInJ = x1is.in(i);
195 if (jInI != iInJ)
196 return false;
197 }
198 }
199 return true;
200 }
201
202 virtual void post(Space& home, SetVarArray& x, IntVarArray&) {
203 SetVarArgs x0(x.slice(0,1,_x0size));
204 SetVarArgs x1(x.slice(_x0size));
205 Gecode::channel(home, x0,x1);
206 }
207 };
208
209 ChannelSet _channelSet12("Channel::Set::1::2", d1, 2,2);
210 ChannelSet _channelSet13("Channel::Set::1::3", d1, 2,3);
211 ChannelSet _channelSet22("Channel::Set::2::2", d3, 2,2);
212 ChannelSet _channelSet23("Channel::Set::2::3", d3, 2,3);
213 ChannelSet _channelSet32("Channel::Set::3::2", d_12, 2,2);
214 ChannelSet _channelSet33("Channel::Set::3::3", d_12, 2,3);
215
216}}}
217
218// STATISTICS: test-set
Passing Boolean variables.
Definition int.hh:738
Integer sets.
Definition int.hh:178
int min(int i) const
Return minimum of range at position i.
bool in(int n) const
Return whether n is included in the set.
int max(int i) const
Return maximum of range at position i.
Integer variable array.
Definition int.hh:791
Range iterator for singleton range.
Passing set variables.
Definition set.hh:491
Set variable array
Definition set.hh:575
Computation spaces.
Definition core.hpp:1775
ArrayTraits< VarArgArray< Var > >::ArgsType slice(int start, int inc=1, int n=-1)
Definition array.hpp:956
int size(void) const
Return size of array (number of elements).
Definition array.hpp:936
Test for Boolean channel constraint
Definition channel.cpp:135
virtual void post(Space &home, SetVarArray &x, IntVarArray &y)
Post constraint on x.
Definition channel.cpp:161
ChannelBool(const char *t, const IntSet &d, int _isize)
Create and register test.
Definition channel.cpp:140
virtual bool solution(const SetAssignment &x) const
Test whether x is solution
Definition channel.cpp:143
Test for integer channel constraint
Definition channel.cpp:100
virtual void post(Space &home, SetVarArray &x, IntVarArray &y)
Post constraint on x.
Definition channel.cpp:126
ChannelInt(const char *t, const IntSet &d, int _ssize, int _isize)
Create and register test.
Definition channel.cpp:105
virtual bool solution(const SetAssignment &x) const
Test whether x is solution
Definition channel.cpp:108
Test for set channel constraint
Definition channel.cpp:174
ChannelSet(const char *t, const IntSet &d, int x0size, int x1size)
Create and register test.
Definition channel.cpp:179
virtual bool solution(const SetAssignment &x) const
Test whether x is solution
Definition channel.cpp:182
virtual void post(Space &home, SetVarArray &x, IntVarArray &)
Post constraint on x.
Definition channel.cpp:202
Test for sorted channeling constraint
Definition channel.cpp:62
ChannelSorted(const char *t)
Create and register test.
Definition channel.cpp:65
virtual bool solution(const SetAssignment &x) const
Test whether x is solution
Definition channel.cpp:68
virtual void post(Space &home, SetVarArray &x, IntVarArray &y)
Post constraint on x.
Definition channel.cpp:93
Range iterator producing subsets of an IntSet.
Definition set.hh:99
Value iterator producing subsets of an IntSet.
Definition set.hh:60
int val(void) const
Return current value.
Definition set.hh:94
Generate all set assignments.
Definition set.hh:142
const Test::Int::Assignment & ints(void) const
Return assignment for integer variables.
Definition set.hh:171
Gecode::IntSet lub
The common superset for all domains.
Definition set.hh:154
SetTest(const std::string &s, int a, const Gecode::IntSet &d, bool r=false, int w=0)
Constructor.
Definition set.hh:304
bool subset(I &i, J &j)
Check whether range iterator i is subset of range iterator j.
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 channelSorted(Home home, const IntVarArgs &x, SetVar y)
Post propagator for and .
Definition channel.cpp:45
Tests for set channeling constraints
Definition channel.cpp:43
ChannelSet _channelSet33("Channel::Set::3::3", d_12, 2, 3)
ChannelBool _channelbool3("Channel::Bool::3", d4, 5)
ChannelSet _channelSet12("Channel::Set::1::2", d1, 2, 2)
ChannelSet _channelSet22("Channel::Set::2::2", d3, 2, 2)
ChannelBool _channelbool1("Channel::Bool::1", d2, 3)
ChannelSet _channelSet32("Channel::Set::3::2", d_12, 2, 2)
ChannelInt _channelint1("Channel::Int::1", d2, 2, 3)
ChannelInt _channelint2("Channel::Int::2", d3, 3, 3)
ChannelSet _channelSet13("Channel::Set::1::3", d1, 2, 3)
ChannelSet _channelSet23("Channel::Set::2::3", d3, 2, 3)
ChannelBool _channelbool2("Channel::Bool::2", d3, 3)
ChannelSorted _channelSorted("Channel::Sorted")
Testing finite sets.
Definition set.cpp:42
General test support.
Definition afc.cpp:39