ga/include/gas/simplega.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 SIMPLEGA_H
00021 #define SIMPLEGA_H
00022 
00023 #include "gaconfig.h"
00024 #include "core/geneticalgo.h"
00025 #include <QList>
00026 
00027 namespace farsa {
00028 
00029 class Evaluation;
00030 class Reproduction;
00031 
00043 class FARSA_GA_API SimpleGA : public GeneticAlgo {
00044 public:
00046     SimpleGA();
00048     virtual ~SimpleGA();
00052     void setNumThreads( int numThreads );
00054     int numThreads();
00056     void setEvaluation( Evaluation* fitfunc );
00061     virtual Evaluation* evaluationPrototype();
00063     virtual QVector<Evaluation*> evaluationPool();
00065     void setReproduction( Reproduction* reproduct );
00067     Reproduction* reproduction();
00070     virtual void initialize();
00072     virtual void gaStep();
00074     virtual void finalize();
00075 
00080     virtual void skipEvaluation();
00081 
00089     virtual void configure( ConfigurationParameters& params, QString prefix );
00096     virtual void save( ConfigurationParameters& params, QString prefix );
00098     static void describe( QString type );
00099 
00100 protected:
00102     Evaluation* fitfunc;
00104     Reproduction* reprod;
00106     typedef enum { initEvaluation, evaluating, nextGeneration_pass1, nextGeneration_pass2, endEvolution } GAPhases;
00108     GAPhases currPhase;
00110     bool isInitialized;
00112     bool isFinalized;
00113 
00114 private:
00116     bool nextGeneration;
00117 
00122     class evaluationThread {
00123     public:
00124         //--- Constructor
00125         evaluationThread( SimpleGA* p, Evaluation* eProto );
00126         //--- Destructor
00127         ~evaluationThread();
00128         //--- LaralGA parent
00129         SimpleGA* parent;
00130         //--- evaluator used by this object
00131         Evaluation* eval;
00132         //--- evaluating genoma
00133         int id;
00134         //--- true when it cannot increment id because the end is reached
00135         bool blocked;
00136         //--- run a step of evaluation
00137         void runStep();
00138         //--- sequence of Genoma to evaluate
00139         QVector<int> sequence;
00140         //--- actual id inside sequence in evaluating
00141         int idSeq;
00142     };
00143 
00145     QList<evaluationThread*> evalThreads;
00147     int numThreadv;
00149     static void runStepWrapper( SimpleGA::evaluationThread* e );
00150 };
00151 
00152 } // end namespace farsa
00153 
00154 #endif