diff --git a/eo/src/eo b/eo/src/eo index 38c61010..95b80408 100644 --- a/eo/src/eo +++ b/eo/src/eo @@ -104,6 +104,8 @@ #include #include #include +#include +#include #include // the batch selection - from an eoSelectOne #include diff --git a/eo/src/eoFitnessScalingSelect.h b/eo/src/eoFitnessScalingSelect.h new file mode 100644 index 00000000..6155dc56 --- /dev/null +++ b/eo/src/eoFitnessScalingSelect.h @@ -0,0 +1,107 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// eoFitnessScalingSelect.h +// (c) GeNeura Team, 1998, Maarten Keijzer 2000, Marc Schoenauer 2001 +/* + 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; either + version 2 of the License, or (at your option) any later version. + + 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: todos@geneura.ugr.es, http://geneura.ugr.es + Marc.Schoenauer@polytechnique.fr + mak@dhi.dk + */ +//----------------------------------------------------------------------------- + +#ifndef eoFitnessScalingSelect_h +#define eoFitnessScalingSelect_h + +//----------------------------------------------------------------------------- + +#include +#include +#include + +//----------------------------------------------------------------------------- +/** eoFitnessScalingSelect: select an individual proportional to its rank + this is actually the linearRanking +*/ +//----------------------------------------------------------------------------- + +template class eoFitnessScalingSelect: public eoSelectOne +{ +public: + typedef typename EOT::Fitness Fitness; + /** Ctor: + * @param _p the selective pressure, should be in [1,2] (2 is the default) + * @param _pop an optional population + */ + eoFitnessScalingSelect(double _p = 2.0, const eoPop& _pop = eoPop()): + pressure(_p), scaledFitness(0) + { + if (minimizing_fitness()) + throw logic_error("eoFitnessScalingSelect: minimizing fitness"); + if (_pop.size() > 0) // a population in Ctor? initialize scaledFitness + { + setup(_pop); + } + } + + // COmputes the coefficients of the linear transform in such a way that + // Pselect(Best) == pressure/sizePop + // Pselect(average) == 1.0/sizePop + // we truncate negative values to 0 - + // we could also adjust the pressure so that worst get 0 scaled fitness + void setup(const eoPop& _pop) + { + unsigned pSize =_pop.size(); + scaledFitness.resize(pSize); + + // best and worse fitnesses + double bestFitness = static_cast (_pop.best_element().fitness()); + double worstFitness = static_cast (_pop.worse_element().fitness()); + + // average fitness + double sum=0.0; + for (unsigned i=0; i(_pop[i].fitness()); + double averageFitness = sum/pSize; + + // the coefficients for linear scaling + double denom = pSize*(bestFitness - averageFitness); + double alpha = (pressure-1)/denom; + double beta = (bestFitness - pressure*averageFitness)/denom; +// if (beta < 0) +// beta = max(beta, -alpha*worstFitness); + for (unsigned i=0; i& _pop) + { + unsigned selected = rng.roulette_wheel(scaledFitness); + return _pop[selected]; + } + +private : + double pressure; + vector scaledFitness; +}; + +#endif + diff --git a/eo/src/eoRankingSelect.h b/eo/src/eoRankingSelect.h new file mode 100644 index 00000000..ee213dff --- /dev/null +++ b/eo/src/eoRankingSelect.h @@ -0,0 +1,90 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// eoRankingSelect.h +// (c) GeNeura Team, 1998, Maarten Keijzer 2000, Marc Schoenauer 2001 +/* + 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; either + version 2 of the License, or (at your option) any later version. + + 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: todos@geneura.ugr.es, http://geneura.ugr.es + Marc.Schoenauer@polytechnique.fr + mak@dhi.dk + */ +//----------------------------------------------------------------------------- + +#ifndef eoRankingSelect_h +#define eoRankingSelect_h + +//----------------------------------------------------------------------------- + +#include +#include +#include + +//----------------------------------------------------------------------------- +/** eoRankingSelect: select an individual proportional to its rank + this is actually the linearRanking +*/ +//----------------------------------------------------------------------------- + +template class eoRankingSelect: public eoSelectOne +{ +public: + /** Ctor: + * @param _p the selective pressure, should be in [1,2] (2 is the default) + * @param _pop an optional population + */ + eoRankingSelect(double _p = 2.0, const eoPop& _pop = eoPop()): + pressure(_p), rank(0), rankFitness(0) + { + if (_pop.size() > 0) + { + setup(_pop); + } + } + + // COmputes the coefficients of the linear transform uin such a way that + // Pselect(Best) == Pselect(sizePop) == pressure/sizePop + // Pselect(average) == 1.0/sizePop + // Pselect(Worst == Pselect(1 == (2-pressure)/sizePop + void setup(const eoPop& _pop) + { + _pop.sort(rank); + unsigned pSize =_pop.size(); + rankFitness.resize(pSize); + double alpha = (2*pressure-2)/(pSize*(pSize-1)); + double beta = (2-pressure)/pSize; + for (unsigned i=0; i& _pop) + { + unsigned selected = rng.roulette_wheel(rankFitness); + return *(rank[selected]); + } + +private : + double pressure; + vector rank; + vector rankFitness; +}; + +#endif +