00001 #include <iostream>
00002
00003 #include "GLBaseBreeder.h"
00004 #include "GLBaseEvaluator.h"
00005 #include "GLBaseRanker.h"
00006 #include "GLBaseSelectionist.h"
00007 #include "GLBaseVariator.h"
00008 #include "GLPopulationStandard.h"
00009 #include "GLStorageSet.h"
00010
00011 static const int INITIAL_SIZE = 200;
00012
00013 GLPopulationStandard::GLPopulationStandard(bool keep_corpses):
00014 GLBasePopulation(new GLStorageSet())
00015 {
00016 m_offsprings = new GLStorageSet();
00017 m_mutants = new GLStorageSet();
00018 m_keep_corpses = keep_corpses;
00019 }
00020
00021 GLPopulationStandard::~GLPopulationStandard()
00022 {
00023
00024 if (m_offsprings != NULL)
00025 {
00026 m_offsprings->deleteAllOrganisms();
00027 delete m_offsprings;
00028 }
00029
00030 if (m_mutants != NULL)
00031 {
00032 m_mutants->deleteAllOrganisms();
00033 delete m_mutants;
00034 }
00035
00036 clearMorgue();
00037 }
00038
00047 int GLPopulationStandard::breed(GLBaseBreeder *breeder,
00048 int max_offsprings)
00049 {
00050 return breeder->breedOrganisms(this, m_main_storage, m_offsprings,
00051 GLConstants::REPLACE,
00052 GLConstants::NO_TWINS,
00053 max_offsprings);
00054 }
00059 void GLPopulationStandard::clearMorgue()
00060 {
00061 for(TGLSetOfOrganismsConst::const_iterator it = m_morgue.begin();
00062 it != m_morgue.end(); ++it)
00063 {
00064 delete (*it);
00065 }
00066 #if DEBUG > 2
00067 cout << m_morgue.size() << " was stored in morgue by the end\n";
00068 #endif
00069 m_morgue.clear();
00070 }
00071
00080 int GLPopulationStandard::evaluateAll(GLBaseEvaluator* evaluator)
00081 {
00082 int evaluated = evaluateOrganisms(m_main_storage, evaluator);
00083 evaluated += evaluateOrganisms(m_offsprings, evaluator);
00084 evaluated += evaluateOrganisms(m_mutants, evaluator);
00085 return evaluated;
00086 }
00087
00095 int GLPopulationStandard::evaluateOrganisms(
00096 GLBaseOrganismStorage *container,
00097 GLBaseEvaluator* evaluator,
00098 bool not_evaluated_only)
00099 {
00100 int counter = 0;
00101
00102 GLBaseOrganismStorage::iterator *it = container->getIterator();
00103 for (; !(it->isEnd()); it->moveNext())
00104 {
00105
00106
00107 if (!not_evaluated_only ||
00108 (not_evaluated_only && !(it->getElement()->isEvaluated())))
00109 {
00110 it->getElement()->evaluate(evaluator);
00111 counter++;
00112 }
00113 }
00114 delete it;
00115 return counter;
00116 }
00117
00127 bool GLPopulationStandard::isOrganismTrulyNew(const
00128 GLBaseOrganism* organism) const
00129 {
00130
00131 if (m_main_storage->findOrganism(organism) != NULL) return false;
00132
00133 if (m_offsprings->findOrganism(organism) != NULL) return false;
00134
00135 if (m_mutants->findOrganism(organism) != NULL) return false;
00136
00137 if (m_morgue.find(organism) != m_morgue.end()) return false;
00138
00139
00140 return true;
00141 }
00142
00158 int GLPopulationStandard::keepFittest(
00159 GLBaseSelectionist* selectionist,
00160 vector<const GLBaseOrganism*>& best_organisms,
00161 int new_size)
00162 {
00163
00164 unsigned newsize = (new_size < 0) ? getSize() : new_size;
00165
00166 vector<GLBaseOrganism*> current_population;
00167 m_main_storage->getOrganismsVector(current_population);
00168
00169
00170 vector<GLBaseOrganism*> new_generation;
00171 m_mutants->getOrganismsVector(new_generation);
00172 m_offsprings->getOrganismsVector(new_generation, GLConstants::APPEND);
00173
00174
00175 vector<GLBaseOrganism*> to_live;
00176 vector<GLBaseOrganism*> to_die;
00177 int counter = selectionist->doSelection(current_population,
00178 new_generation, newsize, to_live, to_die,
00179 best_organisms);
00180
00181
00182 m_main_storage->fillFromVector(to_live, GLConstants::REPLACE);
00183
00184 processCorpses(to_die);
00185
00186 return counter;
00187 }
00188
00200 int GLPopulationStandard::mutate(GLBaseVariator *variator,
00201 int max_mutants,
00202 int mutation_policy)
00203 {
00204
00205 m_mutants->emptyStorage();
00206 int mutated(0);
00207
00208 if (mutation_policy & GLConstants::MUTATE_PARENTS)
00209 {
00210 mutated = variator->applyMutator(this, m_main_storage, m_mutants,
00211 GLConstants::APPEND,
00212 GLConstants::NO_TWINS,
00213 max_mutants);
00214 }
00215
00216
00217 if (max_mutants > 0)
00218 {
00219
00220
00221 max_mutants = max_mutants > mutated ? max_mutants - mutated : 0;
00222 }
00223 if (mutation_policy & GLConstants::MUTATE_CHILDREN)
00224 {
00225 mutated += variator->applyMutator(this, m_offsprings, m_mutants,
00226 GLConstants::APPEND,
00227 GLConstants::NO_TWINS,
00228 max_mutants);
00229 }
00230 return mutated;
00231 }
00232
00240 void GLPopulationStandard::populate(int size,
00241 GLBaseOrganismInitialiser* initialiser,
00242 GLConstants::TTwinsFlags twins)
00243 {
00244 int counter = 0;
00245 int size_10 = size * 10;
00246 while ((m_main_storage->getSize() != size) && (counter < size_10))
00247 {
00248 GLBaseOrganism *org = initialiser->initialiseOrganism();
00249
00250
00251 if (!addOrganism(org, twins))
00252 delete org;
00253 counter++;
00254 }
00255 }
00256
00257 void GLPopulationStandard::prepareForNextGeneration()
00258 {
00259
00260 m_mutants->emptyStorage();
00261 m_offsprings->emptyStorage();
00262 }
00263
00270 void GLPopulationStandard::processCorpses(vector<GLBaseOrganism*>& corpses)
00271 {
00272 if (isKeepCorpses())
00273 {
00274
00275
00276 for(size_t i = 0; i < corpses.size(); i++)
00277 {
00278
00279 if (m_morgue.insert(corpses[i]).second == false)
00280 delete corpses[i];
00281 }
00282 }
00283 else
00284 {
00285
00286 for(size_t i = 0; i < corpses.size(); i++)
00287 {
00288 delete corpses[i];
00289 }
00290 }
00291 corpses.clear();
00292 }