bpp-core  2.2.0
Constraints.h
Go to the documentation of this file.
1 //
2 // File: Constraints.h
3 // Created by: Julien Dutheil
4 // Created on: Thu Dec 25 19:35:17 2003
5 //
6 
7 /*
8  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
9 
10  This software is a computer program whose purpose is to provide classes
11  for numerical calculus.
12 
13  This software is governed by the CeCILL license under French law and
14  abiding by the rules of distribution of free software. You can use,
15  modify and/ or redistribute the software under the terms of the CeCILL
16  license as circulated by CEA, CNRS and INRIA at the following URL
17  "http://www.cecill.info".
18 
19  As a counterpart to the access to the source code and rights to copy,
20  modify and redistribute granted by the license, users are provided only
21  with a limited warranty and the software's author, the holder of the
22  economic rights, and the successive licensors have only limited
23  liability.
24 
25  In this respect, the user's attention is drawn to the risks associated
26  with loading, using, modifying and/or developing or reproducing the
27  software by the user in light of its specific status of free software,
28  that may mean that it is complicated to manipulate, and that also
29  therefore means that it is reserved for developers and experienced
30  professionals having in-depth computer knowledge. Users are therefore
31  encouraged to load and test the software's suitability as regards their
32  requirements in conditions enabling the security of their systems and/or
33  data to be ensured and, more generally, to use and operate it in the
34  same conditions as regards security.
35 
36  The fact that you are presently reading this means that you have had
37  knowledge of the CeCILL license and that you accept its terms.
38  */
39 
40 #ifndef _CONSTRAINTS_H_
41 #define _CONSTRAINTS_H_
42 
43 // From the STL:
44 #include <string>
45 #include <iostream>
46 #include <typeinfo>
47 
48 // From Utils:
49 #include "../Clonable.h"
50 #include "../Text/TextTools.h"
51 #include "../Exceptions.h"
52 
53 #include "NumConstants.h"
54 
55 namespace bpp
56 {
62 class Constraint : public Clonable
63 {
64 public:
66  virtual ~Constraint() {}
67 
68  Constraint* clone() const = 0;
69 
70 public:
77  virtual bool isCorrect(double value) const = 0;
78 
85  virtual bool includes(double min, double max) const = 0;
86 
93  virtual double getLimit(double value) const = 0;
94 
104  virtual double getAcceptedLimit(double value) const = 0;
105 
111  virtual std::string getDescription() const = 0;
112 
119  virtual Constraint* operator&(const Constraint& c) const = 0;
120 
125  virtual bool isEmpty() const = 0;
126 };
127 
136 {
137 protected:
143 
154  double precision_;
155 
156 
157 public:
159  upperBound_(NumConstants::PINF()),
160  inclLowerBound_(true),
161  inclUpperBound_(true),
162  precision_(NumConstants::TINY()) {}
163 
164  IntervalConstraint(double lowerBound, double upperBound, bool inclLower, bool inclUpper, double precision = NumConstants::TINY()) :
165  lowerBound_(lowerBound),
166  upperBound_(upperBound),
167  inclLowerBound_(inclLower),
168  inclUpperBound_(inclUpper),
169  precision_(precision) {}
170 
182  IntervalConstraint(bool isPositive, double bound, bool incl, double precision = NumConstants::TINY()) :
183  lowerBound_(isPositive ? bound : NumConstants::MINF()),
184  upperBound_(isPositive ? NumConstants::PINF() : bound),
185  inclLowerBound_(isPositive ? incl : false),
186  inclUpperBound_(isPositive ? false : incl),
187  precision_(precision) {}
188 
195  IntervalConstraint(std::string& desc) :
196  lowerBound_(NumConstants::MINF()),
197  upperBound_(NumConstants::PINF()),
198  inclLowerBound_(true),
199  inclUpperBound_(true),
200  precision_(NumConstants::TINY())
201  {
202  readDescription(desc);
203  }
204 
205  virtual ~IntervalConstraint() {}
206 
207  IntervalConstraint* clone() const { return new IntervalConstraint(*this); }
208 
209 public:
210  void setLowerBound(double lowerBound, bool strict) { lowerBound_ = lowerBound; inclLowerBound_ = !strict; }
211  void setUpperBound(double upperBound, bool strict) { upperBound_ = upperBound; inclUpperBound_ = !strict; }
212 
213  double getLowerBound() const { return lowerBound_; }
214  double getUpperBound() const { return upperBound_; }
215 
216  bool strictLowerBound() const { return !inclLowerBound_; }
217  bool strictUpperBound() const { return !inclUpperBound_; }
218 
219  bool finiteLowerBound() const { return lowerBound_ > NumConstants::MINF(); }
220  bool finiteUpperBound() const { return upperBound_ < NumConstants::PINF(); }
221 
222  bool includes(double min, double max) const
223  {
224  return (inclLowerBound_ ? min >= getLowerBound() : min > getLowerBound()) &&
225  (inclUpperBound_ ? max <= getUpperBound() : max < getUpperBound());
226  }
227 
228  virtual bool isCorrect(double value) const
229  {
230  return (inclLowerBound_ ? value >= getLowerBound() : value > getLowerBound()) &&
231  (inclUpperBound_ ? value <= getUpperBound() : value < getUpperBound());
232  }
233 
234  bool operator<(double value) const
235  {
236  return inclUpperBound_ ? upperBound_ < value : upperBound_ <= value;
237  }
238 
239  bool operator>(double value) const
240  {
241  return inclLowerBound_ ? lowerBound_ > value : lowerBound_ >= value;
242  }
243 
244  bool operator<=(double value) const
245  {
246  return upperBound_ <= value;
247  }
248 
249  bool operator>=(double value) const
250  {
251  return lowerBound_ >= value;
252  }
253 
254  double getLimit(double value) const
255  {
256  return isCorrect(value) ? value :
257  (*this >= value ? lowerBound_ : upperBound_);
258  }
259 
260  double getAcceptedLimit(double value) const
261  {
262  return isCorrect(value) ? value :
263  (*this >= value ?
266  }
267 
268  double getPrecision() const
269  {
270  return precision_;
271  }
272 
273  std::string getDescription() const
274  {
275  return (inclLowerBound_ ? "[ " : "]")
277  + "; "
279  + (inclUpperBound_ ? "] " : "[");
280  }
281 
291  void readDescription(std::string& desc)
292  {
293  size_t pdp=desc.find(";");
294  size_t dc=desc.find_first_of("[]",1);
295 
296  if (dc==std::string::npos || pdp==std::string::npos ||
297  (desc[0]!=']' && desc[0]!='[') || (pdp >=dc))
298  throw Exception("Constraints::readDescription. Wrong description:" + desc);
299 
300  std::string deb=desc.substr(1,pdp-1);
301  std::string fin=desc.substr(pdp+1, dc-pdp-1);
302 
303  inclLowerBound_=(desc[0]=='[');
304  inclUpperBound_=(desc[dc]==']');
305 
306  lowerBound_ = (deb=="-inf")?NumConstants::MINF():TextTools::toDouble(deb);
307  upperBound_ = ((fin=="+inf") || (fin=="inf"))?NumConstants::PINF():TextTools::toDouble(fin);
308 
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  const IntervalConstraint& pi = dynamic_cast<const IntervalConstraint&>(c);
365 
366  if (lowerBound_ <= pi.lowerBound_)
367  {
370  }
371 
372  if (upperBound_ >= pi.upperBound_)
373  {
376  }
377  if (pi.getPrecision() > precision_)
378  precision_ = pi.getPrecision();
379  } catch(std::bad_cast&) {}
380 
381  return *this;
382  }
383 
389  bool operator==(const IntervalConstraint& i) const
390  {
391  return lowerBound_ == i.lowerBound_
393  && upperBound_ == i.upperBound_
395  }
396 
402  bool operator!=(const IntervalConstraint& i) const
403  {
404  return lowerBound_ != i.lowerBound_
406  || upperBound_ != i.upperBound_
408  }
409 
415  bool operator<=(const IntervalConstraint& i) const
416  {
417  return lowerBound_ >= i.lowerBound_
418  && upperBound_ <= i.upperBound_;
419  }
420 
425  bool isEmpty() const
426  {
427  return ((lowerBound_ > upperBound_) ||
428  ((lowerBound_ > upperBound_) &&
430  }
431 
432 };
433 
434 
435 } // end of namespace bpp.
436 
437 #endif // _CONSTRAINTS_H_
438 
virtual ~Constraint()
Definition: Constraints.h:66
virtual double getLimit(double value) const =0
Give the nearest limit for a bad value.
std::string getDescription() const
Give a short description on the type of constraint.
Definition: Constraints.h:273
double getLowerBound() const
Definition: Constraints.h:213
bool operator!=(const IntervalConstraint &i) const
Tells if this interval is different from another one.
Definition: Constraints.h:402
bool strictLowerBound() const
Definition: Constraints.h:216
this static class contains several useful constant values.
Definition: NumConstants.h:54
An interval, either bounded or not, which can also have infinite bounds.
Definition: Constraints.h:135
This class allows to perform a correspondence analysis.
void setLowerBound(double lowerBound, bool strict)
Definition: Constraints.h:210
IntervalConstraint & operator &=(const Constraint &c)
Intersect this IntervalConstraint with another constraint.
Definition: Constraints.h:361
virtual bool isCorrect(double value) const
Tell if a given value is correct.
Definition: Constraints.h:228
IntervalConstraint * clone() const
Create a copy of this object and send a pointer to it.
Definition: Constraints.h:207
bool operator==(const IntervalConstraint &i) const
Tells if this interval equals another one.
Definition: Constraints.h:389
bool inclLowerBound_
Boolean flags are true if the boundaries are included.
Definition: Constraints.h:148
IntervalConstraint(bool isPositive, double bound, bool incl, double precision=NumConstants::TINY())
Create an interval with an infinite lower/upper bound.
Definition: Constraints.h:182
static double PINF()
Definition: NumConstants.h:91
Constraint * operator &(const Constraint &c) const
Intersect this IntervalConstraint with another one.
Definition: Constraints.h:318
double getAcceptedLimit(double value) const
Give the nearest accepted limit for a bad value.
Definition: Constraints.h:260
static std::string toString(T t)
General template method to convert to a string.
Definition: TextTools.h:189
virtual bool isCorrect(double value) const =0
Tell if a given value is correct.
double precision_
the accepted precision on the boundary (default: 1e-12)
Definition: Constraints.h:154
IntervalConstraint(double lowerBound, double upperBound, bool inclLower, bool inclUpper, double precision=NumConstants::TINY())
Definition: Constraints.h:164
bool finiteLowerBound() const
Definition: Constraints.h:219
bool operator<=(const IntervalConstraint &i) const
Tells if this interval is included or equal in another one.
Definition: Constraints.h:415
bool strictUpperBound() const
Definition: Constraints.h:217
virtual ~IntervalConstraint()
Definition: Constraints.h:205
static double TINY()
Definition: NumConstants.h:81
bool finiteUpperBound() const
Definition: Constraints.h:220
virtual Constraint * operator &(const Constraint &c) const =0
Intersect this Constraint with another one.
virtual std::string getDescription() const =0
Give a short description on the type of constraint.
Constraint * clone() const =0
Create a copy of this object and send a pointer to it.
The constraint interface.
Definition: Constraints.h:62
bool operator<=(double value) const
Definition: Constraints.h:244
bool includes(double min, double max) const
Tell if all the values in a given interval are correct.
Definition: Constraints.h:222
double lowerBound_
The boundaries of the interval.
Definition: Constraints.h:142
virtual bool isEmpty() const =0
Tells if this constraints defines an empty set.
bool operator>=(double value) const
Definition: Constraints.h:249
Exception base class.
Definition: Exceptions.h:57
The Clonable interface (allow an object to be cloned).
Definition: Clonable.h:99
bool isEmpty() const
Tells if this interval is empty.
Definition: Constraints.h:425
IntervalConstraint(std::string &desc)
Create an interval from a string description, using readDescription method.
Definition: Constraints.h:195
void setUpperBound(double upperBound, bool strict)
Definition: Constraints.h:211
double getLimit(double value) const
Give the nearest limit for a bad value.
Definition: Constraints.h:254
double getUpperBound() const
Definition: Constraints.h:214
bool operator<(double value) const
Definition: Constraints.h:234
virtual double getAcceptedLimit(double value) const =0
Give the nearest accepted limit for a bad value.
static double toDouble(const std::string &s, char dec='.', char scientificNotation='e')
Convert from string to double.
Definition: TextTools.cpp:313
bool operator>(double value) const
Definition: Constraints.h:239
virtual bool includes(double min, double max) const =0
Tell if all the values in a given interval are correct.
void readDescription(std::string &desc)
Sets the bounds of the interval from a string.
Definition: Constraints.h:291
double getPrecision() const
Definition: Constraints.h:268
static double MINF()
Definition: NumConstants.h:92