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 #include "test/int.hh"
00040 #include <gecode/minimodel.hh>
00041
00042 using namespace Gecode;
00043
00044 namespace Test { namespace Set {
00045
00047 namespace Int {
00048
00054
00055 static const int d1r[4][2] = {
00056 {-4,-3},{-1,-1},{1,1},{3,5}
00057 };
00058 static IntSet d1(d1r,4);
00059
00060 static IntSet d2(-1,3);
00061 static IntSet d3(0,3);
00062
00063 static IntSet d4(0,4);
00064
00065 static IntSet ds_33(-3,3);
00066
00068 class Card : public SetTest {
00069 public:
00071 Card(const char* t)
00072 : SetTest(t,1,ds_33,false,1) {}
00074 virtual bool solution(const SetAssignment& x) const {
00075 unsigned int s = 0;
00076 for (CountableSetRanges xr(x.lub, x[0]);xr();++xr) s+= xr.width();
00077 if (x.intval() < 0)
00078 return false;
00079 return s==(unsigned int)x.intval();
00080 }
00082 virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
00083 Gecode::cardinality(home, x[0], y[0]);
00084 }
00085 };
00086 Card _card("Int::Card");
00087
00089 class Min : public SetTest {
00090 public:
00092 Min(const char* t)
00093 : SetTest(t,1,ds_33,true,1) {}
00095 virtual bool solution(const SetAssignment& x) const {
00096 CountableSetRanges xr0(x.lub, x[0]);
00097 return xr0() && xr0.min()==x.intval();
00098 }
00100 virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
00101 Gecode::min(home, x[0], y[0]);
00102 }
00104 virtual void post(Space& home, SetVarArray& x, IntVarArray& y,
00105 Reify r) {
00106 Gecode::min(home, x[0], y[0], r);
00107 }
00108 };
00109 Min _min("Int::Min");
00110
00112 class NotMin : public SetTest {
00113 public:
00115 NotMin(const char* t)
00116 : SetTest(t,1,ds_33,false,1) {}
00118 virtual bool solution(const SetAssignment& x) const {
00119 CountableSetRanges xr0(x.lub, x[0]);
00120 return !(xr0() && xr0.min()==x.intval());
00121 }
00123 virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
00124 Gecode::notMin(home, x[0], y[0]);
00125 }
00126 };
00127 NotMin _notmin("Int::NotMin");
00128
00130 class Max : public SetTest {
00131 public:
00133 Max(const char* t)
00134 : SetTest(t,1,ds_33,true,1) {}
00136 virtual bool solution(const SetAssignment& x) const {
00137 CountableSetRanges xr0(x.lub, x[0]);
00138 IntSet x0(xr0);
00139 return x0.ranges() > 0 && x0.max()==x.intval();
00140 }
00142 virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
00143 Gecode::max(home, x[0], y[0]);
00144 }
00146 virtual void post(Space& home, SetVarArray& x, IntVarArray& y,
00147 Reify r) {
00148 Gecode::max(home, x[0], y[0], r);
00149 }
00150 };
00151 Max _max("Int::Max");
00152
00154 class NotMax : public SetTest {
00155 public:
00157 NotMax(const char* t)
00158 : SetTest(t,1,ds_33,false,1) {}
00160 virtual bool solution(const SetAssignment& x) const {
00161 CountableSetRanges xr0(x.lub, x[0]);
00162 IntSet x0(xr0);
00163 return !(x0.ranges() > 0 && x0.max()==x.intval());
00164 }
00166 virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
00167 Gecode::notMax(home, x[0], y[0]);
00168 }
00169 };
00170 NotMax _notmax("Int::NotMax");
00171
00173 class Elem : public SetTest {
00174 public:
00176 Elem(const char* t)
00177 : SetTest(t,1,ds_33,true,1) {}
00179 virtual bool solution(const SetAssignment& x) const {
00180 for (CountableSetValues xr(x.lub, x[0]);xr();++xr)
00181 if (xr.val()==x.intval())
00182 return true;
00183 return false;
00184 }
00186 virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
00187 Gecode::rel(home, x[0], SRT_SUP, y[0]);
00188 }
00190 virtual void post(Space& home, SetVarArray& x, IntVarArray& y,
00191 Reify r) {
00192 Gecode::rel(home, x[0], SRT_SUP, y[0], r);
00193 }
00194 };
00195 Elem _elem("Int::Elem");
00196
00198 class NoElem : public SetTest {
00199 public:
00201 NoElem(const char* t)
00202 : SetTest(t,1,ds_33,false,1) {}
00204 virtual bool solution(const SetAssignment& x) const {
00205 for (CountableSetValues xr(x.lub, x[0]);xr();++xr)
00206 if (xr.val()==x.intval())
00207 return false;
00208 return true;
00209 }
00211 virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
00212 Gecode::rel(home, x[0], SRT_DISJ, y[0]);
00213 }
00214 };
00215 NoElem _noelem("Int::NoElem");
00216
00218 class Rel : public SetTest {
00219 private:
00220 Gecode::SetRelType srt;
00221 bool inverse;
00222 public:
00224 Rel(Gecode::SetRelType srt0, bool inverse0)
00225 : SetTest("Int::Rel::"+str(srt0)+(inverse0 ? "::i" : ""),
00226 1,ds_33,true,1)
00227 , srt(srt0)
00228 , inverse(inverse0) {}
00230 virtual bool solution(const SetAssignment& x) const {
00231 CountableSetRanges xr(x.lub, x[0]);
00232 IntSet is(x.intval(), x.intval());
00233 IntSetRanges dr(is);
00234 Gecode::SetRelType rsrt = srt;
00235 if (inverse) {
00236 switch (srt) {
00237 case SRT_SUB: rsrt = SRT_SUP; break;
00238 case SRT_SUP: rsrt = SRT_SUB; break;
00239 default: break;
00240 }
00241 }
00242 switch (rsrt) {
00243 case SRT_EQ: return Iter::Ranges::equal(xr, dr);
00244 case SRT_NQ: return !Iter::Ranges::equal(xr, dr);
00245 case SRT_SUB: return Iter::Ranges::subset(xr, dr);
00246 case SRT_SUP: return Iter::Ranges::subset(dr, xr);
00247 case SRT_DISJ:
00248 {
00249 Gecode::Iter::Ranges::Inter<CountableSetRanges,IntSetRanges>
00250 inter(xr, dr);
00251 return !inter();
00252 }
00253 case SRT_CMPL:
00254 {
00255 Gecode::Set::RangesCompl<IntSetRanges> drc(dr);
00256 return Iter::Ranges::equal(xr,drc);
00257 }
00258 }
00259 GECODE_NEVER;
00260 return false;
00261 }
00263 virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
00264 if (!inverse)
00265 Gecode::rel(home, x[0], srt, y[0]);
00266 else
00267 Gecode::rel(home, y[0], srt, x[0]);
00268 }
00270 virtual void post(Space& home, SetVarArray& x, IntVarArray& y,
00271 Reify r) {
00272 if (!inverse)
00273 Gecode::rel(home, x[0], srt, y[0], r);
00274 else
00275 Gecode::rel(home, y[0], srt, x[0], r);
00276 }
00277 };
00278 Rel _rel_eq(Gecode::SRT_EQ,false);
00279 Rel _rel_nq(Gecode::SRT_NQ,false);
00280 Rel _rel_sub(Gecode::SRT_SUB,false);
00281 Rel _rel_sup(Gecode::SRT_SUP,false);
00282 Rel _rel_disj(Gecode::SRT_DISJ,false);
00283 Rel _rel_cmpl(Gecode::SRT_CMPL,false);
00284 Rel _rel_eqi(Gecode::SRT_EQ,true);
00285 Rel _rel_nqi(Gecode::SRT_NQ,true);
00286 Rel _rel_subi(Gecode::SRT_SUB,true);
00287 Rel _rel_supi(Gecode::SRT_SUP,true);
00288 Rel _rel_disji(Gecode::SRT_DISJ,true);
00289 Rel _rel_cmpli(Gecode::SRT_CMPL,true);
00290
00292 class IntRel : public SetTest {
00293 private:
00294 Gecode::IntRelType irt;
00295 bool inverse;
00296 public:
00298 IntRel(Gecode::IntRelType irt0, bool inverse0)
00299 : SetTest("Int::IntRel::"+Test::Int::Test::str(irt0)+
00300 (inverse0 ? "::i" : ""),1,ds_33,false,1)
00301 , irt(irt0)
00302 , inverse(inverse0) {}
00304 virtual bool solution(const SetAssignment& x) const {
00305 CountableSetValues xr(x.lub, x[0]);
00306 if (!xr())
00307 return false;
00308 for (; xr(); ++xr) {
00309 switch (irt) {
00310 case Gecode::IRT_EQ:
00311 if (xr.val() != x.intval()) return false;
00312 break;
00313 case Gecode::IRT_NQ:
00314 if (xr.val() == x.intval()) return false;
00315 break;
00316 case Gecode::IRT_GR:
00317 if (!inverse && xr.val() <= x.intval()) return false;
00318 if (inverse && xr.val() >= x.intval()) return false;
00319 break;
00320 case Gecode::IRT_GQ:
00321 if (!inverse && xr.val() < x.intval()) return false;
00322 if (inverse && xr.val() > x.intval()) return false;
00323 break;
00324 case Gecode::IRT_LE:
00325 if (!inverse && xr.val() >= x.intval()) return false;
00326 if (inverse && xr.val() <= x.intval()) return false;
00327 break;
00328 case Gecode::IRT_LQ:
00329 if (!inverse && xr.val() > x.intval()) return false;
00330 if (inverse && xr.val() < x.intval()) return false;
00331 break;
00332 default:
00333 GECODE_NEVER;
00334 return false;
00335 }
00336 }
00337 return true;
00338 }
00340 virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
00341 if (!inverse)
00342 Gecode::rel(home, x[0], irt, y[0]);
00343 else
00344 Gecode::rel(home, y[0], irt, x[0]);
00345 }
00346 };
00347 IntRel _intrel_eq(Gecode::IRT_EQ,false);
00348 IntRel _intrel_nq(Gecode::IRT_NQ,false);
00349 IntRel _intrel_gr(Gecode::IRT_GR,false);
00350 IntRel _intrel_gq(Gecode::IRT_GQ,false);
00351 IntRel _intrel_le(Gecode::IRT_LE,false);
00352 IntRel _intrel_lq(Gecode::IRT_LQ,false);
00353 IntRel _intrel_eqi(Gecode::IRT_EQ,true);
00354 IntRel _intrel_nqi(Gecode::IRT_NQ,true);
00355 IntRel _intrel_gri(Gecode::IRT_GR,true);
00356 IntRel _intrel_gqi(Gecode::IRT_GQ,true);
00357 IntRel _intrel_lei(Gecode::IRT_LE,true);
00358 IntRel _intrel_lqi(Gecode::IRT_LQ,true);
00359
00360
00361 template<class I>
00362 int weightI(const IntArgs& elements,
00363 const IntArgs& weights,
00364 I& iter) {
00365 int sum = 0;
00366 int i = 0;
00367 for (Iter::Ranges::ToValues<I> v(iter); v(); ++v) {
00368
00369 while (elements[i]<v.val()) i++;
00370 assert(elements[i] == v.val());
00371 sum += weights[i];
00372 }
00373 return sum;
00374 }
00375
00377 class Weights : public SetTest {
00378 public:
00379 IntArgs elements;
00380 IntArgs weights;
00381 int minWeight;
00382 int maxWeight;
00384 Weights(const char* t, IntArgs& el, IntArgs& w,
00385 int min = -10000, int max = 10000)
00386 : SetTest(t,1,ds_33,false,1),
00387 elements(el), weights(w), minWeight(min), maxWeight(max) {}
00389 virtual bool solution(const SetAssignment& x) const {
00390 CountableSetRanges x0(x.lub, x[0]);
00391 return x.intval()==weightI(elements,weights,x0) &&
00392 x.intval() >= minWeight && x.intval() <= maxWeight;
00393 }
00395 virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
00396 Gecode::rel(home, minWeight <= y[0]);
00397 Gecode::rel(home, maxWeight >= y[0]);
00398 Gecode::weights(home, elements, weights, x[0], y[0]);
00399 }
00400 };
00401
00402 const int el1v[] = {-3,-2,-1,0,1,2,3};
00403 IntArgs el1(7,el1v);
00404 const int w1v[] = {1,-2,1,1,1,6,1};
00405 IntArgs w1(7,w1v);
00406 Weights _weights1("Int::Weights::1", el1, w1);
00407
00408 const int w2v[] = {-1,-1,-1,10,-1,-1,-1};
00409 IntArgs w2(7,w2v);
00410 Weights _weights2("Int::Weights::2", el1, w2);
00411 Weights _weights3("Int::Weights::3", el1, w2, 3);
00412
00413 const int w4v[] = {1,1,0,0,0,0,0};
00414 IntArgs w4(7,w4v);
00415 Weights _weights4("Int::Weights::4", el1, w4);
00416
00417 }}}
00418
00419