freESRankRecombinator.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: freESRankRecombinator.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 _freESRankRecombinator_txx
00023 #define _freESRankRecombinator_txx
00024 
00025 #include "freESRankRecombinator.h"
00026 
00027 namespace FREE
00028 {
00029   namespace ES
00030   {
00031 
00032 
00033     template <class TIndividual>
00034     RankRecombinator<TIndividual>
00035       ::RankRecombinator()
00036     {
00037       m_RankDirection = true;
00038     }
00039 
00040     template <class TIndividual>
00041     typename RankRecombinator<TIndividual>::IndividualPointer
00042       RankRecombinator<TIndividual>
00043       ::Recombine(const ParentSelectionType& parents) const
00044     {
00045       if (!(parents.size()>0)) throwExceptionMacro("Error. At least one parent is needed for recombination.");
00046 
00047       ParentSelectionType sortedParents = parents;
00048 
00049       if (m_RankDirection)
00050       {
00051         std::sort(sortedParents.begin(), sortedParents.end(), TIndividual::ObjectiveValueIsLesser);
00052       }
00053       else
00054       {
00055         std::sort(sortedParents.begin(), sortedParents.end(), TIndividual::ObjectiveValueIsGreater);
00056       };
00057 
00058       IndividualPointer newIndividual = parents[0]->Clone();
00059 
00060       //reset values
00061       for (unsigned int index = 0; index<newIndividual->ObjectiveParameters().size(); index++)
00062       { //objective parameter
00063         (newIndividual->ObjectiveParameters())[index] = 0.0;
00064       }
00065       for (unsigned int index = 0; index<newIndividual->StrategicParameters().size(); index++)
00066       { //strategic parameter
00067         if ( newIndividual->StrategicParameters().at(index)->GetHandling()!= IndividualType::StrategicParameterType::HDynamic)
00068         {
00069             ((newIndividual->StrategicParameters())[index])->SetValue(0.0);
00070         }
00071       }
00072 
00073       //calculation of the weightes mean
00074       for (unsigned int count = 0; count<sortedParents.size(); count++)
00075       {
00076         double dW = ComputeWeight(count+1, sortedParents.size());
00077 
00078         for (unsigned int index = 0; index<newIndividual->ObjectiveParameters().size(); index++)
00079         { //objective parameter
00080           try
00081           {
00082             typename IndividualType::ObjectiveParameterType newValue = (newIndividual->ObjectiveParameters())[index];
00083             newValue += dW*((sortedParents[count]->ObjectiveParameters())[index]);
00084             (newIndividual->ObjectiveParameters())[index] = newValue;
00085           }
00086           catchAllNPassMacro("Error while recombining objectiv parameter #"+Convert::ToStr(index)+".");
00087         }
00088 
00089         for (unsigned int index = 0; index<newIndividual->StrategicParameters().size(); index++)
00090         { //strategic parameter
00091           if ( newIndividual->StrategicParameters().at(index)->GetHandling()!= IndividualType::StrategicParameterType::HDynamic)
00092           {
00093             try
00094             {
00095               typename IndividualType::StrategicParameterValueType newValue = ((newIndividual->StrategicParameters())[index])->GetValue();
00096               newValue += dW*(((sortedParents[count]->StrategicParameters())[index])->GetValue());
00097               ((newIndividual->StrategicParameters())[index])->SetValue(newValue);
00098             }
00099             catchAllNPassMacro("Error while recombining strategic parameter #"+Convert::ToStr(index)+".");
00100           }
00101         }
00102       }
00103 
00104       return newIndividual;
00105     }
00106 
00107     template <class TIndividual>
00108     double
00109     RankRecombinator<TIndividual>
00110     ::ComputeWeight(const unsigned long lRank, const unsigned long lParentCount) const
00111     {
00112       double dSum = 0;
00113 
00114       for (unsigned long index = 0; index < lParentCount; index++)
00115       {
00116         dSum += std::log(index+1.0);
00117       }
00118 
00119       return (log(lParentCount+1.0)-log((double)lRank)) / ((lParentCount*log(lParentCount+1.0)) - dSum);
00120     };
00121 
00122     template <class TIndividual>
00123     typename RankRecombinator<TIndividual>::WeightVectorType
00124     RankRecombinator<TIndividual>
00125     ::GetWeights(const unsigned long lParentCount) const
00126     {
00127       WeightVectorType weights;
00128       weights.resize(lParentCount);
00129 
00130       for (unsigned long index = 0; index < lParentCount; index++)
00131       {
00132         weights[index] = ComputeWeight(index+1, lParentCount);
00133       }
00134 
00135       return weights;
00136     };
00137 
00138   } // end of namespace ES
00139 } // end of namespace FREE
00140 
00141 #endif

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