ITK  5.3.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  * 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 {
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 
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  }
111 
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);
143 
145  const 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:
179 
180  bool m_Stop{ false };
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::uint8_t
::uint8_t uint8_t
Definition: itkIntTypes.h:29
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::RegularStepGradientDescentBaseOptimizer::m_Value
MeasureType m_Value
Definition: itkRegularStepGradientDescentBaseOptimizer.h:182
itk::RegularStepGradientDescentBaseOptimizer::m_MinimumStepLength
double m_MinimumStepLength
Definition: itkRegularStepGradientDescentBaseOptimizer.h:185
itk::RegularStepGradientDescentBaseOptimizer::m_CurrentIteration
SizeValueType m_CurrentIteration
Definition: itkRegularStepGradientDescentBaseOptimizer.h:190
itk::RegularStepGradientDescentBaseOptimizerEnums::StopCondition::MaximumNumberOfIterations
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:218
itk::RegularStepGradientDescentBaseOptimizer::m_NumberOfIterations
SizeValueType m_NumberOfIterations
Definition: itkRegularStepGradientDescentBaseOptimizer.h:189
itk::RegularStepGradientDescentBaseOptimizer::m_CurrentStepLength
double m_CurrentStepLength
Definition: itkRegularStepGradientDescentBaseOptimizer.h:186
itk::RegularStepGradientDescentBaseOptimizer::m_StopConditionDescription
std::ostringstream m_StopConditionDescription
Definition: itkRegularStepGradientDescentBaseOptimizer.h:191
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::RegularStepGradientDescentBaseOptimizer::m_PreviousGradient
DerivativeType m_PreviousGradient
Definition: itkRegularStepGradientDescentBaseOptimizer.h:178
itk::RegularStepGradientDescentBaseOptimizer::m_RelaxationFactor
double m_RelaxationFactor
Definition: itkRegularStepGradientDescentBaseOptimizer.h:187
itk::RegularStepGradientDescentBaseOptimizer::GetMinimize
bool GetMinimize() const
Definition: itkRegularStepGradientDescentBaseOptimizer.h:91
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:59
itk::RegularStepGradientDescentBaseOptimizerEnums::StopCondition::Unknown
itk::RegularStepGradientDescentBaseOptimizerEnums::StopCondition::StepTooSmall
itk::RegularStepGradientDescentBaseOptimizer::StepAlongGradient
virtual void StepAlongGradient(double, const DerivativeType &)
Definition: itkRegularStepGradientDescentBaseOptimizer.h:166
itk::RegularStepGradientDescentBaseOptimizerEnums::StopCondition::GradientMagnitudeTolerance
itk::RegularStepGradientDescentBaseOptimizerEnums::StopCondition::ImageNotAvailable
itk::RegularStepGradientDescentBaseOptimizer::m_StopCondition
StopConditionEnum m_StopCondition
Definition: itkRegularStepGradientDescentBaseOptimizer.h:188
itkIntTypes.h
itk::RegularStepGradientDescentBaseOptimizer::MinimizeOn
void MinimizeOn()
Definition: itkRegularStepGradientDescentBaseOptimizer.h:101
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::RegularStepGradientDescentBaseOptimizer::m_MaximumStepLength
double m_MaximumStepLength
Definition: itkRegularStepGradientDescentBaseOptimizer.h:184
itk::RegularStepGradientDescentBaseOptimizer::m_GradientMagnitudeTolerance
double m_GradientMagnitudeTolerance
Definition: itkRegularStepGradientDescentBaseOptimizer.h:183
itk::RegularStepGradientDescentBaseOptimizer::m_Maximize
bool m_Maximize
Definition: itkRegularStepGradientDescentBaseOptimizer.h:181
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::m_Gradient
DerivativeType m_Gradient
Definition: itkRegularStepGradientDescentBaseOptimizer.h:177
itk::RegularStepGradientDescentBaseOptimizer::MinimizeOff
void MinimizeOff()
Definition: itkRegularStepGradientDescentBaseOptimizer.h:106
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83