Generated on Mon Aug 25 11:35:39 2008 for Gecode by doxygen 1.5.6

int.cc

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: 2008-02-20 10:27:10 +0100 (Wed, 20 Feb 2008) $ by $Author: tack $
00011  *     $Revision: 6241 $
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 #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,false,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       }
00103     };
00104     Min _min("Int::Min");
00105 
00107     class Max : public SetTest {
00108     public:
00110       Max(const char* t)
00111         : SetTest(t,1,ds_33,false,1) {}
00113       virtual bool solution(const SetAssignment& x) const {
00114         CountableSetRanges xr0(x.lub, x[0]);
00115         IntSet x0(xr0);
00116         return x0.size() > 0 && x0.max()==x.intval();
00117       }
00119       virtual void post(Space* home, SetVarArray& x, IntVarArray& y) {
00120         Gecode::max(home, x[0], y[0]);
00121       }
00122     };
00123     Max _max("Int::Max");
00124 
00126     class Elem : public SetTest {
00127     public:
00129       Elem(const char* t)
00130         : SetTest(t,1,ds_33,true,1) {}
00132       virtual bool solution(const SetAssignment& x) const {
00133         for(CountableSetValues xr(x.lub, x[0]);xr();++xr)
00134           if (xr.val()==x.intval())
00135             return true;
00136         return false;
00137       }
00139       virtual void post(Space* home, SetVarArray& x, IntVarArray& y) {
00140         Gecode::rel(home, x[0], SRT_SUP, y[0]);
00141       }
00143       virtual void post(Space* home, SetVarArray& x, IntVarArray& y, BoolVar b) {
00144         Gecode::rel(home, x[0], SRT_SUP, y[0], b);
00145       }
00146     };
00147     Elem _elem("Int::Elem");
00148 
00150     class NoElem : public SetTest {
00151     public:
00153       NoElem(const char* t)
00154         : SetTest(t,1,ds_33,false,1) {}
00156       virtual bool solution(const SetAssignment& x) const {
00157         for(CountableSetValues xr(x.lub, x[0]);xr();++xr)
00158           if (xr.val()==x.intval())
00159             return false;
00160         return true;
00161       }
00163       virtual void post(Space* home, SetVarArray& x, IntVarArray& y) {
00164         Gecode::rel(home, x[0], SRT_DISJ, y[0]);
00165       }
00166     };
00167     NoElem _noelem("Int::NoElem");
00168 
00170     class Rel : public SetTest {
00171     private:
00172       Gecode::SetRelType srt;
00173       bool inverse;
00174     public:
00176       Rel(Gecode::SetRelType srt0, bool inverse0)
00177         : SetTest("Int::Rel::"+str(srt0)+(inverse0 ? "::i" : ""),
00178                   1,ds_33,true,1)
00179         , srt(srt0)
00180         , inverse(inverse0) {}
00182       virtual bool solution(const SetAssignment& x) const {
00183         CountableSetRanges xr(x.lub, x[0]);
00184         IntSet is(x.intval(), x.intval());
00185         IntSetRanges dr(is);
00186         Gecode::SetRelType rsrt = srt;
00187         if (inverse) {
00188           switch (srt) {
00189             case SRT_SUB: rsrt = SRT_SUP; break;
00190             case SRT_SUP: rsrt = SRT_SUB; break;
00191             default: break;
00192           }
00193         }
00194         switch (rsrt) {
00195         case SRT_EQ: return Iter::Ranges::equal(xr, dr);
00196         case SRT_NQ: return !Iter::Ranges::equal(xr, dr);
00197         case SRT_SUB: return Iter::Ranges::subset(xr, dr);
00198         case SRT_SUP: return Iter::Ranges::subset(dr, xr);
00199         case SRT_DISJ:
00200           {
00201             Gecode::Iter::Ranges::Inter<CountableSetRanges,IntSetRanges>
00202               inter(xr, dr);
00203             return !inter();
00204           }
00205         case SRT_CMPL:
00206           {
00207             Gecode::Set::RangesCompl<IntSetRanges> drc(dr);
00208             return Iter::Ranges::equal(xr,drc);
00209           }
00210         }
00211         GECODE_NEVER;
00212         return false;
00213       }
00215       virtual void post(Space* home, SetVarArray& x, IntVarArray& y) {
00216         if (!inverse)
00217           Gecode::rel(home, x[0], srt, y[0]);
00218         else
00219           Gecode::rel(home, y[0], srt, x[0]);
00220       }
00222       virtual void post(Space* home, SetVarArray& x, IntVarArray& y,
00223                         BoolVar b) {
00224         if (!inverse)
00225           Gecode::rel(home, x[0], srt, y[0], b);
00226         else
00227           Gecode::rel(home, y[0], srt, x[0], b);
00228       }
00229     };
00230     Rel _rel_eq(Gecode::SRT_EQ,false);
00231     Rel _rel_nq(Gecode::SRT_NQ,false);
00232     Rel _rel_sub(Gecode::SRT_SUB,false);
00233     Rel _rel_sup(Gecode::SRT_SUP,false);
00234     Rel _rel_disj(Gecode::SRT_DISJ,false);
00235     Rel _rel_cmpl(Gecode::SRT_CMPL,false);
00236     Rel _rel_eqi(Gecode::SRT_EQ,true);
00237     Rel _rel_nqi(Gecode::SRT_NQ,true);
00238     Rel _rel_subi(Gecode::SRT_SUB,true);
00239     Rel _rel_supi(Gecode::SRT_SUP,true);
00240     Rel _rel_disji(Gecode::SRT_DISJ,true);
00241     Rel _rel_cmpli(Gecode::SRT_CMPL,true);
00242 
00244     class IntRel : public SetTest {
00245     private:
00246       Gecode::IntRelType irt;
00247       bool inverse;
00248     public:
00250       IntRel(Gecode::IntRelType irt0, bool inverse0)
00251         : SetTest("Int::IntRel::"+Test::Int::Test::str(irt0)+
00252                   (inverse0 ? "::i" : ""),1,ds_33,false,1)
00253         , irt(irt0)
00254         , inverse(inverse0) {}
00256       virtual bool solution(const SetAssignment& x) const {
00257         CountableSetValues xr(x.lub, x[0]);
00258         if (!xr())
00259           return false;
00260         for (; xr(); ++xr) {
00261           switch (irt) {
00262           case Gecode::IRT_EQ:
00263             if (xr.val() != x.intval()) return false;
00264             break;
00265           case Gecode::IRT_NQ:
00266             if (xr.val() == x.intval()) return false;
00267             break;
00268           case Gecode::IRT_GR:
00269             if (!inverse && xr.val() <= x.intval()) return false;
00270             if (inverse && xr.val() >= x.intval()) return false;
00271             break;
00272           case Gecode::IRT_GQ:
00273             if (!inverse && xr.val() < x.intval()) return false;
00274             if (inverse && xr.val() > x.intval()) return false;
00275             break;
00276           case Gecode::IRT_LE:
00277             if (!inverse && xr.val() >= x.intval()) return false;
00278             if (inverse && xr.val() <= x.intval()) return false;
00279             break;
00280           case Gecode::IRT_LQ:
00281             if (!inverse && xr.val() > x.intval()) return false;
00282             if (inverse && xr.val() < x.intval()) return false;
00283             break;
00284           default:
00285             GECODE_NEVER;
00286             return false;
00287           }
00288         }
00289         return true;
00290       }
00292       virtual void post(Space* home, SetVarArray& x, IntVarArray& y) {
00293         if (!inverse)
00294           Gecode::rel(home, x[0], irt, y[0]);
00295         else
00296           Gecode::rel(home, y[0], irt, x[0]);
00297       }
00298     };
00299     IntRel _intrel_eq(Gecode::IRT_EQ,false);
00300     IntRel _intrel_nq(Gecode::IRT_NQ,false);
00301     IntRel _intrel_gr(Gecode::IRT_GR,false);
00302     IntRel _intrel_gq(Gecode::IRT_GQ,false);
00303     IntRel _intrel_le(Gecode::IRT_LE,false);
00304     IntRel _intrel_lq(Gecode::IRT_LQ,false);
00305     IntRel _intrel_eqi(Gecode::IRT_EQ,true);
00306     IntRel _intrel_nqi(Gecode::IRT_NQ,true);
00307     IntRel _intrel_gri(Gecode::IRT_GR,true);
00308     IntRel _intrel_gqi(Gecode::IRT_GQ,true);
00309     IntRel _intrel_lei(Gecode::IRT_LE,true);
00310     IntRel _intrel_lqi(Gecode::IRT_LQ,true);
00311 
00312 
00313     template <class I>
00314     int weightI(const IntArgs& elements,
00315                 const IntArgs& weights,
00316                 I& iter) {
00317       int sum = 0;
00318       int i = 0;
00319       for (Iter::Ranges::ToValues<I> v(iter); v(); ++v) {
00320         // Skip all elements below the current
00321         while (elements[i]<v.val()) i++;
00322         assert(elements[i] == v.val());
00323         sum += weights[i];
00324       }
00325       return sum;
00326     }
00327 
00329     class Weights : public SetTest {
00330     public:
00331       IntArgs elements;
00332       IntArgs weights;
00333 
00335       Weights(const char* t)
00336         : SetTest(t,1,ds_33,false,1),
00337           elements(7), weights(7) {
00338         for (int i=-3; i<=3; i++)
00339           elements[i+3] = i;
00340         for (int i=0; i<7; i++)
00341           weights[i] = 1;
00342         weights[1] = -2;
00343         weights[5] = 6;
00344       }
00346       virtual bool solution(const SetAssignment& x) const {
00347         CountableSetRanges x0(x.lub, x[0]);
00348         return x.intval()==weightI(elements,weights,x0);
00349       }
00351       virtual void post(Space* home, SetVarArray& x, IntVarArray& y) {
00352         Gecode::weights(home, elements, weights, x[0], y[0]);
00353       }
00354     };
00355     Weights _weights("Int::Weights");
00356 
00358     class Match : public SetTest {
00359     public:
00361       Match(const char* t)
00362         : SetTest(t,1,ds_33,false,3) {}
00364       virtual bool solution(const SetAssignment& x) const {
00365         if (x.ints()[0]>=x.ints()[1] ||
00366             x.ints()[1]>=x.ints()[2])
00367           return false;
00368         CountableSetValues xr(x.lub, x[0]);
00369         if (!xr())
00370           return false;
00371         if (xr.val() != x.ints()[0])
00372           return false;
00373         ++xr;
00374         if (!xr())
00375           return false;
00376         if (xr.val() != x.ints()[1])
00377           return false;
00378         ++xr;
00379         if (!xr())
00380           return false;
00381         if (xr.val() != x.ints()[2])
00382           return false;
00383         ++xr;
00384         if (xr())
00385           return false;
00386         return true;
00387       }
00389       virtual void post(Space* home, SetVarArray& x, IntVarArray& y) {
00390         Gecode::match(home, x[0], y);
00391       }
00392     };
00393     Match _match("Int::Match");
00394 
00396     class ChannelInt : public SetTest {
00397     private:
00398       int ssize, isize;
00399     public:
00401       ChannelInt(const char* t, const IntSet& d, int _ssize, int _isize)
00402         : SetTest(t,_ssize,d,false,_isize), ssize(_ssize), isize(_isize) {}
00404       virtual bool solution(const SetAssignment& x) const {
00405         for (int i=0; i<isize; i++) {
00406           if (x.ints()[i] < 0 || x.ints()[i] >= ssize)
00407             return false;
00408           Iter::Ranges::Singleton single(i,i);
00409           CountableSetRanges csr(x.lub, x[x.ints()[i]]);
00410           if (!Iter::Ranges::subset(single, csr))
00411             return false;
00412         }
00413         for (int i=0; i<ssize; i++) {
00414           int size = 0;
00415           for (CountableSetValues csv(x.lub, x[i]); csv(); ++csv) {
00416             if (csv.val() < 0 || csv.val() >= isize) return false;
00417             if (x.ints()[csv.val()] != i) return false;
00418             size++;
00419           }
00420         }
00421         return true;
00422       }
00424       virtual void post(Space* home, SetVarArray& x, IntVarArray& y) {
00425         Gecode::channel(home, y, x);
00426       }
00427     };
00428 
00429     ChannelInt _channelint1("Int::Channel::Int::1", d2, 2, 3);
00430     ChannelInt _channelint2("Int::Channel::Int::2", d3, 3, 3);
00431 
00433     class ChannelBool : public SetTest {
00434     private:
00435       int isize;
00436     public:
00438       ChannelBool(const char* t, const IntSet& d, int _isize)
00439         : SetTest(t,1,d,false,_isize), isize(_isize) {}
00441       virtual bool solution(const SetAssignment& x) const {
00442         for (int i=0; i<isize; i++) {
00443           if (x.ints()[i] < 0 || x.ints()[i] > 1)
00444             return false;
00445         }
00446         int cur = 0;
00447         for (CountableSetValues csv(x.lub, x[0]); csv(); ++csv) {
00448           if (csv.val() < 0 || csv.val() >= isize) return false;
00449           if (x.ints()[csv.val()] != 1) return false;
00450           for (; cur<csv.val(); cur++)
00451             if (x.ints()[cur] != 0) return false;
00452           cur = csv.val() + 1;
00453         }
00454         for (; cur<isize; cur++)
00455           if (x.ints()[cur] != 0) return false;
00456         return true;
00457       }
00459       virtual void post(Space* home, SetVarArray& x, IntVarArray& y) {
00460         BoolVarArgs b(y.size());
00461         for (int i=y.size(); i--;)
00462           b[i] = channel(home, y[i]);
00463         Gecode::channel(home, b, x[0]);
00464       }
00465     };
00466 
00467     ChannelBool _channelbool1("Int::Channel::Bool::1", d2, 3);
00468     ChannelBool _channelbool2("Int::Channel::Bool::2", d3, 3);
00469     ChannelBool _channelbool3("Int::Channel::Bool::3", d4, 5);
00470   
00471 }}}
00472 
00473 // STATISTICS: test-set