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