Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

population.cc

Go to the documentation of this file.
00001 
00025 #include <magic/mmath.h>
00026 #include <magic/mstream.h>
00027 
00028 #include "nhp/population.h"
00029 #include "nhp/simplepopula.h"
00030 #include "nhp/gaenvrnmt.h"
00031 #include "nhp/selection.h"
00032 #include "nhp/mutrecord.h"
00033 
00034 // For mutrecord.h
00035 bool MutabilityRecord::record=false;        // Should we record or not
00036 int MutabilityRecord::smFloatSamples=0;
00037 int MutabilityRecord::smFloatVarSamples=0;
00038 int MutabilityRecord::smBoolSamples=0;
00039 double MutabilityRecord::smFloatVarMin=0;
00040 double MutabilityRecord::smFloatVarSum=0;
00041 double MutabilityRecord::smFloatVarMax=0;
00042 double MutabilityRecord::smFloatMin=0;
00043 double MutabilityRecord::smFloatSum=0;
00044 double MutabilityRecord::smFloatMax=0;
00045 double MutabilityRecord::smBoolSum=0;
00046 double MutabilityRecord::smBoolMin=0;
00047 double MutabilityRecord::smBoolMax=0;
00048 
00049 
00051 //                                                                           //
00052 //           -----   _    ----                                               //
00053 //           |      / \  (      |       ___   |   ___                        //
00054 //           |---  /   \  ---  -+- |/\  ___| -+- /   )  ___  \   |           //
00055 //           |     |---|     )  |  |   (   |  |  |---  (   \  \  |           //
00056 //           |____ |   | ___/    \ |    \__|   \  \__   ---/   \_/           //
00057 //                                                      __/   \_/            //
00059 
00060 EAStrategy::EAStrategy (SimplePopulation& pop) : mrPopula (pop) {
00061     allow_same_parents = false;
00062     // selmethod = NULL;
00063 
00064     //
00065     // As a speed optimization we use two populations so that we can
00066     // use copy() operations for creating a descendant instead of
00067     // cloning them each time
00068     // 
00069     // By cloning the population each time
00070     // 14.20u 1.80s 0:16.29 98.2%
00071     // By copying the population to a double-buffered corpse population
00072     // 9.21u 1.87s 0:11.08 100.0%
00073 
00074     // Create the corpse vector
00075     mpNextGen = new Array<Individual> ();
00076     mpNextGen->make (mrPopula.size());
00077 
00078     // Create corpses for the future descendants.
00079     // As you may notice, there is _always_ kept some empty room at
00080     // the beginning of the mpNextGen to place possible elites there
00081     for (int i=mrPopula.mElites; i<mrPopula.size(); i++)
00082         mpNextGen->put (new Individual (mrPopula[0]), i);
00083 
00084 }
00085 
00086 void EAStrategy::evolve (EAEnvironment& envr, TextOStream& out, TextOStream& log)
00087 {
00088     FUNCTION_BEGIN;
00089     
00090     envr.init_cycle ();
00091 
00092     // Evaluate
00093     mrPopula.evaluate (envr, out);
00094     
00095     // Print out cycle reports
00096     out << "Reporting...\n";
00097     mrPopula.report (log);
00098     envr.cycleReport (log, out);
00099 
00100     SelectionSituation situation (mrPopula);
00101     // Order by fitness. Selection methods can use this order if they wish
00102     //RefArray<Individual> pop_order (*mrPopula.mpPopulation);
00103     //pop_order.quicksort ();
00104     
00105     // Create a selection matrix
00106     SelectionMatrix selmat (situation);
00107 
00108     // Create the next generation according to the selection matrix
00109     recombine (situation, selmat);
00110 
00111     // Re-evaluate Tarzan a little...
00112     if (mrPopula.mElites>0) {
00113         out << "Re-evaluating Tarzan...\n";
00114         Individual& tarzan = const_cast<Individual&> (situation.getOrdered(0));
00115         tarzan.evaluate (envr, true);
00116         tarzan.addking ();
00117     }
00118 
00119     FUNCTION_END;
00120 }
00121 
00122 /*******************************************************************************
00123  *
00124 **/
00125 void EAStrategy::recombine (
00126     const SelectionSituation& situation,
00127     const SelectionMatrix&    selmat)
00128 {
00130     // Clone the old population. This is done in two parts because
00131     // we don't want to replicate the elites for no reason
00132 
00133     // Copy the elite references. Warning! Elites are now owned by two
00134     // objects for a while.
00135     for (int i=0; i<mrPopula.mElites; i++)
00136         mpNextGen->put (situation.getOrdered(i), i);
00137 
00139     // Recombine
00140     
00141     const Individual *parent_a, *parent_b;
00142     for (int i=mrPopula.mElites; i<mrPopula.size(); i++) {
00143         // Select two parents
00144         int parent_a_ind, parent_b_ind;
00145         selmat.selectRandomPair (parent_a_ind, parent_b_ind);
00146         parent_a = &situation.getOrdered (parent_a_ind);
00147         parent_b = &situation.getOrdered (parent_b_ind);
00148         
00149         // Recombine them as the descendant
00150         (*mpNextGen)[i].recombine (*parent_a, *parent_b);
00151 
00152         // Mutate the descendant a little
00153         (*mpNextGen)[i].pointMutate (mrPopula.mutRate());
00154 
00155         // Incarnate the descendant
00156         (*mpNextGen)[i].incarnate (true);
00157     }
00158 
00160     // Finally, set the new population as current
00161 
00162     // Swap the next generation as current
00163     Array<Individual>* tmp = mrPopula.mpPopulation;
00164     mrPopula.mpPopulation = mpNextGen;
00165     mpNextGen = tmp;
00166 
00167     // Remove the references to the elites from the original
00168     // population to make the new population their only owner
00169     for (int i=0; i<mrPopula.mElites; i++)
00170         // Since we can cut() only with index, not pointer...
00171         for (int j=0; j<mrPopula.size(); j++)
00172             // Compare pointers
00173             if (mpNextGen->getp(j) == &situation.getOrdered(i)) {
00174                 mpNextGen->cut (j);
00175                 // Move the elite holes to the beginning of the population
00176                 if (mpNextGen->getp (i)) {
00177                     mpNextGen->put (mpNextGen->getp (i), j);
00178                     mpNextGen->cut (i);
00179                 }
00180             }
00181 
00182     // Remove the old population
00183     // delete mrPopula.mpPopulation;
00184 }
00185 
00186 void EAStrategy::addFeaturesTo (Genome& genome) const {
00187 }
00188 
00189 void EAStrategy::print (TextOStream& out) {
00190     out.printf ("Evolving with strategy (e/u[+,]l) = (%d/%d+%d)\n",
00191                 mrPopula.mElites,
00192                 mrPopula.selParams().muFor(mrPopula.size()),
00193                 mrPopula.size());
00194 
00195     out.printf ("Mutation coefficient=%f (binary), %f (int), %f (double rate), "
00196                 "%f (double variance)\n\n",
00197                 mrPopula.mutRate().binaryRate(),
00198                 mrPopula.mutRate().intRate(),
00199                 mrPopula.mutRate().doubleRate(),
00200                 mrPopula.mutRate().doubleVariance());
00201 }
00202 
00203 template<class TYPE>
00204 void checkArray (const Array<TYPE>& arr) {
00205     for (int i=0; i<arr.size(); i++)
00206         if (arr.getp(i))
00207             arr.getp(i)->check ();
00208 }
00209 
00210 void EAStrategy::check () const {
00211     if (mpNextGen) {
00212         mpNextGen->check ();
00213         checkArray<Individual> (*mpNextGen);
00214     }
00215 }
00216 
00217 
00218 
00220 //                                                                           //
00221 //       ----- o                              ----                           //
00222 //       |        |    _    ___   ____  ____ (      |   ___   |   ____       //
00223 //       |---  | -+- |/ \  /   ) (     (      ---  -+-  ___| -+- (           //
00224 //       |     |  |  |   | |---   \__   \__      )  |  (   |  |   \__        //
00225 //       |     |   \ |   |  \__  ____) ____) ___/    \  \__|   \ ____)       //
00226 //                                                                           //
00228 
00229 void FitnessStats::reset () {
00230     mMinFitness = 1E30;
00231     mMaxFitness = 0.0;
00232     mSumFitness = 0.0;
00233     mAvgOver = 0;
00234 }
00235 
00236 void FitnessStats::add (double fitness) {
00237     if (fitness < mMinFitness)
00238         mMinFitness = fitness;
00239 
00240     if (fitness > mMaxFitness)
00241         mMaxFitness = fitness;
00242 
00243     mSumFitness += fitness;
00244 
00245     mAvgOver++;
00246 }
00247 
00248 void FitnessStats::print (TextOStream& out) const {
00249     out.printf ("Fitness min/avg/max = %f / %f / %f\n",
00250                 mMinFitness, mSumFitness/mAvgOver, mMaxFitness);
00251 }
00252 
00253 
00254 
00256 //                                                                          //
00257 //               ----                  |           o                        //
00258 //               |   )       --        |  ___   |           _               //
00259 //               |---   __  |  ) |   | |  ___| -+- |  __  |/ \              //
00260 //               |     /  \ |--  |   | | (   |  |  | /  \ |   |             //
00261 //               |     \__/ |     \__! |  \__|   \ | \__/ |   |             //
00262 //                                                                          //
00264 
00273 Population::Population (EAEnvironment&   envir,   
00274                         const StringMap& params)  
00275         : rpEnvironment (&envir)
00276 {
00277     //
00278     // Set mutation rates
00279     //
00280     double brate = getOrDefault(params,"Population.boolRate", String(0.01)).toDouble ();
00281     double irate = getOrDefault(params,"Population.intRate", String(0.01)).toDouble ();
00282     double frate = getOrDefault(params,"Population.floatRate", String(0.1)).toDouble ();
00283     double fvar =  getOrDefault(params,"Population.floatVariance", String(0.1)).toDouble ();
00284 
00285     mGlobalMutationRate.binaryRate (brate);
00286     mGlobalMutationRate.intRate (irate);
00287     mGlobalMutationRate.doubleRate (frate);
00288     mGlobalMutationRate.doubleVariance (fvar);
00289     mGlobalMutationRate.autoAdaptation (getOrDefault(params,"Population.autoAdapt", String(0)).toInt ());
00290     mAutoadjustGMR = false;
00291 }
00292 
00293 void Population::check () const
00294 {
00295 }
00296 

Generated on Thu Feb 10 20:12:00 2005 for NeHeP by doxygen1.2.18