Generated on Thu Jan 20 2022 00:00:00 for Gecode by doxygen 1.9.1
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.org>
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.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/set.hh"
35 #include "test/int.hh"
36 #include <gecode/minimodel.hh>
37 
38 using namespace Gecode;
39 
40 namespace 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) {}
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  }
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) {}
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  int size = 0;
119  for (CountableSetValues csv(x.lub, x[i]); csv(); ++csv) {
120  if (csv.val() < 0 || csv.val() >= isize) return false;
121  if (x.ints()[csv.val()] != i) return false;
122  size++;
123  }
124  }
125  return true;
126  }
128  virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
129  Gecode::channel(home, y, x);
130  }
131  };
132 
133  ChannelInt _channelint1("Channel::Int::1", d2, 2, 3);
134  ChannelInt _channelint2("Channel::Int::2", d3, 3, 3);
135 
137  class ChannelBool : public SetTest {
138  private:
139  int isize;
140  public:
142  ChannelBool(const char* t, const IntSet& d, int _isize)
143  : SetTest(t,1,d,false,_isize), isize(_isize) {}
145  virtual bool solution(const SetAssignment& x) const {
146  for (int i=0; i<isize; i++) {
147  if (x.ints()[i] < 0 || x.ints()[i] > 1)
148  return false;
149  }
150  int cur = 0;
151  for (CountableSetValues csv(x.lub, x[0]); csv(); ++csv) {
152  if (csv.val() < 0 || csv.val() >= isize) return false;
153  if (x.ints()[csv.val()] != 1) return false;
154  for (; cur<csv.val(); cur++)
155  if (x.ints()[cur] != 0) return false;
156  cur = csv.val() + 1;
157  }
158  for (; cur<isize; cur++)
159  if (x.ints()[cur] != 0) return false;
160  return true;
161  }
163  virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
164  BoolVarArgs b(y.size());
165  for (int i=y.size(); i--;)
166  b[i] = channel(home, y[i]);
167  Gecode::channel(home, b, x[0]);
168  }
169  };
170 
171  ChannelBool _channelbool1("Channel::Bool::1", d2, 3);
172  ChannelBool _channelbool2("Channel::Bool::2", d3, 3);
173  ChannelBool _channelbool3("Channel::Bool::3", d4, 5);
174 
176  class ChannelSet : public SetTest {
177  private:
178  int _x0size, _x1size;
179  public:
181  ChannelSet(const char* t, const IntSet& d, int x0size, int x1size)
182  : SetTest(t,x0size+x1size,d,false), _x0size(x0size), _x1size(x1size) {}
184  virtual bool solution(const SetAssignment& x) const {
185  for (int i=0; i<_x0size; i++) {
186  CountableSetRanges x0ir(x.lub, x[i]);
187  IntSet x0is(x0ir);
188  if (x0is.min() < 0 || x0is.max() >= _x1size)
189  return false;
190  for (int j=0; j<_x1size; j++) {
191  CountableSetRanges x1ir(x.lub, x[_x0size+j]);
192  IntSet x1is(x1ir);
193  if (x1is.min() < 0 || x1is.max() >= _x0size)
194  return false;
195  bool jInI = x0is.in(j);
196  bool iInJ = x1is.in(i);
197  if (jInI != iInJ)
198  return false;
199  }
200  }
201  return true;
202  }
204  virtual void post(Space& home, SetVarArray& x, IntVarArray&) {
205  SetVarArgs x0(x.slice(0,1,_x0size));
206  SetVarArgs x1(x.slice(_x0size));
207  Gecode::channel(home, x0,x1);
208  }
209  };
210 
211  ChannelSet _channelSet12("Channel::Set::1::2", d1, 2,2);
212  ChannelSet _channelSet13("Channel::Set::1::3", d1, 2,3);
213  ChannelSet _channelSet22("Channel::Set::2::2", d3, 2,2);
214  ChannelSet _channelSet23("Channel::Set::2::3", d3, 2,3);
215  ChannelSet _channelSet32("Channel::Set::3::2", d_12, 2,2);
216  ChannelSet _channelSet33("Channel::Set::3::3", d_12, 2,3);
217 
218 }}}
219 
220 // STATISTICS: test-set
struct Gecode::@602::NNF::@65::@66 b
For binary nodes (and, or, eqv)
NodeType t
Type of node.
Definition: bool-expr.cpp:230
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:249
Passing Boolean variables.
Definition: int.hh:712
Integer sets.
Definition: int.hh:174
int min(int i) const
Return minimum of range at position i.
Definition: int-set-1.hpp:152
bool in(int n) const
Return whether n is included in the set.
Definition: int-set-1.hpp:177
int max(int i) const
Return maximum of range at position i.
Definition: int-set-1.hpp:158
Integer variable array.
Definition: int.hh:763
Range iterator for singleton range.
Passing set variables.
Definition: set.hh:488
Set variable array
Definition: set.hh:570
Computation spaces.
Definition: core.hpp:1742
Test for Boolean channel constraint
Definition: channel.cpp:137
virtual void post(Space &home, SetVarArray &x, IntVarArray &y)
Post constraint on x.
Definition: channel.cpp:163
ChannelBool(const char *t, const IntSet &d, int _isize)
Create and register test.
Definition: channel.cpp:142
virtual bool solution(const SetAssignment &x) const
Test whether x is solution
Definition: channel.cpp:145
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:128
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:176
ChannelSet(const char *t, const IntSet &d, int x0size, int x1size)
Create and register test.
Definition: channel.cpp:181
virtual bool solution(const SetAssignment &x) const
Test whether x is solution
Definition: channel.cpp:184
virtual void post(Space &home, SetVarArray &x, IntVarArray &)
Post constraint on x.
Definition: channel.cpp:204
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
Base class for tests with set constraints
Definition: set.hh:273
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)
Definition: channel.cpp:45
Post propagator for SetVar SetOpType SetVar y
Definition: set.hh:767
bool subset(I &i, J &j)
Check whether range iterator i is subset of range iterator j.
unsigned int size(I &i)
Size of all ranges of range iterator i.
Gecode::IntArgs i({1, 2, 3, 4})
ChannelShared csv(Gecode::IPL_VAL)
Gecode::IntSet d(v, 7)
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")
General test support.
Definition: afc.cpp:39