Generated on Tue Apr 18 10:21:38 2017 for Gecode by doxygen 1.6.3

channel.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-11-03 11:52:07 +0100 (Thu, 03 Nov 2011) $ by $Author: tack $
00011  *     $Revision: 12452 $
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 Channel {
00048 
00054 
00055     static IntSet d1(0,2);
00056     static IntSet d_12(-1,2);
00057 
00058     static IntSet d2(-1,3);
00059     static IntSet d3(0,3);
00060 
00061     static IntSet d4(0,4);
00062 
00063     static IntSet ds_33(-3,3);
00064 
00066     class ChannelSorted : public SetTest {
00067     public:
00069       ChannelSorted(const char* t)
00070         : SetTest(t,1,ds_33,false,3) {}
00072       virtual bool solution(const SetAssignment& x) const {
00073         if (x.ints()[0]>=x.ints()[1] ||
00074             x.ints()[1]>=x.ints()[2])
00075           return false;
00076         CountableSetValues xr(x.lub, x[0]);
00077         if (!xr())
00078           return false;
00079         if (xr.val() != x.ints()[0])
00080           return false;
00081         ++xr;
00082         if (!xr())
00083           return false;
00084         if (xr.val() != x.ints()[1])
00085           return false;
00086         ++xr;
00087         if (!xr())
00088           return false;
00089         if (xr.val() != x.ints()[2])
00090           return false;
00091         ++xr;
00092         if (xr())
00093           return false;
00094         return true;
00095       }
00097       virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
00098         Gecode::channelSorted(home, y, x[0]);
00099       }
00100     };
00101     ChannelSorted _channelSorted("Channel::Sorted");
00102 
00104     class ChannelInt : public SetTest {
00105     private:
00106       int ssize, isize;
00107     public:
00109       ChannelInt(const char* t, const IntSet& d, int _ssize, int _isize)
00110         : SetTest(t,_ssize,d,false,_isize), ssize(_ssize), isize(_isize) {}
00112       virtual bool solution(const SetAssignment& x) const {
00113         for (int i=0; i<isize; i++) {
00114           if (x.ints()[i] < 0 || x.ints()[i] >= ssize)
00115             return false;
00116           Iter::Ranges::Singleton single(i,i);
00117           CountableSetRanges csr(x.lub, x[x.ints()[i]]);
00118           if (!Iter::Ranges::subset(single, csr))
00119             return false;
00120         }
00121         for (int i=0; i<ssize; i++) {
00122           int size = 0;
00123           for (CountableSetValues csv(x.lub, x[i]); csv(); ++csv) {
00124             if (csv.val() < 0 || csv.val() >= isize) return false;
00125             if (x.ints()[csv.val()] != i) return false;
00126             size++;
00127           }
00128         }
00129         return true;
00130       }
00132       virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
00133         Gecode::channel(home, y, x);
00134       }
00135     };
00136 
00137     ChannelInt _channelint1("Channel::Int::1", d2, 2, 3);
00138     ChannelInt _channelint2("Channel::Int::2", d3, 3, 3);
00139 
00141     class ChannelBool : public SetTest {
00142     private:
00143       int isize;
00144     public:
00146       ChannelBool(const char* t, const IntSet& d, int _isize)
00147         : SetTest(t,1,d,false,_isize), isize(_isize) {}
00149       virtual bool solution(const SetAssignment& x) const {
00150         for (int i=0; i<isize; i++) {
00151           if (x.ints()[i] < 0 || x.ints()[i] > 1)
00152             return false;
00153         }
00154         int cur = 0;
00155         for (CountableSetValues csv(x.lub, x[0]); csv(); ++csv) {
00156           if (csv.val() < 0 || csv.val() >= isize) return false;
00157           if (x.ints()[csv.val()] != 1) return false;
00158           for (; cur<csv.val(); cur++)
00159             if (x.ints()[cur] != 0) return false;
00160           cur = csv.val() + 1;
00161         }
00162         for (; cur<isize; cur++)
00163           if (x.ints()[cur] != 0) return false;
00164         return true;
00165       }
00167       virtual void post(Space& home, SetVarArray& x, IntVarArray& y) {
00168         BoolVarArgs b(y.size());
00169         for (int i=y.size(); i--;)
00170           b[i] = channel(home, y[i]);
00171         Gecode::channel(home, b, x[0]);
00172       }
00173     };
00174 
00175     ChannelBool _channelbool1("Channel::Bool::1", d2, 3);
00176     ChannelBool _channelbool2("Channel::Bool::2", d3, 3);
00177     ChannelBool _channelbool3("Channel::Bool::3", d4, 5);
00178 
00180     class ChannelSet : public SetTest {
00181     private:
00182       int _x0size, _x1size;
00183     public:
00185       ChannelSet(const char* t, const IntSet& d, int x0size, int x1size)
00186         : SetTest(t,x0size+x1size,d,false), _x0size(x0size), _x1size(x1size) {}
00188       virtual bool solution(const SetAssignment& x) const {
00189         for (int i=0; i<_x0size; i++) {
00190           CountableSetRanges x0ir(x.lub, x[i]);
00191           IntSet x0is(x0ir);
00192           if (x0is.min() < 0 || x0is.max() >= _x1size)
00193             return false;
00194           for (int j=0; j<_x1size; j++) {
00195             CountableSetRanges x1ir(x.lub, x[_x0size+j]);
00196             IntSet x1is(x1ir);
00197             if (x1is.min() < 0 || x1is.max() >= _x0size)
00198               return false;
00199             bool jInI = x0is.in(j);
00200             bool iInJ = x1is.in(i);
00201             if (jInI != iInJ)
00202               return false;
00203           }
00204         }
00205         return true;
00206       }
00208       virtual void post(Space& home, SetVarArray& x, IntVarArray&) {
00209         SetVarArgs x0(x.slice(0,1,_x0size));
00210         SetVarArgs x1(x.slice(_x0size));
00211         Gecode::channel(home, x0,x1);
00212       }
00213     };
00214 
00215     ChannelSet _channelSet12("Channel::Set::1::2", d1, 2,2);
00216     ChannelSet _channelSet13("Channel::Set::1::3", d1, 2,3);
00217     ChannelSet _channelSet22("Channel::Set::2::2", d3, 2,2);
00218     ChannelSet _channelSet23("Channel::Set::2::3", d3, 2,3);
00219     ChannelSet _channelSet32("Channel::Set::3::2", d_12, 2,2);
00220     ChannelSet _channelSet33("Channel::Set::3::3", d_12, 2,3);
00221 
00222 }}}
00223 
00224 // STATISTICS: test-set