ga/include/gas/laralga.h

00001 /********************************************************************************
00002  *  FARSA Genetic Algorithm Library                                             *
00003  *  Copyright (C) 2007-2009 Gianluca Massera <emmegian@yahoo.it>                *
00004  *                                                                              *
00005  *  This program is free software; you can redistribute it and/or modify        *
00006  *  it under the terms of the GNU General Public License as published by        *
00007  *  the Free Software Foundation; either version 2 of the License, or           *
00008  *  (at your option) any later version.                                         *
00009  *                                                                              *
00010  *  This program is distributed in the hope that it will be useful,             *
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00013  *  GNU General Public License for more details.                                *
00014  *                                                                              *
00015  *  You should have received a copy of the GNU General Public License           *
00016  *  along with this program; if not, write to the Free Software                 *
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
00018  ********************************************************************************/
00019 
00020 #ifndef FARSAGA_H
00021 #define FARSAGA_H
00022 
00023 #include "gaconfig.h"
00024 #include "core/geneticalgo.h"
00025 #include <QList>
00026 
00027 namespace farsa {
00028 
00029 class MultiTrials;
00030 class Mutation;
00031 
00044 class FARSA_GA_API LaralGA : public GeneticAlgo {
00045 public:
00047     LaralGA();
00049     virtual ~LaralGA();
00053     void setNumThreads( int numThreads );
00055     int numThreads();
00057     void setFitnessFunction( MultiTrials* fitfunc );
00062     virtual Evaluation* evaluationPrototype();
00064     virtual QVector<Evaluation*> evaluationPool();
00066     MultiTrials* fitnessFunction();
00068     void setMutation( Mutation* mutate );
00070     Mutation* mutation();
00077     void setReproduceParams( int nreproducing, bool useElitism = false, int nelitism = 0 );
00079     int numReproducing();
00081     int numOffspring();
00083     int numElitism();
00085     bool isElitismEnabled();
00090     virtual void initialize();
00092     virtual void gaStep();
00094     virtual void finalize();
00095 
00100     virtual void skipEvaluation();
00101 
00109     virtual void configure( ConfigurationParameters& params, QString prefix );
00116     virtual void save( ConfigurationParameters& params, QString prefix );
00118     static void describe( QString type );
00119 
00120 protected:
00122     MultiTrials* fitfunc;
00124     Mutation* muta;
00126     unsigned int currGenotype;
00128     int nreproducing;
00130     int noffspring;
00132     int nelitism;
00134     bool elitismEnabled;
00136     typedef enum { initEvaluation, evaluating, nextGeneration_pass1, nextGeneration_pass2, endEvolution } GAPhases;
00138     GAPhases currPhase;
00140     bool isInitialized;
00142     bool isFinalized;
00143 
00144 private:
00146     void createNextGeneration();
00148     bool nextGeneration;
00149 
00154     class evaluationThread {
00155     public:
00156         //--- Constructor
00157         evaluationThread( LaralGA* p, MultiTrials* eProto );
00158         //--- Destructor
00159         ~evaluationThread();
00160         //--- LaralGA parent
00161         LaralGA* parent;
00162         //--- evaluator used by this object
00163         MultiTrials* eval;
00164         //--- evaluating genoma
00165         int id;
00166         //--- true when it cannot increment id because the end is reached
00167         bool blocked;
00168         //--- run a step of evaluation
00169         void runStep();
00170         //--- sequence of Genoma to evaluate
00171         QVector<int> sequence;
00172         //--- actual id inside sequence in evaluating
00173         int idSeq;
00174     };
00175 
00177     QList<evaluationThread*> evalThreads;
00179     int numThreadv;
00181     static void runStepWrapper( LaralGA::evaluationThread* e );
00182 };
00183 
00184 } // end namespace farsa
00185 
00186 #endif