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
00035
00036
00037
00038 #include "test/set.hh"
00039
00040 using namespace Gecode;
00041
00042 namespace Test { namespace Set {
00043
00045 namespace Element {
00046
00052
00053 static IntSet ds_12(-1,2);
00054 static IntSet ds_13(-1,3);
00055
00057 class ElementUnion : public SetTest {
00058 public:
00060 ElementUnion(const char* t)
00061 : SetTest(t,5,ds_12,false) {}
00063 virtual bool solution(const SetAssignment& x) const {
00064 int selected = 0;
00065 for (CountableSetValues sel2(x.lub, x[3]); sel2();
00066 ++sel2, selected++);
00067 CountableSetValues x4v(x.lub, x[4]);
00068 if (selected==0)
00069 return !x4v();
00070 GECODE_AUTOARRAY(CountableSetRanges, sel, selected);
00071 CountableSetValues selector(x.lub, x[3]);
00072 for (int i=selected; i--;++selector) {
00073 if (selector.val()>=3 || selector.val()<0)
00074 return false;
00075 sel[i].init(x.lub, x[selector.val()]);
00076 }
00077 Iter::Ranges::NaryUnion<CountableSetRanges> u(sel, selected);
00078
00079 CountableSetRanges z(x.lub, x[4]);
00080 return Iter::Ranges::equal(u, z);
00081 }
00083 virtual void post(Space* home, SetVarArray& x, IntVarArray&) {
00084 SetVarArgs xs(x.size()-2);
00085 for (int i=x.size()-2; i--;)
00086 xs[i]=x[i];
00087 Gecode::elementsUnion(home, xs, x[x.size()-2], x[x.size()-1]);
00088 }
00089 };
00090 ElementUnion _elementunion("Element::Union");
00091
00093 class ElementUnionConst : public SetTest {
00094 private:
00095 const IntSet i0;
00096 const IntSet i1;
00097 const IntSet i2;
00098 public:
00100 ElementUnionConst(const char* t)
00101 : SetTest(t,2,ds_13,false), i0(-3,-3), i1(-1,1), i2(0,2) {}
00103 virtual bool solution(const SetAssignment& x) const {
00104 int selected = 0;
00105 for (CountableSetValues sel2(x.lub, x[0]); sel2();
00106 ++sel2, selected++);
00107 CountableSetValues x4v(x.lub, x[1]);
00108 if (selected==0)
00109 return !x4v();
00110 IntSet iss[] = {i0, i1, i2};
00111 GECODE_AUTOARRAY(IntSetRanges, sel, selected);
00112 CountableSetValues selector(x.lub, x[0]);
00113 for (int i=selected; i--;++selector) {
00114 if (selector.val()>=3 || selector.val()<0)
00115 return false;
00116 sel[i].init(iss[selector.val()]);
00117 }
00118 Iter::Ranges::NaryUnion<IntSetRanges> u(sel, selected);
00119
00120 CountableSetRanges z(x.lub, x[1]);
00121 return Iter::Ranges::equal(u, z);
00122 }
00124 virtual void post(Space* home, SetVarArray& x, IntVarArray&) {
00125 IntSetArgs xs(3);
00126 xs[0] = i0; xs[1] = i1; xs[2] = i2;
00127 Gecode::elementsUnion(home, xs, x[0], x[1]);
00128 }
00129 };
00130 ElementUnionConst _elementunionconst("Element::UnionConst");
00131
00133 class ElementInter : public SetTest {
00134 public:
00136 ElementInter(const char* t)
00137 : SetTest(t,5,ds_12,false) {}
00139 virtual bool solution(const SetAssignment& x) const {
00140 int selected = 0;
00141 for (CountableSetValues sel2(x.lub, x[3]); sel2();
00142 ++sel2, selected++);
00143 CountableSetRanges x4r(x.lub, x[4]);
00144 if (selected==0)
00145 return Iter::Ranges::size(x4r)==Gecode::Set::Limits::card;
00146 GECODE_AUTOARRAY(CountableSetRanges, sel, selected);
00147 CountableSetValues selector(x.lub, x[3]);
00148 for (int i=selected; i--;++selector) {
00149 if (selector.val()>=3 || selector.val()<0)
00150 return false;
00151 sel[i].init(x.lub, x[selector.val()]);
00152 }
00153 Iter::Ranges::NaryInter<CountableSetRanges> u(sel, selected);
00154
00155 CountableSetRanges z(x.lub, x[4]);
00156 return Iter::Ranges::equal(u, z);
00157 }
00159 virtual void post(Space* home, SetVarArray& x, IntVarArray&) {
00160 SetVarArgs xs(x.size()-2);
00161 for (int i=x.size()-2; i--;)
00162 xs[i]=x[i];
00163 Gecode::elementsInter(home, xs, x[x.size()-2], x[x.size()-1]);
00164 }
00165 };
00166 ElementInter _elementinter("Element::Inter");
00167
00169 class ElementInterIn : public SetTest {
00170 public:
00172 ElementInterIn(const char* t)
00173 : SetTest(t,5,ds_12,false) {}
00175 virtual bool solution(const SetAssignment& x) const {
00176 int selected = 0;
00177 for (CountableSetValues sel2(x.lub, x[3]); sel2();
00178 ++sel2, selected++);
00179 CountableSetRanges x4r(x.lub, x[4]);
00180 if (selected==0)
00181 return Iter::Ranges::size(x4r)==4;
00182 GECODE_AUTOARRAY(CountableSetRanges, sel, selected);
00183 CountableSetValues selector(x.lub, x[3]);
00184 for (int i=selected; i--;++selector) {
00185 if (selector.val()>=3 || selector.val()<0)
00186 return false;
00187 sel[i].init(x.lub, x[selector.val()]);
00188 }
00189 Iter::Ranges::NaryInter<CountableSetRanges> u(sel, selected);
00190
00191 CountableSetRanges z(x.lub, x[4]);
00192 return Iter::Ranges::equal(u, z);
00193 }
00195 virtual void post(Space* home, SetVarArray& x, IntVarArray&) {
00196 SetVarArgs xs(x.size()-2);
00197 for (int i=x.size()-2; i--;)
00198 xs[i]=x[i];
00199 Gecode::elementsInter(home, xs, x[x.size()-2], x[x.size()-1], ds_12);
00200 }
00201 };
00202 ElementInterIn _elementinterin("Element::InterIn");
00203
00205 class ElementDisjoint : public SetTest {
00206 public:
00208 ElementDisjoint(const char* t)
00209 : SetTest(t,4,ds_12,false) {}
00211 virtual bool solution(const SetAssignment& x) const {
00212 int selected = 0;
00213 for (CountableSetValues sel2(x.lub, x[3]); sel2();
00214 ++sel2, selected++) {
00215 if (sel2.val() < 0)
00216 return false;
00217 };
00218 if (selected <= 1)
00219 return true;
00220 GECODE_AUTOARRAY(CountableSetRanges, sel, selected);
00221 CountableSetValues selector(x.lub, x[3]);
00222 unsigned int cardsum = 0;
00223 for (int i=selected; i--;++selector) {
00224 if (selector.val()>=3 || selector.val()<0)
00225 return false;
00226 sel[i].init(x.lub, x[selector.val()]);
00227 CountableSetRanges xicard(x.lub, x[selector.val()]);
00228 cardsum += Iter::Ranges::size(xicard);
00229 }
00230 Iter::Ranges::NaryUnion<CountableSetRanges> u(sel, selected);
00231 return Iter::Ranges::size(u) == cardsum;
00232 }
00234 virtual void post(Space* home, SetVarArray& x, IntVarArray&) {
00235 SetVarArgs xs(x.size()-1);
00236 for (int i=x.size()-1; i--;)
00237 xs[i]=x[i];
00238 Gecode::elementsDisjoint(home, xs, x[x.size()-1]);
00239 }
00240 };
00241 ElementDisjoint _elementdisjoint("Element::Disjoint");
00242
00244 class ElementSet : public SetTest {
00245 public:
00247 ElementSet(const char* t)
00248 : SetTest(t,4,ds_12,false,true) {}
00250 virtual bool solution(const SetAssignment& x) const {
00251 if (x.intval() < 0 || x.intval() > 2)
00252 return false;
00253 CountableSetRanges z(x.lub, x[3]);
00254 CountableSetRanges y(x.lub, x[x.intval()]);
00255 return Iter::Ranges::equal(y, z);
00256 }
00258 virtual void post(Space* home, SetVarArray& x, IntVarArray& y) {
00259 SetVarArgs xs(x.size()-1);
00260 for (int i=x.size()-1; i--;)
00261 xs[i]=x[i];
00262 Gecode::element(home, xs, y[0], x[x.size()-1]);
00263 }
00264 };
00265 ElementSet _elementset("Element::Set");
00266
00268 class ElementSetConst : public SetTest {
00269 private:
00270 const IntSet i0;
00271 const IntSet i1;
00272 const IntSet i2;
00273 public:
00275 ElementSetConst(const char* t)
00276 : SetTest(t,1,ds_13,false,true), i0(-3,-3), i1(-1,1), i2(0,2) {}
00278 virtual bool solution(const SetAssignment& x) const {
00279 if (x.intval() < 0 || x.intval() > 2)
00280 return false;
00281 CountableSetRanges xr(x.lub, x[0]);
00282 IntSet iss[] = {i0, i1, i2};
00283 IntSetRanges isr(iss[x.intval()]);
00284 return Iter::Ranges::equal(xr, isr);
00285 }
00287 virtual void post(Space* home, SetVarArray& x, IntVarArray& y) {
00288 IntSetArgs xs(3);
00289 xs[0] = i0; xs[1] = i1; xs[2] = i2;
00290 Gecode::element(home, xs, y[0], x[0]);
00291 }
00292 };
00293 ElementSetConst _elementsetconst("Element::SetConst");
00294
00296
00297 }}}
00298
00299