// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- //----------------------------------------------------------------------------- // eoSGA.h // (c) GeNeura Team, 2000 - EEAAX 1999 - Maarten Keijzer 2000 /* 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 _eoSGA_h #define _eoSGA_h #include #include #include #include #include #include #include #include /** The Simple Genetic Algorithm, following Holland and Goldberg * Needs a selector (class eoSelectOne) a crossover (eoQuad, * i.e. a 2->2 operator) and a mutation with their respective rates, * of course an evaluation function (eoEvalFunc) and a continuator * (eoContinue) which gives the stopping criterion. Performs full * generational replacement. */ template class eoSGA : public eoAlgo { public : // added this second ctor as I didn't like the ordering of the parameters // in the one above. Any objection :-) MS eoSGA( eoSelectOne& _select, eoQuadOp& _cross, float _crate, eoMonOp& _mutate, float _mrate, eoEvalFunc& _eval, eoContinue& _cont) : cont(_cont), mutate(_mutate), mutationRate(_mrate), cross(_cross), crossoverRate(_crate), select(_select), eval(_eval) {} void operator()(eoPop& _pop) { eoPop offspring; do { select(_pop, offspring); unsigned i; for (i=0; i<_pop.size()/2; i++) { if ( rng.flip(crossoverRate) ) { // this crossover generates 2 offspring from two parents if (cross(offspring[2*i], offspring[2*i+1])) { offspring[2*i].invalidate(); offspring[2*i+1].invalidate(); } } } for (i=0; i < _pop.size(); i++) { if (rng.flip(mutationRate) ) { if (mutate(offspring[i])) offspring[i].invalidate(); } } _pop.swap(offspring); apply(eval, _pop); } while (cont(_pop)); } private : eoContinue& cont; /// eoInvalidateMonOp invalidates the embedded operator eoInvalidateMonOp mutate; float mutationRate; // eoInvalidateQuadOp invalidates the embedded operator eoInvalidateQuadOp cross; float crossoverRate; eoSelectPerc select; eoEvalFunc& eval; }; #endif