00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "freExhaustiveSOOptimizer.h"
00024 #include "itkCommand.h"
00025 #include "itkEventObject.h"
00026
00027 namespace FREE
00028 {
00029
00033 ExhaustiveSOOptimizer
00034 ::ExhaustiveSOOptimizer()
00035 {
00036
00037 itkDebugMacro("Constructor");
00038
00039 m_StepLength = 1.0;
00040 m_CurrentIteration = 0;
00041 m_CurrentValue = 0;
00042 m_CurrentDecomposedValue = 0;
00043 m_CurrentIndex.Fill(0);
00044 m_Stop = false;
00045 m_NumberOfSteps.Fill(0);
00046 m_ParameterAxis.Fill(-1);
00047 m_AxisType.Fill(0);
00048 }
00049
00050 unsigned int
00051 ExhaustiveSOOptimizer
00052 ::GetAxisCount() const
00053 {
00054 return m_NumberOfSteps.GetSize();
00055 };
00056
00057 void ExhaustiveSOOptimizer::StartOptimization( void )
00058 {
00059 if (m_CostFunction.IsNull()) throwExceptionMacro("Error: cannot optimize. No cost function is defined.");
00060 this->StartWalking();
00061 }
00062
00063
00064 void
00065 ExhaustiveSOOptimizer
00066 ::StartWalking( void )
00067 {
00068
00069 itkDebugMacro("StartWalking");
00070 this->InvokeEvent( itk::StartEvent() );
00071
00072 m_MinimumMetricValuePosition = this->GetInitialPosition();
00073 m_MaximumMetricValuePosition = this->GetInitialPosition();
00074
00075
00076
00077 m_MaximumDecomposedMetricValue.Fill(itk::NumericTraits< DecomposedMeasureType::ValueType >::NonpositiveMin());
00078 m_MinimumDecomposedMetricValue.Fill(itk::NumericTraits< DecomposedMeasureType::ValueType >::max());
00079 m_MaximumMetricValue = itk::NumericTraits< MeasureType >::NonpositiveMin();
00080 m_MinimumMetricValue = itk::NumericTraits< MeasureType >::max();
00081
00082 m_CurrentIteration = 0;
00083 m_MaximumNumberOfIterations = 1;
00084
00085 const unsigned int spaceDimension = this->GetAxisCount();
00086
00087 if (spaceDimension != m_AxisType.Size()) throwExceptionMacro("Error: not all axis have a specified type. Please ensure the correct initialisation of parameter axis and axis type.");
00088
00089 for (unsigned int i=0; i< spaceDimension; i++)
00090 {
00091 m_MaximumNumberOfIterations *= (2 * m_NumberOfSteps[i] + 1);
00092 }
00093
00094 m_CurrentIndex.SetSize(spaceDimension);
00095 m_CurrentIndex.Fill(0);
00096
00097 this->SetCurrentPosition( this->GetInitialPosition() );
00098
00099 itkDebugMacro("Calling ResumeWalking");
00100
00101 this->ResumeWalking();
00102 }
00103
00104
00108 void
00109 ExhaustiveSOOptimizer
00110 ::ResumeWalking( void )
00111 {
00112 itkDebugMacro("ResumeWalk");
00113 m_Stop = false;
00114
00115 while( !m_Stop )
00116 {
00117 ParametersType currentPosition = this->GetCurrentPosition();
00118
00119 if( m_Stop )
00120 {
00121 StopWalking();
00122 break;
00123 }
00124
00125 m_CurrentValue = this->GetValue( currentPosition );
00126 m_CurrentDecomposedValue = this->GetCostFunction()->GetCurrentDecomposedValue();
00127
00128 if (m_CurrentValue > m_MaximumMetricValue)
00129 {
00130 m_MaximumMetricValue = m_CurrentValue;
00131 m_MaximumMetricValuePosition = currentPosition;
00132 m_MaximumDecomposedMetricValue = m_CurrentDecomposedValue;
00133 }
00134 if (m_CurrentValue < m_MinimumMetricValue)
00135 {
00136 m_MinimumMetricValue = m_CurrentValue;
00137 m_MinimumMetricValuePosition = currentPosition;
00138 m_MinimumDecomposedMetricValue = m_CurrentDecomposedValue;
00139 }
00140
00141 if( m_Stop )
00142 {
00143 this->StopWalking();
00144 break;
00145 }
00146
00147 this->AdvanceOneStep();
00148
00149 m_CurrentIteration++;
00150
00151 }
00152
00153 if (m_MinimumIsBest)
00154 {
00155 m_BestPosition = m_MinimumMetricValuePosition;
00156 m_BestValue = m_MinimumMetricValue;
00157 }
00158 else
00159 {
00160 m_BestPosition = m_MaximumMetricValuePosition;
00161 m_BestValue = m_MaximumMetricValue;
00162 }
00163 }
00164
00165
00166 void
00167 ExhaustiveSOOptimizer
00168 ::StopWalking( void )
00169 {
00170
00171 itkDebugMacro("StopWalking");
00172
00173 m_Stop = true;
00174 this->InvokeEvent( itk::EndEvent() );
00175 }
00176
00177
00178 bool
00179 ExhaustiveSOOptimizer
00180 ::IsStoppable() const
00181 {
00182 return true;
00183 };
00184
00185
00186 void
00187 ExhaustiveSOOptimizer
00188 ::StopOptimization()
00189 {
00190 this->StopWalking();
00191 };
00192
00193 bool
00194 ExhaustiveSOOptimizer
00195 ::IsResumeable() const
00196 {
00197 return true;
00198 };
00199
00200 void
00201 ExhaustiveSOOptimizer
00202 ::ResumeOptimization()
00203 {
00204 this->ResumeWalking();
00205 };
00206
00207
00208 void
00209 ExhaustiveSOOptimizer
00210 ::AdvanceOneStep( void )
00211 {
00212
00213 itkDebugMacro("AdvanceOneStep");
00214
00215 const unsigned int spaceDimension =
00216 m_CostFunction->GetNumberOfParameters();
00217
00218 ScalesType scales = this->GetScales();
00219
00220
00221 if (scales.size() != spaceDimension) throwExceptionMacro("The size of Scales is "<< scales.size()<< ", but the NumberOfParameters is "<< spaceDimension<< ".");
00222
00223 ParametersType newPosition( spaceDimension );
00224 ParametersType currentPosition = this->GetCurrentPosition();
00225
00226 IncrementIndex( newPosition );
00227
00228 itkDebugMacro(<<"new position = " << newPosition );
00229
00230 this->InvokeEvent( itk::IterationEvent() );
00231 this->SetCurrentPosition( newPosition );
00232 }
00233
00234 void
00235 ExhaustiveSOOptimizer
00236 ::IncrementIndex( ParametersType &newPosition )
00237 {
00238
00239 unsigned int idx = 0;
00240
00241 const unsigned int spaceDimension =
00242 m_CostFunction->GetNumberOfParameters();
00243
00244 const unsigned int axisDimension = this->GetAxisCount();
00245
00246 while( idx < axisDimension )
00247 {
00248 m_CurrentIndex[idx]++;
00249
00250 if( m_CurrentIndex[idx] > (2*m_NumberOfSteps[idx]))
00251 {
00252 m_CurrentIndex[idx]=0;
00253 idx++;
00254 }
00255 else
00256 {
00257 break;
00258 }
00259 }
00260
00261 if( idx==axisDimension )
00262 {
00263 m_Stop = true;
00264 }
00265
00266 for(unsigned int i=0; i<spaceDimension; i++)
00267 {
00268
00269 long axisIndex = m_ParameterAxis[i];
00270 if (axisIndex==-1) axisIndex = i;
00271
00272
00273 if (m_AxisType[axisIndex]==0)
00274 {
00275 newPosition[i] = (m_CurrentIndex[axisIndex]-m_NumberOfSteps[axisIndex]) *
00276 m_StepLength * this->GetScales()[i] +
00277 this->GetInitialPosition()[i];
00278 }
00279 else
00280 {
00281 double dB = m_StepLength * this->GetScales()[i];
00282 double dP = m_CurrentIndex[axisIndex]-m_NumberOfSteps[axisIndex];
00283
00284 newPosition[i] = pow(dB, dP) * this->GetInitialPosition()[i];
00285 }
00286 }
00287 }
00288
00289
00290
00291 void
00292 ExhaustiveSOOptimizer
00293 ::PrintSelf(std::ostream& os, itk::Indent indent) const
00294 {
00295 Superclass::PrintSelf(os,indent);
00296
00297 os << indent << "NumberOfSteps = " << m_NumberOfSteps << std::endl;
00298 os << indent << "ParameterAxis = " << m_ParameterAxis << std::endl;
00299 os << indent << "CurrentIteration = " << m_CurrentIteration << std::endl;
00300 os << indent << "Stop = " << m_Stop << std::endl;
00301 os << indent << "CurrentParameter = " << m_CurrentParameter << std::endl;
00302 os << indent << "StepLength = " << m_StepLength << std::endl;
00303 os << indent << "CurrentIndex = " << m_CurrentIndex << std::endl;
00304 os << indent << "MaximumNumberOfIterations = " << m_MaximumNumberOfIterations << std::endl;
00305 os << indent << "MaximumMetricValue = " << m_MaximumMetricValue << std::endl;
00306 os << indent << "MinimumMetricValue = " << m_MinimumMetricValue << std::endl;
00307 os << indent << "MinimumMetricValuePosition = " << m_MinimumMetricValuePosition << std::endl;
00308 os << indent << "MaximumMetricValuePosition = " << m_MaximumMetricValuePosition << std::endl;
00309 }
00310
00311
00312 }
00313
00314