ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkRegularStepGradientDescentBaseOptimizer.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkRegularStepGradientDescentBaseOptimizer_h
00019 #define __itkRegularStepGradientDescentBaseOptimizer_h
00020 
00021 #include "itkIntTypes.h"
00022 #include "itkSingleValuedNonLinearOptimizer.h"
00023 
00024 namespace itk
00025 {
00032 class ITK_EXPORT RegularStepGradientDescentBaseOptimizer:
00033   public SingleValuedNonLinearOptimizer
00034 {
00035 public:
00037   typedef RegularStepGradientDescentBaseOptimizer Self;
00038   typedef SingleValuedNonLinearOptimizer          Superclass;
00039   typedef SmartPointer< Self >                    Pointer;
00040   typedef SmartPointer< const Self >              ConstPointer;
00041 
00043   itkNewMacro(Self);
00044 
00046   itkTypeMacro(RegularStepGradientDescentBaseOptimizer,
00047                SingleValuedNonLinearOptimizer);
00048 
00050   typedef enum {
00051     GradientMagnitudeTolerance = 1,
00052     StepTooSmall = 2,
00053     ImageNotAvailable = 3,
00054     CostFunctionError = 4,
00055     MaximumNumberOfIterations = 5,
00056     Unknown = 6
00057     } StopConditionType;
00058 
00060   itkSetMacro(Maximize, bool);
00061   itkGetConstReferenceMacro(Maximize, bool);
00062   itkBooleanMacro(Maximize);
00063   bool GetMinimize() const
00064   { return !m_Maximize; }
00065   void SetMinimize(bool v)
00066   { this->SetMaximize(!v); }
00067   void    MinimizeOn(void)
00068   { SetMaximize(false); }
00069   void    MinimizeOff(void)
00070   { SetMaximize(true); }
00072 
00074   void    StartOptimization(void);
00075 
00078   void    ResumeOptimization(void);
00079 
00082   void    StopOptimization(void);
00083 
00085   itkSetMacro(MaximumStepLength, double);
00086   itkSetMacro(MinimumStepLength, double);
00087   itkSetMacro(RelaxationFactor, double);
00088   itkSetMacro(NumberOfIterations, SizeValueType);
00089   itkSetMacro(GradientMagnitudeTolerance, double);
00090   itkGetConstReferenceMacro(CurrentStepLength, double);
00091   itkGetConstReferenceMacro(MaximumStepLength, double);
00092   itkGetConstReferenceMacro(MinimumStepLength, double);
00093   itkGetConstReferenceMacro(RelaxationFactor, double);
00094   itkGetConstReferenceMacro(NumberOfIterations, SizeValueType);
00095   itkGetConstReferenceMacro(GradientMagnitudeTolerance, double);
00096   itkGetConstMacro(CurrentIteration, unsigned int);
00097   itkGetConstReferenceMacro(StopCondition, StopConditionType);
00098   itkGetConstReferenceMacro(Value, MeasureType);
00099   itkGetConstReferenceMacro(Gradient, DerivativeType);
00101 
00103   virtual const std::string GetStopConditionDescription() const;
00104 
00105 protected:
00106   RegularStepGradientDescentBaseOptimizer();
00107   virtual ~RegularStepGradientDescentBaseOptimizer() {}
00108   void PrintSelf(std::ostream & os, Indent indent) const;
00109 
00113   virtual void AdvanceOneStep(void);
00114 
00120   virtual void StepAlongGradient(
00121     double,
00122     const DerivativeType &)
00123   {
00124     ExceptionObject ex;
00125 
00126     ex.SetLocation(__FILE__);
00127     ex.SetDescription("This method MUST be overloaded in derived classes");
00128     throw ex;
00129   }
00130 
00131 private:
00132   RegularStepGradientDescentBaseOptimizer(const Self &); //purposely not
00133                                                          // implemented
00134   void operator=(const Self &);                          //purposely not
00135 
00136   // implemented
00137 protected:
00138   DerivativeType m_Gradient;
00139   DerivativeType m_PreviousGradient;
00140 
00141   bool               m_Stop;
00142   bool               m_Maximize;
00143   MeasureType        m_Value;
00144   double             m_GradientMagnitudeTolerance;
00145   double             m_MaximumStepLength;
00146   double             m_MinimumStepLength;
00147   double             m_CurrentStepLength;
00148   double             m_RelaxationFactor;
00149   StopConditionType  m_StopCondition;
00150   SizeValueType      m_NumberOfIterations;
00151   SizeValueType      m_CurrentIteration;
00152   std::ostringstream m_StopConditionDescription;
00153 };
00154 } // end namespace itk
00155 
00156 #endif
00157