Generated on Sat Feb 7 2015 02:01:20 for Gecode by doxygen 1.8.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  * Last modified:
10  * $Date: 2011-11-03 11:52:07 +0100 (Thu, 03 Nov 2011) $ by $Author: tack $
11  * $Revision: 12452 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 #include "test/set.hh"
39 #include "test/int.hh"
40 #include <gecode/minimodel.hh>
41 
42 using namespace Gecode;
43 
44 namespace Test { namespace Set {
45 
47  namespace Channel {
48 
54 
55  static IntSet d1(0,2);
56  static IntSet d_12(-1,2);
57 
58  static IntSet d2(-1,3);
59  static IntSet d3(0,3);
60 
61  static IntSet d4(0,4);
62 
63  static IntSet ds_33(-3,3);
64 
66  class ChannelSorted : public SetTest {
67  public:
69  ChannelSorted(const char* t)
70  : SetTest(t,1,ds_33,false,3) {}
72  virtual bool solution(const SetAssignment& x) const {
73  if (x.ints()[0]>=x.ints()[1] ||
74  x.ints()[1]>=x.ints()[2])
75  return false;
76  CountableSetValues xr(x.lub, x[0]);
77  if (!xr())
78  return false;
79  if (xr.val() != x.ints()[0])
80  return false;
81  ++xr;
82  if (!xr())
83  return false;
84  if (xr.val() != x.ints()[1])
85  return false;
86  ++xr;
87  if (!xr())
88  return false;
89  if (xr.val() != x.ints()[2])
90  return false;
91  ++xr;
92  if (xr())
93  return false;
94  return true;
95  }
97  virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
98  Gecode::channelSorted(home, y, x[0]);
99  }
100  };
101  ChannelSorted _channelSorted("Channel::Sorted");
102 
104  class ChannelInt : public SetTest {
105  private:
106  int ssize, isize;
107  public:
109  ChannelInt(const char* t, const IntSet& d, int _ssize, int _isize)
110  : SetTest(t,_ssize,d,false,_isize), ssize(_ssize), isize(_isize) {}
112  virtual bool solution(const SetAssignment& x) const {
113  for (int i=0; i<isize; i++) {
114  if (x.ints()[i] < 0 || x.ints()[i] >= ssize)
115  return false;
116  Iter::Ranges::Singleton single(i,i);
117  CountableSetRanges csr(x.lub, x[x.ints()[i]]);
118  if (!Iter::Ranges::subset(single, csr))
119  return false;
120  }
121  for (int i=0; i<ssize; i++) {
122  int size = 0;
123  for (CountableSetValues csv(x.lub, x[i]); csv(); ++csv) {
124  if (csv.val() < 0 || csv.val() >= isize) return false;
125  if (x.ints()[csv.val()] != i) return false;
126  size++;
127  }
128  }
129  return true;
130  }
132  virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
133  Gecode::channel(home, y, x);
134  }
135  };
136 
137  ChannelInt _channelint1("Channel::Int::1", d2, 2, 3);
138  ChannelInt _channelint2("Channel::Int::2", d3, 3, 3);
139 
141  class ChannelBool : public SetTest {
142  private:
143  int isize;
144  public:
146  ChannelBool(const char* t, const IntSet& d, int _isize)
147  : SetTest(t,1,d,false,_isize), isize(_isize) {}
149  virtual bool solution(const SetAssignment& x) const {
150  for (int i=0; i<isize; i++) {
151  if (x.ints()[i] < 0 || x.ints()[i] > 1)
152  return false;
153  }
154  int cur = 0;
155  for (CountableSetValues csv(x.lub, x[0]); csv(); ++csv) {
156  if (csv.val() < 0 || csv.val() >= isize) return false;
157  if (x.ints()[csv.val()] != 1) return false;
158  for (; cur<csv.val(); cur++)
159  if (x.ints()[cur] != 0) return false;
160  cur = csv.val() + 1;
161  }
162  for (; cur<isize; cur++)
163  if (x.ints()[cur] != 0) return false;
164  return true;
165  }
167  virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
168  BoolVarArgs b(y.size());
169  for (int i=y.size(); i--;)
170  b[i] = channel(home, y[i]);
171  Gecode::channel(home, b, x[0]);
172  }
173  };
174 
175  ChannelBool _channelbool1("Channel::Bool::1", d2, 3);
176  ChannelBool _channelbool2("Channel::Bool::2", d3, 3);
177  ChannelBool _channelbool3("Channel::Bool::3", d4, 5);
178 
180  class ChannelSet : public SetTest {
181  private:
182  int _x0size, _x1size;
183  public:
185  ChannelSet(const char* t, const IntSet& d, int x0size, int x1size)
186  : SetTest(t,x0size+x1size,d,false), _x0size(x0size), _x1size(x1size) {}
188  virtual bool solution(const SetAssignment& x) const {
189  for (int i=0; i<_x0size; i++) {
190  CountableSetRanges x0ir(x.lub, x[i]);
191  IntSet x0is(x0ir);
192  if (x0is.min() < 0 || x0is.max() >= _x1size)
193  return false;
194  for (int j=0; j<_x1size; j++) {
195  CountableSetRanges x1ir(x.lub, x[_x0size+j]);
196  IntSet x1is(x1ir);
197  if (x1is.min() < 0 || x1is.max() >= _x0size)
198  return false;
199  bool jInI = x0is.in(j);
200  bool iInJ = x1is.in(i);
201  if (jInI != iInJ)
202  return false;
203  }
204  }
205  return true;
206  }
208  virtual void post(Space& home, SetVarArray& x, IntVarArray&) {
209  SetVarArgs x0(x.slice(0,1,_x0size));
210  SetVarArgs x1(x.slice(_x0size));
211  Gecode::channel(home, x0,x1);
212  }
213  };
214 
215  ChannelSet _channelSet12("Channel::Set::1::2", d1, 2,2);
216  ChannelSet _channelSet13("Channel::Set::1::3", d1, 2,3);
217  ChannelSet _channelSet22("Channel::Set::2::2", d3, 2,2);
218  ChannelSet _channelSet23("Channel::Set::2::3", d3, 2,3);
219  ChannelSet _channelSet32("Channel::Set::3::2", d_12, 2,2);
220  ChannelSet _channelSet33("Channel::Set::3::3", d_12, 2,3);
221 
222 }}}
223 
224 // STATISTICS: test-set
Test for Boolean channel constraint
Definition: channel.cpp:141
virtual void post(Space &home, SetVarArray &x, IntVarArray &y)
Post constraint on x.
Definition: channel.cpp:167
void channelSorted(Home home, const IntVarArgs &x, SetVar y)
Post propagator for and .
Definition: channel.cpp:49
NodeType t
Type of node.
Definition: bool-expr.cpp:234
Range iterator for singleton range.
virtual void post(Space &home, SetVarArray &x, IntVarArray &)
Post constraint on x.
Definition: channel.cpp:208
ChannelSet _channelSet33("Channel::Set::3::3", d_12, 2, 3)
void channel(Home home, FloatVar x0, IntVar x1)
Post propagator for channeling a float and an integer variable .
Definition: arithmetic.cpp:218
bool in(int n) const
Return whether n is included in the set.
Definition: int-set-1.hpp:140
ChannelSorted _channelSorted("Channel::Sorted")
ChannelInt _channelint2("Channel::Int::2", d3, 3, 3)
ChannelInt _channelint1("Channel::Int::1", d2, 2, 3)
ChannelSorted(const char *t)
Create and register test.
Definition: channel.cpp:69
virtual void post(Space &home, SetVarArray &x, IntVarArray &y)
Post constraint on x.
Definition: channel.cpp:97
Integer variable array.
Definition: int.hh:741
Computation spaces.
Definition: core.hpp:1362
ChannelSet _channelSet32("Channel::Set::3::2", d_12, 2, 2)
ChannelSet _channelSet22("Channel::Set::2::2", d3, 2, 2)
ChannelShared csv(Gecode::ICL_VAL)
ChannelSet _channelSet23("Channel::Set::2::3", d3, 2, 3)
Gecode::IntSet d(v, 7)
virtual bool solution(const SetAssignment &x) const
Test whether x is solution
Definition: channel.cpp:149
Gecode::IntArgs i(4, 1, 2, 3, 4)
ChannelSet _channelSet13("Channel::Set::1::3", d1, 2, 3)
virtual void post(Space &home, SetVarArray &x, IntVarArray &y)
Post constraint on x.
Definition: channel.cpp:132
const Test::Int::Assignment & ints(void) const
Return assignment for integer variables.
Definition: set.hh:187
unsigned int size(I &i)
Size of all ranges of range iterator i.
Gecode::IntSet lub
The common superset for all domains.
Definition: set.hh:170
ChannelBool _channelbool2("Channel::Bool::2", d3, 3)
Integer sets.
Definition: int.hh:171
int val(void) const
Return current value.
Definition: set.hh:110
ArrayTraits< VarArgArray< Var > >::ArgsType slice(int start, int inc=1, int n=-1)
Definition: array.hpp:1005
Value iterator producing subsets of an IntSet.
Definition: set.hh:76
Passing Boolean variables.
Definition: int.hh:690
General test support.
Definition: afc.cpp:43
Passing set variables.
Definition: set.hh:490
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
Base class for tests with set constraints
Definition: set.hh:271
Generate all set assignments.
Definition: set.hh:158
ChannelInt(const char *t, const IntSet &d, int _ssize, int _isize)
Create and register test.
Definition: channel.cpp:109
virtual bool solution(const SetAssignment &x) const
Test whether x is solution
Definition: channel.cpp:188
Range iterator producing subsets of an IntSet.
Definition: set.hh:114
Test for sorted channeling constraint
Definition: channel.cpp:66
ChannelSet _channelSet12("Channel::Set::1::2", d1, 2, 2)
bool subset(I &i, J &j)
Check whether range iterator i is subset of range iterator j.
Set variable array
Definition: set.hh:571
struct Gecode::@518::NNF::@57::@58 b
For binary nodes (and, or, eqv)
Gecode toplevel namespace
ChannelBool(const char *t, const IntSet &d, int _isize)
Create and register test.
Definition: channel.cpp:146
ChannelBool _channelbool1("Channel::Bool::1", d2, 3)
Test for integer channel constraint
Definition: channel.cpp:104
ChannelBool _channelbool3("Channel::Bool::3", d4, 5)
Test for set channel constraint
Definition: channel.cpp:180
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:985
ChannelSet(const char *t, const IntSet &d, int x0size, int x1size)
Create and register test.
Definition: channel.cpp:185
virtual bool solution(const SetAssignment &x) const
Test whether x is solution
Definition: channel.cpp:72
int max(int i) const
Return maximum of range at position i.
Definition: int-set-1.hpp:121
virtual bool solution(const SetAssignment &x) const
Test whether x is solution
Definition: channel.cpp:112
int min(int i) const
Return minimum of range at position i.
Definition: int-set-1.hpp:115