ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkRegularStepGradientDescentBaseOptimizer.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkRegularStepGradientDescentBaseOptimizer_h
19 #define itkRegularStepGradientDescentBaseOptimizer_h
20 
21 #include "itkIntTypes.h"
23 #include "ITKOptimizersExport.h"
24 
25 namespace itk
26 {
33 class ITKOptimizers_EXPORT RegularStepGradientDescentBaseOptimizer:
35 {
36 public:
37  ITK_DISALLOW_COPY_AND_ASSIGN(RegularStepGradientDescentBaseOptimizer);
38 
44 
46  itkNewMacro(Self);
47 
51 
53  typedef enum {
54  GradientMagnitudeTolerance = 1,
55  StepTooSmall = 2,
56  ImageNotAvailable = 3,
57  CostFunctionError = 4,
58  MaximumNumberOfIterations = 5,
59  Unknown = 6
60  } StopConditionType;
61 
63  itkSetMacro(Maximize, bool);
64  itkGetConstReferenceMacro(Maximize, bool);
65  itkBooleanMacro(Maximize);
66  bool GetMinimize() const
67  { return !m_Maximize; }
68  void SetMinimize(bool v)
69  { this->SetMaximize(!v); }
70  void MinimizeOn()
71  { SetMaximize(false); }
72  void MinimizeOff()
73  { SetMaximize(true); }
75 
77  void StartOptimization() override;
78 
81  void ResumeOptimization();
82 
85  void StopOptimization();
86 
88  itkSetMacro(MaximumStepLength, double);
89  itkSetMacro(MinimumStepLength, double);
90  itkSetMacro(RelaxationFactor, double);
91  itkSetMacro(NumberOfIterations, SizeValueType);
92  itkSetMacro(GradientMagnitudeTolerance, double);
93  itkGetConstReferenceMacro(CurrentStepLength, double);
94  itkGetConstReferenceMacro(MaximumStepLength, double);
95  itkGetConstReferenceMacro(MinimumStepLength, double);
96  itkGetConstReferenceMacro(RelaxationFactor, double);
97  itkGetConstReferenceMacro(NumberOfIterations, SizeValueType);
98  itkGetConstReferenceMacro(GradientMagnitudeTolerance, double);
99  itkGetConstMacro(CurrentIteration, unsigned int);
100  itkGetConstReferenceMacro(StopCondition, StopConditionType);
101  itkGetConstReferenceMacro(Value, MeasureType);
102  itkGetConstReferenceMacro(Gradient, DerivativeType);
104 
106  const std::string GetStopConditionDescription() const override;
107 
108 protected:
110  ~RegularStepGradientDescentBaseOptimizer() override = default;
111  void PrintSelf(std::ostream & os, Indent indent) const override;
112 
116  virtual void AdvanceOneStep();
117 
123  virtual void StepAlongGradient(
124  double,
125  const DerivativeType &)
126  {
127  ExceptionObject ex;
128 
129  ex.SetLocation(__FILE__);
130  ex.SetDescription("This method MUST be overloaded in derived classes");
131  throw ex;
132  }
133 
134 private:
135 protected:
136  DerivativeType m_Gradient;
137  DerivativeType m_PreviousGradient;
138 
139  bool m_Stop{false};
141  MeasureType m_Value;
150  std::ostringstream m_StopConditionDescription;
151 };
152 } // end namespace itk
153 
154 #endif
Light weight base class for most itk classes.
virtual void SetDescription(const std::string &s)
virtual void SetLocation(const std::string &s)
unsigned long SizeValueType
Definition: itkIntTypes.h:83
This class is a base for the Optimization methods that optimize a single valued function.
Standard exception handling object.
virtual void StepAlongGradient(double, const DerivativeType &)
Control indentation during Print() invocation.
Definition: itkIndent.h:49