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

simplepopula.cc

Go to the documentation of this file.
00001 
00025 #include <magic/mpararr.h>
00026 #include <magic/mdatastream.h>
00027 #include "nhp/simplepopula.h"
00028 #include "nhp/gaenvrnmt.h"
00029 #include "nhp/mutrecord.h"
00030 
00031 
00032 SimplePopulation::SimplePopulation (EAEnvironment& envir, const StringMap& params)
00033         : Population (envir, params)
00034 {
00035     // Set selection parameters
00036     int popSize = getOrDefault (params, "SimplePopulation.size", String(20)).toInt ();
00037     mElites     = getOrDefault (params, "EAStrategy.elites", String(int (popSize*0.1))).toInt ();
00038     mUseGlobalElites = true;
00039 
00040     int u = getOrDefault (params, "Selection.mu", String(int(popSize*0.2))).toInt ();
00041     if (u>0)
00042         mSelectionParams.setMu (u);
00043     else if (mElites>=0)
00044         mSelectionParams.setMu (u);
00045     else
00046         mSelectionParams.setMuPart (0.2);
00047 
00048     // Optional parameter
00049     mSelectionParams.setMuPart (getOrDefault (params, "Selection.muPart", String(0.2)).toDouble ());
00050 
00051     int    q  = getOrDefault(params,"Selection.q", String(3)).toInt ();
00052     double ep = getOrDefault(params,"Selection.eta+", String(1.2)).toDouble (); 
00053 
00054     mSelectionParams.setQ (q);
00055     mSelectionParams.setEtaPlus (ep);
00056 
00057     // Check which strategy parameters to self-adapt
00058     bool am  = getOrDefault(params,"Selection.adaptMu", String(0)).toInt ();
00059     bool aq  = getOrDefault(params,"Selection.adaptQ", String(0)).toInt ();
00060     bool aep = getOrDefault(params,"Selection.adaptEta+", String(0)).toInt ();
00061     mSelectionParams.adaptParams (am, aq, aep);
00062     mUseGlobalMu = !am;
00063     mUseGlobalQ = !aq;
00064     mUseGlobalEtaPlus = !aep;
00065 
00066     /**************************************************************************/
00067     // Create a template for an Individual
00068 
00069     Genome templ;
00070     rpEnvironment->addFeaturesTo (templ);     // Add environment genes
00071     addFeaturesTo (templ);                    // Add population  genes
00072     templ.addPrivateGenes (templ, params);    // Add genome      genes
00073     Individual::addGenesTo (templ, params);   // Add individual  genes
00074     
00075     // Set individual-based autoadaptive mutation
00076     templ.selfadjust (mGlobalMutationRate.autoAdaptation());
00077 
00078     /**************************************************************************/
00079     // Create the population from the template individual
00080 
00081     // Create population
00082     mpPopulation = new Array<Individual> ();
00083     mpPopulation->make (popSize); // 20021125: This was popSize-1 for unknown reason
00084 
00085     // Create individuals
00086     for (int i=0; i<mpPopulation->size(); i++) {
00087         mpPopulation->put (new Individual (templ), i);
00088         (*mpPopulation) [i].setSelector(mSelectionParams);
00089         (*mpPopulation) [i].init ();
00090         (*mpPopulation) [i].incarnate (true);
00091     }
00092 
00093     // Set evolution strategy
00094     mpStrategy = new EAStrategy (*this);
00095 
00096     failtrace_begin;
00097     params.failByThrowOnce ();
00098     if (!params.getp("EAStrategy.silent") || params["EAStrategy.silent"] == "0") {
00099         mpStrategy->print (mOuts);
00100     }
00101     failtrace_end;
00102     
00103     // Initialize other parameters;
00104     minsimilarity = getOrDefault (params, "EAStrategy.minSimilarity", String(0.1)).toDouble ();
00105     mAge = 0;
00106 
00107     // Set logging
00108     mEvolog.autoFlush ();
00109     mOuts.autoFlush ();
00110 }
00111 
00112 SimplePopulation::~SimplePopulation () {
00113     delete mpStrategy;
00114     delete mpPopulation;
00115 }
00116 
00117 void SimplePopulation::addFeaturesTo (Genome& genome) const {
00118 }
00119 
00120 double SimplePopulation::evolve (int gens, const char* logfile, double target_fitn)
00121 {
00122     FUNCTION_BEGIN;
00123     
00124     // Open logfile
00125     FILE* save = NULL;
00126     if (logfile) {
00127         save = fopen (logfile, "w");
00128         ASSERTWITH (save, format ("Log file '%s' couldn't be opened", logfile));
00129 
00130         fprintf (save, "Generation, min_fitness, avg_fitness, max_fitness\n");
00131         fflush (save);
00132     }
00133     if (save)
00134         mEvolog.setDevice (new File (save));
00135 
00136     // mOuts.setFlag (EAStrategy::TRACE_RECOMBINATION);
00137     // mOuts.setFlag (EAStrategy::TRACE_MUTATION);
00138     
00139     // For a number of generations
00140     for (int g=0; g<gens; g++) {
00141 
00142         if (MutabilityRecord::record)
00143             MutabilityRecord::reset ();
00144 
00145         // Evolve for one generation
00146         failtrace (mpStrategy->evolve (*rpEnvironment, mOuts, mEvolog));
00147 
00148         // Check the termination criteria
00149         if (target_fitn != -1 && rpEnvironment->bestfitn < target_fitn)
00150             break;
00151         
00152         mEvolog << "\n";
00153     }
00154     
00155     if (save)
00156         fclose (save);
00157 
00158     FUNCTION_END;
00159     return mFitnessStats.minFitness();
00160 }
00161 
00162 void SimplePopulation::evaluate (EAEnvironment& environment, TextOStream& out)
00163 {
00164     FUNCTION_BEGIN;
00165 
00166     mFitnessStats.reset ();
00167 
00168     for (int i=0; i<size(); i++) {
00169         out.printf ("Indv%3d: ", i);
00170         
00171         failtrace ((*this) [i].print (out));
00172         double fitness =  (*this) [i].evaluate (environment);
00173 
00174         mFitnessStats.add (fitness);
00175 
00176         out.printf (", fitn = %f", fitness);
00177         if ((*this)[i].averaged_over ()>1)
00178             out.printf (" (%d evls)", (*this)[i].averaged_over ());
00179         if ((*this)[i].getkings())
00180             out.printf (", kings=%3d\n", (*this)[i].getkings());
00181         out << "\n";
00182     }
00183 
00184     out.printf ("SimplePopulation report gen %d: ", mAge);
00185     mFitnessStats.print (out);
00186 
00187     if (mAutoadjustGMR) {
00188         // Something here
00189     }
00190     
00191     mAge++;
00192 
00193     FUNCTION_END;
00194 }
00195 
00196 void SimplePopulation::resetFitnesses ()
00197 {
00198     for (int i=0; i<size(); i++) {
00199     }
00200 }
00201 
00202 void SimplePopulation::print (TextOStream& out) const {
00203     FUNCTION_BEGIN;
00204     
00205     out << "SimplePopulation {\n";
00206     out.printf ("size=%d,\n", size());
00207     
00208     for (int i=0; i<size(); i++) {
00209         (*this)[i].print (out);
00210         out << "\n";
00211     }
00212 
00213     out << "}\n";
00214 
00215     FUNCTION_END;
00216 }
00217 
00218 void SimplePopulation::report (TextOStream& log) const
00219 {
00220     log.printf ("%d %.30f %.30f %.30f ", mAge,
00221                 mFitnessStats.minFitness(), mFitnessStats.avgFitness(),
00222                 mFitnessStats.maxFitness());
00223     if (MutabilityRecord::record)
00224         log.printf ("%f %f %f %f %f %f %f %.30f %f",
00225                     MutabilityRecord::boolMin(),
00226                     MutabilityRecord::boolAvg(),
00227                     MutabilityRecord::boolMax(),
00228                     MutabilityRecord::floatMin(),
00229                     MutabilityRecord::floatAvg(),
00230                     MutabilityRecord::floatMax(),
00231                     MutabilityRecord::floatVarMin(),
00232                     MutabilityRecord::floatVarAvg(),
00233                     MutabilityRecord::floatVarMax());
00234     log.flush ();
00235 }
00236 
00237 void SimplePopulation::check () const {
00238     ASSERT (mpPopulation);
00239     for (int i=0; i<mpPopulation->size(); i++)
00240         (*mpPopulation)[i].check ();
00241     rpEnvironment->check ();
00242     mpStrategy->check ();
00243 }
00244 
00245 

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