From 8340168ee8dbb26a28db34c21c6ef6cfd12ec741 Mon Sep 17 00:00:00 2001 From: Johann Dreo Date: Thu, 20 Jun 2013 13:48:28 +0200 Subject: [PATCH] BUGFIX: correct handling of initialization security in dual fitness Remove the operator= overloads. Represent badly initialized dual fitness by a "?" for the feasibility. --- eo/src/eoDualFitness.h | 61 ++++++++++++++---------------------------- 1 file changed, 20 insertions(+), 41 deletions(-) diff --git a/eo/src/eoDualFitness.h b/eo/src/eoDualFitness.h index a6d6aa54e..32bd91851 100644 --- a/eo/src/eoDualFitness.h +++ b/eo/src/eoDualFitness.h @@ -111,20 +111,17 @@ public: * If you use this interface, you MUST set the feasibility BEFORE * asking for it or the value. Or else, an assert will fail in debug mode. */ - template - eoDualFitness( T value ) : + eoDualFitness( const double value ) : _value(value), _is_feasible(false), _feasible_init(false) - { - } - + {} //! Copy constructor eoDualFitness(const eoDualFitness& other) : _value(other._value), _is_feasible(other._is_feasible), - _feasible_init(true) + _feasible_init(other._feasible_init) {} //! Constructor from explicit value/feasibility @@ -159,7 +156,7 @@ public: //! Explicitly set the feasibility. Useful if you have used previously the instantiation on a single scalar. inline void is_feasible( bool feasible ) { - this->is_feasible( feasible ); + this->_is_feasible = feasible; this->_feasible_init = true; } @@ -169,37 +166,6 @@ public: return _value; } - //! Copy operator from a std::pair - eoDualFitness& operator=( const std::pair& v ) - { - this->_value = v.first; - this->is_feasible( v.second ); - this->_feasible_init = true; - return *this; - } - - //! Copy operator from another eoDualFitness - template - eoDualFitness & operator=( const eoDualFitness& other ) - { - if (this != &other) { - this->_value = other._value; - this->is_feasible( other.is_feasible() ); - this->_feasible_init = other._feasible_init; - } - return *this; - } - - //! Copy operator from a scalar - template - eoDualFitness& operator=(const T v) - { - this->_value = v; - this->_is_feasible = false; - this->_feasible_init = false; - return *this; - } - //! Comparison that separate feasible individuals from unfeasible ones. Feasible are always better /*! * Use less as a default comparison operator @@ -236,7 +202,7 @@ public: bool operator>=(const eoDualFitness& other ) const { return !(*this < other); } //! Equal: if the other is equal to me - bool operator==(const eoDualFitness& other) const { return ( _is_feasible == other._is_feasible ) && ( _value == other._value ); } + bool operator==(const eoDualFitness& other) const { return ( this->is_feasible() == other.is_feasible() ) && ( _value == other._value ); } public: @@ -263,9 +229,11 @@ public: this->_value += that._value; // 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; + // If the other was not correctly initialized + this->_feasible_init = that._feasible_init; + return *this; } @@ -282,9 +250,13 @@ public: { this->_value -= that._value; + // true only if the two are feasible, else false this->_is_feasible = this->_is_feasible && that._is_feasible; + // If the other was not correctly initialized + this->_feasible_init = that._feasible_init; + return *this; } @@ -305,6 +277,9 @@ public: // true only if the two are feasible, else false this->_is_feasible = this->_is_feasible && that._is_feasible; + // If the other was not correctly initialized + this->_feasible_init = that._feasible_init; + return *this; } @@ -355,7 +330,11 @@ public: friend std::ostream& operator<<( std::ostream& os, const eoDualFitness & fitness ) { - os << fitness._value << " " << fitness.is_feasible(); + if( fitness._feasible_init ) { + os << fitness._value << " " << fitness.is_feasible(); + } else { + os << fitness._value << " ?"; + } return os; }