ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkSPSAOptimizer.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 __itkSPSAOptimizer_h
00019 #define __itkSPSAOptimizer_h
00020 
00021 #include "itkSingleValuedNonLinearOptimizer.h"
00022 #include "itkMersenneTwisterRandomVariateGenerator.h"
00023 
00024 namespace itk
00025 {
00043 class ITK_EXPORT SPSAOptimizer:
00044   public SingleValuedNonLinearOptimizer
00045 {
00046 public:
00047 
00049   typedef SPSAOptimizer                  Self;
00050   typedef SingleValuedNonLinearOptimizer Superclass;
00051   typedef SmartPointer< Self >           Pointer;
00052   typedef SmartPointer< const Self >     ConstPointer;
00053 
00055   itkNewMacro(Self);
00056 
00058   itkTypeMacro(SPSAOptimizer, SingleValuedNonLinearOptimizer);
00059 
00061   typedef enum {
00062     Unknown,
00063     MaximumNumberOfIterations,
00064     BelowTolerance,
00065     MetricError
00066     } StopConditionType;
00067 
00069   virtual void AdvanceOneStep(void);
00070 
00072   void StartOptimization(void);
00073 
00076   void ResumeOptimization(void);
00077 
00080   void StopOptimization(void);
00081 
00083   virtual MeasureType GetValue(void) const;
00084 
00086   virtual MeasureType GetValue(const ParametersType & parameters) const;
00087 
00101   virtual void GuessParameters(
00102     SizeValueType numberOfGradientEstimates,
00103     double initialStepSize);
00104 
00106   itkGetConstMacro(CurrentIteration, SizeValueType);
00107 
00109   itkGetConstMacro(StopCondition, StopConditionType);
00110 
00112   itkGetConstMacro(LearningRate, double);
00113 
00115   itkGetConstMacro(GradientMagnitude, double);
00116 
00118   itkGetConstReferenceMacro(Gradient, DerivativeType);
00119 
00121   itkSetMacro(Sa, double);
00122   itkGetConstMacro(Sa, double);
00123   // For backward compatibility
00124   void Seta(double a) { SetSa(a); }
00125   double Geta() { return GetSa(); }
00127 
00129   itkSetMacro(Sc, double);
00130   itkGetConstMacro(Sc, double);
00131   // For backward compatibility
00132   void Setc(double c) { SetSc(c); }
00133   double Getc() { return GetSc(); }
00135 
00137   itkSetMacro(A, double);
00138   itkGetConstMacro(A, double);
00140 
00142   itkSetMacro(Alpha, double);
00143   itkGetConstMacro(Alpha, double);
00145 
00147   itkSetMacro(Gamma, double);
00148   itkGetConstMacro(Gamma, double);
00150 
00152   itkGetConstMacro(Maximize, bool);
00153   itkSetMacro(Maximize, bool);
00154   itkBooleanMacro(Maximize);
00155   bool GetMinimize() const
00156   { return !m_Maximize; }
00157   void SetMinimize(bool v)
00158   { this->SetMaximize(!v); }
00159   void MinimizeOn()
00160   { this->MaximizeOff(); }
00161   void MinimizeOff()
00162   { this->MaximizeOn(); }
00164 
00170   itkSetMacro(NumberOfPerturbations, SizeValueType);
00171   itkGetConstMacro(NumberOfPerturbations, SizeValueType);
00173 
00185   itkGetConstMacro(StateOfConvergence, double);
00186 
00188   itkSetMacro(StateOfConvergenceDecayRate, double);
00189   itkGetConstMacro(StateOfConvergenceDecayRate, double);
00191 
00193   itkSetMacro(MinimumNumberOfIterations, SizeValueType);
00194   itkGetConstMacro(MinimumNumberOfIterations, SizeValueType);
00196 
00198   itkSetMacro(MaximumNumberOfIterations, SizeValueType);
00199   itkGetConstMacro(MaximumNumberOfIterations, SizeValueType);
00201 
00203   itkSetMacro(Tolerance, double);
00204   itkGetConstMacro(Tolerance, double);
00206 
00208   const std::string GetStopConditionDescription() const;
00209 
00210 protected:
00211 
00212   SPSAOptimizer();
00213   virtual ~SPSAOptimizer() {}
00214 
00216   void PrintSelf(std::ostream & os, Indent indent) const;
00217 
00219   DerivativeType m_Gradient;
00220 
00221   double m_LearningRate;
00222 
00223   DerivativeType m_Delta;
00224 
00225   bool m_Stop;
00226 
00227   StopConditionType m_StopCondition;
00228 
00229   double m_StateOfConvergence;
00230 
00231   SizeValueType m_CurrentIteration;
00232 
00234   Statistics::MersenneTwisterRandomVariateGenerator::Pointer m_Generator;
00235 
00237   virtual double Compute_a(SizeValueType k) const;
00238 
00243   virtual double Compute_c(SizeValueType k) const;
00244 
00246   virtual void GenerateDelta(const unsigned int spaceDimension);
00247 
00252   virtual void ComputeGradient(
00253     const ParametersType & parameters,
00254     DerivativeType & gradient);
00255 
00256 private:
00257 
00258   SPSAOptimizer(const Self &);    // purposely not implemented
00259   void operator=(const Self &);   // purposely not implemented
00260 
00262   SizeValueType m_MinimumNumberOfIterations;
00263   SizeValueType m_MaximumNumberOfIterations;
00264   double        m_StateOfConvergenceDecayRate;
00265   double        m_Tolerance;
00266   bool          m_Maximize;
00267   double        m_GradientMagnitude;
00268   SizeValueType m_NumberOfPerturbations;
00269 
00271   double m_Sa;
00272   double m_Sc;
00273   double m_A;
00274   double m_Alpha;
00275   double m_Gamma;
00276 }; // end class SPSAOptimizer
00277 } // end namespace itk
00278 
00279 #endif // end #ifndef __itkSPSAOptimizer_h
00280