ga/include/gas/nsga2.h

00001 /********************************************************************************
00002  *  FARSA Genetic Algorithm Library                                             *
00003  *  Copyright (C) 2007-2011 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 NSGA2_H
00021 #define NSGA2_H
00022 
00023 #include "gaconfig.h"
00024 #include "core/geneticalgo.h"
00025 #include "core/genotype.h"
00026 #include "core/genome.h"
00027 #include <QList>
00028 
00029 namespace farsa {
00030 
00031 class Evaluation;
00032 class Reproduction;
00033 
00043 class FARSA_GA_API NSGA2 : public GeneticAlgo {
00044 public:
00046     NSGA2();
00048     virtual ~NSGA2();
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 nsgaGenotype {
00123     public:
00124         nsgaGenotype( Genotype* g=NULL, int rank=0, double distance=0 ) {
00125             genotype = g;
00126             this->rank = rank;
00127             this->distance = distance;
00128         };
00130         Genotype* genotype;
00132         int rank;
00134         double distance;
00136         int dominationCounter;
00138         bool operator<( const nsgaGenotype& g ) const {
00139             return this->distance < g.distance;
00140         };
00142         bool operator==( const nsgaGenotype& g ) const {
00143             return this->genotype == g.genotype;
00144         };
00145     };
00146     typedef QVector<nsgaGenotype*> nsgaGenome;
00148     Genome lastPareto;
00150     void crowdingDistanceAssignment( nsgaGenome& genome );
00152     QVector<nsgaGenome> fastNonDominatedSort( nsgaGenome& pareto );
00154     static bool crowdingDistanceGreaterThan( const nsgaGenotype* g1, const nsgaGenotype* g2 ) {
00155         return g1->distance > g2->distance;
00156     };
00158     class nObjectiveGreaterThan {
00159     public:
00160         bool operator()( const nsgaGenotype* g1, const nsgaGenotype* g2 ) {
00161             return g1->genotype->objective( currentObjective ) > g2->genotype->objective( currentObjective );
00162         };
00163         int currentObjective;   
00164     };
00165 
00170     class evaluationThread {
00171     public:
00172         //--- Constructor
00173         evaluationThread( NSGA2* p, Evaluation* eProto );
00174         //--- Destructor
00175         ~evaluationThread();
00176         //--- LaralGA parent
00177         NSGA2* parent;
00178         //--- evaluator used by this object
00179         Evaluation* eval;
00180         //--- evaluating genoma
00181         int id;
00182         //--- true when it cannot increment id because the end is reached
00183         bool blocked;
00184         //--- run a step of evaluation
00185         void runStep();
00186         //--- sequence of Genoma to evaluate
00187         QVector<int> sequence;
00188         //--- actual id inside sequence in evaluating
00189         int idSeq;
00190     };
00191 
00193     QList<evaluationThread*> evalThreads;
00195     int numThreadv;
00197     static void runStepWrapper( NSGA2::evaluationThread* e );
00198 };
00199 
00200 } // end namespace farsa
00201 
00202 #endif