// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- //----------------------------------------------------------------------------- // eoSequential.h // (c) GeNeura Team, 1998 - Maarten Keijzer 2000, Marc Schoenauer, 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 mak@dhi.dk */ //----------------------------------------------------------------------------- #ifndef eoSequential_h #define eoSequential_h /** Contains the following classes: * - eoSequentialSelect, returns all individuals one by one, * either sorted or shuffled * - eoEliteSequentialSelect, returns all indivisuals one by one * starting with best, continuing shuffled (see G3 engine) */ #include #include //----------------------------------------------------------------------------- /** eoSequentialSelect: returns all individual in order * looping back to the beginning when exhasuted * can be from best to worse, or in random order * * It is the eoSelectOne equivalent of eoDetSelect - * though eoDetSelect always returns individuals from best to worst */ //----------------------------------------------------------------------------- template class eoSequentialSelect: public eoSelectOne { public: /** Ctor: sets the current pter to MAXINT so init will take place first time not very elegant, maybe ... */ #ifdef _MSC_VER eoSequentialSelect(bool _ordered = true) : ordered(_ordered), current(std::MAXINT) {} #else eoSequentialSelect(bool _ordered = true) : ordered(_ordered), current(MAXINT) {} #endif void setup(const eoPop& _pop) { eoPters.resize(_pop.size()); if (ordered) // probably we could have a marker to avoid re-sorting _pop.sort(eoPters); else _pop.shuffle(eoPters); current=0; } virtual const EOT& operator()(const eoPop& _pop) { if (current >= _pop.size()) setup(_pop); unsigned eoN = current; current++; return *eoPters[eoN] ; } private: bool ordered; unsigned current; std::vector eoPters; }; //----------------------------------------------------------------------------- /** eoEliteSequentialSelect: returns the best individual first, * then the others in sequence (random order). * for G3 evolution engine, see Deb, Anad and Joshi, CEC 2002 * * As eoSequentialSelect, it is an eoSelectOne to be used within the eoEaseyEA * algo, but conceptually it should be a global eoSelect, as * it selects a bunch of guys in one go (done in the setup function now) */ template class eoEliteSequentialSelect: public eoSelectOne { public: /** Ctor: sets the current pter to MAXINT so init will take place first time not very elegant, maybe ... */ #ifdef _MSC_VER eoEliteSequentialSelect(): current(std::MAXINT) {} #else eoEliteSequentialSelect(): current(MAXINT) {} #endif void setup(const eoPop& _pop) { eoPters.resize(_pop.size()); _pop.shuffle(eoPters); // now get the best unsigned int ibest = 0; const EOT * best = eoPters[0]; if (_pop.size() == 1) throw std::runtime_error("Trying eoEliteSequentialSelect with only one individual!"); for (unsigned i=1; i<_pop.size(); i++) if (*eoPters[i]>*best) { ibest = i; best = eoPters[ibest]; } // and put it upfront const EOT *ptmp = eoPters[0]; eoPters[0]=best; eoPters[ibest] = ptmp; // exit after setting current current=0; } virtual const EOT& operator()(const eoPop& _pop) { if (current >= _pop.size()) setup(_pop); unsigned eoN = current; current++; return *eoPters[eoN] ; } private: unsigned current; std::vector eoPters; }; #endif