ga/include/gas/parallelga.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 PARALLELGA_H
00021 #define PARALLELGA_H
00022 
00023 #include "gaconfig.h"
00024 #include "core/geneticalgo.h"
00025 #include <QList>
00026 #include <QFuture>
00027 
00028 namespace farsa {
00029 
00030 class Evaluation;
00031 class Reproduction;
00032 
00044 class FARSA_GA_API ParallelGA : public GeneticAlgo {
00045 public:
00047     ParallelGA();
00049     virtual ~ParallelGA();
00053     void setNumThreads( int numThreads );
00055     int numThreads();
00057     void setEvaluation( Evaluation* fitfunc );
00062     virtual Evaluation* evaluationPrototype();
00064     virtual QVector<Evaluation*> evaluationPool();
00066     void setReproduction( Reproduction* reproduct );
00068     Reproduction* reproduction();
00071     virtual void initialize();
00073     virtual void gaStep();
00075     virtual void finalize();
00076 
00081     virtual void skipEvaluation();
00082 
00090     virtual void configure( ConfigurationParameters& params, QString prefix );
00097     virtual void save( ConfigurationParameters& params, QString prefix );
00099     static void describe( QString type );
00100 
00101 protected:
00103     Evaluation* fitfunc;
00105     Reproduction* reprod;
00107     typedef enum { initEvaluation, evaluating, nextGeneration_pass1, nextGeneration_pass2, endEvolution } GAPhases;
00109     GAPhases currPhase;
00111     bool isInitialized;
00113     bool isFinalized;
00114 
00115 private:
00117     bool nextGeneration;
00118 
00123     class evaluationThread {
00124     public:
00125         //--- Constructor
00126         evaluationThread( Evaluation* eProto );
00127         //--- Destructor
00128         ~evaluationThread();
00129         //--- evaluator used by this object
00130         Evaluation* eval;
00131         //--- evaluating genoma
00132         int id;
00133         //--- run a step of evaluation
00134         void runStep();
00135         //--- sequence of Genoma to evaluate
00136         QVector<int> sequence;
00137         //--- actual id inside sequence in evaluating
00138         int idSeq;
00139     };
00140 
00142     QList<evaluationThread*> evalThreads;
00144     int numThreadv;
00146     static void runStepWrapper( ParallelGA::evaluationThread* e );
00148     QFuture<void>* future;
00149 };
00150 
00151 } // end namespace farsa
00152 
00153 #endif