Generated on Tue Apr 18 10:21:39 2017 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  *  Last modified:
00010  *     $Date: 2017-03-15 16:09:31 +0100 (Wed, 15 Mar 2017) $ by $Author: schulte $
00011  *     $Revision: 15582 $
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 Exec {
00046 
00052 
00053      class IntWait : public Test {
00054      protected:
00056        bool sf;
00057      public:
00059        IntWait(int n, bool sf0)
00060          : Test("Wait::Int::"+str(n)+"::"+
00061                 (sf0 ? "std::function" : "funptr"),n,0,n,false), sf(sf0) {}
00063        virtual bool solution(const Assignment& x) const {
00064          for (int i=0; i<x.size(); i++)
00065            for (int j=i+1; j<x.size(); j++)
00066              if (x[i] == x[j])
00067                return false;
00068          return true;
00069        }
00071        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00072          using namespace Gecode;
00073          auto f = static_cast<std::function<void(Space&)>>
00074            ([](Space& home) { c(home); });
00075          if (x.size() > 1) {
00076            if (sf)
00077              Gecode::wait(home, x, f);
00078            else
00079              Gecode::wait(home, x, &c);
00080          } else {
00081            if (sf)
00082              Gecode::wait(home, x[0], f);
00083            else
00084              Gecode::wait(home, x[0], &c);
00085          }
00086        }
00088        static void c(Gecode::Space& _home) {
00089          TestSpace& home = static_cast<TestSpace&>(_home);
00090          for (int i=0; i<home.x.size(); i++)
00091            for (int j=i+1; j<home.x.size(); j++)
00092              if (home.x[i].val() == home.x[j].val())
00093                home.fail();
00094        }
00095      };
00096 
00098      class BoolWait : public Test {
00099      protected:
00101        bool sf;
00102      public:
00104        BoolWait(int n, bool sf0)
00105          : Test("Wait::Bool::"+str(n)+"::"+
00106                 (sf0 ? "std::function" : "funptr"),n,0,1,false), sf(sf0) {}
00108        virtual bool solution(const Assignment& x) const {
00109          int t=0;
00110          for (int i=0; i<x.size(); i++)
00111            t += x[i];
00112          return t==2;
00113        }
00115        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00116          using namespace Gecode;
00117          BoolVarArgs b(x.size());
00118          for (int i=b.size(); i--; )
00119            b[i]=channel(home,x[i]);
00120          auto f = static_cast<std::function<void(Space&)>>
00121            ([](Space& home) { c(home); });
00122          if (b.size() > 1) {
00123            if (sf)
00124              Gecode::wait(home, b, f);
00125            else
00126              Gecode::wait(home, b, &c);
00127          } else {
00128            if (sf)
00129              Gecode::wait(home, b[0], f);
00130            else
00131              Gecode::wait(home, b[0], &c);
00132          }
00133        }
00135        static void c(Gecode::Space& _home) {
00136          TestSpace& home = static_cast<TestSpace&>(_home);
00137          int t=0;
00138          for (int i=0; i<home.x.size(); i++)
00139            t += home.x[i].val();
00140          if (t!=2)
00141            home.fail();
00142        }
00143      };
00144 
00146      class When : public Test {
00147      protected:
00149        bool sf;
00150      public:
00152        When(bool sf0) 
00153          : Test(std::string("When::")+
00154                 (sf0 ? "std::function" : "funptr"),1,0,1,false), sf(sf0) {}
00156        virtual bool solution(const Assignment& x) const {
00157          return x[0]==0;
00158        }
00160        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00161          using namespace Gecode;
00162          if (sf) {
00163            auto sft = static_cast<std::function<void(Space&)>>
00164              ([](Space& home) { t(home); });
00165            auto sfe = static_cast<std::function<void(Space&)>>
00166              ([](Space& home) { e(home); });
00167            when(home, channel(home, x[0]), sft, sfe);
00168          } else {
00169            when(home, channel(home, x[0]), &t, &e);
00170          }
00171        }
00173        static void t(Gecode::Space& home) {
00174          home.fail();
00175        }
00177        static void e(Gecode::Space& home) {
00178          (void) home;
00179        }
00180      };
00181 
00182      IntWait iw1t(1,true), iw2t(2,true), iw3t(3,true), iw4t(4,true);
00183      IntWait iw1f(1,false), iw2f(2,false), iw3f(3,false), iw4f(4,false);
00184      BoolWait bw1t(1,true), bw2t(2,true), bw3t(3,true), bw4t(4,true);
00185      BoolWait bw1f(1,false), bw2f(2,false), bw3f(3,false), bw4f(4,false);
00186 
00187 
00188      When whent(true);
00189      When whenf(false);
00191 
00192    }
00193 
00194 }}
00195 
00196 // STATISTICS: test-int