Generated on Thu Mar 22 10:39:36 2012 for Gecode by doxygen 1.6.3

dom.cpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Guido Tack <tack@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Guido Tack, 2005
00008  *
00009  *  Last modified:
00010  *     $Date: 2011-08-24 17:04:35 +0200 (Wed, 24 Aug 2011) $ by $Author: tack $
00011  *     $Revision: 12347 $
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  *  Permission is hereby granted, free of charge, to any person obtaining
00018  *  a copy of this software and associated documentation files (the
00019  *  "Software"), to deal in the Software without restriction, including
00020  *  without limitation the rights to use, copy, modify, merge, publish,
00021  *  distribute, sublicense, and/or sell copies of the Software, and to
00022  *  permit persons to whom the Software is furnished to do so, subject to
00023  *  the following conditions:
00024  *
00025  *  The above copyright notice and this permission notice shall be
00026  *  included in all copies or substantial portions of the Software.
00027  *
00028  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00029  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00030  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00031  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00032  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00033  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00034  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00035  *
00036  */
00037 
00038 #include "test/set.hh"
00039 
00040 using namespace Gecode;
00041 
00042 namespace Test { namespace Set {
00043 
00045   namespace Dom {
00046 
00052 
00053     static const int d1r[4][2] = {
00054       {-4,-3},{-1,-1},{1,1},{3,5}
00055     };
00056     static IntSet d1(d1r,4);
00057 
00058     static const int d1cr[5][2] = {
00059       {Gecode::Set::Limits::min,-5},
00060       {-2,-2},{0,0},{2,2},
00061       {6,Gecode::Set::Limits::max}
00062     };
00063     static IntSet d1c(d1cr,5);
00064 
00065     static IntSet ds_33(-3,3);
00066 
00067     static const int d2r[2][2] = {
00068       {Gecode::Set::Limits::min,-4}, {4,Gecode::Set::Limits::max}
00069     };
00070     static IntSet ds_33c(d2r,2);
00071     static IntSet ds_55(-5,5);
00072 
00073     namespace {
00074       static int minSymDiff(const SetAssignment& x, const IntSet& is) {
00075         typedef Iter::Ranges::Diff<CountableSetRanges,IntSetRanges> DiffA;
00076         CountableSetRanges xr00(x.lub, x[0]);
00077         IntSetRanges xr10(is);
00078         DiffA a(xr00,xr10);
00079         typedef Iter::Ranges::Diff<IntSetRanges,CountableSetRanges> DiffB;
00080         CountableSetRanges xr01(x.lub, x[0]);
00081         IntSetRanges xr11(is);
00082         DiffB b(xr11,xr01);
00083         Iter::Ranges::Union<DiffA,DiffB> u(a,b);
00084         return u() ? u.min() : Gecode::Set::Limits::max+1;
00085       }
00086       template<class I>
00087       static bool in(int i, I& c, bool eq=false) {
00088         if (eq && i==Gecode::Set::Limits::max+1)
00089           return true;
00090         Iter::Ranges::Singleton s(i,i);
00091         return Iter::Ranges::subset(s,c);
00092       }
00093     }
00094 
00096     class DomRange : public SetTest {
00097     private:
00098       Gecode::SetRelType srt;
00099       IntSet is;
00100     public:
00102       DomRange(SetRelType srt0)
00103         : SetTest("Dom::Range::"+str(srt0),1,ds_55,true), srt(srt0)
00104         , is(srt == Gecode::SRT_CMPL ? ds_33c: ds_33) {}
00106       virtual bool solution(const SetAssignment& x) const {
00107         CountableSetRanges xr(x.lub, x[0]);
00108         IntSetRanges dr(is);
00109         switch (srt) {
00110         case SRT_EQ: return Iter::Ranges::equal(xr, dr);
00111         case SRT_LQ: return (!xr()) || in(minSymDiff(x,is),dr,true);
00112         case SRT_LE: return xr() ? in(minSymDiff(x,is),dr) : dr();
00113         case SRT_GQ: return (!dr()) || in(minSymDiff(x,is),xr,true);
00114         case SRT_GR: return dr() ? in(minSymDiff(x,is),xr) : xr();
00115         case SRT_NQ: return !Iter::Ranges::equal(xr, dr);
00116         case SRT_SUB: return Iter::Ranges::subset(xr, dr);
00117         case SRT_SUP: return Iter::Ranges::subset(dr, xr);
00118         case SRT_DISJ:
00119           {
00120             Gecode::Iter::Ranges::Inter<CountableSetRanges,IntSetRanges>
00121               inter(xr, dr);
00122             return !inter();
00123           }
00124         case SRT_CMPL:
00125           {
00126             Gecode::Set::RangesCompl<IntSetRanges> drc(dr);
00127             return Iter::Ranges::equal(xr,drc);
00128           }
00129         }
00130         GECODE_NEVER;
00131         return false;
00132       }
00134       virtual void post(Space& home, SetVarArray& x, IntVarArray&) {
00135         Gecode::dom(home, x[0], srt, is);
00136       }
00138       virtual void post(Space& home, SetVarArray& x, IntVarArray&,BoolVar b) {
00139         Gecode::dom(home, x[0], srt, is, b);
00140       }
00141     };
00142     DomRange _domrange_eq(SRT_EQ);
00143     DomRange _domrange_lq(SRT_LQ);
00144     DomRange _domrange_le(SRT_LE);
00145     DomRange _domrange_gq(SRT_GQ);
00146     DomRange _domrange_gr(SRT_GR);
00147     DomRange _domrange_nq(SRT_NQ);
00148     DomRange _domrange_sub(SRT_SUB);
00149     DomRange _domrange_sup(SRT_SUP);
00150     DomRange _domrange_disj(SRT_DISJ);
00151     DomRange _domrange_cmpl(SRT_CMPL);
00152 
00154     class DomIntRange : public SetTest {
00155     private:
00156       Gecode::SetRelType srt;
00157     public:
00159       DomIntRange(Gecode::SetRelType srt0)
00160         : SetTest("Dom::IntRange::"+str(srt0),1,ds_55,true), srt(srt0) {}
00162       virtual bool solution(const SetAssignment& x) const {
00163         CountableSetRanges xr(x.lub, x[0]);
00164         IntSet is(-3,-1);
00165         IntSetRanges dr(is);
00166         switch (srt) {
00167         case SRT_EQ: return Iter::Ranges::equal(xr, dr);
00168         case SRT_LQ: return (!xr()) || in(minSymDiff(x,is),dr,true);
00169         case SRT_LE: return xr() ? in(minSymDiff(x,is),dr) : dr();
00170         case SRT_GQ: return (!dr()) || in(minSymDiff(x,is),xr,true);
00171         case SRT_GR: return dr() ? in(minSymDiff(x,is),xr) : xr();
00172         case SRT_NQ: return !Iter::Ranges::equal(xr, dr);
00173         case SRT_SUB: return Iter::Ranges::subset(xr, dr);
00174         case SRT_SUP: return Iter::Ranges::subset(dr, xr);
00175         case SRT_DISJ:
00176           {
00177             Gecode::Iter::Ranges::Inter<CountableSetRanges,IntSetRanges>
00178               inter(xr, dr);
00179             return !inter();
00180           }
00181         case SRT_CMPL:
00182           {
00183             Gecode::Set::RangesCompl<IntSetRanges> drc(dr);
00184             return Iter::Ranges::equal(xr,drc);
00185           }
00186         }
00187         GECODE_NEVER;
00188         return false;
00189       }
00191       virtual void post(Space& home, SetVarArray& x, IntVarArray&) {
00192         Gecode::dom(home, x[0], srt, -3, -1);
00193       }
00195       virtual void post(Space& home, SetVarArray& x, IntVarArray&,BoolVar b) {
00196         Gecode::dom(home, x[0], srt, -3, -1, b);
00197       }
00198     };
00199     DomIntRange _domintrange_eq(SRT_EQ);
00200     DomIntRange _domintrange_lq(SRT_LQ);
00201     DomIntRange _domintrange_le(SRT_LE);
00202     DomIntRange _domintrange_gq(SRT_GQ);
00203     DomIntRange _domintrange_gr(SRT_GR);
00204     DomIntRange _domintrange_nq(SRT_NQ);
00205     DomIntRange _domintrange_sub(SRT_SUB);
00206     DomIntRange _domintrange_sup(SRT_SUP);
00207     DomIntRange _domintrange_disj(SRT_DISJ);
00208     DomIntRange _domintrange_cmpl(SRT_CMPL);
00209 
00211     class DomInt : public SetTest {
00212     private:
00213       Gecode::SetRelType srt;
00214     public:
00216       DomInt(Gecode::SetRelType srt0)
00217         : SetTest("Dom::Int::"+str(srt0),1,ds_55,true), srt(srt0) {}
00219       virtual bool solution(const SetAssignment& x) const {
00220         CountableSetRanges xr(x.lub, x[0]);
00221         IntSet is(-3,-3);
00222         IntSetRanges dr(is);
00223         switch (srt) {
00224         case SRT_EQ: return Iter::Ranges::equal(xr, dr);
00225         case SRT_LQ: return (!xr()) || in(minSymDiff(x,is),dr,true);
00226         case SRT_LE: return xr() ? in(minSymDiff(x,is),dr) : dr();
00227         case SRT_GQ: return (!dr()) || in(minSymDiff(x,is),xr,true);
00228         case SRT_GR: return dr() ? in(minSymDiff(x,is),xr) : xr();
00229         case SRT_NQ: return !Iter::Ranges::equal(xr, dr);
00230         case SRT_SUB: return Iter::Ranges::subset(xr, dr);
00231         case SRT_SUP: return Iter::Ranges::subset(dr, xr);
00232         case SRT_DISJ:
00233           {
00234             Gecode::Iter::Ranges::Inter<CountableSetRanges,IntSetRanges>
00235               inter(xr, dr);
00236             return !inter();
00237           }
00238         case SRT_CMPL:
00239           {
00240             Gecode::Set::RangesCompl<IntSetRanges> drc(dr);
00241             return Iter::Ranges::equal(xr,drc);
00242           }
00243         }
00244         GECODE_NEVER;
00245         return false;
00246       }
00248       virtual void post(Space& home, SetVarArray& x, IntVarArray&) {
00249         Gecode::dom(home, x[0], srt, -3);
00250       }
00252       virtual void post(Space& home, SetVarArray& x, IntVarArray&,BoolVar b) {
00253         Gecode::dom(home, x[0], srt, -3, b);
00254       }
00255     };
00256     DomInt _domint_eq(SRT_EQ);
00257     DomInt _domint_lq(SRT_LQ);
00258     DomInt _domint_le(SRT_LE);
00259     DomInt _domint_gq(SRT_GQ);
00260     DomInt _domint_gr(SRT_GR);
00261     DomInt _domint_nq(SRT_NQ);
00262     DomInt _domint_sub(SRT_SUB);
00263     DomInt _domint_sup(SRT_SUP);
00264     DomInt _domint_disj(SRT_DISJ);
00265     DomInt _domint_cmpl(SRT_CMPL);
00266 
00268     class DomDom : public SetTest {
00269     private:
00270       Gecode::SetRelType srt;
00271       Gecode::IntSet is;
00272     public:
00274       DomDom(Gecode::SetRelType srt0)
00275         : SetTest("Dom::Dom::"+str(srt0),1,d1,true), srt(srt0)
00276         , is(srt == Gecode::SRT_CMPL ? d1c: d1) {}
00278       virtual bool solution(const SetAssignment& x) const {
00279         CountableSetRanges xr(x.lub, x[0]);
00280         IntSetRanges dr(is);
00281         switch (srt) {
00282         case SRT_EQ: return Iter::Ranges::equal(xr, dr);
00283         case SRT_LQ: return (!xr()) || in(minSymDiff(x,is),dr,true);
00284         case SRT_LE: return xr() ? in(minSymDiff(x,is),dr) : dr();
00285         case SRT_GQ: return (!dr()) || in(minSymDiff(x,is),xr,true);
00286         case SRT_GR: return dr() ? in(minSymDiff(x,is),xr) : xr();
00287         case SRT_NQ: return !Iter::Ranges::equal(xr, dr);
00288         case SRT_SUB: return Iter::Ranges::subset(xr, dr);
00289         case SRT_SUP: return Iter::Ranges::subset(dr, xr);
00290         case SRT_DISJ:
00291           {
00292             Gecode::Iter::Ranges::Inter<CountableSetRanges,IntSetRanges>
00293               inter(xr, dr);
00294             return !inter();
00295           }
00296         case SRT_CMPL:
00297           {
00298             Gecode::Set::RangesCompl<IntSetRanges> drc(dr);
00299             return Iter::Ranges::equal(xr,drc);
00300           }
00301         }
00302         GECODE_NEVER;
00303         return false;
00304       }
00306       virtual void post(Space& home, SetVarArray& x, IntVarArray&) {
00307         Gecode::dom(home, x[0], srt, is);
00308       }
00310       virtual void post(Space& home, SetVarArray& x, IntVarArray&,BoolVar b) {
00311         Gecode::dom(home, x[0], srt, is, b);
00312       }
00313     };
00314     DomDom _domdom_eq(SRT_EQ);
00315     DomDom _domdom_lq(SRT_LQ);
00316     DomDom _domdom_le(SRT_LE);
00317     DomDom _domdom_gq(SRT_GQ);
00318     DomDom _domdom_gr(SRT_GR);
00319     DomDom _domdom_nq(SRT_NQ);
00320     DomDom _domdom_sub(SRT_SUB);
00321     DomDom _domdom_sup(SRT_SUP);
00322     DomDom _domdom_disj(SRT_DISJ);
00323     DomDom _domdom_cmpl(SRT_CMPL);
00324 
00326     class CardRange : public SetTest {
00327     public:
00329       CardRange(void)
00330         : SetTest("Dom::CardRange",1,d1,false) {}
00332       virtual bool solution(const SetAssignment& x) const {
00333         CountableSetRanges xr(x.lub, x[0]);
00334         unsigned int card = Iter::Ranges::size(xr);
00335         return card >= 2 && card <= 3;
00336       }
00338       virtual void post(Space& home, SetVarArray& x, IntVarArray&) {
00339         Gecode::cardinality(home, x[0], 2, 3);
00340       }
00341     };
00342     CardRange _cardRange;
00343 
00344 }}}
00345 
00346 // STATISTICS: test-set