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

channel.cc

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Christian Schulte <schulte@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Christian Schulte, 2006
00008  *
00009  *  Last modified:
00010  *     $Date: 2008-08-20 18:02:33 +0200 (Wed, 20 Aug 2008) $ by $Author: schulte $
00011  *     $Revision: 7665 $
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/int.hh"
00039 
00040 #include "gecode/minimodel.hh"
00041 
00042 namespace Test { namespace Int {
00043 
00045    namespace Channel {
00046    
00052 
00053      class ChannelFull : public Test {
00054      private:
00055        int xoff; //< Offset for the x variables
00056        int yoff; //< Offset for the y variables
00057      public:
00059        ChannelFull(Gecode::IntConLevel icl,
00060                    unsigned int xoff0 = 0, unsigned int yoff0 = 0)
00061          : Test("Channel::Full::"+str(icl)+"::"+
00062                 str(xoff0)+"::"+str(yoff0),8,0,3,false,icl),
00063            xoff(xoff0), yoff(yoff0) {
00064          testdomcon = false;
00065        }
00067        virtual bool solution(const Assignment& x) const {
00068          for (int i=0; i<4; i++)
00069            if (x[4+x[i]] != i)
00070              return false;
00071          return true;
00072        }
00074        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00075          using namespace Gecode;
00076          IntVarArgs xa(4); IntVarArgs ya(4);
00077          for (int i=4; i--; ) {
00078            if (xoff != 0) {
00079              IntVar xo(home, xoff, 3+xoff);
00080              Gecode::post(home, x[i] == xo-xoff);
00081              xa[i] = xo;
00082            } else {
00083              xa[i] = x[i];             
00084            }
00085            if (yoff != 0) {
00086              IntVar yo(home, yoff, 3+yoff);
00087              Gecode::post(home, x[4+i] == yo-yoff);
00088              ya[i] = yo;
00089            } else {
00090              ya[i] = x[4+i];             
00091            }
00092          }
00093          channel(home, xa, xoff, ya, yoff, icl);
00094        }
00095      };
00096      
00098      class ChannelHalf : public Test {
00099      public:
00101        ChannelHalf(Gecode::IntConLevel icl)
00102          : Test("Channel::Half::"+str(icl),6,0,5,false,icl) {
00103          testdomcon = false;
00104        }
00106        virtual bool solution(const Assignment& x) const {
00107          for (int i=0; i<6; i++)
00108            for (int j=i+1; j<6; j++)
00109              if (x[i] == x[j])
00110                return false;
00111          return true;
00112        }
00114        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00115          using namespace Gecode;
00116          Gecode::IntVarArgs y(6);
00117          for (int i=0; i<6; i++)
00118            y[i].init(home,dom);
00119          for (int i=0; i<6; i++)
00120            for (int j=0; j<6; j++) {
00121              Gecode::BoolVar b(home,0,1);
00122              rel(home, x[i], Gecode::IRT_EQ, j, b);
00123              rel(home, y[j], Gecode::IRT_EQ, i, b);
00124            }
00125          channel(home, x, y, icl);
00126        }
00127      };
00128      
00130      class ChannelShared : public Test {
00131      public:
00133        ChannelShared(Gecode::IntConLevel icl)
00134          : Test("Channel::Shared::"+str(icl),6,0,5,false,icl) {
00135          testdomcon = false;
00136        }
00138        virtual bool solution(const Assignment& x) const {
00139          for (int i=0; i<6; i++)
00140            if (x[x[i]] != i)
00141              return false;
00142          return true;
00143        }
00145        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00146          using namespace Gecode;
00147          channel(home, x, x, icl);
00148        }
00149      };
00150      
00152      class ChannelLinkSingle : public Test {
00153      public:
00155        ChannelLinkSingle(void)
00156          : Test("Channel::Bool::Single",2,-1,2) {
00157          testdomcon = false;
00158        }
00160        virtual bool solution(const Assignment& x) const {
00161          return ((x[0]==0) || (x[0]==1)) && (x[0]==x[1]);
00162        }
00164        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00165          using namespace Gecode;
00166          Gecode::BoolVar b(home,0,1);
00167          channel(home, x[0], b);
00168          channel(home, x[1], b);
00169        }
00170      };
00171      
00173      class ChannelLinkMulti : public Test {
00174      private:
00175        int o;
00176      public:
00178        ChannelLinkMulti(const std::string& s, int min, int max, int o0)
00179          : Test("Channel::Bool::Multi::"+s,7,min,max), o(o0) {
00180          testdomcon = false;
00181        }
00183        virtual bool solution(const Assignment& x) const {
00184          int n = x.size()-1;
00185          for (int i=n; i--; )
00186            if ((x[i] != 0) && (x[i] != 1))
00187              return false;
00188          int k=x[n]-o;
00189          if ((k<0) || (k>=n))
00190            return false;
00191          for (int i=0; i<k; i++)
00192            if (x[i] != 0)
00193              return false;
00194          for (int i=k+1; i<n; i++)
00195            if (x[i] != 0)
00196              return false;
00197          return x[k] == 1;
00198        }
00200        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00201          using namespace Gecode;
00202          int n=x.size()-1;
00203          Gecode::BoolVarArgs b(n);
00204          for (int i=n; i--; )
00205            b[i]=channel(home,x[i]);
00206          channel(home, b, x[n], o);
00207        }
00208      };
00209      
00210    
00211    
00212      ChannelFull cfd(Gecode::ICL_DOM);
00213      ChannelFull cfv(Gecode::ICL_VAL);
00214 
00215      ChannelFull cfd11(Gecode::ICL_DOM,1,1);
00216      ChannelFull cfv11(Gecode::ICL_VAL,1,1);
00217 
00218      ChannelFull cfd35(Gecode::ICL_DOM,3,5);
00219      ChannelFull cfv35(Gecode::ICL_VAL,3,5);
00220    
00221      ChannelHalf chd(Gecode::ICL_DOM);
00222      ChannelHalf chv(Gecode::ICL_VAL);
00223    
00224      ChannelShared csd(Gecode::ICL_DOM);
00225      ChannelShared csv(Gecode::ICL_VAL);
00226    
00227      ChannelLinkSingle cls;
00228    
00229      ChannelLinkMulti clma("A", 0, 5, 0);
00230      ChannelLinkMulti clmb("B", 1, 6, 1);
00231      ChannelLinkMulti clmc("C",-1, 4,-1);
00233    
00234    }
00235 }}
00236 
00237 // STATISTICS: test-int
00238