ITK  6.0.0
Insight Toolkit
itkRegularStepGradientDescentBaseOptimizer.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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  * https://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 {
32 {
33 public:
38  enum class StopCondition : uint8_t
39  {
41  StepTooSmall = 2,
45  Unknown = 6
46  };
47 };
48 // Define how to print enumeration
49 extern ITKOptimizers_EXPORT std::ostream &
51 
59 {
60 public:
61  ITK_DISALLOW_COPY_AND_MOVE(RegularStepGradientDescentBaseOptimizer);
62 
68 
70  itkNewMacro(Self);
71 
73  itkOverrideGetNameOfClassMacro(RegularStepGradientDescentBaseOptimizer);
74 
76 #if !defined(ITK_LEGACY_REMOVE)
77  // We need to expose the enum values at the class level
78  // for backwards compatibility
79  static constexpr StopConditionEnum GradientMagnitudeTolerance = StopConditionEnum::GradientMagnitudeTolerance;
80  static constexpr StopConditionEnum StepTooSmall = StopConditionEnum::StepTooSmall;
81  static constexpr StopConditionEnum ImageNotAvailable = StopConditionEnum::ImageNotAvailable;
82  static constexpr StopConditionEnum CostFunctionError = StopConditionEnum::CostFunctionError;
83  static constexpr StopConditionEnum MaximumNumberOfIterations = StopConditionEnum::MaximumNumberOfIterations;
84  static constexpr StopConditionEnum Unknown = StopConditionEnum::Unknown;
85 #endif
86 
87  itkSetMacro(Maximize, bool);
88  itkGetConstReferenceMacro(Maximize, bool);
89  itkBooleanMacro(Maximize);
90  bool
91  GetMinimize() const
92  {
93  return !m_Maximize;
94  }
95  void
96  SetMinimize(bool v)
97  {
98  this->SetMaximize(!v);
99  }
100  void
102  {
103  SetMaximize(false);
104  }
105  void
107  {
108  SetMaximize(true);
109  }
113  void
114  StartOptimization() override;
115 
118  void
119  ResumeOptimization();
120 
123  void
124  StopOptimization();
125 
127  itkSetMacro(MaximumStepLength, double);
128  itkSetMacro(MinimumStepLength, double);
129  itkSetMacro(RelaxationFactor, double);
130  itkSetMacro(NumberOfIterations, SizeValueType);
131  itkSetMacro(GradientMagnitudeTolerance, double);
132  itkGetConstReferenceMacro(CurrentStepLength, double);
133  itkGetConstReferenceMacro(MaximumStepLength, double);
134  itkGetConstReferenceMacro(MinimumStepLength, double);
135  itkGetConstReferenceMacro(RelaxationFactor, double);
136  itkGetConstReferenceMacro(NumberOfIterations, SizeValueType);
137  itkGetConstReferenceMacro(GradientMagnitudeTolerance, double);
138  itkGetConstMacro(CurrentIteration, unsigned int);
139  itkGetConstReferenceMacro(StopCondition, StopConditionEnum);
140  itkGetConstReferenceMacro(Value, MeasureType);
141  itkGetConstReferenceMacro(Gradient, DerivativeType);
145  std::string
146  GetStopConditionDescription() const override;
147 
148 protected:
150  ~RegularStepGradientDescentBaseOptimizer() override = default;
151  void
152  PrintSelf(std::ostream & os, Indent indent) const override;
153 
157  virtual void
158  AdvanceOneStep();
159 
165  virtual void
167  {
168  ExceptionObject ex;
169 
170  ex.SetLocation(__FILE__);
171  ex.SetDescription("This method MUST be overloaded in derived classes");
172  throw ex;
173  }
174 
175 private:
176 protected:
177  DerivativeType m_Gradient{};
178  DerivativeType m_PreviousGradient{};
179 
180  bool m_Stop{ false };
181  bool m_Maximize{};
182  MeasureType m_Value{};
183  double m_GradientMagnitudeTolerance{};
184  double m_MaximumStepLength{};
185  double m_MinimumStepLength{};
186  double m_CurrentStepLength{};
187  double m_RelaxationFactor{};
188  StopConditionEnum m_StopCondition{};
189  SizeValueType m_NumberOfIterations{};
190  SizeValueType m_CurrentIteration{};
191  std::ostringstream m_StopConditionDescription{};
192 };
193 } // end namespace itk
194 
195 #endif
itk::RegularStepGradientDescentBaseOptimizer::SetMinimize
void SetMinimize(bool v)
Definition: itkRegularStepGradientDescentBaseOptimizer.h:96
itk::RegularStepGradientDescentBaseOptimizerEnums
Contains all enum classes for RegularStepGradientDescentBaseOptimizer class.
Definition: itkRegularStepGradientDescentBaseOptimizer.h:31
itk::SingleValuedNonLinearOptimizer
This class is a base for the Optimization methods that optimize a single valued function.
Definition: itkSingleValuedNonLinearOptimizer.h:35
itk::RegularStepGradientDescentBaseOptimizer
Implement a gradient descent optimizer.
Definition: itkRegularStepGradientDescentBaseOptimizer.h:58
itk::RegularStepGradientDescentBaseOptimizerEnums::StopCondition::MaximumNumberOfIterations
itk::operator<<
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::RegularStepGradientDescentBaseOptimizer::GetMinimize
bool GetMinimize() const
Definition: itkRegularStepGradientDescentBaseOptimizer.h:91
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:55
itk::RegularStepGradientDescentBaseOptimizerEnums::StopCondition::Unknown
itk::ExceptionObject::SetLocation
virtual void SetLocation(const std::string &s)
itk::RegularStepGradientDescentBaseOptimizerEnums::StopCondition::StepTooSmall
itk::ExceptionObject::SetDescription
virtual void SetDescription(const std::string &s)
itk::RegularStepGradientDescentBaseOptimizer::StepAlongGradient
virtual void StepAlongGradient(double, const DerivativeType &)
Definition: itkRegularStepGradientDescentBaseOptimizer.h:166
itk::RegularStepGradientDescentBaseOptimizerEnums::StopCondition::GradientMagnitudeTolerance
itk::RegularStepGradientDescentBaseOptimizerEnums::StopCondition::ImageNotAvailable
itkIntTypes.h
itk::ExceptionObject
Standard exception handling object.
Definition: itkExceptionObject.h:50
itk::RegularStepGradientDescentBaseOptimizer::MinimizeOn
void MinimizeOn()
Definition: itkRegularStepGradientDescentBaseOptimizer.h:101
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itk::SingleValuedNonLinearOptimizer::MeasureType
CostFunctionType::MeasureType MeasureType
Definition: itkSingleValuedNonLinearOptimizer.h:62
itk::Array
Array class with size defined at construction time.
Definition: itkArray.h:47
itk::RegularStepGradientDescentBaseOptimizerEnums::StopCondition
StopCondition
Definition: itkRegularStepGradientDescentBaseOptimizer.h:38
itk::RegularStepGradientDescentBaseOptimizerEnums::StopCondition::CostFunctionError
StopCondition
itkSingleValuedNonLinearOptimizer.h
itk::RegularStepGradientDescentBaseOptimizer::MinimizeOff
void MinimizeOff()
Definition: itkRegularStepGradientDescentBaseOptimizer.h:106
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:86