00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
00173 evaluationThread( NSGA2* p, Evaluation* eProto );
00174
00175 ~evaluationThread();
00176
00177 NSGA2* parent;
00178
00179 Evaluation* eval;
00180
00181 int id;
00182
00183 bool blocked;
00184
00185 void runStep();
00186
00187 QVector<int> sequence;
00188
00189 int idSeq;
00190 };
00191
00193 QList<evaluationThread*> evalThreads;
00195 int numThreadv;
00197 static void runStepWrapper( NSGA2::evaluationThread* e );
00198 };
00199
00200 }
00201
00202 #endif