diff --git a/eo/src/eoDualFitness.h b/eo/src/eoDualFitness.h index 3fb9c0a37..a758be051 100644 --- a/eo/src/eoDualFitness.h +++ b/eo/src/eoDualFitness.h @@ -29,6 +29,10 @@ Authors: #include #include #include // for std::pair +#include + +#include +#include /** @addtogroup Evaluation * @{ @@ -63,7 +67,7 @@ Authors: template class eoDualFitness { -private: +protected: //! Scalar type of the fitness (generally a double) BaseType _value; @@ -241,6 +245,68 @@ typedef eoDualFitness > eoMinimizingDualFitness; template< class EOT> bool eoIsFeasible ( const EOT & sol ) { return sol.fitness().is_feasible(); } + +/** Embed two eoStat and call the first one on the feasible individuals and + * the second one on the unfeasible ones, merge the two resulting value in + * a string, separated by a given marker. + */ +//template +template +class eoDualStatSwitch : public eoStat< EOT, std::string > +{ +public: + using eoStat::value; + +// eoDualStatSwitch( eoStat & stat_feasible, eoStat & stat_unfeasible, std::string sep=" " ) : + eoDualStatSwitch( EOSTAT & stat_feasible, EOSTAT & stat_unfeasible, std::string sep=" " ) : + _stat_feasible(stat_feasible), + _stat_unfeasible(stat_unfeasible), + _sep(sep), + eoStat( + "?"+sep+"?", + stat_feasible.longName()+sep+stat_unfeasible.longName() + ) + { } + + virtual void operator()( const eoPop & pop ) + { + eoPop pop_feasible; + pop_feasible.reserve(pop.size()); + + eoPop pop_unfeasible; + pop_unfeasible.reserve(pop.size()); + + for( typename eoPop::const_iterator ieot=pop.begin(), iend=pop.end(); ieot!=iend; ++ieot ) { + /* + if( ieot->invalid() ) { + eo::log << eo::errors << "ERROR: trying to access to an invalid fitness" << std::endl; + } + */ + if( ieot->fitness().is_feasible() ) { + pop_feasible.push_back( *ieot ); + } else { + pop_unfeasible.push_back( *ieot ); + } + } + + _stat_feasible( pop_feasible ); + _stat_unfeasible( pop_unfeasible ); + + std::ostringstream out; + out << _stat_feasible.value() << _sep << _stat_unfeasible.value(); + + value() = out.str(); + } + +protected: +// eoStat & _stat_feasible; +// eoStat & _stat_unfeasible; + EOSTAT & _stat_feasible; + EOSTAT & _stat_unfeasible; + + std::string _sep; +}; + /** @} */ #endif // _eoDualFitness_h_