Generated on Thu Apr 11 13:58:58 2019 for Gecode by doxygen 1.6.3

dom.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, 2005
00008  *
00009  *  This file is part of Gecode, the generic constraint
00010  *  development environment:
00011  *     http://www.gecode.org
00012  *
00013  *  Permission is hereby granted, free of charge, to any person obtaining
00014  *  a copy of this software and associated documentation files (the
00015  *  "Software"), to deal in the Software without restriction, including
00016  *  without limitation the rights to use, copy, modify, merge, publish,
00017  *  distribute, sublicense, and/or sell copies of the Software, and to
00018  *  permit persons to whom the Software is furnished to do so, subject to
00019  *  the following conditions:
00020  *
00021  *  The above copyright notice and this permission notice shall be
00022  *  included in all copies or substantial portions of the Software.
00023  *
00024  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00028  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00029  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00030  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00031  *
00032  */
00033 
00034 #include <gecode/minimodel.hh>
00035 
00036 #include "test/int.hh"
00037 
00038 namespace Test { namespace Int {
00039 
00041    namespace Dom {
00042 
00048 
00049      class DomInt : public Test {
00050      public:
00052        DomInt(int n)
00053          : Test("Dom::Int::"+str(n),n,-4,4,n == 1,
00054                 Gecode::IPL_DOM) {}
00056        virtual bool solution(const Assignment& x) const {
00057          for (int i=x.size(); i--; )
00058            if (x[i] != -2)
00059              return false;
00060          return true;
00061        }
00063        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00064          if (x.size() == 1)
00065            Gecode::dom(home, x[0], -2);
00066          else
00067            Gecode::dom(home, x, -2);
00068        }
00070        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
00071                          Gecode::Reify r) {
00072          assert(x.size() == 1);
00073          if (Base::rand(2) != 0) {
00074            Gecode::dom(home, x[0], -2, r);
00075          } else {
00076            switch (r.mode()) {
00077            case Gecode::RM_EQV:
00078              Gecode::rel(home, Gecode::dom(x[0], -2) == r.var()); break;
00079            case Gecode::RM_IMP:
00080              Gecode::rel(home, Gecode::dom(x[0], -2) << r.var()); break;
00081            case Gecode::RM_PMI:
00082              Gecode::rel(home, Gecode::dom(x[0], -2) >> r.var()); break;
00083            default: GECODE_NEVER;
00084            }
00085          }
00086        }
00087      };
00088 
00089 
00091      class DomRange : public Test {
00092      public:
00094        DomRange(int n)
00095          : Test("Dom::Range::"+str(n),n,-4,4,n == 1,
00096                 Gecode::IPL_DOM) {}
00098        virtual bool solution(const Assignment& x) const {
00099          for (int i=x.size(); i--; )
00100            if ((x[i] < -2) || (x[i] > 2))
00101              return false;
00102          return true;
00103        }
00105        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00106          if (x.size() == 1)
00107            Gecode::dom(home, x[0], -2, 2);
00108          else
00109            Gecode::dom(home, x, -2, 2);
00110        }
00112        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
00113                          Gecode::Reify r) {
00114          assert(x.size() == 1);
00115          if (Base::rand(2) != 0) {
00116            Gecode::dom(home, x[0], -2, 2, r);
00117          } else {
00118            switch (r.mode()) {
00119            case Gecode::RM_EQV:
00120              Gecode::rel(home, Gecode::dom(x[0], -2, 2) == r.var()); break;
00121            case Gecode::RM_IMP:
00122              Gecode::rel(home, Gecode::dom(x[0], -2, 2) << r.var()); break;
00123            case Gecode::RM_PMI:
00124              Gecode::rel(home, Gecode::dom(x[0], -2, 2) >> r.var()); break;
00125            default: GECODE_NEVER;
00126            }
00127          }
00128        }
00129      };
00130 
00132      class DomRangeEmpty : public Test {
00133      public:
00135        DomRangeEmpty(void) : Test("Dom::Range::Empty",1,-4,4,true) {}
00137        virtual bool solution(const Assignment&) const {
00138          return false;
00139        }
00141        virtual void post(Gecode::Space& home, Gecode::IntVarArray&) {
00142          home.fail();
00143        }
00145        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
00146                          Gecode::Reify r) {
00147          Gecode::dom(home, x[0], 3, 2, r);
00148        }
00149      };
00150 
00151 
00152      const int r[4][2] = {
00153        {-4,-3},{-1,-1},{1,1},{3,5}
00154      };
00155      Gecode::IntSet d(r,4);
00156 
00158      class DomDom : public Test {
00159      public:
00161        DomDom(int n)
00162          : Test("Dom::Dom::"+str(n),n,-6,6,n == 1,
00163                 Gecode::IPL_DOM) {}
00165        virtual bool solution(const Assignment& x) const {
00166          for (int i=x.size(); i--; )
00167            if (!(((x[i] >= -4) && (x[i] <= -3)) ||
00168                  ((x[i] >= -1) && (x[i] <= -1)) ||
00169                  ((x[i] >=  1) && (x[i] <=  1)) ||
00170                  ((x[i] >=  3) && (x[i] <=  5))))
00171              return false;
00172          return true;
00173        }
00175        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00176          if (x.size() == 1)
00177            Gecode::dom(home, x[0], d);
00178          else
00179            Gecode::dom(home, x, d);
00180        }
00182        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
00183                          Gecode::Reify r) {
00184          assert(x.size() == 1);
00185          if (Base::rand(2) != 0) {
00186            Gecode::dom(home, x[0], d, r);
00187          } else {
00188            switch (r.mode()) {
00189            case Gecode::RM_EQV:
00190              Gecode::rel(home, Gecode::dom(x[0], d) == r.var()); break;
00191            case Gecode::RM_IMP:
00192              Gecode::rel(home, Gecode::dom(x[0], d) << r.var()); break;
00193            case Gecode::RM_PMI:
00194              Gecode::rel(home, Gecode::dom(x[0], d) >> r.var()); break;
00195            default: GECODE_NEVER;
00196            }
00197          }
00198        }
00199      };
00200 
00201      DomInt di1(1);
00202      DomInt di3(3);
00203      DomRange dr1(1);
00204      DomRange dr3(3);
00205      DomDom dd1(1);
00206      DomDom dd3(3);
00207      DomRangeEmpty dre;
00209 
00210    }
00211 }}
00212 
00213 // STATISTICS: test-int
00214