Added classes to hendle bounds:
- eoGeneralRealBound that can be initialized using a string (and hence can be easily read as a parameter) - eoIntBound and all other integer-equivalent of the classes in eoRealBound.h Note that there is no equivalent to eoRealVectorBounds for vector of integers
This commit is contained in:
parent
50b395d16d
commit
7a695e65df
5 changed files with 1078 additions and 62 deletions
|
|
@ -79,53 +79,53 @@ public:
|
|||
|
||||
/** Self-Test: true if ***both*** a min and a max
|
||||
*/
|
||||
virtual bool isBounded(void) = 0;
|
||||
virtual bool isBounded(void) const = 0;
|
||||
|
||||
/** Self-Test: true if no min ***and*** no max
|
||||
* hence no further need to test/truncate/fold anything
|
||||
*/
|
||||
virtual bool hasNoBoundAtAll(void) = 0;
|
||||
virtual bool hasNoBoundAtAll(void) const = 0;
|
||||
|
||||
/** Self-Test: bounded from below???
|
||||
*/
|
||||
virtual bool isMinBounded(void) = 0;
|
||||
virtual bool isMinBounded(void) const = 0;
|
||||
|
||||
/** Self-Test: bounded from above???
|
||||
*/
|
||||
virtual bool isMaxBounded(void) = 0;
|
||||
virtual bool isMaxBounded(void) const = 0;
|
||||
|
||||
/** Test on a value: is it in bounds?
|
||||
*/
|
||||
virtual bool isInBounds(double) = 0;
|
||||
virtual bool isInBounds(double) const = 0;
|
||||
|
||||
/** Put value back into bounds - by folding back and forth
|
||||
*/
|
||||
virtual void foldsInBounds(double &) = 0;
|
||||
virtual void foldsInBounds(double &) const = 0;
|
||||
|
||||
/** Put value back into bounds - by truncating to a boundary value
|
||||
*/
|
||||
virtual void truncate(double &) = 0;
|
||||
virtual void truncate(double &) const = 0;
|
||||
|
||||
/** get minimum value
|
||||
* @std::exception if does not exist
|
||||
*/
|
||||
virtual double minimum() = 0;
|
||||
virtual double minimum() const = 0 ;
|
||||
/** get maximum value
|
||||
* @std::exception if does not exist
|
||||
*/
|
||||
virtual double maximum() = 0;
|
||||
virtual double maximum() const = 0 ;
|
||||
/** get range
|
||||
* @std::exception if unbounded
|
||||
*/
|
||||
virtual double range() = 0;
|
||||
virtual double range() const = 0;
|
||||
|
||||
/** random generator of uniform numbers in bounds
|
||||
* @std::exception if unbounded
|
||||
*/
|
||||
virtual double uniform(eoRng & _rng = eo::rng) = 0;
|
||||
virtual double uniform(eoRng & _rng = eo::rng) const = 0;
|
||||
|
||||
/** for memory managements - ugly */
|
||||
virtual eoRealBounds * dup() = 0;
|
||||
virtual eoRealBounds * dup() const = 0;
|
||||
};
|
||||
|
||||
/** A default class for unbounded variables
|
||||
|
|
@ -135,28 +135,28 @@ class eoRealNoBounds : public eoRealBounds
|
|||
public:
|
||||
virtual ~eoRealNoBounds(){}
|
||||
|
||||
virtual bool isBounded(void) {return false;}
|
||||
virtual bool hasNoBoundAtAll(void) {return true;}
|
||||
virtual bool isMinBounded(void) {return false;}
|
||||
virtual bool isMaxBounded(void) {return false;}
|
||||
virtual void foldsInBounds(double &) {return;}
|
||||
virtual void truncate(double &) {return;}
|
||||
virtual bool isInBounds(double) {return true;}
|
||||
virtual bool isBounded(void) const {return false;}
|
||||
virtual bool hasNoBoundAtAll(void) const {return true;}
|
||||
virtual bool isMinBounded(void) const {return false;}
|
||||
virtual bool isMaxBounded(void) const {return false;}
|
||||
virtual void foldsInBounds(double &) const {return;}
|
||||
virtual void truncate(double &) const {return;}
|
||||
virtual bool isInBounds(double) const {return true;}
|
||||
|
||||
virtual double minimum()
|
||||
virtual double minimum() const
|
||||
{
|
||||
throw std::logic_error("Trying to get minimum of unbounded eoRealBounds");
|
||||
}
|
||||
virtual double maximum()
|
||||
virtual double maximum() const
|
||||
{
|
||||
throw std::logic_error("Trying to get maximum of unbounded eoRealBounds");
|
||||
}
|
||||
virtual double range()
|
||||
virtual double range() const
|
||||
{
|
||||
throw std::logic_error("Trying to get range of unbounded eoRealBounds");
|
||||
}
|
||||
|
||||
virtual double uniform(eoRng & _rng = eo::rng)
|
||||
virtual double uniform(eoRng & _rng = eo::rng) const
|
||||
{
|
||||
throw std::logic_error("Trying to generate uniform values in unbounded eoRealBounds");
|
||||
}
|
||||
|
|
@ -183,7 +183,7 @@ public:
|
|||
}
|
||||
|
||||
/** for memory managements - ugly */
|
||||
virtual eoRealBounds * dup()
|
||||
virtual eoRealBounds * dup() const
|
||||
{
|
||||
return new eoRealNoBounds(*this);
|
||||
}
|
||||
|
|
@ -212,23 +212,23 @@ public :
|
|||
}
|
||||
|
||||
// accessors
|
||||
virtual double minimum() { return repMinimum; }
|
||||
virtual double maximum() { return repMaximum; }
|
||||
virtual double range() { return repRange; }
|
||||
virtual double minimum() const { return repMinimum; }
|
||||
virtual double maximum() const { return repMaximum; }
|
||||
virtual double range() const { return repRange; }
|
||||
|
||||
// description
|
||||
virtual bool isBounded(void) {return true;}
|
||||
virtual bool hasNoBoundAtAll(void) {return false;}
|
||||
virtual bool isMinBounded(void) {return true;}
|
||||
virtual bool isMaxBounded(void) {return true;}
|
||||
virtual bool isBounded(void) const {return true;}
|
||||
virtual bool hasNoBoundAtAll(void) const {return false;}
|
||||
virtual bool isMinBounded(void) const {return true;}
|
||||
virtual bool isMaxBounded(void) const {return true;}
|
||||
|
||||
virtual double uniform(eoRng & _rng = eo::rng)
|
||||
virtual double uniform(eoRng & _rng = eo::rng) const
|
||||
{
|
||||
return repMinimum + _rng.uniform(repRange);
|
||||
}
|
||||
|
||||
// says if a given double is within the bounds
|
||||
virtual bool isInBounds(double _r)
|
||||
virtual bool isInBounds(double _r) const
|
||||
{
|
||||
if (_r < repMinimum)
|
||||
return false;
|
||||
|
|
@ -238,7 +238,7 @@ public :
|
|||
}
|
||||
|
||||
// folds a value into bounds
|
||||
virtual void foldsInBounds(double & _r)
|
||||
virtual void foldsInBounds(double & _r) const
|
||||
{
|
||||
long iloc;
|
||||
double dlargloc = 2 * range() ;
|
||||
|
|
@ -267,7 +267,7 @@ public :
|
|||
}
|
||||
|
||||
// truncates to the bounds
|
||||
virtual void truncate(double & _r)
|
||||
virtual void truncate(double & _r) const
|
||||
{
|
||||
if (_r < repMinimum)
|
||||
_r = repMinimum;
|
||||
|
|
@ -298,7 +298,7 @@ public :
|
|||
}
|
||||
|
||||
/** for memory managements - ugly */
|
||||
virtual eoRealBounds * dup()
|
||||
virtual eoRealBounds * dup() const
|
||||
{
|
||||
return new eoRealInterval(*this);
|
||||
}
|
||||
|
|
@ -324,31 +324,31 @@ public :
|
|||
{}
|
||||
|
||||
// accessors
|
||||
virtual double minimum() { return repMinimum; }
|
||||
virtual double minimum() const { return repMinimum; }
|
||||
|
||||
virtual double maximum()
|
||||
virtual double maximum() const
|
||||
{
|
||||
throw std::logic_error("Trying to get maximum of eoRealBelowBound");
|
||||
}
|
||||
virtual double range()
|
||||
virtual double range() const
|
||||
{
|
||||
throw std::logic_error("Trying to get range of eoRealBelowBound");
|
||||
}
|
||||
|
||||
// random generators
|
||||
virtual double uniform(eoRng & _rng = eo::rng)
|
||||
virtual double uniform(eoRng & _rng = eo::rng) const
|
||||
{
|
||||
throw std::logic_error("Trying to generate uniform values in eoRealBelowBound");
|
||||
}
|
||||
|
||||
// description
|
||||
virtual bool isBounded(void) {return false;}
|
||||
virtual bool hasNoBoundAtAll(void) {return false;}
|
||||
virtual bool isMinBounded(void) {return true;}
|
||||
virtual bool isMaxBounded(void) {return false;}
|
||||
virtual bool isBounded(void) const {return false;}
|
||||
virtual bool hasNoBoundAtAll(void) const {return false;}
|
||||
virtual bool isMinBounded(void) const {return true;}
|
||||
virtual bool isMaxBounded(void) const {return false;}
|
||||
|
||||
// says if a given double is within the bounds
|
||||
virtual bool isInBounds(double _r)
|
||||
virtual bool isInBounds(double _r) const
|
||||
{
|
||||
if (_r < repMinimum)
|
||||
return false;
|
||||
|
|
@ -356,7 +356,7 @@ public :
|
|||
}
|
||||
|
||||
// folds a value into bounds
|
||||
virtual void foldsInBounds(double & _r)
|
||||
virtual void foldsInBounds(double & _r) const
|
||||
{
|
||||
// easy as a pie: symmetry w.r.t. minimum
|
||||
if (_r < repMinimum) // nothing to do otherwise
|
||||
|
|
@ -365,7 +365,7 @@ public :
|
|||
}
|
||||
|
||||
// truncates to the bounds
|
||||
virtual void truncate(double & _r)
|
||||
virtual void truncate(double & _r) const
|
||||
{
|
||||
if (_r < repMinimum)
|
||||
_r = repMinimum;
|
||||
|
|
@ -394,7 +394,7 @@ public :
|
|||
}
|
||||
|
||||
/** for memory managements - ugly */
|
||||
virtual eoRealBounds * dup()
|
||||
virtual eoRealBounds * dup() const
|
||||
{
|
||||
return new eoRealBelowBound(*this);
|
||||
}
|
||||
|
|
@ -419,31 +419,31 @@ public :
|
|||
{}
|
||||
|
||||
// accessors
|
||||
virtual double maximum() { return repMaximum; }
|
||||
virtual double maximum() const { return repMaximum; }
|
||||
|
||||
virtual double minimum()
|
||||
virtual double minimum() const
|
||||
{
|
||||
throw std::logic_error("Trying to get minimum of eoRealAboveBound");
|
||||
}
|
||||
virtual double range()
|
||||
virtual double range() const
|
||||
{
|
||||
throw std::logic_error("Trying to get range of eoRealAboveBound");
|
||||
}
|
||||
|
||||
// random generators
|
||||
virtual double uniform(eoRng & _rng = eo::rng)
|
||||
virtual double uniform(eoRng & _rng = eo::rng) const
|
||||
{
|
||||
throw std::logic_error("Trying to generate uniform values in eoRealAboveBound");
|
||||
}
|
||||
|
||||
// description
|
||||
virtual bool isBounded(void) {return false;}
|
||||
virtual bool hasNoBoundAtAll(void) {return false;}
|
||||
virtual bool isMinBounded(void) {return false;}
|
||||
virtual bool isMaxBounded(void) {return true;}
|
||||
virtual bool isBounded(void) const {return false;}
|
||||
virtual bool hasNoBoundAtAll(void) const {return false;}
|
||||
virtual bool isMinBounded(void) const {return false;}
|
||||
virtual bool isMaxBounded(void) const {return true;}
|
||||
|
||||
// says if a given double is within the bounds
|
||||
virtual bool isInBounds(double _r)
|
||||
virtual bool isInBounds(double _r) const
|
||||
{
|
||||
if (_r > repMaximum)
|
||||
return false;
|
||||
|
|
@ -451,7 +451,7 @@ public :
|
|||
}
|
||||
|
||||
// folds a value into bounds
|
||||
virtual void foldsInBounds(double & _r)
|
||||
virtual void foldsInBounds(double & _r) const
|
||||
{
|
||||
// easy as a pie: symmetry w.r.t. maximum
|
||||
if (_r > repMaximum) // nothing to do otherwise
|
||||
|
|
@ -460,7 +460,7 @@ public :
|
|||
}
|
||||
|
||||
// truncates to the bounds
|
||||
virtual void truncate(double & _r)
|
||||
virtual void truncate(double & _r) const
|
||||
{
|
||||
if (_r > repMaximum)
|
||||
_r = repMaximum;
|
||||
|
|
@ -489,7 +489,7 @@ public :
|
|||
}
|
||||
|
||||
/** for memory managements - ugly */
|
||||
virtual eoRealBounds * dup()
|
||||
virtual eoRealBounds * dup() const
|
||||
{
|
||||
return new eoRealAboveBound(*this);
|
||||
}
|
||||
|
|
@ -498,4 +498,152 @@ private :
|
|||
double repMaximum;
|
||||
};
|
||||
|
||||
//////////////////////// tentative for a general BOUND class that is constructed from a string
|
||||
|
||||
class eoGeneralRealBounds : public eoRealBounds
|
||||
{
|
||||
public:
|
||||
/** Ctor: from a string, chooses the type of bound */
|
||||
eoGeneralRealBounds(std::string _s)
|
||||
{
|
||||
repBound = getBoundsFromString(_s);
|
||||
}
|
||||
|
||||
/** Need a Cpy Ctor because we are allocating memory */
|
||||
eoGeneralRealBounds(eoGeneralRealBounds & _b)
|
||||
{
|
||||
// replicate the embedded bound (I'm pretty sure there is another
|
||||
// way to do that !!!
|
||||
|
||||
bool minBounded = _b.isMinBounded();
|
||||
bool maxBounded = _b.isMaxBounded();
|
||||
double minimum, maximum;
|
||||
const eoRealBounds & bb = _b.theBounds();
|
||||
if (minBounded) minimum = bb.minimum();
|
||||
if (maxBounded) maximum = bb.maximum();
|
||||
|
||||
if (minBounded && maxBounded)
|
||||
repBound = new eoRealInterval(minimum, maximum);
|
||||
else if (!minBounded && !maxBounded) // no bound at all
|
||||
repBound = new eoRealNoBounds;
|
||||
else if (!minBounded && maxBounded)
|
||||
repBound = new eoRealAboveBound(maximum);
|
||||
else if (minBounded && !maxBounded)
|
||||
repBound = new eoRealBelowBound(minimum);
|
||||
}
|
||||
|
||||
eoGeneralRealBounds& operator=(const eoGeneralRealBounds& _b)
|
||||
{
|
||||
// replicate the embedded bound (I'm pretty sure there is another
|
||||
// way to do that !!!
|
||||
|
||||
bool minBounded = _b.isMinBounded();
|
||||
bool maxBounded = _b.isMaxBounded();
|
||||
double minimum, maximum;
|
||||
const eoRealBounds & bb = _b.theBounds();
|
||||
if (minBounded) minimum = bb.minimum();
|
||||
if (maxBounded) maximum = bb.maximum();
|
||||
|
||||
// first delete the embedded bounds if necessary
|
||||
if (repBound)
|
||||
delete repBound;
|
||||
// now reallocate
|
||||
if (minBounded && maxBounded)
|
||||
repBound = new eoRealInterval(minimum, maximum);
|
||||
else if (!minBounded && !maxBounded) // no bound at all
|
||||
repBound = new eoRealNoBounds;
|
||||
else if (!minBounded && maxBounded)
|
||||
repBound = new eoRealAboveBound(maximum);
|
||||
else if (minBounded && !maxBounded)
|
||||
repBound = new eoRealBelowBound(minimum);
|
||||
return (*this);
|
||||
}
|
||||
|
||||
|
||||
/** Need a Dtor because we allocate an actual bound */
|
||||
~eoGeneralRealBounds()
|
||||
{
|
||||
delete repBound;
|
||||
}
|
||||
|
||||
///// and now all methods from the embedded bounds
|
||||
/** Self-Test: true if ***both*** a min and a max
|
||||
*/
|
||||
virtual bool isBounded(void) const {return repBound->isBounded();}
|
||||
|
||||
/** Self-Test: true if no min ***and*** no max
|
||||
* hence no further need to test/truncate/fold anything
|
||||
*/
|
||||
virtual bool hasNoBoundAtAll(void) const {return repBound->hasNoBoundAtAll();}
|
||||
|
||||
/** Self-Test: bounded from below???
|
||||
*/
|
||||
virtual bool isMinBounded(void) const {return repBound->isMinBounded();}
|
||||
|
||||
/** Self-Test: bounded from above???
|
||||
*/
|
||||
virtual bool isMaxBounded(void) const {return repBound->isMaxBounded();}
|
||||
|
||||
/** Test on a value: is it in bounds?
|
||||
*/
|
||||
virtual bool isInBounds(double _x) const {return repBound->isInBounds(_x);}
|
||||
|
||||
/** Put value back into bounds - by folding back and forth
|
||||
*/
|
||||
virtual void foldsInBounds(double & _x) const {return repBound->foldsInBounds(_x);}
|
||||
|
||||
/** Put value back into bounds - by truncating to a boundary value
|
||||
*/
|
||||
virtual void truncate(double & _x) const {return repBound->truncate(_x);}
|
||||
|
||||
/** get minimum value
|
||||
* @std::exception if does not exist
|
||||
*/
|
||||
virtual double minimum() const {return repBound->minimum();}
|
||||
/** get maximum value
|
||||
* @std::exception if does not exist
|
||||
*/
|
||||
virtual double maximum() const {return repBound->maximum();}
|
||||
/** get range
|
||||
* @std::exception if unbounded
|
||||
*/
|
||||
virtual double range() const {return repBound->range();}
|
||||
|
||||
/** random generator of uniform numbers in bounds
|
||||
* @std::exception if unbounded
|
||||
*/
|
||||
virtual double uniform(eoRng & _rng = eo::rng) const {return repBound->uniform();}
|
||||
|
||||
/** for memory managements - ugly */
|
||||
virtual eoRealBounds * dup() const {return repBound->dup();}
|
||||
|
||||
/** for efficiency, it's better to use the embedded boud directly */
|
||||
const eoRealBounds & theBounds() const { return *repBound;}
|
||||
|
||||
/** don't forget the printOn method -
|
||||
* again that of the embedded bound
|
||||
*/
|
||||
virtual void printOn(std::ostream& _os) const
|
||||
{
|
||||
repBound->printOn(_os);
|
||||
}
|
||||
|
||||
/** no readFrom ??? Have to check that later */
|
||||
virtual void readFrom(std::istream& _is)
|
||||
{
|
||||
std::string s;
|
||||
_is >> s;
|
||||
if (repBound)
|
||||
delete repBound;
|
||||
repBound = getBoundsFromString(s);
|
||||
}
|
||||
|
||||
private:
|
||||
// reading from a string
|
||||
eoRealBounds * getBoundsFromString(std::string);
|
||||
|
||||
eoRealBounds * repBound;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
|
|
|||
Reference in a new issue