00001
00002 #ifndef __SELECTION_H__
00003 #define __SELECTION_H__
00004
00005 #include <magic/mmatrix.h>
00006 #include <magic/mrefarray.h>
00007 #include "nhp/individual.h"
00008
00009
00010 class SimplePopulation;
00011 class Gentainer;
00012
00013 class SelectionSituation;
00014
00015
00017
00018
00019
00020
00021
00022
00023
00025
00036 class SelectionMatrix : public Object {
00037 public:
00038
00042 explicit SelectionMatrix (const SelectionSituation& situation);
00043 ~SelectionMatrix () {}
00044
00051 void selectRandomPair (int& a, int& b) const;
00052
00054 TextOStream& operator>> (TextOStream& out) const {return mSelection >> out;}
00055
00056 protected:
00057
00060 void calculateMatrix (const SelectionSituation& situation);
00061
00062 private:
00063 SelectionMatrix();
00064
00066 Matrix mSelection;
00067
00068 friend class Selector;
00069 };
00070
00071
00072
00074
00075
00076
00077
00078
00080
00089 class SelectionSituation : public Object {
00090 public:
00091
00099 SelectionSituation (const SimplePopulation& pop);
00100
00101 const SimplePopulation& population () const {return mrPop;}
00102
00106 const Individual& getOrdered (int i) const {return mOrdPop[i];}
00107
00108 private:
00109
00111 const SimplePopulation& mrPop;
00112
00114 RefArray<Individual> mOrdPop;
00115 };
00116
00117
00118
00120
00121
00122
00123
00124
00126
00133 class SelectionPrms : public Object {
00134 public:
00135
00136 explicit SelectionPrms ();
00137 explicit SelectionPrms (const SelectionPrms& o) {copy(o);}
00138 ~SelectionPrms () {}
00139
00142 void useOnlyMethod (int m);
00143
00146 void adaptParams (bool mu=true, bool etaPlus=true,
00147 bool q=true);
00148
00149 void adaptWeights (bool v=true) {mAdaptiveWeights=v;}
00150
00153 void setMu (int m);
00154
00158 void setMuPart (double part);
00159
00162 void setEtaPlus (double etaplus);
00163
00166 void setQ (int q);
00167
00171 int muFor (int populsize) const;
00172
00173 void copy (const SelectionPrms& o);
00174
00175 protected:
00179 double mMuPart;
00180
00182 int mMu;
00183
00185 bool mAdaptiveMu;
00186
00188 double mEtaPlus;
00189
00191 bool mAdaptiveEtaPlus;
00192
00194 int mQ;
00195
00197 bool mAdaptiveQ;
00198
00204 bool mWeightedSelection;
00205
00208 Vector mSelMethodW;
00209
00213 bool mAdaptiveWeights;
00214
00215 friend class Selector;
00216 };
00217
00218
00219
00221
00222
00223
00224
00225
00226
00227
00229
00239 class Selector : public SelectionPrms {
00240 public:
00241 explicit Selector () : SelectionPrms () {mScore=0;}
00242 explicit Selector (const SelectionPrms& orig);
00243 ~Selector () {}
00244
00259 static void addGenesTo (Gentainer& g, const StringMap& params);
00260
00263 void read (const Genome& g);
00264
00268 double select (const SelectionSituation& situation,
00269 int self_i, int other_j) const;
00270
00274 int score () const {return mScore;}
00275
00277 void virginize () {mScore=0;}
00278
00279 enum smconsts {MULAMBDASELECTION=0, LINEARRANKING, PROPORTIONAL, TOURNAMENT,
00280 number_of_methods};
00281
00282 protected:
00291 virtual double selectWithMethod (const SelectionSituation& situation, int smno, int i, int j) const;
00292
00297 double muLambdaSelection (const SelectionSituation& situation, int i, int j) const;
00298
00303 double linearRanking (const SelectionSituation& situation, int i, int j) const;
00304
00307 double proportionalSelection (const SelectionSituation& situation, int i, int j) const;
00308
00311 double tournamentSelection (const SelectionSituation& situation, int i, int j) const;
00312
00313 protected:
00319 int mScore;
00320
00321 void operator= (const Selector& other) {}
00322 void operator= (const SelectionPrms& other) {}
00323 };
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338 #endif
00339