00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include "test/set.hh"
00035 #include "test/int.hh"
00036 #include <gecode/minimodel.hh>
00037
00038 using namespace Gecode;
00039
00040 namespace Test { namespace Set {
00041
00043 namespace Channel {
00044
00050
00051 static IntSet d1(0,2);
00052 static IntSet d_12(-1,2);
00053
00054 static IntSet d2(-1,3);
00055 static IntSet d3(0,3);
00056
00057 static IntSet d4(0,4);
00058
00059 static IntSet ds_33(-3,3);
00060
00062 class ChannelSorted : public SetTest {
00063 public:
00065 ChannelSorted(const char* t)
00066 : SetTest(t,1,ds_33,false,3) {}
00068 virtual bool solution(const SetAssignment& x) const {
00069 if (x.ints()[0]>=x.ints()[1] ||
00070 x.ints()[1]>=x.ints()[2])
00071 return false;
00072 CountableSetValues xr(x.lub, x[0]);
00073 if (!xr())
00074 return false;
00075 if (xr.val() != x.ints()[0])
00076 return false;
00077 ++xr;
00078 if (!xr())
00079 return false;
00080 if (xr.val() != x.ints()[1])
00081 return false;
00082 ++xr;
00083 if (!xr())
00084 return false;
00085 if (xr.val() != x.ints()[2])
00086 return false;
00087 ++xr;
00088 if (xr())
00089 return false;
00090 return true;
00091 }
00093 virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
00094 Gecode::channelSorted(home, y, x[0]);
00095 }
00096 };
00097 ChannelSorted _channelSorted("Channel::Sorted");
00098
00100 class ChannelInt : public SetTest {
00101 private:
00102 int ssize, isize;
00103 public:
00105 ChannelInt(const char* t, const IntSet& d, int _ssize, int _isize)
00106 : SetTest(t,_ssize,d,false,_isize), ssize(_ssize), isize(_isize) {}
00108 virtual bool solution(const SetAssignment& x) const {
00109 for (int i=0; i<isize; i++) {
00110 if (x.ints()[i] < 0 || x.ints()[i] >= ssize)
00111 return false;
00112 Iter::Ranges::Singleton single(i,i);
00113 CountableSetRanges csr(x.lub, x[x.ints()[i]]);
00114 if (!Iter::Ranges::subset(single, csr))
00115 return false;
00116 }
00117 for (int i=0; i<ssize; i++) {
00118 int size = 0;
00119 for (CountableSetValues csv(x.lub, x[i]); csv(); ++csv) {
00120 if (csv.val() < 0 || csv.val() >= isize) return false;
00121 if (x.ints()[csv.val()] != i) return false;
00122 size++;
00123 }
00124 }
00125 return true;
00126 }
00128 virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
00129 Gecode::channel(home, y, x);
00130 }
00131 };
00132
00133 ChannelInt _channelint1("Channel::Int::1", d2, 2, 3);
00134 ChannelInt _channelint2("Channel::Int::2", d3, 3, 3);
00135
00137 class ChannelBool : public SetTest {
00138 private:
00139 int isize;
00140 public:
00142 ChannelBool(const char* t, const IntSet& d, int _isize)
00143 : SetTest(t,1,d,false,_isize), isize(_isize) {}
00145 virtual bool solution(const SetAssignment& x) const {
00146 for (int i=0; i<isize; i++) {
00147 if (x.ints()[i] < 0 || x.ints()[i] > 1)
00148 return false;
00149 }
00150 int cur = 0;
00151 for (CountableSetValues csv(x.lub, x[0]); csv(); ++csv) {
00152 if (csv.val() < 0 || csv.val() >= isize) return false;
00153 if (x.ints()[csv.val()] != 1) return false;
00154 for (; cur<csv.val(); cur++)
00155 if (x.ints()[cur] != 0) return false;
00156 cur = csv.val() + 1;
00157 }
00158 for (; cur<isize; cur++)
00159 if (x.ints()[cur] != 0) return false;
00160 return true;
00161 }
00163 virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
00164 BoolVarArgs b(y.size());
00165 for (int i=y.size(); i--;)
00166 b[i] = channel(home, y[i]);
00167 Gecode::channel(home, b, x[0]);
00168 }
00169 };
00170
00171 ChannelBool _channelbool1("Channel::Bool::1", d2, 3);
00172 ChannelBool _channelbool2("Channel::Bool::2", d3, 3);
00173 ChannelBool _channelbool3("Channel::Bool::3", d4, 5);
00174
00176 class ChannelSet : public SetTest {
00177 private:
00178 int _x0size, _x1size;
00179 public:
00181 ChannelSet(const char* t, const IntSet& d, int x0size, int x1size)
00182 : SetTest(t,x0size+x1size,d,false), _x0size(x0size), _x1size(x1size) {}
00184 virtual bool solution(const SetAssignment& x) const {
00185 for (int i=0; i<_x0size; i++) {
00186 CountableSetRanges x0ir(x.lub, x[i]);
00187 IntSet x0is(x0ir);
00188 if (x0is.min() < 0 || x0is.max() >= _x1size)
00189 return false;
00190 for (int j=0; j<_x1size; j++) {
00191 CountableSetRanges x1ir(x.lub, x[_x0size+j]);
00192 IntSet x1is(x1ir);
00193 if (x1is.min() < 0 || x1is.max() >= _x0size)
00194 return false;
00195 bool jInI = x0is.in(j);
00196 bool iInJ = x1is.in(i);
00197 if (jInI != iInJ)
00198 return false;
00199 }
00200 }
00201 return true;
00202 }
00204 virtual void post(Space& home, SetVarArray& x, IntVarArray&) {
00205 SetVarArgs x0(x.slice(0,1,_x0size));
00206 SetVarArgs x1(x.slice(_x0size));
00207 Gecode::channel(home, x0,x1);
00208 }
00209 };
00210
00211 ChannelSet _channelSet12("Channel::Set::1::2", d1, 2,2);
00212 ChannelSet _channelSet13("Channel::Set::1::3", d1, 2,3);
00213 ChannelSet _channelSet22("Channel::Set::2::2", d3, 2,2);
00214 ChannelSet _channelSet23("Channel::Set::2::3", d3, 2,3);
00215 ChannelSet _channelSet32("Channel::Set::3::2", d_12, 2,2);
00216 ChannelSet _channelSet33("Channel::Set::3::3", d_12, 2,3);
00217
00218 }}}
00219
00220