freESCommaSelection.txx

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   F.R.E.E. - flexible registration evaluation engine
00004   Version:   v.1.0.0
00005   Date:      $Date: 2006/09/01 12:00:00 $
00006   Module:    $RCSfile: freESCommaSelection.txx,v $
00007   Language:  C++
00008 
00009 
00010 
00011   Copyright (c) 2007 Ralf o Floca (Department of Medical Informatics,
00012   Institute for Medical Biometry and Informatics, University of Heidelberg,
00013   Germany). All rights reserved.
00014   See FREECopyright.txt or http://www.mi.med.uni-hd.de/free/copyright.htm
00015   for details.
00016 
00017      This software is distributed WITHOUT ANY WARRANTY; without even 
00018      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00019      PURPOSE.  See the above copyright notices for more information.
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       //search for individual that should be removed because of their age
00051       if (pPopulation->Size()>0)
00052       {
00053         for (long index = pPopulation->Size()-1; index > -1 ; index--)
00054         { 
00055           // the ID of new children of the current generation is 1+ the population generation id,
00056           // so this childs will have an age of 0.
00057           unsigned long age = (pPopulation->GetGenerationID()+1)-((*pPopulation)[index])->GetGenerationID();
00058           if ( age > m_MaxGenerationAge)
00059           {
00060             //remove old individual and add it to the population of removed individuals
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       //now remove the worst individuals (similar to plus selection)
00070       PopulationPointer removedPop2 = this->m_PlusSelection->Select(pPopulation, newSize);
00071 
00072       //join both removed populations
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   } // end of namespace ES
00082 } // end of namespace FREE
00083 
00084 #endif

Generated at Sat Oct 13 15:53:34 2007 for f.r.e.e. - Flexible Registration and Evaluation Engine by doxygen 1.5.3 written by Dimitri van Heesch, © 1997-2000