From 06ac548f96a1867936bb4eac0fcfc22bdb684eb9 Mon Sep 17 00:00:00 2001 From: evomarc Date: Fri, 23 Aug 2002 15:56:09 +0000 Subject: [PATCH] Adding more repalcements utilities (see do/make_easea_algo.h) --- eo/src/eoOneToOneBreeder.h | 122 ++++++++++++++++++++++++++++++++++ eo/src/eoSharing.h | 131 +++++++++++++++++++++++++++++++++++++ eo/src/eoTruncSelect.h | 73 +++++++++++++++++++++ 3 files changed, 326 insertions(+) create mode 100644 eo/src/eoOneToOneBreeder.h create mode 100644 eo/src/eoSharing.h create mode 100644 eo/src/eoTruncSelect.h diff --git a/eo/src/eoOneToOneBreeder.h b/eo/src/eoOneToOneBreeder.h new file mode 100644 index 00000000..7b85d869 --- /dev/null +++ b/eo/src/eoOneToOneBreeder.h @@ -0,0 +1,122 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// eoOneToOneBreeder.h +// (c) Maarten Keijzer and 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: mkeijzer@dhi.dk + Marc.Schoenauer@polytechnique.fr + */ +//----------------------------------------------------------------------------- + +#ifndef eoOneToOneBreeder_h +#define eoOneToOneBreeder_h + +//----------------------------------------------------------------------------- + +#include +#include +#include +#include +#include +#include + +/***************************************************************************** + * eoOneToOneBreeder: transforms a population using + * - an operator that MODIFIES only one parent from the populator + * (though it can use any number aside) and thus generates ONE offspring) + * - a local replacement between the parent and its offspring + * + * Typically, Differential Evolution (Storn and Price 94) and Deb et al's + * G3 can be built on this + **************************************************************************** + */ +template +class eoOneToOneBreeder: public eoBreed +{ + public: + /** Ctor: + * @param _op a general operator (must MODIFY only ONE parent) + * @param _eval an eoEvalFunc to evaluate the offspring + * @param _select a selectoOne, to be used for all selections [sequential] + * @param _pReplace probability that the best of parent/offspring wins [1] + * @param _howMany eoHowMany offpsring to generate [100%] + */ + eoOneToOneBreeder( + eoGenOp& _op, + eoEvalFunc & _eval, + double _pReplace = 1.0, + eoHowMany _howMany = eoHowMany(1.0) ) : + op(_op), eval(_eval), select( false ), + pReplace(_pReplace), howMany(_howMany) {} + + + /** The breeder: iteratively calls the genOp ONCE on a selective populator + * after having recorded the parent + * Then does the replacement + * + * @param _parents the initial population + * @param _offspring the resulting population (content -if any- is lost) + */ + void operator()(const eoPop& _parents, eoPop& _offspring) + { + unsigned target = howMany(_parents.size()); + + _offspring.clear(); + eoSelectivePopulator popit(_parents, _offspring, select); + + for (unsigned iParent=0; iParent<_parents.size(); iParent++) + { + unsigned pos = popit.tellp(); // remember current position + EOT theParent = *popit; // remember the parent itself + + // now apply operator - will modify the parent + op(popit); + + // replacement + EOT & leOffspring = *popit; + + // check: only one offspring? + unsigned posEnd = popit.tellp(); + if (posEnd != pos) + throw runtime_error("Operator can only generate a SINGLE offspring in eoOneToOneBreeder"); + + // do the tournament between parent and offspring + eval(leOffspring); // first need to evaluate the offspring + if (theParent > leOffspring) // old parent better than offspring + if (rng.uniform() < pReplace) // if probability + leOffspring = theParent; // replace + cout << "============ Final =========================\n"; + cout << _offspring << endl; + // finally, go to next guy to handle + ++popit; + } + } + + /// The class name. + virtual string className() const { return "eoOneToOneBreeder"; } + + private: + eoGenOp& op; + eoEvalFunc & eval; + eoSequentialSelect select; + double pReplace; + eoHowMany howMany; +}; + +#endif + diff --git a/eo/src/eoSharing.h b/eo/src/eoSharing.h new file mode 100644 index 00000000..f2178adf --- /dev/null +++ b/eo/src/eoSharing.h @@ -0,0 +1,131 @@ +/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + + ----------------------------------------------------------------------------- + eoSharing.h + (c) Maarten Keijzer, 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: Marc.Schoenauer@inria.fr + mkeijzer@dhi.dk + */ +//----------------------------------------------------------------------------- + +#ifndef eoSharing_h +#define eoPerf2Worth_h + +#include +#include + +/** Sharing is a perf2worth class that implements + * Goldberg and Richardson's basic sharing +*/ +// template > +template +class eoSharing : public eoPerf2Worth +{ + public: + /** Ctor with only nicheSize: will use the default eoQuadDistance */ + eoSharing(double _nicheSize) : eoPerf2Worth("Sharing"), + nicheSize(_nicheSize), + dist(repDist) + {} + + eoSharing(double _nicheSize, Dist _dist) : eoPerf2Worth("Sharing"), + nicheSize(_nicheSize), + dist(_dist) + {} + + /** Computes shared fitnesses + */ + void operator()(const eoPop& _pop) + { + unsigned i, j, + pSize=_pop.size(); + if (pSize <= 1) + throw runtime_error("Apptempt to do sharing with population of size 1"); + value.resize(pSize); + vector sim(pSize); // to hold the similarities + vector distMatrix(pSize*(pSize-1)/2); // to hold the distances + + // compute the distances + distMatrix(0,0)=0; + for (i=1; i + { + public: + // Ctor : sets size + dMatrix(unsigned _s) : vector(_s*(_s-1)), rSize(_s) {} + + /** simple accessor */ + double operator()(unsigned _i, unsigned _j) const + { + return this->operator[](_i*rSize + _j); + } + + /** reference - to set values */ + double & operator()(unsigned _i, unsigned _j) + { + return this->operator[](_i*rSize + _j); + } + + /** just in case */ + void printOn(ostream & _os) + { + unsigned index=0; + for (unsigned i=0; ioperator[](index++) << " " ; + _os << endl; + } + _os << endl; + } + + private: + unsigned rSize; // row size (== number of columns!) + }; + + // private data of class eoSharing +private: + double nicheSize; + eoQuadDistance repDist; // default distance + eoDistance & dist; // allows to pass a specific distance +}; + +#endif diff --git a/eo/src/eoTruncSelect.h b/eo/src/eoTruncSelect.h new file mode 100644 index 00000000..313d3c2d --- /dev/null +++ b/eo/src/eoTruncSelect.h @@ -0,0 +1,73 @@ +/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + + ----------------------------------------------------------------------------- + eoTruncSelect.h + (c) Maarten Keijzer, Marc Schoenauer, GeNeura Team, 2002 + + 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 + mkeijzer@dhi.dk + */ +//----------------------------------------------------------------------------- + +#ifndef _eoTruncSelect_h +#define _eoTruncSelect_h + + +//----------------------------------------------------------------------------- +#include +#include +#include +//----------------------------------------------------------------------------- + +/** eoTruncSelect selects individuals after truncating the population + * using eoSelectOne as it's mechanism. + * Therefore eoSelectMany needs an eoSelectOne in its ctor + * It will use an eoHowMnay to determine the number of guys to keep, +*/ +template +class eoTruncSelect : public eoSelect +{ + public: + /** Ctor: from an eoSelect (and an eoMany to tell how many are kept for selectino */ + eoSelectMany(eoSelectOne& _select, eoHowMany _howMany) + : select(_select), howMany(_howMany) {} + + /** + The implementation repeatidly selects an individual + + @param _source the source population + @param _dest the resulting population (size of this population is the number of times eoSelectOne is called. It empties the destination and adds the selection into it) + */ + virtual void operator()(const eoPop& _source, eoPop& _dest) + { + unsigned target = howMany(_source.size()); + + _dest.resize(target); + + select.setup(_source); + + for (size_t i = 0; i < _dest.size(); ++i) + _dest[i] = select(_source); + } + +private : + eoSelectOne& select; + eoHowMany howMany; +}; + +#endif