From c9da01c70de5f35f5548127f743fd0cef927089c Mon Sep 17 00:00:00 2001 From: Johann Dreo Date: Fri, 7 Jun 2013 12:42:27 +0200 Subject: [PATCH] Add missing arithmetic operators to eoDualFitness --- eo/src/eoDualFitness.h | 52 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/eo/src/eoDualFitness.h b/eo/src/eoDualFitness.h index 674fe075c..7bf88ed47 100644 --- a/eo/src/eoDualFitness.h +++ b/eo/src/eoDualFitness.h @@ -108,12 +108,11 @@ public: * type, if needed. For example, this is possible: * eoDualFitness > fit; * double val = 1.0; - * fit = val; * val = fit; */ - operator BaseType(void) const { return _value; } + operator BaseType(void) const { return _value; } + - inline bool is_feasible() const { return _is_feasible; @@ -143,6 +142,14 @@ public: return *this; } + //! Copy operator from a std::pair + template + eoDualFitness& operator=(const T v) + { + _value = v; + return *this; + } + //! Comparison that separate feasible individuals from unfeasible ones. Feasible are always better /*! * Use less as a default comparison operator @@ -183,6 +190,11 @@ public: public: + /* FIXME it would be better to raise errors (or warnings) if one try to apply arithmetics operators between feasible + * and unfeasible fitnesses. This necessitates to add wrappers for operators that aggregates sets of dual fitnesses + * (like eoStat), both for separating feasibility and for aggregating them. + */ + //! Add a given fitness to the current one template friend @@ -209,6 +221,31 @@ public: return from; } + + //! Add a given fitness to the current one + template + friend + eoDualFitness & operator/=( eoDualFitness & from, const eoDualFitness & that ) + { + from._value /= that._value; + + // true only if the two are feasible, else false + from._is_feasible = from._is_feasible && that._is_feasible; + + return from; + } + + + //! Add a given fitness to the current one + template + friend + eoDualFitness & operator/=( eoDualFitness & from, T that ) + { + from._value /= that; + + return from; + } + // Add this fitness's value to that other, and return a _new_ instance with the result. template eoDualFitness operator+(const eoDualFitness & that) @@ -225,6 +262,15 @@ public: return from -= that; } + + // Add this fitness's value to that other, and return a _new_ instance with the result. + template + eoDualFitness operator/(const eoDualFitness & that) + { + eoDualFitness from( *this ); + return from /= that; + } + //! Print an eoDualFitness instance as a pair of numbers, separated by a space template friend