bpp-core3  3.0.0
Constraints.h
Go to the documentation of this file.
1 //
2 // File: Constraints.h
3 // Authors:
4 // Julien Dutheil
5 // Created: 2003-12-25 19:35:17
6 //
7 
8 /*
9  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
10 
11  This software is a computer program whose purpose is to provide classes
12  for numerical calculus.
13 
14  This software is governed by the CeCILL license under French law and
15  abiding by the rules of distribution of free software. You can use,
16  modify and/ or redistribute the software under the terms of the CeCILL
17  license as circulated by CEA, CNRS and INRIA at the following URL
18  "http://www.cecill.info".
19 
20  As a counterpart to the access to the source code and rights to copy,
21  modify and redistribute granted by the license, users are provided only
22  with a limited warranty and the software's author, the holder of the
23  economic rights, and the successive licensors have only limited
24  liability.
25 
26  In this respect, the user's attention is drawn to the risks associated
27  with loading, using, modifying and/or developing or reproducing the
28  software by the user in light of its specific status of free software,
29  that may mean that it is complicated to manipulate, and that also
30  therefore means that it is reserved for developers and experienced
31  professionals having in-depth computer knowledge. Users are therefore
32  encouraged to load and test the software's suitability as regards their
33  requirements in conditions enabling the security of their systems and/or
34  data to be ensured and, more generally, to use and operate it in the
35  same conditions as regards security.
36 
37  The fact that you are presently reading this means that you have had
38  knowledge of the CeCILL license and that you accept its terms.
39 */
40 
41 #ifndef BPP_NUMERIC_CONSTRAINTS_H
42 #define BPP_NUMERIC_CONSTRAINTS_H
43 
44 
45 
46 // From the STL:
47 #include <string>
48 #include <iostream>
49 #include <typeinfo>
50 
51 // From Utils:
52 #include "../Clonable.h"
53 #include "../Text/TextTools.h"
54 #include "../Exceptions.h"
55 
56 #include "NumConstants.h"
57 
58 namespace bpp
59 {
65 class Constraint : public Clonable
66 {
67 public:
69  virtual ~Constraint() {}
70 
71  Constraint* clone() const = 0;
72 
73 public:
80  virtual bool isCorrect(double value) const = 0;
81 
88  virtual bool includes(double min, double max) const = 0;
89 
96  virtual double getLimit(double value) const = 0;
97 
107  virtual double getAcceptedLimit(double value) const = 0;
108 
114  virtual std::string getDescription() const = 0;
115 
122  virtual Constraint* operator&(const Constraint& c) const = 0;
123 
128  virtual bool isEmpty() const = 0;
129 };
130 
139 {
140 protected:
146 
157  double precision_;
158 
159 public:
161  upperBound_(NumConstants::PINF()),
162  inclLowerBound_(true),
163  inclUpperBound_(true),
164  precision_(NumConstants::TINY()) {}
165 
166  IntervalConstraint(double lowerBound, double upperBound, bool inclLower, bool inclUpper, double precision = NumConstants::TINY()) :
167  lowerBound_(lowerBound),
168  upperBound_(upperBound),
169  inclLowerBound_(inclLower),
170  inclUpperBound_(inclUpper),
171  precision_(precision) {}
172 
184  IntervalConstraint(bool isPositive, double bound, bool incl, double precision = NumConstants::TINY()) :
185  lowerBound_(isPositive ? bound : NumConstants::MINF()),
186  upperBound_(isPositive ? NumConstants::PINF() : bound),
187  inclLowerBound_(isPositive ? incl : false),
188  inclUpperBound_(isPositive ? false : incl),
189  precision_(precision) {}
190 
197  IntervalConstraint(std::string& desc) :
198  lowerBound_(NumConstants::MINF()),
199  upperBound_(NumConstants::PINF()),
200  inclLowerBound_(true),
201  inclUpperBound_(true),
202  precision_(NumConstants::TINY())
203  {
204  readDescription(desc);
205  }
206 
207  virtual ~IntervalConstraint() {}
208 
209  IntervalConstraint* clone() const { return new IntervalConstraint(*this); }
210 
211 public:
212  void setLowerBound(double lowerBound, bool strict) { lowerBound_ = lowerBound; inclLowerBound_ = !strict; }
213  void setUpperBound(double upperBound, bool strict) { upperBound_ = upperBound; inclUpperBound_ = !strict; }
214 
215  double getLowerBound() const { return lowerBound_; }
216  double getUpperBound() const { return upperBound_; }
217 
218  bool strictLowerBound() const { return !inclLowerBound_; }
219  bool strictUpperBound() const { return !inclUpperBound_; }
220 
221  bool finiteLowerBound() const { return lowerBound_ > NumConstants::MINF(); }
222  bool finiteUpperBound() const { return upperBound_ < NumConstants::PINF(); }
223 
224  bool includes(double min, double max) const
225  {
226  return (inclLowerBound_ ? min >= getLowerBound() : min > getLowerBound()) &&
227  (inclUpperBound_ ? max <= getUpperBound() : max < getUpperBound());
228  }
229 
230  virtual bool isCorrect(double value) const
231  {
232  return (inclLowerBound_ ? value >= getLowerBound() : value > getLowerBound()) &&
233  (inclUpperBound_ ? value <= getUpperBound() : value < getUpperBound());
234  }
235 
236  bool operator<(double value) const
237  {
238  return inclUpperBound_ ? upperBound_ < value : upperBound_ <= value;
239  }
240 
241  bool operator>(double value) const
242  {
243  return inclLowerBound_ ? lowerBound_ > value : lowerBound_ >= value;
244  }
245 
246  bool operator<=(double value) const
247  {
248  return upperBound_ <= value;
249  }
250 
251  bool operator>=(double value) const
252  {
253  return lowerBound_ >= value;
254  }
255 
256  double getLimit(double value) const
257  {
258  return isCorrect(value) ? value :
259  (*this >= value ? lowerBound_ : upperBound_);
260  }
261 
262  double getAcceptedLimit(double value) const
263  {
264  return isCorrect(value) ? value :
265  (*this >= value ?
268  }
269 
270  double getPrecision() const
271  {
272  return precision_;
273  }
274 
275  std::string getDescription() const
276  {
277  return (inclLowerBound_ ? "[ " : "]")
279  + "; "
281  + (inclUpperBound_ ? "] " : "[");
282  }
283 
292  void readDescription(std::string& desc)
293  {
294  size_t pdp = desc.find(";");
295  size_t dc = desc.find_first_of("[]", 1);
296 
297  if (dc == std::string::npos || pdp == std::string::npos ||
298  (desc[0] != ']' && desc[0] != '[') || (pdp >= dc))
299  throw Exception("Constraints::readDescription. Wrong description:" + desc);
300 
301  std::string deb = desc.substr(1, pdp - 1);
302  std::string fin = desc.substr(pdp + 1, dc - pdp - 1);
303 
304  inclLowerBound_ = (desc[0] == '[');
305  inclUpperBound_ = (desc[dc] == ']');
306 
307  lowerBound_ = (deb == "-inf") ? NumConstants::MINF() : TextTools::toDouble(deb);
308  upperBound_ = ((fin == "+inf") || (fin == "inf")) ? NumConstants::PINF() : TextTools::toDouble(fin);
309  }
310 
318  Constraint* operator&(const Constraint& c) const
319  {
320  double lowerBound, upperBound;
321  bool inclLowerBound, inclUpperBound;
322 
323  const IntervalConstraint* pi = dynamic_cast<const IntervalConstraint*>(&c);
324 
325  if (pi)
326  {
327  if (lowerBound_ <= pi->lowerBound_)
328  {
329  lowerBound = pi->lowerBound_;
330  inclLowerBound = pi->inclLowerBound_;
331  }
332  else
333  {
334  lowerBound = lowerBound_;
335  inclLowerBound = inclLowerBound_;
336  }
337 
338  if (upperBound_ >= pi->upperBound_)
339  {
340  upperBound = pi->upperBound_;
341  inclUpperBound = pi->inclUpperBound_;
342  }
343  else
344  {
345  upperBound = upperBound_;
346  inclUpperBound = inclUpperBound_;
347  }
348  return new IntervalConstraint(lowerBound, upperBound, inclLowerBound, inclUpperBound, (precision_ > pi->getPrecision()) ? precision_ : pi->getPrecision());
349  }
350  else
351  return 0;
352  }
353 
362  {
363  try
364  {
365  const IntervalConstraint& pi = dynamic_cast<const IntervalConstraint&>(c);
366 
367  if (lowerBound_ <= pi.lowerBound_)
368  {
371  }
372 
373  if (upperBound_ >= pi.upperBound_)
374  {
377  }
378  if (pi.getPrecision() > precision_)
379  precision_ = pi.getPrecision();
380  }
381  catch (std::bad_cast&) {}
382 
383  return *this;
384  }
385 
391  bool operator==(const IntervalConstraint& i) const
392  {
393  return lowerBound_ == i.lowerBound_
395  && upperBound_ == i.upperBound_
397  }
398 
404  bool operator!=(const IntervalConstraint& i) const
405  {
406  return lowerBound_ != i.lowerBound_
408  || upperBound_ != i.upperBound_
410  }
411 
417  bool operator<=(const IntervalConstraint& i) const
418  {
419  return lowerBound_ >= i.lowerBound_
420  && upperBound_ <= i.upperBound_;
421  }
422 
426  bool isEmpty() const
427  {
428  return (lowerBound_ > upperBound_) ||
429  ((lowerBound_ > upperBound_) &&
431  }
432 };
433 } // end of namespace bpp.
434 #endif // BPP_NUMERIC_CONSTRAINTS_H
The Clonable interface (allow an object to be cloned).
Definition: Clonable.h:103
The constraint interface.
Definition: Constraints.h:66
virtual double getLimit(double value) const =0
Give the nearest limit for a bad value.
virtual std::string getDescription() const =0
Give a short description on the type of constraint.
virtual ~Constraint()
Definition: Constraints.h:69
virtual double getAcceptedLimit(double value) const =0
Give the nearest accepted limit for a bad value.
virtual bool isCorrect(double value) const =0
Tell if a given value is correct.
virtual bool includes(double min, double max) const =0
Tell if all the values in a given interval are correct.
virtual bool isEmpty() const =0
Tells if this constraints defines an empty set.
Constraint * clone() const =0
Create a copy of this object and send a pointer to it.
virtual Constraint * operator&(const Constraint &c) const =0
Intersect this Constraint with another one.
Exception base class. Overload exception constructor (to control the exceptions mechanism)....
Definition: Exceptions.h:59
An interval, either bounded or not, which can also have infinite bounds.
Definition: Constraints.h:139
std::string getDescription() const
Give a short description on the type of constraint.
Definition: Constraints.h:275
bool operator<=(double value) const
Definition: Constraints.h:246
bool operator!=(const IntervalConstraint &i) const
Tells if this interval is different from another one.
Definition: Constraints.h:404
virtual bool isCorrect(double value) const
Tell if a given value is correct.
Definition: Constraints.h:230
IntervalConstraint(std::string &desc)
Create an interval from a string description, using readDescription method.
Definition: Constraints.h:197
double precision_
the accepted precision on the boundary (default: 1e-12)
Definition: Constraints.h:157
bool isEmpty() const
Tells if this interval is empty.
Definition: Constraints.h:426
double getAcceptedLimit(double value) const
Give the nearest accepted limit for a bad value.
Definition: Constraints.h:262
IntervalConstraint(double lowerBound, double upperBound, bool inclLower, bool inclUpper, double precision=NumConstants::TINY())
Definition: Constraints.h:166
bool includes(double min, double max) const
Tell if all the values in a given interval are correct.
Definition: Constraints.h:224
double getLowerBound() const
Definition: Constraints.h:215
bool strictLowerBound() const
Definition: Constraints.h:218
virtual ~IntervalConstraint()
Definition: Constraints.h:207
void setLowerBound(double lowerBound, bool strict)
Definition: Constraints.h:212
double getLimit(double value) const
Give the nearest limit for a bad value.
Definition: Constraints.h:256
bool operator<=(const IntervalConstraint &i) const
Tells if this interval is included or equal in another one.
Definition: Constraints.h:417
bool inclLowerBound_
Boolean flags are true if the boundaries are included.
Definition: Constraints.h:151
Constraint * operator&(const Constraint &c) const
Intersect this IntervalConstraint with another one.
Definition: Constraints.h:318
bool operator<(double value) const
Definition: Constraints.h:236
IntervalConstraint(bool isPositive, double bound, bool incl, double precision=NumConstants::TINY())
Create an interval with an infinite lower/upper bound.
Definition: Constraints.h:184
bool operator==(const IntervalConstraint &i) const
Tells if this interval equals another one.
Definition: Constraints.h:391
IntervalConstraint & operator&=(const Constraint &c)
Intersect this IntervalConstraint with another constraint.
Definition: Constraints.h:361
bool finiteLowerBound() const
Definition: Constraints.h:221
bool strictUpperBound() const
Definition: Constraints.h:219
double lowerBound_
The boundaries of the interval.
Definition: Constraints.h:145
bool operator>(double value) const
Definition: Constraints.h:241
bool operator>=(double value) const
Definition: Constraints.h:251
bool finiteUpperBound() const
Definition: Constraints.h:222
IntervalConstraint * clone() const
Create a copy of this object and send a pointer to it.
Definition: Constraints.h:209
double getPrecision() const
Definition: Constraints.h:270
double getUpperBound() const
Definition: Constraints.h:216
void readDescription(std::string &desc)
Sets the bounds of the interval from a string.
Definition: Constraints.h:292
void setUpperBound(double upperBound, bool strict)
Definition: Constraints.h:213
this static class contains several useful constant values.
Definition: NumConstants.h:57
static double TINY()
Definition: NumConstants.h:82
static double MINF()
Definition: NumConstants.h:93
static double PINF()
Definition: NumConstants.h:92
double toDouble(const std::string &s, char dec, char scientificNotation)
Convert from string to double.
Definition: TextTools.cpp:255
std::string toString(T t)
General template method to convert to a string.
Definition: TextTools.h:153