Generated on Fri Oct 19 11:24:55 2018 for Gecode by doxygen 1.6.3

linear.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  *     Vincent Barichard <Vincent.Barichard@univ-angers.fr>
00006  *
00007  *  Copyright:
00008  *     Christian Schulte, 2005
00009  *     Vincent Barichard, 2012
00010  *
00011  *  This file is part of Gecode, the generic constraint
00012  *  development environment:
00013  *     http://www.gecode.org
00014  *
00015  *  Permission is hereby granted, free of charge, to any person obtaining
00016  *  a copy of this software and associated documentation files (the
00017  *  "Software"), to deal in the Software without restriction, including
00018  *  without limitation the rights to use, copy, modify, merge, publish,
00019  *  distribute, sublicense, and/or sell copies of the Software, and to
00020  *  permit persons to whom the Software is furnished to do so, subject to
00021  *  the following conditions:
00022  *
00023  *  The above copyright notice and this permission notice shall be
00024  *  included in all copies or substantial portions of the Software.
00025  *
00026  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00027  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00028  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00029  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00030  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00031  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00032  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00033  *
00034  */
00035 
00036 #include "test/float.hh"
00037 
00038 #include <gecode/minimodel.hh>
00039 
00040 namespace Test { namespace Float {
00041 
00043    namespace Linear {
00044 
00046      bool one(const Gecode::FloatValArgs& a) {
00047       for (int i=a.size(); i--; )
00048         if (a[i] != 1)
00049           return false;
00050       return true;
00051     }
00052 
00058 
00059      class FloatFloat : public Test {
00060      protected:
00062        Gecode::FloatValArgs a;
00064        Gecode::FloatRelType frt;
00066        Gecode::FloatNum c;
00067      public:
00069        FloatFloat(const std::string& s, const Gecode::FloatVal& d,
00070                   const Gecode::FloatValArgs& a0, Gecode::FloatRelType frt0,
00071                   Gecode::FloatNum c0, Gecode::FloatNum st)
00072          : Test("Linear::Float::"+
00073                 str(frt0)+"::"+s+"::"+str(c0)+"::"
00074                 +str(a0.size()),
00075                 a0.size(),d,st,CPLT_ASSIGNMENT,true),
00076            a(a0), frt(frt0), c(c0) {
00077          using namespace Gecode;
00078          testfix = false;
00079          if ((frt == FRT_NQ) || (frt == FRT_LE) || (frt == FRT_GR) || reified)
00080            testsubsumed = false;
00081        }
00083        virtual MaybeType solution(const Assignment& x) const {
00084          Gecode::FloatVal e = 0.0;
00085          for (int i=x.size(); i--; )
00086            e += a[i]*x[i];
00087          switch (cmp(e, frt, Gecode::FloatVal(c))) {
00088          case MT_FALSE: {
00089            Gecode::FloatVal eError = e;
00090            for (int i=x.size(); i--; )
00091              eError -= a[i]*x[i];
00092            if (cmp(e+eError, frt, Gecode::FloatVal(c)) == MT_FALSE)
00093              return MT_FALSE;
00094            else
00095              return MT_MAYBE;
00096          }
00097          case MT_TRUE:
00098            return MT_TRUE;
00099          case MT_MAYBE:
00100            return MT_MAYBE;
00101          }
00102          GECODE_NEVER;
00103          return MT_FALSE;
00104        }
00106        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
00107          if (one(a))
00108            Gecode::linear(home, x, frt, c);
00109          else
00110            Gecode::linear(home, a, x, frt, c);
00111        }
00113        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x,
00114                          Gecode::Reify r) {
00115          if (one(a))
00116            Gecode::linear(home, x, frt, c, r);
00117          else
00118            Gecode::linear(home, a, x, frt, c, r);
00119        }
00120      };
00121 
00123      class FloatVar : public Test {
00124      protected:
00126        Gecode::FloatValArgs a;
00128        Gecode::FloatRelType frt;
00129      public:
00131        FloatVar(const std::string& s, const Gecode::FloatVal& d,
00132                 const Gecode::FloatValArgs& a0, Gecode::FloatRelType frt0, Gecode::FloatNum st)
00133          : Test("Linear::Var::"+
00134                 str(frt0)+"::"+s+"::"+str(a0.size()),
00135                 a0.size()+1,d,st,CPLT_ASSIGNMENT,true),
00136            a(a0), frt(frt0) {
00137          using namespace Gecode;
00138          testfix = false;
00139          if ((frt == FRT_NQ) || (frt == FRT_LE) || (frt == FRT_GR) || reified)
00140            testsubsumed = false;
00141        }
00143        virtual MaybeType solution(const Assignment& x) const {
00144          Gecode::FloatVal e = 0.0;
00145          for (int i=a.size(); i--; )
00146            e += a[i]*x[i];
00147          switch (cmp(e, frt, x[a.size()])) {
00148          case MT_FALSE: {
00149            Gecode::FloatVal eError = e;
00150            for (int i=a.size(); i--; )
00151              eError -= a[i]*x[i];
00152            if (cmp(e+eError, frt, x[a.size()]) == MT_FALSE)
00153              return MT_FALSE;
00154            else
00155              return MT_MAYBE;
00156          }
00157          case MT_TRUE:
00158            return MT_TRUE;
00159          case MT_MAYBE:
00160            return MT_MAYBE;
00161          }
00162          GECODE_NEVER;
00163          return MT_FALSE;
00164        }
00166        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
00167          int n = a.size();
00168          Gecode::FloatVarArgs y(n);
00169          for (int i=n; i--; )
00170            y[i] = x[i];
00171          if (one(a))
00172            Gecode::linear(home, y, frt, x[n]);
00173          else
00174            Gecode::linear(home, a, y, frt, x[n]);
00175        }
00177        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x,
00178                          Gecode::Reify r) {
00179          int n = a.size();
00180          Gecode::FloatVarArgs y(n);
00181          for (int i=n; i--; )
00182            y[i] = x[i];
00183          if (one(a))
00184            Gecode::linear(home, y, frt, x[n], r);
00185          else
00186            Gecode::linear(home, a, y, frt, x[n], r);
00187        }
00188      };
00189 
00191      class Create {
00192      public:
00194        Create(void) {
00195          using namespace Gecode;
00196          {
00197            FloatNum step = 0.7;
00198            FloatVal f1(-2,2);
00199            FloatVal f2(-3,-1);
00200            FloatVal f3(3,8);
00201 
00202            FloatValArgs a1(1);
00203            a1[0] = 0.0;
00204 
00205            for (FloatRelTypes frts; frts(); ++frts) {
00206              (void) new FloatFloat("11",f1,a1,frts.frt(),0.0,step);
00207              (void) new FloatVar("11",f1,a1,frts.frt(),step);
00208              (void) new FloatFloat("21",f2,a1,frts.frt(),0.0,step);
00209              (void) new FloatVar("21",f2,a1,frts.frt(),step);
00210              (void) new FloatFloat("31",f3,a1,frts.frt(),1.0,step);
00211            }
00212 
00213            const FloatVal av2[4] = {1.0,1.0,1.0,1.0};
00214            const FloatVal av3[4] = {1.0,-1.0,-1.0,1.0};
00215            const FloatVal av4[4] = {2.0,3.0,5.0,7.0};
00216            const FloatVal av5[4] = {-2.0,3.0,-5.0,7.0};
00217 
00218            for (int i=1; i<=4; i++) {
00219              FloatValArgs a2(i, av2);
00220              FloatValArgs a3(i, av3);
00221              FloatValArgs a4(i, av4);
00222              FloatValArgs a5(i, av5);
00223              for (FloatRelTypes frts; frts(); ++frts) {
00224                (void) new FloatFloat("12",f1,a2,frts.frt(),0.0,step);
00225                (void) new FloatFloat("13",f1,a3,frts.frt(),0.0,step);
00226                (void) new FloatFloat("14",f1,a4,frts.frt(),0.0,step);
00227                (void) new FloatFloat("15",f1,a5,frts.frt(),0.0,step);
00228                (void) new FloatFloat("22",f2,a2,frts.frt(),0.0,step);
00229                (void) new FloatFloat("23",f2,a3,frts.frt(),0.0,step);
00230                (void) new FloatFloat("24",f2,a4,frts.frt(),0.0,step);
00231                (void) new FloatFloat("25",f2,a5,frts.frt(),0.0,step);
00232                (void) new FloatFloat("32",f3,a2,frts.frt(),1.0,step);
00233                if (i < 4) {
00234                  (void) new FloatVar("12",f1,a2,frts.frt(),step);
00235                  (void) new FloatVar("13",f1,a3,frts.frt(),step);
00236                  (void) new FloatVar("14",f1,a4,frts.frt(),step);
00237                  (void) new FloatVar("15",f1,a5,frts.frt(),step);
00238                  (void) new FloatVar("22",f2,a2,frts.frt(),step);
00239                  (void) new FloatVar("23",f2,a3,frts.frt(),step);
00240                  (void) new FloatVar("24",f2,a4,frts.frt(),step);
00241                  (void) new FloatVar("25",f2,a5,frts.frt(),step);
00242                }
00243              }
00244            }
00245          }
00246        }
00247      };
00248 
00249      Create c;
00251 
00252    }
00253 }}
00254 
00255 // STATISTICS: test-float