geneticalgo.h
1 /********************************************************************************
2  * FARSA Genetic Algorithm Library *
3  * Copyright (C) 2007-2009 Gianluca Massera <emmegian@yahoo.it> *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the Free Software *
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
18  ********************************************************************************/
19 
20 #ifndef GENETICALGO_H
21 #define GENETICALGO_H
22 
23 #include "gaconfig.h"
24 #include "parametersettable.h"
25 #include <QVector>
26 
27 namespace farsa {
28 
29 class Genotype;
30 class Genome;
31 class Evaluation;
32 class GeneticAlgo;
33 
42 class FARSA_GA_API GeneticAlgoIODelegate {
43 public:
47  virtual void saveData( GeneticAlgo* /*ga*/ ) { }
51  virtual void recoverData( GeneticAlgo* /*ga*/ ) { };
52 };
53 
59 class FARSA_GA_API GeneticAlgoObserver {
60 public:
62  virtual ~GeneticAlgoObserver() { };
64  virtual void onEndGeneration() { };
65 };
66 
79 public:
81  GeneticAlgo( );
83  virtual ~GeneticAlgo();
85  ParameterSettableUI* getUIManager();
87  void addObserver( GeneticAlgoObserver* observer );
89  void removeObserver( GeneticAlgoObserver* observer );
94  virtual void initialize() = 0;
99  virtual void gaStep() = 0;
104  virtual void finalize() = 0;
109  void evolve( unsigned int generationToReach );
113  void evolve();
115  bool isEvolutionEnded();
117  bool isEvaluationDone();
119  void setGenome( Genome* gen );
121  Genome* genome();
123  const Genome* genome() const;
128  virtual Evaluation* evaluationPrototype() = 0;
132  virtual QVector<Evaluation*> evaluationPool() = 0;
138  void setGeneration( unsigned int gen );
140  unsigned int generation() const;
142  void setNumGenerations( int g );
144  int numGenerations();
149  virtual void skipEvaluation() = 0;
154  QVector< QVector<double> > bestFits() const;
156  QVector< QVector<double> > averageFits() const;
158  QVector<double> meanHammingDist() const;
160  QVector<double> varianceHammingDist() const;
162  QVector<double> standardDeviationHammingDist() const;
166  void setIODelegate( GeneticAlgoIODelegate* iodelegate );
168  GeneticAlgoIODelegate* getIODelegate() const;
169 
170 protected:
177  void updateStats();
178 
183 
192 
194  void notifyEndGeneration();
195 
199  unsigned int generationv;
201  unsigned int numGens;
202 
204  QVector< QVector<double> > bestfits;
206  QVector< QVector<double> > avgfits;
208  QVector<double> meanHdists;
210  QVector<double> varHdists;
212  QVector<double> stdHdists;
213 
217  QList<GeneticAlgoObserver*> observers;
218 };
219 
220 } // end namespace farsa
221 
222 #endif