00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _freESCommaSelection_txx
00023 #define _freESCommaSelection_txx
00024
00025 #include "freESCommaSelection.h"
00026
00027 namespace FREE
00028 {
00029 namespace ES
00030 {
00031
00032 template <class TIndividual>
00033 CommaSelection<TIndividual>
00034 ::CommaSelection()
00035 {
00036 m_MaxGenerationAge = 0;
00037 this->m_PlusSelection = PrivatPlusSelectionType::New();
00038 }
00039
00040 template <class TIndividual>
00041 typename CommaSelection<TIndividual>::PopulationPointer
00042 CommaSelection<TIndividual>
00043 ::Select(PopulationType* pPopulation, PopulationSizeType newSize) const
00044 {
00045 if (!pPopulation) throwExceptionMacro("Error. Passed population is NULL.");
00046 if (pPopulation->Size()<newSize) throwExceptionMacro("Error. Passed population is already smaller the prefered size, nothing to select. Population size: "<<pPopulation->Size()<<"; new size: "<<newSize);
00047
00048 PopulationPointer removedPopulation = PopulationType::New();
00049
00050
00051 if (pPopulation->Size()>0)
00052 {
00053 for (long index = pPopulation->Size()-1; index > -1 ; index--)
00054 {
00055
00056
00057 unsigned long age = (pPopulation->GetGenerationID()+1)-((*pPopulation)[index])->GetGenerationID();
00058 if ( age > m_MaxGenerationAge)
00059 {
00060
00061 removedPopulation->push_back((*pPopulation)[index]);
00062 pPopulation->erase(pPopulation->begin()+index);
00063 }
00064 }
00065 }
00066
00067 if (pPopulation->Size()<newSize) throwExceptionMacro("Error. Passed population has not enough new children. After removing old individuals population is too small. Ensure that the number of new children per generation is greater or equal the number of parents, when using a comma selection.");
00068
00069
00070 PopulationPointer removedPop2 = this->m_PlusSelection->Select(pPopulation, newSize);
00071
00072
00073 for (unsigned long index = 0; index < removedPop2->Size(); index++)
00074 {
00075 removedPopulation->push_back(removedPop2->ElementAt(index));
00076 }
00077
00078 return removedPopulation;
00079 };
00080
00081 }
00082 }
00083
00084 #endif