freExhaustiveSOOptimizerController.cxx

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: freExhaustiveSOOptimizerController.cxx,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 
00023 #include "freExhaustiveSOOptimizerController.h"
00024 #include "freExceptions.h"
00025 #include "freTransformSetupAdaptor.h"
00026 
00027 namespace FREE
00028 {
00029 
00033 
00034 
00035 
00036 DefineParameterMacro(ExhaustiveSOOptimizerController,StepLength,"StepLength","Determines the basic step length of the optimizer along every search axis.");
00037 DefineParameterMacro(ExhaustiveSOOptimizerController,NumberOfSteps,"NumberOfSteps","Number of steps to the left and to the right of the initial value along the search axis.");
00038 DefineParameterMacro(ExhaustiveSOOptimizerController,ParameterAxis,"ParameterAxis","Indicates the search axis a parameter belongs to.");
00039 DefineParameterMacro(ExhaustiveSOOptimizerController,LogarithmicAxis,"LogarithmicAxis","Indicates if the axis will be searched linear (flase) or logarithmic (true).");
00040 DefineParameterMacro(ExhaustiveSOOptimizerController,MinimumIsBest,"MinimumIsBest","Indicates if the best result found by the search is the maximum finding or minimum.");
00041 
00042 
00043 ExhaustiveSOOptimizerController::
00044 ExhaustiveSOOptimizerController()
00045 {
00046   //Profile settings
00047   this->UpdateControllerID(ControllerID::ExhaustiveSOOptimizerController);
00048   this->m_Description = "Optimizes by an exhaustive search approach.";
00049 };
00050 
00051 void
00052 ExhaustiveSOOptimizerController::
00053 GenerateProfile(CtrlProfile::ControllerProfile& profile,
00054                   const SessionComponentCache* pComponentCache,
00055                   bool bRegardOldSetup) const
00056 {
00057   Superclass::GenerateProfile(profile,pComponentCache,bRegardOldSetup);
00058 
00059         //Parameters
00060   profile.Parameters().AddParameter(cParam_StepLength,CtrlProfile::Parameter::PVTDouble,cParamDsc_StepLength,1,"1.0");
00061   profile.Parameters().AddParameter(cParam_NumberOfSteps,CtrlProfile::Parameter::PVTULong,cParamDsc_NumberOfSteps,1,"5");
00062   profile.Parameters().AddParameter(cParam_ParameterAxis,Parameter::PVTLong,cParamDsc_ParameterAxis,1,"-1");
00063   profile.Parameters().AddParameter(cParam_LogarithmicAxis,Parameter::PVTLong,cParamDsc_LogarithmicAxis,1,"0");
00064   profile.Parameters().AddParameter(cParam_MinimumIsBest,CtrlProfile::Parameter::PVTBool,cParamDsc_MinimumIsBest,1,"true");
00065 };
00066 
00067 
00068 long
00069 ExhaustiveSOOptimizerController::
00070 GetMaxIterationCount(const SessionComponentCache* pComponentCache, unsigned int iResolutionLevel) const
00071 {
00072         if (!pComponentCache) throwCtrlExceptionMacro("","Passed pComponentCache is NULL.");
00073         if (!pComponentCache->SetupIsAssigned()) throwCtrlExceptionMacro("","Cache has no setup assigned.");
00074 
00075   Parameter::Pointer smpParam = this->GetParameter(pComponentCache,std::string(cParam_StepLength));
00076   if (smpParam.IsNull()) throwCtrlExceptionMacro("","Error; missing parameter: "<< cParam_StepLength);
00077 
00078   long lResult = 1;
00079     
00080   for (unsigned int index = 0; index<smpParam->ParameterSize(); index++)
00081   {
00082     long lSteps;
00083                 smpParam->GetParameterValue(lSteps,index);
00084     lResult *= lSteps;
00085   }
00086 
00087   return lResult;
00088 };
00089 
00090 void
00091 ExhaustiveSOOptimizerController::
00092 ActualizeMainComponent(ComponentType* pMainComponent,
00093                        SessionComponentCache* pComponentCache, SessionInfo* pSessionInfo,
00094                                                                                          const unsigned int& iActLevel) const
00095 {
00096         Superclass::ActualizeMainComponent(pMainComponent, pComponentCache,
00097                                                                                                                                                  pSessionInfo, iActLevel);
00098 
00099         double dStepLength;
00100         bool bMinIsBest;
00101   ComponentType::StepsType numberOfSteps;
00102   ComponentType::ParameterAxisType parameterAxis;
00103   ComponentType::AxisTypeType logAxis;
00104 
00105   try
00106   {
00107     SessionAccessor::GetParameterValue(pComponentCache,cParam_StepLength,dStepLength);
00108     SessionAccessor::GetParameterValue(pComponentCache,cParam_MinimumIsBest,bMinIsBest);
00109   }
00110   catchAllNPassMacro("Error while retrieving parameter values.");    
00111 
00112   Parameter::Pointer smpParameter = SessionAccessor::GetParameter(pComponentCache,cParam_NumberOfSteps);
00113   if (smpParameter.IsNull()) throwExceptionMacro("Missing parameter: " << cParam_NumberOfSteps);
00114         numberOfSteps = Convert::ParameterLayerToArray<ComponentType::StepsType>(*(smpParameter->GetParameterLayer(0)));
00115 
00116   smpParameter = SessionAccessor::GetParameter(pComponentCache,cParam_ParameterAxis);
00117   if (smpParameter.IsNull()) throwExceptionMacro("Missing parameter: " << cParam_ParameterAxis);
00118         parameterAxis = Convert::ParameterLayerToArray<ComponentType::ParameterAxisType>(*(smpParameter->GetParameterLayer(0)));
00119 
00120   smpParameter = SessionAccessor::GetParameter(pComponentCache,cParam_LogarithmicAxis);
00121   if (smpParameter.IsNull()) throwExceptionMacro("Missing parameter: " << cParam_LogarithmicAxis);
00122         logAxis = Convert::ParameterLayerToArray<ComponentType::AxisTypeType>(*(smpParameter->GetParameterLayer(0)));
00123 
00124   ComponentSetup* pTransformSetup = pComponentCache->Setup()->GetComponentByIDPath("../"+ComponentID(cComp_MainTransform));
00125   if (!pTransformSetup) throwCtrlExceptionMacro("","No main transform setup found to gather parameter scales");
00126   TransformSetupAdaptor adaptor(pTransformSetup);
00127 
00128   pMainComponent->SetStepLength(dStepLength);
00129         pMainComponent->SetInitialPosition( adaptor.GetInitialTransformParameters() );
00130   pMainComponent->SetNumberOfSteps(numberOfSteps);
00131   pMainComponent->SetParameterAxis(parameterAxis);
00132   pMainComponent->SetAxisType(logAxis);
00133         pMainComponent->SetMinimumIsBest(bMinIsBest);
00134 };
00135 
00136 void
00137 ExhaustiveSOOptimizerController::
00138 SetStatisticEntryMainComponent(StatisticEntry& rStatisticEntry,
00139                                              ComponentType* pMainComponent,
00140                                              SessionComponentCache* pMainComponentCache,
00141                                              SessionInfo* pSessionInfo,
00142                                              StatisticDictionary& rDictionary) const
00143 {
00144   ParameterArrayType parameters = pMainComponent->GetCurrentPosition();
00145 
00146   std::string sName = "Position #";
00147   std::string sIDPath = pMainComponentCache->GetIDPath();
00148   std::string sCommment = "Optimizer position. Meaning depends on chosen transformation";
00149   StatisticValueDefinition* pEntry = rDictionary.GetValueDefinitionByName(sIDPath,sName+"0");
00150     
00151   if (!pEntry) //Entry is not recorded yet, so do so.
00152   {
00153     std::string sName = "Position #";
00154     pEntry = rDictionary.AddValueDefinition(sIDPath,sName+"0", sCommment);
00155     for (unsigned int i = 1; i<parameters.Size(); i++)
00156     {
00157       rDictionary.AddValueDefinition(sIDPath,sName+Convert::ToStr(i),sCommment);
00158     }
00159 
00160     rDictionary.AddValueDefinition(sIDPath,"OptimizerValue", "Composed value of the optimizer");
00161   }
00162   
00163   for (unsigned int i = 0; i<parameters.Size(); i++)
00164   {
00165     rStatisticEntry.AddValue(Convert::ToStr(parameters.GetElement(i)),pEntry->GetRefID()+i);
00166   }
00167 
00168   rStatisticEntry.AddValue(Convert::ToStr(pMainComponent->GetCurrentValue()),pEntry->GetRefID()+parameters.Size());
00169 };
00170 
00171 } //end of namespace free

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