MOGAL

gafactory.h

Go to the documentation of this file.
00001 /*
00002     
00003   This file is a part of MOGAL, a Multi-Objective Genetic Algorithm
00004   Library.
00005   
00006   Copyright (C) 2008-2012 Jori Liesenborgs
00007 
00008   Contact: jori.liesenborgs@gmail.com
00009 
00010   This library is free software; you can redistribute it and/or
00011   modify it under the terms of the GNU Lesser General Public
00012   License as published by the Free Software Foundation; either
00013   version 2.1 of the License, or (at your option) any later version.
00014 
00015   This library is distributed in the hope that it will be useful,
00016   but WITHOUT ANY WARRANTY; without even the implied warranty of
00017   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018   Lesser General Public License for more details.
00019 
00020   You should have received a copy of the GNU Lesser General Public
00021   License along with this library; if not, write to the Free Software
00022   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  
00023   USA
00024 
00025 */
00026 
00031 #ifndef MOGAL_GAFACTORY_H
00032 
00033 #define MOGAL_GAFACTORY_H
00034 
00035 #include "mogalconfig.h"
00036 #include "geneticalgorithm.h"
00037 #include "randomnumbergenerator.h"
00038 #include <vector>
00039 
00040 namespace mogal
00041 {
00042 
00044 class MOGAL_IMPORTEXPORT GAFactoryParams : public errut::ErrorBase
00045 {
00046 protected:
00047         GAFactoryParams()                                                                               { }
00048 public:
00049         ~GAFactoryParams()                                                                              { }
00050 
00055         virtual bool write(serut::SerializationInterface &si) const = 0;
00056 
00061         virtual bool read(serut::SerializationInterface &si) = 0;
00062 };
00063 
00070 class MOGAL_IMPORTEXPORT GAFactory : public errut::ErrorBase
00071 {
00072 protected:
00073         GAFactory()                                                                                     { }
00074 public:
00075         ~GAFactory()                                                                                    { }
00076 
00080         virtual GAFactoryParams *createParamsInstance() const = 0;
00081         
00085         virtual const GAFactoryParams *getCurrentParameters() const = 0;
00086 
00093         virtual bool init(const GAFactoryParams *pFactoryParams) = 0;
00094 
00100         virtual size_t getNumberOfFitnessComponents() const = 0;
00101         
00105         virtual Genome *createNewGenome() const = 0;
00106 
00110         virtual size_t getMaximalGenomeSize() const = 0;
00111 
00115         virtual size_t getMaximalFitnessSize() const = 0;
00116 
00118         virtual bool writeGenome(serut::SerializationInterface &si, const Genome *pGenome) const = 0;
00119 
00121         virtual bool writeGenomeFitness(serut::SerializationInterface &si, const Genome *pGenome) const = 0;
00122 
00126         virtual bool writeCommonGenerationInfo(serut::SerializationInterface &si) const = 0;
00127 
00129         virtual bool readGenome(serut::SerializationInterface &si, Genome **pGenome) const = 0;
00130 
00132         virtual bool readGenomeFitness(serut::SerializationInterface &si, Genome *pGenome) const = 0;
00133 
00135         virtual bool readCommonGenerationInfo(serut::SerializationInterface &si) = 0;
00136 
00148         virtual void onGeneticAlgorithmStep(int generation, bool *pGenerationInfoChanged, bool *pStopAlgorithm) = 0;
00149 
00151         virtual void onGeneticAlgorithmStart()                                                          { }
00152 
00154         virtual void onGeneticAlgorithmStop()                                                           { }
00155 
00157         virtual void onSortedPopulation(const std::vector<GenomeWrapper> &population)                   { }
00158 
00160         virtual void sort(std::vector<GenomeWrapper> &population) = 0;
00161 
00163         virtual void updateBestGenomes(std::vector<GenomeWrapper> &population) = 0;
00164 
00166         virtual void breed(const std::vector<GenomeWrapper> &population, 
00167                            std::vector<GenomeWrapper> &newPopulation) = 0;
00168 
00170         virtual void introduceMutations(std::vector<GenomeWrapper> &population) = 0;
00171 
00175         virtual Genome *selectPreferredGenome(const std::list<Genome *> &bestGenomes) const = 0;
00176 
00177         // TODO: for CUDA ND sort, see also copyFitnessValuesTo in genome.h
00178         virtual bool hasFloatingPointFitnessValues() const                                              { return false; }
00179 
00181         virtual const RandomNumberGenerator *getRandomNumberGenerator() const = 0;
00182 protected:
00184         GeneticAlgorithm *getCurrentAlgorithm()                                                         { return m_pCurrentAlgorithm; }
00185 
00187         GeneticAlgorithmParams getCurrentAlgorithmParameters() const                                    { return m_gaParams; }
00188 private:
00189         void setCurrentAlgorithm(GeneticAlgorithm *pAlgorithm, const GeneticAlgorithmParams &params)    { m_pCurrentAlgorithm = pAlgorithm; m_gaParams = params; }
00190         
00191         GeneticAlgorithm *m_pCurrentAlgorithm;
00192         GeneticAlgorithmParams m_gaParams;
00193 
00194         friend class GeneticAlgorithm;
00195 };
00196 
00197 } // end namespace
00198 
00199 #endif // MOGAL_GAFACTORY_H
00200