From 47c4f58eb8a989cc387cccedcadd795a1fb31e06 Mon Sep 17 00:00:00 2001 From: Johann Dreo Date: Mon, 25 Oct 2010 09:50:50 +0200 Subject: [PATCH] Stat object to compute ratio of feasible indviduals in a pop using eoDualFitness --- eo/src/eoDualFitness.h | 16 ++++++++ eo/src/utils/eoFeasibleRatioStat.h | 65 ++++++++++++++++++++++++++++++ eo/src/utils/eoStat.h | 11 +++-- 3 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 eo/src/utils/eoFeasibleRatioStat.h diff --git a/eo/src/eoDualFitness.h b/eo/src/eoDualFitness.h index c57d9fcc7..946f95c6c 100644 --- a/eo/src/eoDualFitness.h +++ b/eo/src/eoDualFitness.h @@ -50,6 +50,11 @@ Authors: * * This class overrides operator<() to use the Compare template argument and handle feasibility. * Over operators are coded using this sole function. + * + * Standard arithmetic operators are provided to add or substract dual fitnesses. + * They behave as expected for the fitness value and gives priority to unfeasible fitness + * (i.e. when adding or substracting dual fitness, the only case when the result will be + * a feasible fitness is when both are feasible, else the result is an unfeasibe fitness) */ template class eoDualFitness @@ -90,6 +95,11 @@ public: _is_feasible(dual.second) {} + inline bool is_feasible() const + { + return _is_feasible; + } + //! Copy operator eoDualFitness& operator=(const eoDualFitness& other) { @@ -216,5 +226,11 @@ typedef eoDualFitness > eoMaximizingDualFitness; //! Compare dual fitnesses as if we were minimizing typedef eoDualFitness > eoMinimizingDualFitness; +//! A predicate that returns the feasibility of a given dual fitness +/** Use this in STL algorithm that use binary predicates (e.g. count_if, find_if, etc.) + */ +template< class EOT> +bool eoIsFeasible ( const EOT & sol ) { return sol.fitness().is_feasible(); } + #endif // _eoDualFitness_h_ diff --git a/eo/src/utils/eoFeasibleRatioStat.h b/eo/src/utils/eoFeasibleRatioStat.h new file mode 100644 index 000000000..1092e9036 --- /dev/null +++ b/eo/src/utils/eoFeasibleRatioStat.h @@ -0,0 +1,65 @@ + +/* + +(c) 2010 Thales group + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; version 2 + of the License. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +Contact: http://eodev.sourceforge.net + +Authors: + Johann Dréo + +*/ + +#ifndef _eoFeasibleRatioStat_h_ +#define _eoFeasibleRatioStat_h_ + +#include + +#include +#include + +#include "eoStat.h" + +//! Ratio of the number of individuals with a feasible dual fitness in the population (@see eoDualFitness) +template +class eoFeasibleRatioStat : public eoStat< EOT, double > +{ +public: + using eoStat::value; + + eoFeasibleRatioStat( std::string description = "FeasibleRatio" ) : eoStat( 0.0, description ) {} + + virtual void operator()( const eoPop & pop ) + { +#ifndef NDEBUG + assert( pop.size() > 0 ); + + double count = static_cast( std::count_if( pop.begin(), pop.end(), eoIsFeasible ) ); + double size = static_cast( pop.size() ); + double ratio = count/size; + eo::log << eo::xdebug << "eoFeasibleRatioStat: " << count << " / " << size << " = " << ratio << std::endl; + value() = ratio; +#else + value() = static_cast( std::count_if( pop.begin(), pop.end(), eoIsFeasible ) ) / static_cast( pop.size() ); +#endif + } + + virtual std::string className(void) const { return "eoFeasibleRatioStat"; } +}; + +#endif // _eoFeasibleRatioStat_h_ + diff --git a/eo/src/utils/eoStat.h b/eo/src/utils/eoStat.h index bc267e11e..6897bfb54 100644 --- a/eo/src/utils/eoStat.h +++ b/eo/src/utils/eoStat.h @@ -3,6 +3,7 @@ //----------------------------------------------------------------------------- // eoStat.h // (c) Marc Schoenauer, Maarten Keijzer and GeNeura Team, 2000 +// (c) 2010 Thales group /* This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -18,9 +19,13 @@ License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - Contact: todos@geneura.ugr.es, http://geneura.ugr.es - Marc.Schoenauer@polytechnique.fr - mkeijzer@dhi.dk +Contact: http://eodev.sourceforge.net + + Authors: + todos@geneura.ugr.es, http://geneura.ugr.es + Marc.Schoenauer@polytechnique.fr + mkeijzer@dhi.dk + Johann Dréo */ //-----------------------------------------------------------------------------