float.hpp
Go to the documentation of this file.
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christian Schulte <schulte@gecode.org>
* Mikael Lagerkvist <lagerkvist@gecode.org>
* Vincent Barichard <Vincent.Barichard@univ-angers.fr>
*
* Copyright:
* Christian Schulte, 2005
* Mikael Lagerkvist, 2006
* Vincent Barichard, 2012
*
* This file is part of Gecode, the generic constraint
* development environment:
* http://www.gecode.org
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
namespace Test { namespace Float {
/*
* Assignments
*
*/
inline
Assignment::Assignment(int n0, const Gecode::FloatVal& d0)
: n(n0), d(d0) {}
inline int
Assignment::size(void) const {
return n;
}
inline
Assignment::~Assignment(void) {}
inline
CpltAssignment::CpltAssignment(int n, const Gecode::FloatVal& d, Gecode::FloatNum s)
: Assignment(n,d),
dsv(new Gecode::FloatVal[static_cast<unsigned int>(n)]),
step(s) {
using namespace Gecode;
for (int i=n; i--; )
dsv[i] = FloatVal(d.min(),nextafter(d.min(),d.max()));
}
inline bool
CpltAssignment::operator()(void) const {
return dsv[0].min() <= d.max();
}
inline Gecode::FloatVal
CpltAssignment::operator[](int i) const {
assert((i>=0) && (i<n));
return dsv[i];
}
inline void
CpltAssignment::set(int i, const Gecode::FloatVal& val) {
assert((i>=0) && (i<n));
dsv[i] = val;
}
inline
CpltAssignment::~CpltAssignment(void) {
delete [] dsv;
}
inline
ExtAssignment::ExtAssignment(int n, const Gecode::FloatVal& d, Gecode::FloatNum s, const Test * pb)
: Assignment(n,d),curPb(pb),
dsv(new Gecode::FloatVal[static_cast<unsigned int>(n)]),
step(s) {
using namespace Gecode;
for (int i=n-1; i--; )
dsv[i] = FloatVal(d.min(),nextafter(d.min(),d.max()));
++(*this);
}
inline bool
ExtAssignment::operator()(void) const {
return dsv[0].min() <= d.max();
}
inline Gecode::FloatVal
ExtAssignment::operator[](int i) const {
assert((i>=0) && (i<n));
return dsv[i];
}
inline void
ExtAssignment::set(int i, const Gecode::FloatVal& val) {
assert((i>=0) && (i<n));
dsv[i] = val;
}
inline
ExtAssignment::~ExtAssignment(void) {
delete [] dsv;
}
forceinline Gecode::FloatNum
RandomAssignment::randval(void) {
using namespace Gecode;
using namespace Gecode::Float;
Rounding r;
return
r.add_down(
d.min(),
r.mul_down(
r.div_down(
Base::rand(static_cast<unsigned int>(Gecode::Int::Limits::max)),
static_cast<FloatNum>(Gecode::Int::Limits::max)
),
r.sub_down(d.max(),d.min())
)
);
}
inline
RandomAssignment::RandomAssignment(int n, const Gecode::FloatVal& d, int a0)
: Assignment(n,d), vals(new Gecode::FloatVal[n]), a(a0) {
for (int i=n; i--; )
vals[i] = randval();
}
inline bool
RandomAssignment::operator()(void) const {
return a>0;
}
inline Gecode::FloatVal
RandomAssignment::operator[](int i) const {
assert((i>=0) && (i<n));
return vals[i];
}
inline void
RandomAssignment::set(int i, const Gecode::FloatVal& val) {
assert((i>=0) && (i<n));
vals[i] = val;
}
inline
RandomAssignment::~RandomAssignment(void) {
delete [] vals;
}
/*
* Tests with float constraints
*
*/
forceinline bool
Test::eqv(void) const {
return reified && ((rms & (1 << Gecode::RM_EQV)) != 0);
}
forceinline bool
Test::imp(void) const {
return reified && ((rms & (1 << Gecode::RM_IMP)) != 0);
}
forceinline bool
Test::pmi(void) const {
return reified && ((rms & (1 << Gecode::RM_PMI)) != 0);
}
inline
Test::Test(const std::string& s, int a, const Gecode::FloatVal& d,
Gecode::FloatNum st, AssignmentType at,
bool r)
: Base("Float::"+s), arity(a), dom(d), step(st), assigmentType(at),
reified(r), rms((1 << Gecode::RM_EQV) |
(1 << Gecode::RM_IMP) |
(1 << Gecode::RM_PMI)),
testsearch(true), testfix(true), testsubsumed(true) {}
inline
Test::Test(const std::string& s, int a, Gecode::FloatNum min,
Gecode::FloatNum max, Gecode::FloatNum st, AssignmentType at,
bool r)
: Base("Float::"+s), arity(a), dom(min,max), step(st),
assigmentType(at), reified(r),
rms((1 << Gecode::RM_EQV) |
(1 << Gecode::RM_IMP) |
(1 << Gecode::RM_PMI)),
testsearch(true), testfix(true), testsubsumed(true) {}
inline
std::string
Test::str(Gecode::FloatRelType frt) {
using namespace Gecode;
switch (frt) {
case FRT_EQ: return "Eq";
case FRT_NQ: return "Nq";
case FRT_LQ: return "Lq";
case FRT_LE: return "Le";
case FRT_GQ: return "Gq";
case FRT_GR: return "Gr";
default: ;
}
GECODE_NEVER;
return "NONE";
}
inline
std::string
Test::str(Gecode::FloatNum f) {
std::stringstream s;
s << f;
return s.str();
}
inline
std::string
Test::str(Gecode::FloatVal f) {
std::stringstream s;
s << "[" << f.min() << ":" << f.max() << "]";
return s.str();
}
inline
std::string
Test::str(const Gecode::FloatValArgs& x) {
std::string s = "";
for (int i=0; i<x.size()-1; i++)
s += str(x[i]) + ",";
return "[" + s + str(x[x.size()-1]) + "]";
}
inline MaybeType
Test::cmp(Gecode::FloatVal x, Gecode::FloatRelType r, Gecode::FloatVal y) {
using namespace Gecode;
switch (r) {
case FRT_EQ:
if (x == y) return MT_TRUE;
if (x != y) return MT_FALSE;
break;
case FRT_NQ:
if (x != y) return MT_TRUE;
if (x == y) return MT_FALSE;
break;
case FRT_LQ:
if (x <= y) return MT_TRUE;
if (x > y) return MT_FALSE;
break;
case FRT_LE:
if (x < y) return MT_TRUE;
if (x >= y) return MT_FALSE;
break;
case FRT_GQ:
if (x >= y) return MT_TRUE;
if (x < y) return MT_FALSE;
break;
case FRT_GR:
if (x > y) return MT_TRUE;
if (x <= y) return MT_FALSE;
break;
default: ;
}
return MT_MAYBE;
}
inline MaybeType
Test::eq(Gecode::FloatVal x, Gecode::FloatVal y) {
return cmp(x, Gecode::FRT_EQ, y);
}
inline bool
Test::flip(void) {
return Base::rand(2U) == 0U;
}
inline MaybeType
operator &(MaybeType a, MaybeType b) {
switch (a) {
case MT_TRUE: return b;
case MT_FALSE: return MT_FALSE;
default: ;
}
return (b == MT_FALSE) ? MT_FALSE : MT_MAYBE;
}
inline
FloatRelTypes::FloatRelTypes(void)
: i(sizeof(frts)/sizeof(Gecode::FloatRelType)-1) {}
inline void
FloatRelTypes::reset(void) {
i = sizeof(frts)/sizeof(Gecode::FloatRelType)-1;
}
inline bool
FloatRelTypes::operator()(void) const {
return i>=0;
}
inline void
FloatRelTypes::operator++(void) {
i--;
}
inline Gecode::FloatRelType
FloatRelTypes::frt(void) const {
return frts[i];
}
}}
// STATISTICS: test-float
Gecode::ArgArrayBase::size
int size(void) const
Return size of array (number of elements).
Definition array.hpp:1607
Gecode::FloatValArgs
Passing float arguments.
Definition float.hh:950
Gecode::FloatVal
Float value type.
Definition float.hh:334
Gecode::Float::Rounding
Floating point rounding policy.
Definition float.hh:154
Test::Base::rand
static Gecode::Support::RandomGenerator rand
Random number generator.
Definition test.hh:134
Test::Base::Base
Base(const std::string &s)
Create and register test with name s.
Definition test.cpp:59
Test::Float::Assignment::n
int n
Number of variables.
Definition float.hh:82
Test::Float::Assignment::Assignment
Assignment(int n0, const Gecode::FloatVal &d0)
Initialize assignments for n0 variables and values d0.
Definition float.hpp:45
Test::Float::Assignment::d
Gecode::FloatVal d
Domain for each variable.
Definition float.hh:83
Test::Float::Assignment::size
int size(void) const
Return number of variables.
Definition float.hpp:48
Test::Float::Assignment::~Assignment
virtual ~Assignment(void)
Destructor.
Definition float.hpp:52
Test::Float::CpltAssignment::~CpltAssignment
virtual ~CpltAssignment(void)
Destructor.
Definition float.hpp:78
Test::Float::CpltAssignment::operator[]
virtual Gecode::FloatVal operator[](int i) const
Return value for variable i.
Definition float.hpp:68
Test::Float::CpltAssignment::step
Gecode::FloatNum step
Step for next assignment.
Definition float.hh:105
Test::Float::CpltAssignment::dsv
Gecode::FloatVal * dsv
Iterator for each variable.
Definition float.hh:104
Test::Float::CpltAssignment::set
virtual void set(int i, const Gecode::FloatVal &val)
Set assignment to value val for variable i.
Definition float.hpp:73
Test::Float::CpltAssignment::operator()
virtual bool operator()(void) const
Test whether all assignments have been iterated.
Definition float.hpp:64
Test::Float::CpltAssignment::CpltAssignment
CpltAssignment(int n, const Gecode::FloatVal &d, Gecode::FloatNum s)
Initialize assignments for n variables and values d with step s.
Definition float.hpp:55
Test::Float::ExtAssignment::ExtAssignment
ExtAssignment(int n, const Gecode::FloatVal &d, Gecode::FloatNum s, const Test *pb)
Initialize assignments for n variables and values d with step s.
Definition float.hpp:83
Test::Float::ExtAssignment::~ExtAssignment
virtual ~ExtAssignment(void)
Destructor.
Definition float.hpp:107
Test::Float::ExtAssignment::set
virtual void set(int i, const Gecode::FloatVal &val)
Set assignment to value val for variable i.
Definition float.hpp:102
Test::Float::ExtAssignment::dsv
Gecode::FloatVal * dsv
Iterator for each variable.
Definition float.hh:125
Test::Float::ExtAssignment::operator[]
virtual Gecode::FloatVal operator[](int i) const
Return value for variable i.
Definition float.hpp:97
Test::Float::ExtAssignment::step
Gecode::FloatNum step
Step for next assignment.
Definition float.hh:126
Test::Float::ExtAssignment::curPb
const Test * curPb
Current problem used to complete assignment.
Definition float.hh:124
Test::Float::ExtAssignment::operator()
virtual bool operator()(void) const
Test whether all assignments have been iterated.
Definition float.hpp:93
Test::Float::FloatRelTypes::operator++
void operator++(void)
Increment to next relation type.
Definition float.hpp:300
Test::Float::FloatRelTypes::frt
Gecode::FloatRelType frt(void) const
Return current relation type.
Definition float.hpp:304
Test::Float::FloatRelTypes::FloatRelTypes
FloatRelTypes(void)
Initialize iterator.
Definition float.hpp:289
Test::Float::FloatRelTypes::operator()
bool operator()(void) const
Test whether iterator is done.
Definition float.hpp:296
Test::Float::FloatRelTypes::reset
void reset(void)
Reset iterator.
Definition float.hpp:292
Test::Float::RandomAssignment::RandomAssignment
RandomAssignment(int n, const Gecode::FloatVal &d, int a)
Initialize for a assignments for n variables and values d.
Definition float.hpp:130
Test::Float::RandomAssignment::operator[]
virtual Gecode::FloatVal operator[](int i) const
Return value for variable i.
Definition float.hpp:141
Test::Float::RandomAssignment::set
virtual void set(int i, const Gecode::FloatVal &val)
Set assignment to value val for variable i.
Definition float.hpp:146
Test::Float::RandomAssignment::randval
Gecode::FloatNum randval(void)
Definition float.hpp:112
Test::Float::RandomAssignment::vals
Gecode::FloatVal * vals
The current values for the variables.
Definition float.hh:146
Test::Float::RandomAssignment::operator()
virtual bool operator()(void) const
Test whether all assignments have been iterated.
Definition float.hpp:137
Test::Float::RandomAssignment::a
int a
How many assigments still to be generated Generate new value according to domain.
Definition float.hh:147
Test::Float::RandomAssignment::~RandomAssignment
virtual ~RandomAssignment(void)
Destructor.
Definition float.hpp:151
Test::Float::Test::rms
int rms
Which reification modes are supported.
Definition float.hh:257
Test::Float::Test::eq
static MaybeType eq(Gecode::FloatVal x, Gecode::FloatVal y)
Whether x and y are equal.
Definition float.hpp:268
Test::Float::Test::cmp
static MaybeType cmp(Gecode::FloatVal x, Gecode::FloatRelType r, Gecode::FloatVal y)
Compare x and y with respect to r.
Definition float.hpp:235
Test::Float::Test::testsubsumed
bool testsubsumed
Whether to test for subsumption.
Definition float.hh:263
Test::Float::Test::testfix
bool testfix
Whether to perform fixpoint test.
Definition float.hh:261
Test::Float::Test::eqv
bool eqv(void) const
Test whether equivalence as reification mode is supported.
Definition float.hpp:160
Test::Float::Test::arity
int arity
Number of variables.
Definition float.hh:247
Test::Float::Test::str
static std::string str(Gecode::FloatRelType frt)
Map float relation to string.
Definition float.hpp:194
Test::Float::Test::flip
bool flip(void)
Flip a coin and return true or false randomly.
Definition float.hpp:273
Test::Float::Test::step
Gecode::FloatNum step
Step for going to next solution.
Definition float.hh:251
Test::Float::Test::pmi
bool pmi(void) const
Test whether reverse implication as reification mode is supported.
Definition float.hpp:168
Test::Float::Test::Test
Test(const std::string &s, int a, const Gecode::FloatVal &d, Gecode::FloatNum st, AssignmentType at, bool r)
Constructor.
Definition float.hpp:172
Test::Float::Test::reified
bool reified
Does the constraint also exist as reified constraint.
Definition float.hh:255
Test::Float::Test::testsearch
bool testsearch
Whether to perform search test.
Definition float.hh:259
Test::Float::Test::assigmentType
AssignmentType assigmentType
Gives the type of assignment to use.
Definition float.hh:253
Test::Float::Test::dom
Gecode::FloatVal dom
Domain of variables.
Definition float.hh:249
Test::Float::Test::imp
bool imp(void) const
Test whether implication as reification mode is supported.
Definition float.hpp:164
Test::Int::Test::str
static std::string str(Gecode::IntPropLevel ipl)
Map integer propagation level to string.
Definition int.hpp:209
Gecode::FloatNum
double FloatNum
Floating point number base type.
Definition float.hh:106
Gecode::FloatRelType
FloatRelType
Relation types for floats.
Definition float.hh:1068
Gecode::FRT_EQ
@ FRT_EQ
Equality ( ).
Definition float.hh:1069
Gecode::FRT_GR
@ FRT_GR
Greater ( ).
Definition float.hh:1074
Gecode::FRT_NQ
@ FRT_NQ
Disequality ( ).
Definition float.hh:1070
Gecode::FRT_LE
@ FRT_LE
Less ( ).
Definition float.hh:1072
Gecode::FRT_LQ
@ FRT_LQ
Less or equal ( ).
Definition float.hh:1071
Gecode::FRT_GQ
@ FRT_GQ
Greater or equal ( ).
Definition float.hh:1073
Gecode::RM_IMP
@ RM_IMP
Implication for reification.
Definition int.hh:862
Gecode::RM_PMI
@ RM_PMI
Inverse implication for reification.
Definition int.hh:869
Gecode::RM_EQV
@ RM_EQV
Equivalence for reification (default).
Definition int.hh:855
Gecode::Float
Floating point numbers.
Gecode::Int::Limits::max
const int max
Largest allowed integer value.
Definition int.hh:116
Gecode
Gecode toplevel namespace
Gecode::min
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition arithmetic.cpp:67
Gecode::max
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition arithmetic.cpp:49
Test::Float
Testing domain floats.
Definition float.cpp:43
Test::Float::AssignmentType
AssignmentType
Assignment possible types.
Definition float.hh:61
Test::Float::MaybeType
MaybeType
Type for comparisons and solutions.
Definition float.hh:51
Test::Float::MT_MAYBE
@ MT_MAYBE
Definition float.hh:54
Test::Float::MT_FALSE
@ MT_FALSE
Definition float.hh:52
Test::Float::MT_TRUE
@ MT_TRUE
Definition float.hh:53
Test
General test support.
Definition afc.cpp:39
Test::r
Region r
Definition region.cpp:65
forceinline
#define forceinline
Definition config.hpp:187
GECODE_NEVER
#define GECODE_NEVER
Assert that this command is never executed.
Definition macros.hpp:56