Generated on Thu Mar 22 10:39:35 2012 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  *     Christian Schulte <schulte@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Christian Schulte, 2006
00008  *
00009  *  Last modified:
00010  *     $Date: 2012-02-22 06:04:20 +0100 (Wed, 22 Feb 2012) $ by $Author: tack $
00011  *     $Revision: 12537 $
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(int xoff0, int yoff0, Gecode::IntConLevel icl)
00060          : Test("Channel::Full::"+str(xoff0)+"::"+str(yoff0)+"::"+str(icl),
00061                 8,0,3,false,icl),
00062            xoff(xoff0), yoff(yoff0) {
00063          contest = CTL_NONE;
00064        }
00066        virtual bool solution(const Assignment& x) const {
00067          for (int i=0; i<4; i++)
00068            if (x[4+x[i]] != i)
00069              return false;
00070          return true;
00071        }
00073        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00074          using namespace Gecode;
00075          IntVarArgs xa(4); IntVarArgs ya(4);
00076          for (int i=4; i--; ) {
00077            if (xoff != 0) {
00078              IntVar xo(home, xoff, 3+xoff);
00079              Gecode::rel(home, x[i] == xo-xoff);
00080              xa[i] = xo;
00081            } else {
00082              xa[i] = x[i];
00083            }
00084            if (yoff != 0) {
00085              IntVar yo(home, yoff, 3+yoff);
00086              Gecode::rel(home, x[4+i] == yo-yoff);
00087              ya[i] = yo;
00088            } else {
00089              ya[i] = x[4+i];
00090            }
00091          }
00092          channel(home, xa, xoff, ya, yoff, icl);
00093        }
00094      };
00095 
00097      class ChannelHalf : public Test {
00098      public:
00100        ChannelHalf(Gecode::IntConLevel icl)
00101          : Test("Channel::Half::"+str(icl),6,0,5,false,icl) {
00102          contest = CTL_NONE;
00103        }
00105        virtual bool solution(const Assignment& x) const {
00106          for (int i=0; i<6; i++)
00107            for (int j=i+1; j<6; j++)
00108              if (x[i] == x[j])
00109                return false;
00110          return true;
00111        }
00113        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00114          using namespace Gecode;
00115          Gecode::IntVarArgs y(home,6,dom);
00116          for (int i=0; i<6; i++)
00117            for (int j=0; j<6; j++) {
00118              Gecode::BoolVar b(home,0,1);
00119              rel(home, x[i], Gecode::IRT_EQ, j, b);
00120              rel(home, y[j], Gecode::IRT_EQ, i, b);
00121            }
00122          channel(home, x, y, icl);
00123        }
00124      };
00125 
00127      class ChannelShared : public Test {
00128      public:
00130        ChannelShared(Gecode::IntConLevel icl)
00131          : Test("Channel::Shared::"+str(icl),6,0,5,false,icl) {
00132          contest = CTL_NONE;
00133        }
00135        virtual bool solution(const Assignment& x) const {
00136          for (int i=0; i<6; i++)
00137            if (x[x[i]] != i)
00138              return false;
00139          return true;
00140        }
00142        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00143          using namespace Gecode;
00144          channel(home, x, x, icl);
00145        }
00146      };
00147 
00149      class ChannelLinkSingle : public Test {
00150      public:
00152        ChannelLinkSingle(void)
00153          : Test("Channel::Bool::Single",2,-1,2) {
00154          contest = CTL_NONE;
00155        }
00157        virtual bool solution(const Assignment& x) const {
00158          return ((x[0]==0) || (x[0]==1)) && (x[0]==x[1]);
00159        }
00161        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00162          using namespace Gecode;
00163          Gecode::BoolVar b(home,0,1);
00164          channel(home, x[0], b);
00165          channel(home, x[1], b);
00166        }
00167      };
00168 
00170      class ChannelLinkMulti : public Test {
00171      private:
00172        int o;
00173      public:
00175        ChannelLinkMulti(const std::string& s, int min, int max, int o0)
00176          : Test("Channel::Bool::Multi::"+s,7,min,max), o(o0) {
00177        }
00179        virtual bool solution(const Assignment& x) const {
00180          int n = x.size()-1;
00181          for (int i=n; i--; )
00182            if ((x[i] != 0) && (x[i] != 1))
00183              return false;
00184          int k=x[n]-o;
00185          if ((k<0) || (k>=n))
00186            return false;
00187          for (int i=0; i<k; i++)
00188            if (x[i] != 0)
00189              return false;
00190          for (int i=k+1; i<n; i++)
00191            if (x[i] != 0)
00192              return false;
00193          return x[k] == 1;
00194        }
00196        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00197          using namespace Gecode;
00198          int n=x.size()-1;
00199          Gecode::BoolVarArgs b(n);
00200          for (int i=n; i--; )
00201            b[i]=channel(home,x[i]);
00202          channel(home, b, x[n], o);
00203        }
00204      };
00205 
00206 
00207 
00208      ChannelFull cfd(0,0,Gecode::ICL_DOM);
00209      ChannelFull cfv(0,0,Gecode::ICL_VAL);
00210 
00211      ChannelFull cfd11(1,1,Gecode::ICL_DOM);
00212      ChannelFull cfv11(1,1,Gecode::ICL_VAL);
00213 
00214      ChannelFull cfd35(3,5,Gecode::ICL_DOM);
00215      ChannelFull cfv35(3,5,Gecode::ICL_VAL);
00216 
00217      ChannelHalf chd(Gecode::ICL_DOM);
00218      ChannelHalf chv(Gecode::ICL_VAL);
00219 
00220      ChannelShared csd(Gecode::ICL_DOM);
00221      ChannelShared csv(Gecode::ICL_VAL);
00222 
00223      ChannelLinkSingle cls;
00224 
00225      ChannelLinkMulti clma("A", 0, 5, 0);
00226      ChannelLinkMulti clmb("B", 1, 6, 1);
00227      ChannelLinkMulti clmc("C",-1, 4,-1);
00229 
00230    }
00231 }}
00232 
00233 // STATISTICS: test-int
00234