Generated on Tue May 22 09:39:46 2018 for Gecode by doxygen 1.6.3

exec.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, 2009
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 "test/int.hh"
00035 
00036 #include <gecode/minimodel.hh>
00037 
00038 namespace Test { namespace Int {
00039 
00041    namespace Exec {
00042 
00048 
00049      class IntWait : public Test {
00050      protected:
00052        bool sf;
00053      public:
00055        IntWait(int n, bool sf0)
00056          : Test("Wait::Int::"+str(n)+"::"+
00057                 (sf0 ? "std::function" : "funptr"),n,0,n,false), sf(sf0) {}
00059        virtual bool solution(const Assignment& x) const {
00060          for (int i=0; i<x.size(); i++)
00061            for (int j=i+1; j<x.size(); j++)
00062              if (x[i] == x[j])
00063                return false;
00064          return true;
00065        }
00067        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00068          using namespace Gecode;
00069          auto f = static_cast<std::function<void(Space&)>>
00070            ([](Space& home) { c(home); });
00071          if (x.size() > 1) {
00072            if (sf)
00073              Gecode::wait(home, x, f);
00074            else
00075              Gecode::wait(home, x, &c);
00076          } else {
00077            if (sf)
00078              Gecode::wait(home, x[0], f);
00079            else
00080              Gecode::wait(home, x[0], &c);
00081          }
00082        }
00084        static void c(Gecode::Space& _home) {
00085          TestSpace& home = static_cast<TestSpace&>(_home);
00086          for (int i=0; i<home.x.size(); i++)
00087            for (int j=i+1; j<home.x.size(); j++)
00088              if (home.x[i].val() == home.x[j].val())
00089                home.fail();
00090        }
00091      };
00092 
00094      class BoolWait : public Test {
00095      protected:
00097        bool sf;
00098      public:
00100        BoolWait(int n, bool sf0)
00101          : Test("Wait::Bool::"+str(n)+"::"+
00102                 (sf0 ? "std::function" : "funptr"),n,0,1,false), sf(sf0) {}
00104        virtual bool solution(const Assignment& x) const {
00105          int t=0;
00106          for (int i=0; i<x.size(); i++)
00107            t += x[i];
00108          return t==2;
00109        }
00111        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00112          using namespace Gecode;
00113          BoolVarArgs b(x.size());
00114          for (int i=b.size(); i--; )
00115            b[i]=channel(home,x[i]);
00116          auto f = static_cast<std::function<void(Space&)>>
00117            ([](Space& home) { c(home); });
00118          if (b.size() > 1) {
00119            if (sf)
00120              Gecode::wait(home, b, f);
00121            else
00122              Gecode::wait(home, b, &c);
00123          } else {
00124            if (sf)
00125              Gecode::wait(home, b[0], f);
00126            else
00127              Gecode::wait(home, b[0], &c);
00128          }
00129        }
00131        static void c(Gecode::Space& _home) {
00132          TestSpace& home = static_cast<TestSpace&>(_home);
00133          int t=0;
00134          for (int i=0; i<home.x.size(); i++)
00135            t += home.x[i].val();
00136          if (t!=2)
00137            home.fail();
00138        }
00139      };
00140 
00142      class When : public Test {
00143      protected:
00145        bool sf;
00146      public:
00148        When(bool sf0) 
00149          : Test(std::string("When::")+
00150                 (sf0 ? "std::function" : "funptr"),1,0,1,false), sf(sf0) {}
00152        virtual bool solution(const Assignment& x) const {
00153          return x[0]==0;
00154        }
00156        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00157          using namespace Gecode;
00158          if (sf) {
00159            auto sft = static_cast<std::function<void(Space&)>>
00160              ([](Space& home) { t(home); });
00161            auto sfe = static_cast<std::function<void(Space&)>>
00162              ([](Space& home) { e(home); });
00163            when(home, channel(home, x[0]), sft, sfe);
00164          } else {
00165            when(home, channel(home, x[0]), &t, &e);
00166          }
00167        }
00169        static void t(Gecode::Space& home) {
00170          home.fail();
00171        }
00173        static void e(Gecode::Space& home) {
00174          (void) home;
00175        }
00176      };
00177 
00178      IntWait iw1t(1,true), iw2t(2,true), iw3t(3,true), iw4t(4,true);
00179      IntWait iw1f(1,false), iw2f(2,false), iw3f(3,false), iw4f(4,false);
00180      BoolWait bw1t(1,true), bw2t(2,true), bw3t(3,true), bw4t(4,true);
00181      BoolWait bw1f(1,false), bw2f(2,false), bw3f(3,false), bw4f(4,false);
00182 
00183 
00184      When whent(true);
00185      When whenf(false);
00187 
00188    }
00189 
00190 }}
00191 
00192 // STATISTICS: test-int