simplega.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 SIMPLEGA_H
21 #define SIMPLEGA_H
22 
23 #include "gaconfig.h"
24 #include "core/geneticalgo.h"
25 #include <QList>
26 
27 namespace farsa {
28 
29 class Evaluation;
30 class Reproduction;
31 
43 class FARSA_GA_API SimpleGA : public GeneticAlgo {
44 public:
46  SimpleGA();
48  virtual ~SimpleGA();
52  void setNumThreads( int numThreads );
54  int numThreads();
56  void setEvaluation( Evaluation* fitfunc );
61  virtual Evaluation* evaluationPrototype();
63  virtual QVector<Evaluation*> evaluationPool();
65  void setReproduction( Reproduction* reproduct );
67  Reproduction* reproduction();
70  virtual void initialize();
72  virtual void gaStep();
74  virtual void finalize();
75 
80  virtual void skipEvaluation();
81 
89  virtual void configure( ConfigurationParameters& params, QString prefix );
96  virtual void save( ConfigurationParameters& params, QString prefix );
98  static void describe( QString type );
99 
100 protected:
106  typedef enum { initEvaluation, evaluating, nextGeneration_pass1, nextGeneration_pass2, endEvolution } GAPhases;
113 
114 private:
116  bool nextGeneration;
117 
122  class evaluationThread {
123  public:
124  //--- Constructor
125  evaluationThread( SimpleGA* p, Evaluation* eProto );
126  //--- Destructor
127  ~evaluationThread();
128  //--- LaralGA parent
129  SimpleGA* parent;
130  //--- evaluator used by this object
131  Evaluation* eval;
132  //--- evaluating genoma
133  int id;
134  //--- true when it cannot increment id because the end is reached
135  bool blocked;
136  //--- run a step of evaluation
137  void runStep();
138  //--- sequence of Genoma to evaluate
139  QVector<int> sequence;
140  //--- actual id inside sequence in evaluating
141  int idSeq;
142  };
143 
145  QList<evaluationThread*> evalThreads;
147  int numThreadv;
149  static void runStepWrapper( SimpleGA::evaluationThread* e );
150 };
151 
152 } // end namespace farsa
153 
154 #endif