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

element.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-08 18:04:53 +0200 (Mon, 08 Aug 2011) $ by $Author: schulte $
00011  *     $Revision: 12253 $
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 <gecode/minimodel.hh>
00039 
00040 #include "test/set.hh"
00041 
00042 using namespace Gecode;
00043 
00044 namespace Test { namespace Set {
00045 
00047   namespace Element {
00048 
00054 
00055     static IntSet ds_12(-1,2);
00056     static IntSet ds_13(-1,3);
00057 
00059     class ElementUnion : public SetTest {
00060     public:
00062       ElementUnion(const char* t)
00063         : SetTest(t,5,ds_12,false) {}
00065       virtual bool solution(const SetAssignment& x) const {
00066         int selected = 0;
00067         for (CountableSetValues sel2(x.lub, x[3]); sel2();
00068              ++sel2, selected++) {}
00069         CountableSetValues x4v(x.lub, x[4]);
00070         if (selected==0)
00071           return !x4v();
00072         CountableSetRanges* sel = new CountableSetRanges[selected];
00073         CountableSetValues selector(x.lub, x[3]);
00074         for (int i=selected; i--;++selector) {
00075           if (selector.val()>=3 || selector.val()<0) {
00076             delete[] sel;
00077             return false;
00078           }
00079           sel[i].init(x.lub, x[selector.val()]);
00080         }
00081         
00082         FakeSpace* fs = new FakeSpace;
00083         bool ret;
00084         {
00085           Region r(*fs);
00086           Iter::Ranges::NaryUnion u(r, sel, selected);
00087           
00088           CountableSetRanges z(x.lub, x[4]);
00089           ret = Iter::Ranges::equal(u, z);
00090         }
00091         delete[] sel;
00092         delete fs;
00093         return ret;
00094       }
00096       virtual void post(Space& home, SetVarArray& x, IntVarArray&) {
00097         SetVarArgs xs(x.size()-2);
00098         for (int i=x.size()-2; i--;)
00099           xs[i]=x[i];
00100         Gecode::element(home, SOT_UNION, xs, x[x.size()-2], x[x.size()-1]);
00101       }
00102     };
00103     ElementUnion _elementunion("Element::Union");
00104 
00106     class ElementUnionConst : public SetTest {
00107     private:
00108       const IntSet i0;
00109       const IntSet i1;
00110       const IntSet i2;
00111     public:
00113       ElementUnionConst(const char* t)
00114         : SetTest(t,2,ds_13,false), i0(-3,-3), i1(-1,1), i2(0,2) {}
00116       virtual bool solution(const SetAssignment& x) const {
00117         int selected = 0;
00118         for (CountableSetValues sel2(x.lub, x[0]); sel2();
00119              ++sel2, selected++) {}
00120         CountableSetValues x4v(x.lub, x[1]);
00121         if (selected==0)
00122           return !x4v();
00123         IntSet iss[] = {i0, i1, i2};
00124         IntSetRanges* sel = new IntSetRanges[selected];
00125         CountableSetValues selector(x.lub, x[0]);
00126         for (int i=selected; i--;++selector) {
00127           if (selector.val()>=3 || selector.val()<0) {
00128             delete[] sel;
00129             return false;
00130           }
00131           sel[i].init(iss[selector.val()]);
00132         }
00133 
00134         FakeSpace* fs = new FakeSpace;
00135         bool ret;
00136         {
00137           Region r(*fs);
00138           Iter::Ranges::NaryUnion u(r, sel, selected);
00139 
00140           CountableSetRanges z(x.lub, x[1]);
00141           ret = Iter::Ranges::equal(u, z);
00142         }
00143         delete[] sel;
00144         delete fs;
00145         return ret;
00146       }
00148       virtual void post(Space& home, SetVarArray& x, IntVarArray&) {
00149         IntSetArgs xs(3);
00150         xs[0] = i0; xs[1] = i1; xs[2] = i2;
00151         Gecode::element(home, SOT_UNION, xs, x[0], x[1]);
00152       }
00153     };
00154     ElementUnionConst _elementunionconst("Element::UnionConst");
00155 
00157     class ElementInter : public SetTest {
00158     public:
00160       ElementInter(const char* t)
00161         : SetTest(t,5,ds_12,false) {}
00163       virtual bool solution(const SetAssignment& x) const {
00164         int selected = 0;
00165         for (CountableSetValues sel2(x.lub, x[3]); sel2();
00166              ++sel2, selected++) {}
00167         CountableSetRanges x4r(x.lub, x[4]);
00168         if (selected==0)
00169           return Iter::Ranges::size(x4r)==Gecode::Set::Limits::card;
00170         CountableSetRanges* sel = new CountableSetRanges[selected];
00171         CountableSetValues selector(x.lub, x[3]);
00172         for (int i=selected; i--;++selector) {
00173           if (selector.val()>=3 || selector.val()<0) {
00174             delete[] sel;
00175             return false;
00176           }
00177           sel[i].init(x.lub, x[selector.val()]);
00178         }
00179         FakeSpace* fs = new FakeSpace;
00180         bool ret;
00181         {
00182           Region r(*fs);
00183           Iter::Ranges::NaryInter u(r, sel, selected);
00184 
00185           CountableSetRanges z(x.lub, x[4]);
00186           ret = Iter::Ranges::equal(u, z);
00187         }
00188         delete fs;
00189         delete [] sel;
00190         return ret;
00191       }
00193       virtual void post(Space& home, SetVarArray& x, IntVarArray&) {
00194         SetVarArgs xs(x.size()-2);
00195         for (int i=x.size()-2; i--;)
00196           xs[i]=x[i];
00197         Gecode::element(home, SOT_INTER, xs, x[x.size()-2], x[x.size()-1]);
00198       }
00199     };
00200     ElementInter _elementinter("Element::Inter");
00201 
00203     class ElementInterIn : public SetTest {
00204     public:
00206       ElementInterIn(const char* t)
00207         : SetTest(t,5,ds_12,false) {}
00209       virtual bool solution(const SetAssignment& x) const {
00210         int selected = 0;
00211         for (CountableSetValues sel2(x.lub, x[3]); sel2();
00212              ++sel2, selected++) {}
00213         CountableSetRanges x4r(x.lub, x[4]);
00214         if (selected==0)
00215           return Iter::Ranges::size(x4r)==4;
00216         CountableSetRanges* sel = new CountableSetRanges[selected];
00217         CountableSetValues selector(x.lub, x[3]);
00218         for (int i=selected; i--;++selector) {
00219           if (selector.val()>=3 || selector.val()<0) {
00220             delete[] sel;
00221             return false;
00222           }
00223           sel[i].init(x.lub, x[selector.val()]);
00224         }
00225         FakeSpace* fs = new FakeSpace;
00226         bool ret;
00227         {
00228           Region r(*fs);
00229           Iter::Ranges::NaryInter u(r,sel, selected);
00230           
00231           CountableSetRanges z(x.lub, x[4]);
00232           ret = Iter::Ranges::equal(u, z);
00233         }
00234         delete fs;
00235         delete [] sel;
00236         return ret;
00237       }
00239       virtual void post(Space& home, SetVarArray& x, IntVarArray&) {
00240         SetVarArgs xs(x.size()-2);
00241         for (int i=x.size()-2; i--;)
00242           xs[i]=x[i];
00243         Gecode::element(home, SOT_INTER, xs, x[x.size()-2], x[x.size()-1], 
00244           ds_12);
00245       }
00246     };
00247     ElementInterIn _elementinterin("Element::InterIn");
00248 
00250     class ElementDisjoint : public SetTest {
00251     public:
00253       ElementDisjoint(const char* t)
00254         : SetTest(t,5,ds_12,false) {}
00256       virtual bool solution(const SetAssignment& x) const {
00257         int selected = 0;
00258         for (CountableSetValues sel2(x.lub, x[3]); sel2();
00259              ++sel2, selected++) {
00260           if (sel2.val() < 0)
00261             return false;
00262         }
00263         CountableSetValues x4v(x.lub, x[4]);
00264         if (selected == 0)
00265           return !x4v();
00266         CountableSetRanges* sel = new CountableSetRanges[selected];
00267         CountableSetValues selector(x.lub, x[3]);
00268         unsigned int cardsum = 0;
00269         for (int i=selected; i--;++selector) {
00270           if (selector.val()>=3 || selector.val()<0) {
00271             delete[] sel;
00272             return false;
00273           }
00274           sel[i].init(x.lub, x[selector.val()]);
00275           CountableSetRanges xicard(x.lub, x[selector.val()]);
00276           cardsum += Iter::Ranges::size(xicard);
00277         }
00278         
00279         bool ret;
00280         FakeSpace* fs = new FakeSpace;
00281         {
00282           Region r(*fs);
00283           Iter::Ranges::NaryUnion u(r, sel, selected);
00284           ret = Iter::Ranges::size(u) == cardsum;
00285           u.reset();
00286           CountableSetRanges z(x.lub, x[4]);
00287           ret &= Iter::Ranges::equal(u, z);
00288         }
00289         delete fs;
00290         delete[] sel;
00291         return ret;
00292       }
00294       virtual void post(Space& home, SetVarArray& x, IntVarArray&) {
00295         SetVarArgs xs(x.size()-2);
00296         for (int i=x.size()-2; i--;)
00297           xs[i]=x[i];
00298         Gecode::element(home, SOT_DUNION, xs, x[x.size()-2], x[x.size()-1]);
00299       }
00300     };
00301     ElementDisjoint _elementdisjoint("Element::Disjoint");
00302 
00304     class ElementSet : public SetTest {
00305     public:
00307       ElementSet(const char* t)
00308         : SetTest(t,4,ds_12,false,true) {}
00310       virtual bool solution(const SetAssignment& x) const {
00311         if (x.intval() < 0 || x.intval() > 2)
00312           return false;
00313         CountableSetRanges z(x.lub, x[3]);
00314         CountableSetRanges y(x.lub, x[x.intval()]);
00315         return Iter::Ranges::equal(y, z);
00316       }
00318       virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
00319         SetVarArgs xs(x.size()-1);
00320         for (int i=x.size()-1; i--;)
00321           xs[i]=x[i];
00322         Gecode::element(home, xs, y[0], x[x.size()-1]);
00323       }
00324     };
00325     ElementSet _elementset("Element::Set");
00326 
00328     class ElementSetConst : public SetTest {
00329     private:
00330       const IntSet i0;
00331       const IntSet i1;
00332       const IntSet i2;
00333     public:
00335       ElementSetConst(const char* t)
00336         : SetTest(t,1,ds_13,false,true), i0(-3,-3), i1(-1,1), i2(0,2) {}
00338       virtual bool solution(const SetAssignment& x) const {
00339         if (x.intval() < 0 || x.intval() > 2)
00340           return false;
00341         CountableSetRanges xr(x.lub, x[0]);
00342         IntSet iss[] = {i0, i1, i2};
00343         IntSetRanges isr(iss[x.intval()]);
00344         return Iter::Ranges::equal(xr, isr);
00345       }
00347       virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
00348         IntSetArgs xs(3);
00349         xs[0] = i0; xs[1] = i1; xs[2] = i2;
00350         Gecode::element(home, xs, y[0], x[0]);
00351       }
00352     };
00353     ElementSetConst _elementsetconst("Element::SetConst");
00354 
00356     class MatrixIntSet : public SetTest {
00357      protected:
00359        Gecode::IntSetArgs tm;
00360      public:
00362        MatrixIntSet(void)
00363          : SetTest("Element::Matrix::IntSet",1,IntSet(0,3),false,2), 
00364            tm(4) {
00365          tm[0]=IntSet(0,0); tm[1]=IntSet(1,1);
00366          tm[2]=IntSet(2,2); tm[3]=IntSet(3,3);
00367        }
00369        virtual bool solution(const SetAssignment& x) const {
00370          // Get integer assignment
00371          const Int::Assignment& y = x.ints();
00372          // x-coordinate: y[0], y-coordinate: y[1], result: x[0]
00373          using namespace Gecode;
00374          if ((y[0] > 1) || (y[1] > 1))
00375            return false;
00376          Matrix<IntSetArgs> m(tm,2,2);
00377          IntSetRanges a(m(y[0],y[1]));
00378          CountableSetRanges b(x.lub, x[0]);
00379          return Iter::Ranges::equal(a,b);
00380        }
00382        virtual void post(Gecode::Space& home, Gecode::SetVarArray& x,
00383                          Gecode::IntVarArray& y) {
00384          // x-coordinate: x[0], y-coordinate: x[1], result: x[2]
00385          using namespace Gecode;
00386          Matrix<IntSetArgs> m(tm,2,2);
00387          element(home, m, y[0], y[1], x[0]);
00388        }
00389      };
00390 
00391     MatrixIntSet _emis;
00392 
00394 
00395 }}}
00396 
00397 // STATISTICS: test-set