More generic eoDualFitness

Allow an imitialization on a double only, BUT an assert fails if it is not 0.0
Remove friendship on operators, because there is two differerent templated classes declared later.
More operators with base types.
This commit is contained in:
Johann Dreo 2013-06-07 17:32:30 +02:00
commit 10ea97f78d

View file

@ -81,10 +81,23 @@ public:
* Unfeasible by default * Unfeasible by default
*/ */
eoDualFitness() : eoDualFitness() :
_value(), _value(0.0),
_is_feasible(false) _is_feasible(false)
{} {}
//! Empty initialization
/*!
* Unfeasible by default
*/
template<class T>
eoDualFitness( T value ) :
_value(value),
_is_feasible(false)
{
assert( _value == 0 );
}
//! Copy constructor //! Copy constructor
eoDualFitness(const eoDualFitness& other) : eoDualFitness(const eoDualFitness& other) :
_value(other._value), _value(other._value),
@ -126,8 +139,8 @@ public:
//! Copy operator from a std::pair //! Copy operator from a std::pair
eoDualFitness& operator=(const std::pair<BaseType,bool>& v) eoDualFitness& operator=(const std::pair<BaseType,bool>& v)
{ {
_value = v.first; this->_value = v.first;
_is_feasible = v.second; this->_is_feasible = v.second;
return *this; return *this;
} }
@ -142,13 +155,16 @@ public:
return *this; return *this;
} }
//! Copy operator from a std::pair /*
//! Copy operator from a scalar
template<class T> template<class T>
eoDualFitness& operator=(const T v) eoDualFitness& operator=(const T v)
{ {
_value = v; this->_value = v;
this->_is_feasible = false;
return *this; return *this;
} }
*/
//! Comparison that separate feasible individuals from unfeasible ones. Feasible are always better //! Comparison that separate feasible individuals from unfeasible ones. Feasible are always better
/*! /*!
@ -195,55 +211,74 @@ public:
* (like eoStat), both for separating feasibility and for aggregating them. * (like eoStat), both for separating feasibility and for aggregating them.
*/ */
// NOTE: we cannot declare this set of operator classes as friend, because there is two differerent templated classes declared later
// (for minimizing and maximizing)
//! Add a given fitness to the current one //! Add a given fitness to the current one
template <class F, class Cmp> template <class F, class Cmp>
friend // friend
eoDualFitness<F,Cmp> & operator+=( eoDualFitness<F,Cmp> & from, const eoDualFitness<F,Cmp> & that ) eoDualFitness<F,Cmp> & operator+=( /*eoDualFitness<F,Cmp> & from,*/ const eoDualFitness<F,Cmp> & that )
{ {
from._value += that._value; // from._value += that._value;
this->_value += that._value;
// true only if the two are feasible, else false // true only if the two are feasible, else false
from._is_feasible = from._is_feasible && that._is_feasible; // from._is_feasible = from._is_feasible && that._is_feasible;
this->_is_feasible = this->_is_feasible && that._is_feasible;
return from; return *this;
} }
//! Substract a given fitness to the current one //! Substract a given fitness to the current one
template <class F, class Cmp> template <class F, class Cmp>
friend eoDualFitness<F,Cmp> & operator-=( const eoDualFitness<F,Cmp> & that )
eoDualFitness<F,Cmp> & operator-=( eoDualFitness<F,Cmp> & from, const eoDualFitness<F,Cmp> & that )
{ {
from._value -= that._value; this->_value -= that._value;
// true only if the two are feasible, else false // true only if the two are feasible, else false
from._is_feasible = from._is_feasible && that._is_feasible; this->_is_feasible = this->_is_feasible && that._is_feasible;
return from; return *this;
} }
//! Add a given fitness to the current one
template <class T, class F, class Cmp>
eoDualFitness<F,Cmp> & operator+=( const T that )
{
this->_value += that;
return *this;
}
//! Substract a given fitness to the current one
template <class T, class F, class Cmp>
eoDualFitness<F,Cmp> & operator-=( const T that )
{
this->_value -= that;
return *this;
}
//! Add a given fitness to the current one //! Add a given fitness to the current one
template <class F, class Cmp> template <class F, class Cmp>
friend eoDualFitness<F,Cmp> & operator/=( const eoDualFitness<F,Cmp> & that )
eoDualFitness<F,Cmp> & operator/=( eoDualFitness<F,Cmp> & from, const eoDualFitness<F,Cmp> & that )
{ {
from._value /= that._value; this->_value /= that._value;
// true only if the two are feasible, else false // true only if the two are feasible, else false
from._is_feasible = from._is_feasible && that._is_feasible; this->_is_feasible = this->_is_feasible && that._is_feasible;
return from; return *this;
} }
//! Add a given fitness to the current one //! Add a given fitness to the current one
template <class T, class F, class Cmp> template <class T, class F, class Cmp>
friend eoDualFitness<F,Cmp> & operator/=( T that )
eoDualFitness<F,Cmp> & operator/=( eoDualFitness<F,Cmp> & from, T that )
{ {
from._value /= that; this->_value /= that;
return from; return *this;
} }
// Add this fitness's value to that other, and return a _new_ instance with the result. // Add this fitness's value to that other, and return a _new_ instance with the result.
@ -273,17 +308,15 @@ public:
//! Print an eoDualFitness instance as a pair of numbers, separated by a space //! Print an eoDualFitness instance as a pair of numbers, separated by a space
template <class F, class Cmp> template <class F, class Cmp>
friend std::ostream& operator<<(std::ostream& os)
std::ostream& operator<<(std::ostream& os, const eoDualFitness<F, Cmp>& f)
{ {
os << f._value << " " << f._is_feasible; os << this->_value << " " << this->_is_feasible;
return os; return os;
} }
//! Read an eoDualFitness instance as a pair of numbers, separated by a space //! Read an eoDualFitness instance as a pair of numbers, separated by a space
template <class F, class Cmp> template <class F, class Cmp>
friend std::istream& operator>>(std::istream& is)
std::istream& operator>>(std::istream& is, eoDualFitness<F, Cmp>& f)
{ {
F value; F value;
is >> value; is >> value;
@ -291,7 +324,8 @@ public:
bool feasible; bool feasible;
is >> feasible; is >> feasible;
f = std::make_pair<F,bool>( value, feasible ); this->_value = value;
this->_is_feasible = feasible;
return is; return is;
} }
}; };