5 #ifndef BPP_NUMERIC_CONSTRAINTS_H 6 #define BPP_NUMERIC_CONSTRAINTS_H 15 #include "../Clonable.h" 16 #include "../Text/TextTools.h" 17 #include "../Exceptions.h" 44 virtual bool isCorrect(
double value)
const = 0;
52 virtual bool includes(
double min,
double max)
const = 0;
60 virtual double getLimit(
double value)
const = 0;
91 virtual bool isEmpty()
const = 0;
124 inclLowerBound_(true),
125 inclUpperBound_(true),
129 lowerBound_(lowerBound),
130 upperBound_(upperBound),
131 inclLowerBound_(inclLower),
132 inclUpperBound_(inclUpper),
133 precision_(precision) {}
148 inclLowerBound_(isPositive ? incl : false),
149 inclUpperBound_(isPositive ? false : incl),
150 precision_(precision) {}
159 inclLowerBound_(true),
160 inclUpperBound_(true),
163 readDescription(desc);
171 void setLowerBound(
double lowerBound,
bool strict) { lowerBound_ = lowerBound; inclLowerBound_ = !strict; }
172 void setUpperBound(
double upperBound,
bool strict) { upperBound_ = upperBound; inclUpperBound_ = !strict; }
183 bool includes(
double min,
double max)
const override 185 return (inclLowerBound_ ? min >= getLowerBound() : min > getLowerBound()) &&
186 (inclUpperBound_ ? max <= getUpperBound() : max < getUpperBound());
191 return (inclLowerBound_ ? value >= getLowerBound() : value > getLowerBound()) &&
192 (inclUpperBound_ ? value <= getUpperBound() : value < getUpperBound());
197 return inclUpperBound_ ? upperBound_ < value : upperBound_ <= value;
202 return inclLowerBound_ ? lowerBound_ > value : lowerBound_ >= value;
207 return upperBound_ <= value;
212 return lowerBound_ >= value;
218 (*
this >= value ? lowerBound_ : upperBound_);
225 strictLowerBound() ? lowerBound_ + precision_ : lowerBound_ :
226 strictUpperBound() ? upperBound_ - precision_ : upperBound_);
236 return (inclLowerBound_ ?
"[ " :
"]")
240 + (inclUpperBound_ ?
"] " :
"[");
252 size_t pdp = desc.find(
";");
253 size_t dc = desc.find_first_of(
"[]", 1);
255 if (dc == std::string::npos || pdp == std::string::npos ||
256 (desc[0] !=
']' && desc[0] !=
'[') || (pdp >= dc))
257 throw Exception(
"Constraints::readDescription. Wrong description:" + desc);
259 std::string deb = desc.substr(1, pdp - 1);
260 std::string fin = desc.substr(pdp + 1, dc - pdp - 1);
262 inclLowerBound_ = (desc[0] ==
'[');
263 inclUpperBound_ = (desc[dc] ==
']');
278 double lowerBound, upperBound;
279 bool inclLowerBound, inclUpperBound;
285 if (lowerBound_ <= pi->lowerBound_)
292 lowerBound = lowerBound_;
293 inclLowerBound = inclLowerBound_;
303 upperBound = upperBound_;
304 inclUpperBound = inclUpperBound_;
339 catch (std::bad_cast&) {}
386 return (lowerBound_ > upperBound_) ||
387 ((lowerBound_ > upperBound_) &&
388 inclUpperBound_ && inclLowerBound_);
392 #endif // BPP_NUMERIC_CONSTRAINTS_H virtual bool includes(double min, double max) const =0
Tell if all the values in a given interval are correct.
virtual ConstraintInterface * operator &(const ConstraintInterface &c) const =0
Intersect this Constraint with another one.
double getLimit(double value) const override
Give the nearest limit for a bad value.
virtual double getLimit(double value) const =0
Give the nearest limit for a bad value.
double getLowerBound() const
bool operator!=(const IntervalConstraint &i) const
Tells if this interval is different from another one.
bool strictLowerBound() const
double toDouble(const std::string &s, char dec, char scientificNotation)
Convert from string to double.
this static class contains several useful constant values.
An interval, either bounded or not, which can also have infinite bounds.
void setLowerBound(double lowerBound, bool strict)
The constraint interface.
bool includes(double min, double max) const override
Tell if all the values in a given interval are correct.
bool operator==(const IntervalConstraint &i) const
Tells if this interval equals another one.
bool inclLowerBound_
Boolean flags are true if the boundaries are included.
IntervalConstraint(bool isPositive, double bound, bool incl, double precision=NumConstants::TINY())
Create an interval with an infinite lower/upper bound.
double precision_
the accepted precision on the boundary (default: 1e-12)
IntervalConstraint(double lowerBound, double upperBound, bool inclLower, bool inclUpper, double precision=NumConstants::TINY())
virtual bool isCorrect(double value) const =0
Tell if a given value is correct.
bool finiteLowerBound() const
bool operator<=(const IntervalConstraint &i) const
Tells if this interval is included or equal in another one.
ConstraintInterface * clone() const =0
Create a copy of this object and send a pointer to it.
bool strictUpperBound() const
virtual ~IntervalConstraint()
bool finiteUpperBound() const
void operator &=(std::vector< T > &v1, const C &c)
bool operator<=(double value) const
std::string getDescription() const override
Give a short description on the type of constraint.
virtual std::string getDescription() const =0
Give a short description on the type of constraint.
virtual ~ConstraintInterface()
IntervalConstraint * clone() const override
Create a copy of this object and send a pointer to it.
virtual double getAcceptedLimit(double value) const =0
Give the nearest accepted limit for a bad value.
double lowerBound_
The boundaries of the interval.
bool isEmpty() const override
Tells if this interval is empty.
virtual bool isEmpty() const =0
Tells if this constraints defines an empty set.
bool operator>=(double value) const
double getAcceptedLimit(double value) const override
Give the nearest accepted limit for a bad value.
Exception base class. Overload exception constructor (to control the exceptions mechanism). Destructor is already virtual (from std::exception)
The Clonable interface (allow an object to be cloned).
IntervalConstraint(std::string &desc)
Create an interval from a string description, using readDescription method.
void setUpperBound(double upperBound, bool strict)
double getUpperBound() const
bool operator<(double value) const
std::string toString(T t)
General template method to convert to a string.
bool operator>(double value) const
void readDescription(std::string &desc)
Sets the bounds of the interval from a string.
double getPrecision() const
virtual bool isCorrect(double value) const override
Tell if a given value is correct.