Main Page | Modules | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

GenerationalEvolution.h

Go to the documentation of this file.
00001 /*
00002 Teem - an open source evolutionary software framework
00003 http://lis.epfl.ch/resources/Teem
00004 Stephane Magnenat <stephane.magnenat@epfl.ch>,
00005 Antoine Beyeler <antoine.beyeler@epfl.ch>,
00006 and other contributors. See AUTHORS for details
00007 Laboratory of Intelligent Systems, EPFL, Lausanne
00008 
00009 This program is free software; you can redistribute it and/or modify
00010 it under the terms of the GNU General Public License as published by
00011 the Free Software Foundation; either version 2 of the License, or
00012 (at your option) any later version.
00013 
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with this program; if not, write to the Free Software
00021 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022 */
00023 
00024 #ifndef GENERATIONAL_EVOLUTION_H
00025 #define GENERATIONAL_EVOLUTION_H
00026 
00027 #include "Evolution.h"
00028 #include <ishtar/settings.h>
00029 
00030 #include <string>
00031 
00036 namespace Ishtar
00037 {
00038     class InputStream;
00039     class OutputStream;
00040 }
00041 
00042 namespace Teem
00043 {
00044     class Genome;
00045     
00048     class GenerationalIndividual
00049     {
00050     public:
00051         Genome *genome; 
00052         double fitness; 
00053         double trialCount; 
00054         
00055     public:
00057         GenerationalIndividual();
00059         void reset(void);
00061         void normalize(void);
00062         
00064         void load(Ishtar::InputStream *stream);
00066         void save(Ishtar::OutputStream *stream);
00067     };
00068     
00071     typedef std::valarray<GenerationalIndividual> GenerationalPopulation;
00072     
00076     class Evaluable : public Setupable
00077     {
00078     public:
00080         enum EvaluationMode
00081         {
00082             MODE_EVOLUTION = 0, 
00083             MODE_TEST_INDIVIDUAL    
00084         };
00085     
00086     public:
00088         virtual ~Evaluable() { }
00089         
00091         virtual void compute(GenotypeDecoders &decoders, GenerationalPopulation &individuals, unsigned gen, unsigned ind, EvaluationMode mode) = 0;
00092     };
00093     
00096     inline bool individualBacksort (const GenerationalIndividual & a, const GenerationalIndividual & b)
00097     {
00098         return a.fitness > b.fitness;
00099     }
00100     
00103     class GenerationalEvolutionRun : public EvolutionRun
00104     {
00105     protected:
00106         // members
00108         GenerationalPopulation population;
00110         Ishtar::Variable<double> crossoverProbability;
00112         Ishtar::Variable<double> elitismRatio; 
00114         Ishtar::Variable<double> reproductionRatio; 
00116         Ishtar::Variable<unsigned> populationCount;
00118         Ishtar::Variable<unsigned> generationCount;
00119         
00121         unsigned generation;
00123         bool wasLoaded;
00124         
00125     protected:
00126         // support
00128         void deleteGenomes(void);
00130         bool loadPopulationFromStream(Experiment* experiment, Ishtar::InputStream *stream);
00131         
00133         double computeMean(const std::valarray<GenerationalIndividual> &group);
00135         double computeSD(const std::valarray<GenerationalIndividual> &group);
00137         double computeDiversity(const std::valarray<GenerationalIndividual> &group);
00138         
00139         // steps for each generation
00141         virtual void evaluatePopulation(Evaluable *fitness) = 0;
00143         virtual void reproducePopulation(void) = 0;
00145         virtual void logPopulation(void);
00147         virtual bool savePopulation(void);
00148         
00149         // wrapping
00151         virtual bool loadPopulation(Experiment* experiment, unsigned generation);
00153         virtual void run(Evaluable *fitness);
00155         virtual void testBestIndividual(Evaluable *fitness) = 0;
00157         virtual void testIndividual(Evaluable *fitness, size_t number) = 0;
00158     
00159     public:
00161         GenerationalEvolutionRun();
00163         virtual ~GenerationalEvolutionRun();
00164         
00165         // Main functions
00166         
00169         virtual void createGenotypeDecoders(Setupable *experiment) = 0;
00170         
00173         virtual void createRandomPopulation();
00174         
00176         virtual bool loadPopulation(Experiment* experiment);
00177 
00179         virtual bool loadPopulation(Experiment* experiment, const char *generation) { return loadPopulation(experiment, atoi(generation)); }
00180         
00182         virtual void run(Setupable *experiment);
00183         
00185         virtual void testBestIndividual(Setupable *experiment);
00187         virtual void testIndividual(Setupable *experiment, const char *);
00188     };
00189     
00192     class SingleDecoderEvolutionRun : public GenerationalEvolutionRun
00193     {
00194     public:
00196         SingleDecoderEvolutionRun() : genotypeDecoderName("teem.genotypeDecoderName", "FeedForward") { }
00198         virtual ~SingleDecoderEvolutionRun() { }
00199         
00200     protected:
00202         Ishtar::Variable<std::string> genotypeDecoderName;
00203         
00205         virtual void createGenotypeDecoders(Setupable *experiment);
00206     };
00207     
00210     class SimpleEvolutionRun: public SingleDecoderEvolutionRun
00211     {
00212     protected:
00214         virtual void evaluatePopulation(Evaluable *fitness);
00216         virtual void reproducePopulation(void);
00217         
00218         virtual void testBestIndividual(Evaluable *fitness);
00219         virtual void testIndividual(Evaluable *fitness, size_t number);
00220         
00221     public:
00223         SimpleEvolutionRun();
00224     };
00225     
00228     class MultiDecoderEvolutionRun : public GenerationalEvolutionRun
00229     {
00230     public:
00232         MultiDecoderEvolutionRun();
00234         virtual ~MultiDecoderEvolutionRun() { }
00235         
00236     protected:
00238         Ishtar::Variable<unsigned> numGenotypeDecoder;
00239         
00241         std::vector<Ishtar::Variable<std::string> > genotypeDecoderNames;
00242         
00244         virtual void createGenotypeDecoders(Setupable *experiment);
00245     };
00246     
00255     class SimpleMultiGenomeEvolutionRun :  public MultiDecoderEvolutionRun
00256     {
00257     protected:
00259         virtual void evaluatePopulation(Evaluable *fitness);
00261         virtual void reproducePopulation(void);
00263         virtual void createRandomPopulation();
00264         
00265         virtual void testBestIndividual(Evaluable *fitness);
00266         virtual void testIndividual(Evaluable *fitness, size_t number);
00267         
00268     public:
00270         SimpleMultiGenomeEvolutionRun();
00271     };
00272 }
00273 
00274 #endif

Generated on Mon Oct 24 17:38:25 2005 for Teem by  doxygen 1.4.2