MOGAL
|
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_GENETICALGORITHM_H 00032 00033 #define MOGAL_GENETICALGORITHM_H 00034 00035 #include "mogalconfig.h" 00036 #include "genome.h" 00037 #include <serut/serializationinterface.h> 00038 #include <enut/networklayeraddress.h> 00039 #include <stdlib.h> 00040 #include <vector> 00041 #include <list> 00042 00043 namespace mogal 00044 { 00045 00046 class GAFactory; 00047 class GAFactoryParams; 00048 class GAModule; 00049 class Genome; 00050 00084 class MOGAL_IMPORTEXPORT GeneticAlgorithmParams : public errut::ErrorBase 00085 { 00086 public: 00088 GeneticAlgorithmParams(); 00089 00094 GeneticAlgorithmParams(double beta, bool elitism, bool includeBest, double crossoverRate); 00095 ~GeneticAlgorithmParams(); 00096 00098 double getBeta() const { return m_beta; } 00099 00101 bool useElitism() const { return m_elitism; } 00102 00106 bool alwaysIncludeBestGenome() const { return m_alwaysIncludeBest; } 00107 00109 double getCrossOverRate() const { return m_crossoverRate; } 00110 00111 bool write(serut::SerializationInterface &si); 00112 bool read(serut::SerializationInterface &si); 00113 private: 00114 double m_beta, m_crossoverRate; 00115 bool m_elitism, m_alwaysIncludeBest; 00116 }; 00117 00119 class MOGAL_IMPORTEXPORT GeneticAlgorithm : public errut::ErrorBase 00120 { 00121 public: 00122 GeneticAlgorithm(); 00123 ~GeneticAlgorithm(); 00124 00132 bool run(GAFactory &factory, size_t populationSize, const GeneticAlgorithmParams *pParams = 0); 00133 00142 bool run(const std::string &factoryName, size_t populationSize, 00143 const std::string &baseDir, const GAFactoryParams *pFactoryParams, 00144 const GeneticAlgorithmParams *pParams = 0); 00145 00158 bool run(const nut::NetworkLayerAddress &serverAddress, uint16_t serverPort, 00159 const std::string &factoryName, size_t populationSize, 00160 GAFactory &factory, const GeneticAlgorithmParams *pParams = 0); 00161 00174 bool run(const nut::NetworkLayerAddress &serverAddress, uint16_t serverPort, const std::string &factoryName, 00175 size_t populationSize, const std::string &baseDir, const GAFactoryParams *pFactoryParams, 00176 const GeneticAlgorithmParams *pParams = 0); 00177 00179 void getBestGenomes(std::list<Genome *> &genomes) const; 00180 00182 void getBestGenomes(std::vector<Genome *> &genomes) const; 00183 00185 size_t getNumberOfBestGenomes() const { return m_bestGenomes.size(); } 00186 00188 void setBestGenomes(const std::list<Genome *> &genomes); 00189 00193 Genome *selectPreferredGenome() const; 00194 00196 void clearBestGenomes(); 00197 00199 int getCurrentGeneration() const { return m_generationCount; } 00200 protected: 00201 virtual bool calculateFitness(std::vector<GenomeWrapper> &population); 00202 virtual bool onAlgorithmLoop(GAFactory &factory, bool generationInfoChanged); 00203 virtual void onCurrentBest(const std::list<Genome *> &bestGenomes) const { } 00204 virtual void feedbackStatus(const std::string &str) const { } 00205 virtual bool stopRemoteAlgorithm() { return false; } 00206 private: 00207 bool processNewBestGenomes(GAFactory &factory, serut::SerializationInterface &si); 00208 00209 std::list<Genome *> m_bestGenomes; 00210 GAModule *m_pGAModule; 00211 GAFactory *m_pLoadedFactory; 00212 GAFactory *m_pCurrentFactory; 00213 int m_generationCount; 00214 }; 00215 00216 } // end namespace 00217 00218 #endif // MOGAL_GENETICALGORITHM_H 00219