Generated on Mon Jul 6 18:09:08 2009 for Gecode by doxygen 1.5.9

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: 2009-05-14 19:35:46 +0200 (Thu, 14 May 2009) $ by $Author: tack $
00011  *     $Revision: 9118 $
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 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         CountableSetRanges* sel = new CountableSetRanges[selected];
00071         CountableSetValues selector(x.lub, x[3]);
00072         for (int i=selected; i--;++selector) {
00073           if (selector.val()>=3 || selector.val()<0) {
00074             delete[] sel;
00075             return false;
00076           }
00077           sel[i].init(x.lub, x[selector.val()]);
00078         }
00079         Iter::Ranges::NaryUnion<CountableSetRanges> u(sel, selected);
00080 
00081         CountableSetRanges z(x.lub, x[4]);
00082         bool ret = Iter::Ranges::equal(u, z);
00083         delete[] sel;
00084         return ret;
00085       }
00087       virtual void post(Space& home, SetVarArray& x, IntVarArray&) {
00088         SetVarArgs xs(x.size()-2);
00089         for (int i=x.size()-2; i--;)
00090           xs[i]=x[i];
00091         Gecode::element(home, SOT_UNION, xs, x[x.size()-2], x[x.size()-1]);
00092       }
00093     };
00094     ElementUnion _elementunion("Element::Union");
00095 
00097     class ElementUnionConst : public SetTest {
00098     private:
00099       const IntSet i0;
00100       const IntSet i1;
00101       const IntSet i2;
00102     public:
00104       ElementUnionConst(const char* t)
00105         : SetTest(t,2,ds_13,false), i0(-3,-3), i1(-1,1), i2(0,2) {}
00107       virtual bool solution(const SetAssignment& x) const {
00108         int selected = 0;
00109         for (CountableSetValues sel2(x.lub, x[0]); sel2();
00110              ++sel2, selected++) {}
00111         CountableSetValues x4v(x.lub, x[1]);
00112         if (selected==0)
00113           return !x4v();
00114         IntSet iss[] = {i0, i1, i2};
00115         IntSetRanges* sel = new IntSetRanges[selected];
00116         CountableSetValues selector(x.lub, x[0]);
00117         for (int i=selected; i--;++selector) {
00118           if (selector.val()>=3 || selector.val()<0) {
00119             delete[] sel;
00120             return false;
00121           }
00122           sel[i].init(iss[selector.val()]);
00123         }
00124         Iter::Ranges::NaryUnion<IntSetRanges> u(sel, selected);
00125 
00126         CountableSetRanges z(x.lub, x[1]);
00127         bool ret = Iter::Ranges::equal(u, z);
00128         delete[] sel;
00129         return ret;
00130       }
00132       virtual void post(Space& home, SetVarArray& x, IntVarArray&) {
00133         IntSetArgs xs(3);
00134         xs[0] = i0; xs[1] = i1; xs[2] = i2;
00135         Gecode::element(home, SOT_UNION, xs, x[0], x[1]);
00136       }
00137     };
00138     ElementUnionConst _elementunionconst("Element::UnionConst");
00139 
00141     class ElementInter : public SetTest {
00142     public:
00144       ElementInter(const char* t)
00145         : SetTest(t,5,ds_12,false) {}
00147       virtual bool solution(const SetAssignment& x) const {
00148         int selected = 0;
00149         for (CountableSetValues sel2(x.lub, x[3]); sel2();
00150              ++sel2, selected++) {}
00151         CountableSetRanges x4r(x.lub, x[4]);
00152         if (selected==0)
00153           return Iter::Ranges::size(x4r)==Gecode::Set::Limits::card;
00154         CountableSetRanges* sel = new CountableSetRanges[selected];
00155         CountableSetValues selector(x.lub, x[3]);
00156         for (int i=selected; i--;++selector) {
00157           if (selector.val()>=3 || selector.val()<0) {
00158             delete[] sel;
00159             return false;
00160           }
00161           sel[i].init(x.lub, x[selector.val()]);
00162         }
00163         Iter::Ranges::NaryInter<CountableSetRanges> u(sel, selected);
00164 
00165         CountableSetRanges z(x.lub, x[4]);
00166         bool ret = Iter::Ranges::equal(u, z);
00167         delete[] sel;
00168         return ret;
00169       }
00171       virtual void post(Space& home, SetVarArray& x, IntVarArray&) {
00172         SetVarArgs xs(x.size()-2);
00173         for (int i=x.size()-2; i--;)
00174           xs[i]=x[i];
00175         Gecode::element(home, SOT_INTER, xs, x[x.size()-2], x[x.size()-1]);
00176       }
00177     };
00178     ElementInter _elementinter("Element::Inter");
00179 
00181     class ElementInterIn : public SetTest {
00182     public:
00184       ElementInterIn(const char* t)
00185         : SetTest(t,5,ds_12,false) {}
00187       virtual bool solution(const SetAssignment& x) const {
00188         int selected = 0;
00189         for (CountableSetValues sel2(x.lub, x[3]); sel2();
00190              ++sel2, selected++) {}
00191         CountableSetRanges x4r(x.lub, x[4]);
00192         if (selected==0)
00193           return Iter::Ranges::size(x4r)==4;
00194         CountableSetRanges* sel = new CountableSetRanges[selected];
00195         CountableSetValues selector(x.lub, x[3]);
00196         for (int i=selected; i--;++selector) {
00197           if (selector.val()>=3 || selector.val()<0) {
00198             delete[] sel;
00199             return false;
00200           }
00201           sel[i].init(x.lub, x[selector.val()]);
00202         }
00203         Iter::Ranges::NaryInter<CountableSetRanges> u(sel, selected);
00204 
00205         CountableSetRanges z(x.lub, x[4]);
00206         bool ret = Iter::Ranges::equal(u, z);
00207         delete[] sel;
00208         return ret;
00209       }
00211       virtual void post(Space& home, SetVarArray& x, IntVarArray&) {
00212         SetVarArgs xs(x.size()-2);
00213         for (int i=x.size()-2; i--;)
00214           xs[i]=x[i];
00215         Gecode::element(home, SOT_INTER, xs, x[x.size()-2], x[x.size()-1], 
00216           ds_12);
00217       }
00218     };
00219     ElementInterIn _elementinterin("Element::InterIn");
00220 
00222     class ElementDisjoint : public SetTest {
00223     public:
00225       ElementDisjoint(const char* t)
00226         : SetTest(t,5,ds_12,false) {}
00228       virtual bool solution(const SetAssignment& x) const {
00229         int selected = 0;
00230         for (CountableSetValues sel2(x.lub, x[3]); sel2();
00231              ++sel2, selected++) {
00232           if (sel2.val() < 0)
00233             return false;
00234         }
00235         CountableSetValues x4v(x.lub, x[4]);
00236         if (selected == 0)
00237           return !x4v();
00238         CountableSetRanges* sel = new CountableSetRanges[selected];
00239         CountableSetValues selector(x.lub, x[3]);
00240         unsigned int cardsum = 0;
00241         for (int i=selected; i--;++selector) {
00242           if (selector.val()>=3 || selector.val()<0) {
00243             delete[] sel;
00244             return false;
00245           }
00246           sel[i].init(x.lub, x[selector.val()]);
00247           CountableSetRanges xicard(x.lub, x[selector.val()]);
00248           cardsum += Iter::Ranges::size(xicard);
00249         }
00250         Iter::Ranges::NaryUnion<CountableSetRanges> u(sel, selected);
00251         Iter::Ranges::Cache<Iter::Ranges::NaryUnion<CountableSetRanges> >
00252           uc(u);
00253         bool ret = Iter::Ranges::size(uc) == cardsum;
00254         uc.reset();
00255         CountableSetRanges z(x.lub, x[4]);
00256         ret &= Iter::Ranges::equal(uc, z);
00257         delete[] sel;
00258         return ret;
00259       }
00261       virtual void post(Space& home, SetVarArray& x, IntVarArray&) {
00262         SetVarArgs xs(x.size()-2);
00263         for (int i=x.size()-2; i--;)
00264           xs[i]=x[i];
00265         Gecode::element(home, SOT_DUNION, xs, x[x.size()-2], x[x.size()-1]);
00266       }
00267     };
00268     ElementDisjoint _elementdisjoint("Element::Disjoint");
00269 
00271     class ElementSet : public SetTest {
00272     public:
00274       ElementSet(const char* t)
00275         : SetTest(t,4,ds_12,false,true) {}
00277       virtual bool solution(const SetAssignment& x) const {
00278         if (x.intval() < 0 || x.intval() > 2)
00279           return false;
00280         CountableSetRanges z(x.lub, x[3]);
00281         CountableSetRanges y(x.lub, x[x.intval()]);
00282         return Iter::Ranges::equal(y, z);
00283       }
00285       virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
00286         SetVarArgs xs(x.size()-1);
00287         for (int i=x.size()-1; i--;)
00288           xs[i]=x[i];
00289         Gecode::element(home, xs, y[0], x[x.size()-1]);
00290       }
00291     };
00292     ElementSet _elementset("Element::Set");
00293 
00295     class ElementSetConst : public SetTest {
00296     private:
00297       const IntSet i0;
00298       const IntSet i1;
00299       const IntSet i2;
00300     public:
00302       ElementSetConst(const char* t)
00303         : SetTest(t,1,ds_13,false,true), i0(-3,-3), i1(-1,1), i2(0,2) {}
00305       virtual bool solution(const SetAssignment& x) const {
00306         if (x.intval() < 0 || x.intval() > 2)
00307           return false;
00308         CountableSetRanges xr(x.lub, x[0]);
00309         IntSet iss[] = {i0, i1, i2};
00310         IntSetRanges isr(iss[x.intval()]);
00311         return Iter::Ranges::equal(xr, isr);
00312       }
00314       virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
00315         IntSetArgs xs(3);
00316         xs[0] = i0; xs[1] = i1; xs[2] = i2;
00317         Gecode::element(home, xs, y[0], x[0]);
00318       }
00319     };
00320     ElementSetConst _elementsetconst("Element::SetConst");
00321 
00323 
00324 }}}
00325 
00326 // STATISTICS: test-set