00001 #include <iostream> 00002 #include "GLBaseMutator.h" 00003 #include "GLBaseOrganism.h" 00004 #include "GLBaseOrganismStorage.h" 00005 #include "GLBasePopulation.h" 00006 #include "GLVariatorStandard.h" 00012 GLVariatorStandard::GLVariatorStandard(GLBaseMutator* mutator): 00013 GLBaseVariator(mutator) 00014 { 00015 }//constructor 00016 00020 GLVariatorStandard::~GLVariatorStandard() 00021 { 00022 }//destructor 00023 00042 int GLVariatorStandard::applyMutator( 00043 GLBasePopulation* population, 00044 GLBaseOrganismStorage* organisms, 00045 GLBaseOrganismStorage* mutants, 00046 GLConstants::TStorageFilling fill, 00047 GLConstants::TTwinsFlags twins, 00048 int max_mutants) 00049 { 00050 //clean the container with mutants, if necessary. 00051 if (fill != GLConstants::APPEND) mutants->emptyStorage(); 00052 //see, if maximum number of mutants is 0... 00053 if (max_mutants == 0) return 0; 00054 //get the iterator for the source container 00055 int counter = 0; 00056 GLBaseOrganismStorage::const_iterator* it = organisms->getIteratorConst(); 00057 //loop through all elements of the container 00058 for(; !it->isEnd(); ++(*it)) 00059 { 00060 //create a clone and try to mutate it 00061 GLBaseOrganism *clone = it->getElement()->makeClone(); 00062 if (m_mutator->mutateOrganism(clone)) 00063 { 00064 //mutated, now check, if it is truly new organism 00065 //-- check if the organism is new 00066 if ((twins == GLConstants::TWINS_ALLOWED) || 00067 population->isOrganismTrulyNew(clone)) 00068 { 00069 //in some case the storage does not allow to add twins anyway, 00070 //so ensure, that we delete clone to avoid memory leak. 00071 if (mutants->addOrganism(clone, twins)) 00072 { 00073 counter++; 00074 } 00075 else 00076 { 00077 delete clone; 00078 } 00079 //check if we need to continue 00080 if ((max_mutants > 0) && (counter == max_mutants)) 00081 { 00082 break; 00083 } 00084 } 00085 else 00086 { 00087 delete clone; 00088 } 00089 } 00090 else 00091 { 00092 delete clone; 00093 } 00094 } 00095 delete it; 00096 return counter; 00097 }//applyMutator