nsga2.h
1 /********************************************************************************
2  * FARSA Genetic Algorithm Library *
3  * Copyright (C) 2007-2011 Gianluca Massera <emmegian@yahoo.it> *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the Free Software *
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
18  ********************************************************************************/
19 
20 #ifndef NSGA2_H
21 #define NSGA2_H
22 
23 #include "gaconfig.h"
24 #include "core/geneticalgo.h"
25 #include "core/genotype.h"
26 #include "core/genome.h"
27 #include <QList>
28 
29 namespace farsa {
30 
31 class Evaluation;
32 class Reproduction;
33 
43 class FARSA_GA_API NSGA2 : public GeneticAlgo {
44 public:
46  NSGA2();
48  virtual ~NSGA2();
52  void setNumThreads( int numThreads );
54  int numThreads();
56  void setEvaluation( Evaluation* fitfunc );
61  virtual Evaluation* evaluationPrototype();
63  virtual QVector<Evaluation*> evaluationPool();
65  void setReproduction( Reproduction* reproduct );
67  Reproduction* reproduction();
70  virtual void initialize();
72  virtual void gaStep();
74  virtual void finalize();
75 
80  virtual void skipEvaluation();
81 
89  virtual void configure( ConfigurationParameters& params, QString prefix );
96  virtual void save( ConfigurationParameters& params, QString prefix );
98  static void describe( QString type );
99 
100 protected:
106  typedef enum { initEvaluation, evaluating, nextGeneration_pass1, nextGeneration_pass2, endEvolution } GAPhases;
113 
114 private:
116  bool nextGeneration;
117 
122  class nsgaGenotype {
123  public:
124  nsgaGenotype( Genotype* g=NULL, int rank=0, double distance=0 ) {
125  genotype = g;
126  this->rank = rank;
127  this->distance = distance;
128  };
130  Genotype* genotype;
132  int rank;
134  double distance;
136  int dominationCounter;
138  bool operator<( const nsgaGenotype& g ) const {
139  return this->distance < g.distance;
140  };
142  bool operator==( const nsgaGenotype& g ) const {
143  return this->genotype == g.genotype;
144  };
145  };
146  typedef QVector<nsgaGenotype*> nsgaGenome;
148  Genome lastPareto;
150  void crowdingDistanceAssignment( nsgaGenome& genome );
152  QVector<nsgaGenome> fastNonDominatedSort( nsgaGenome& pareto );
154  static bool crowdingDistanceGreaterThan( const nsgaGenotype* g1, const nsgaGenotype* g2 ) {
155  return g1->distance > g2->distance;
156  };
158  class nObjectiveGreaterThan {
159  public:
160  bool operator()( const nsgaGenotype* g1, const nsgaGenotype* g2 ) {
161  return g1->genotype->objective( currentObjective ) > g2->genotype->objective( currentObjective );
162  };
163  int currentObjective;
164  };
165 
170  class evaluationThread {
171  public:
172  //--- Constructor
173  evaluationThread( NSGA2* p, Evaluation* eProto );
174  //--- Destructor
175  ~evaluationThread();
176  //--- LaralGA parent
177  NSGA2* parent;
178  //--- evaluator used by this object
179  Evaluation* eval;
180  //--- evaluating genoma
181  int id;
182  //--- true when it cannot increment id because the end is reached
183  bool blocked;
184  //--- run a step of evaluation
185  void runStep();
186  //--- sequence of Genoma to evaluate
187  QVector<int> sequence;
188  //--- actual id inside sequence in evaluating
189  int idSeq;
190  };
191 
193  QList<evaluationThread*> evalThreads;
195  int numThreadv;
197  static void runStepWrapper( NSGA2::evaluationThread* e );
198 };
199 
200 } // end namespace farsa
201 
202 #endif