laralga.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 FARSAGA_H
21 #define FARSAGA_H
22 
23 #include "gaconfig.h"
24 #include "core/geneticalgo.h"
25 #include <QList>
26 
27 namespace farsa {
28 
29 class MultiTrials;
30 class Mutation;
31 
44 class FARSA_GA_API LaralGA : public GeneticAlgo {
45 public:
47  LaralGA();
49  virtual ~LaralGA();
53  void setNumThreads( int numThreads );
55  int numThreads();
57  void setFitnessFunction( MultiTrials* fitfunc );
62  virtual Evaluation* evaluationPrototype();
64  virtual QVector<Evaluation*> evaluationPool();
66  MultiTrials* fitnessFunction();
68  void setMutation( Mutation* mutate );
70  Mutation* mutation();
77  void setReproduceParams( int nreproducing, bool useElitism = false, int nelitism = 0 );
79  int numReproducing();
81  int numOffspring();
83  int numElitism();
85  bool isElitismEnabled();
90  virtual void initialize();
92  virtual void gaStep();
94  virtual void finalize();
95 
100  virtual void skipEvaluation();
101 
109  virtual void configure( ConfigurationParameters& params, QString prefix );
116  virtual void save( ConfigurationParameters& params, QString prefix );
118  static void describe( QString type );
119 
120 protected:
126  unsigned int currGenotype;
132  int nelitism;
136  typedef enum { initEvaluation, evaluating, nextGeneration_pass1, nextGeneration_pass2, endEvolution } GAPhases;
143 
144 private:
146  void createNextGeneration();
148  bool nextGeneration;
149 
154  class evaluationThread {
155  public:
156  //--- Constructor
157  evaluationThread( LaralGA* p, MultiTrials* eProto );
158  //--- Destructor
159  ~evaluationThread();
160  //--- LaralGA parent
161  LaralGA* parent;
162  //--- evaluator used by this object
163  MultiTrials* eval;
164  //--- evaluating genoma
165  int id;
166  //--- true when it cannot increment id because the end is reached
167  bool blocked;
168  //--- run a step of evaluation
169  void runStep();
170  //--- sequence of Genoma to evaluate
171  QVector<int> sequence;
172  //--- actual id inside sequence in evaluating
173  int idSeq;
174  };
175 
177  QList<evaluationThread*> evalThreads;
179  int numThreadv;
181  static void runStepWrapper( LaralGA::evaluationThread* e );
182 };
183 
184 } // end namespace farsa
185 
186 #endif