Generated on Tue Apr 18 10:21:43 2017 for Gecode by doxygen 1.6.3

float.hpp

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  *     Mikael Lagerkvist <lagerkvist@gecode.org>
00006  *     Vincent Barichard <Vincent.Barichard@univ-angers.fr>
00007  *
00008  *  Copyright:
00009  *     Christian Schulte, 2005
00010  *     Mikael Lagerkvist, 2006
00011  *     Vincent Barichard, 2012
00012  *
00013  *  Last modified:
00014  *     $Date: 2016-04-19 17:19:45 +0200 (Tue, 19 Apr 2016) $ by $Author: schulte $
00015  *     $Revision: 14967 $
00016  *
00017  *  This file is part of Gecode, the generic constraint
00018  *  development environment:
00019  *     http://www.gecode.org
00020  *
00021  *  Permission is hereby granted, free of charge, to any person obtaining
00022  *  a copy of this software and associated documentation files (the
00023  *  "Software"), to deal in the Software without restriction, including
00024  *  without limitation the rights to use, copy, modify, merge, publish,
00025  *  distribute, sublicense, and/or sell copies of the Software, and to
00026  *  permit persons to whom the Software is furnished to do so, subject to
00027  *  the following conditions:
00028  *
00029  *  The above copyright notice and this permission notice shall be
00030  *  included in all copies or substantial portions of the Software.
00031  *
00032  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00033  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00034  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00035  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00036  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00037  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00038  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00039  *
00040  */
00041 
00042 namespace Test { namespace Float {
00043 
00044   /*
00045    * Assignments
00046    *
00047    */
00048   inline
00049   Assignment::Assignment(int n0, const Gecode::FloatVal& d0)
00050     : n(n0), d(d0) {}
00051   inline int
00052   Assignment::size(void) const {
00053     return n;
00054   }
00055   inline
00056   Assignment::~Assignment(void) {}
00057 
00058   inline
00059   CpltAssignment::CpltAssignment(int n, const Gecode::FloatVal& d, Gecode::FloatNum s)
00060     : Assignment(n,d),
00061       dsv(new Gecode::FloatVal[static_cast<unsigned int>(n)]),
00062       step(s) {
00063     using namespace Gecode;
00064     for (int i=n; i--; )
00065       dsv[i] = FloatVal(d.min(),nextafter(d.min(),d.max()));
00066   }
00067   inline bool
00068   CpltAssignment::operator()(void) const {
00069     return dsv[0].min() <= d.max();
00070   }
00071   inline Gecode::FloatVal
00072   CpltAssignment::operator[](int i) const {
00073     assert((i>=0) && (i<n));
00074     return dsv[i];
00075   }
00076   inline void
00077   CpltAssignment::set(int i, const Gecode::FloatVal& val) {
00078     assert((i>=0) && (i<n));
00079     dsv[i] = val;
00080   }
00081   inline
00082   CpltAssignment::~CpltAssignment(void) {
00083     delete [] dsv;
00084   }
00085 
00086   inline
00087   ExtAssignment::ExtAssignment(int n, const Gecode::FloatVal& d, Gecode::FloatNum s, const Test * pb)
00088   : Assignment(n,d),curPb(pb),
00089   dsv(new Gecode::FloatVal[static_cast<unsigned int>(n)]),
00090   step(s) {
00091     using namespace Gecode;
00092     for (int i=n-1; i--; )
00093       dsv[i] = FloatVal(d.min(),nextafter(d.min(),d.max()));
00094     ++(*this);
00095   }
00096   inline bool
00097   ExtAssignment::operator()(void) const {
00098     return dsv[0].min() <= d.max();
00099   }
00100   inline Gecode::FloatVal
00101   ExtAssignment::operator[](int i) const {
00102     assert((i>=0) && (i<n));
00103     return dsv[i];
00104   }
00105   inline void
00106   ExtAssignment::set(int i, const Gecode::FloatVal& val) {
00107     assert((i>=0) && (i<n));
00108     dsv[i] = val;
00109   }
00110   inline
00111   ExtAssignment::~ExtAssignment(void) {
00112     delete [] dsv;
00113   }
00114 
00115   forceinline Gecode::FloatNum
00116   RandomAssignment::randval(void) {
00117     using namespace Gecode;
00118     using namespace Gecode::Float;
00119     Rounding r;
00120     return
00121       r.add_down(
00122         d.min(),
00123         r.mul_down(
00124           r.div_down(
00125             Base::rand(static_cast<unsigned int>(Gecode::Int::Limits::max)),
00126             static_cast<FloatNum>(Gecode::Int::Limits::max)
00127           ),
00128           r.sub_down(d.max(),d.min())
00129         )
00130       );
00131   }
00132 
00133   inline
00134   RandomAssignment::RandomAssignment(int n, const Gecode::FloatVal& d, int a0)
00135     : Assignment(n,d), vals(new Gecode::FloatVal[n]), a(a0) {
00136     for (int i=n; i--; )
00137       vals[i] = randval();
00138   }
00139 
00140   inline bool
00141   RandomAssignment::operator()(void) const {
00142     return a>0;
00143   }
00144   inline Gecode::FloatVal
00145   RandomAssignment::operator[](int i) const {
00146     assert((i>=0) && (i<n));
00147     return vals[i];
00148   }
00149   inline void
00150   RandomAssignment::set(int i, const Gecode::FloatVal& val) {
00151     assert((i>=0) && (i<n));
00152     vals[i] = val;
00153   }
00154   inline
00155   RandomAssignment::~RandomAssignment(void) {
00156     delete [] vals;
00157   }
00158 
00159   /*
00160    * Tests with float constraints
00161    *
00162    */
00163   forceinline bool
00164   Test::eqv(void) const {
00165     return reified && ((rms & (1 << Gecode::RM_EQV)) != 0);
00166   }
00167   forceinline bool
00168   Test::imp(void) const {
00169     return reified && ((rms & (1 << Gecode::RM_IMP)) != 0);
00170   }
00171   forceinline bool
00172   Test::pmi(void) const {
00173     return reified && ((rms & (1 << Gecode::RM_PMI)) != 0);
00174   }
00175   inline
00176   Test::Test(const std::string& s, int a, const Gecode::FloatVal& d,
00177              Gecode::FloatNum st, AssignmentType at,
00178              bool r)
00179     : Base("Float::"+s), arity(a), dom(d), step(st), assigmentType(at),
00180       reified(r), rms((1 << Gecode::RM_EQV) ||
00181                       (1 << Gecode::RM_IMP) ||
00182                       (1 << Gecode::RM_PMI)),
00183       testsearch(true), testfix(true), testsubsumed(true) {}
00184 
00185   inline
00186   Test::Test(const std::string& s, int a, Gecode::FloatNum min,
00187              Gecode::FloatNum max, Gecode::FloatNum st, AssignmentType at,
00188              bool r)
00189       : Base("Float::"+s), arity(a), dom(min,max), step(st),
00190         assigmentType(at), reified(r),
00191         rms((1 << Gecode::RM_EQV) ||
00192             (1 << Gecode::RM_IMP) ||
00193             (1 << Gecode::RM_PMI)),
00194         testsearch(true), testfix(true), testsubsumed(true) {}
00195 
00196   inline
00197   std::string
00198   Test::str(Gecode::FloatRelType frt) {
00199     using namespace Gecode;
00200     switch (frt) {
00201     case FRT_EQ: return "Eq";
00202     case FRT_NQ: return "Nq";
00203     case FRT_LQ: return "Lq";
00204     case FRT_LE: return "Le";
00205     case FRT_GQ: return "Gq";
00206     case FRT_GR: return "Gr";
00207     default: ;
00208     }
00209     GECODE_NEVER;
00210     return "NONE";
00211   }
00212 
00213   inline
00214   std::string
00215   Test::str(Gecode::FloatNum f) {
00216     std::stringstream s;
00217     s << f;
00218     return s.str();
00219   }
00220 
00221   inline
00222   std::string
00223   Test::str(Gecode::FloatVal f) {
00224     std::stringstream s;
00225     s << "[" << f.min() << ":" << f.max() << "]";
00226     return s.str();
00227   }
00228 
00229   inline
00230   std::string
00231   Test::str(const Gecode::FloatValArgs& x) {
00232     std::string s = "";
00233     for (int i=0; i<x.size()-1; i++)
00234       s += str(x[i]) + ",";
00235     return "[" + s + str(x[x.size()-1]) + "]";
00236   }
00237 
00238   inline MaybeType
00239   Test::cmp(Gecode::FloatVal x, Gecode::FloatRelType r, Gecode::FloatVal y) {
00240     using namespace Gecode;
00241     switch (r) {
00242     case FRT_EQ:
00243       if (x == y) return MT_TRUE;
00244       if (x != y) return MT_FALSE;
00245       break;
00246     case FRT_NQ:
00247       if (x != y) return MT_TRUE;
00248       if (x == y) return MT_FALSE;
00249       break;
00250     case FRT_LQ:
00251       if (x <= y) return MT_TRUE;
00252       if (x > y) return MT_FALSE;
00253       break;
00254     case FRT_LE:
00255       if (x < y) return MT_TRUE;
00256       if (x >= y) return MT_FALSE;
00257       break;
00258     case FRT_GQ:
00259       if (x >= y) return MT_TRUE;
00260       if (x < y) return MT_FALSE;
00261       break;
00262     case FRT_GR:
00263       if (x > y) return MT_TRUE;
00264       if (x <= y) return MT_FALSE;
00265       break;
00266     default: ;
00267     }
00268     return MT_MAYBE;
00269   }
00270 
00271   inline MaybeType
00272   Test::eq(Gecode::FloatVal x, Gecode::FloatVal y) {
00273     return cmp(x, Gecode::FRT_EQ, y);
00274   }
00275 
00276   inline bool
00277   Test::flip(void) {
00278     return Base::rand(2U) == 0U;
00279   }
00280 
00281   inline MaybeType
00282   operator &(MaybeType a, MaybeType b) {
00283     switch (a) {
00284     case MT_TRUE:  return b;
00285     case MT_FALSE: return MT_FALSE;
00286     default: ;
00287     }
00288     return (b == MT_FALSE) ? MT_FALSE : MT_MAYBE;
00289   }
00290 
00291 
00292   inline
00293   FloatRelTypes::FloatRelTypes(void)
00294     : i(sizeof(frts)/sizeof(Gecode::FloatRelType)-1) {}
00295   inline void
00296   FloatRelTypes::reset(void) {
00297     i = sizeof(frts)/sizeof(Gecode::FloatRelType)-1;
00298   }
00299   inline bool
00300   FloatRelTypes::operator()(void) const {
00301     return i>=0;
00302   }
00303   inline void
00304   FloatRelTypes::operator++(void) {
00305     i--;
00306   }
00307   inline Gecode::FloatRelType
00308   FloatRelTypes::frt(void) const {
00309     return frts[i];
00310   }
00311 
00312 }}
00313 
00314 // STATISTICS: test-float
00315