ITK  5.2.0
Insight Toolkit
itkGradientDescentOptimizer.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 itkGradientDescentOptimizer_h
19 #define itkGradientDescentOptimizer_h
20 
21 #include "itkIntTypes.h"
23 #include "ITKOptimizersExport.h"
24 #include <string>
25 namespace itk
26 {
32 {
33 public:
38  {
41  };
42 };
43 // Define how to print enumeration
44 extern ITKOptimizers_EXPORT std::ostream &
46 
72 class ITKOptimizers_EXPORT GradientDescentOptimizer : public SingleValuedNonLinearOptimizer
73 {
74 public:
75  ITK_DISALLOW_COPY_AND_MOVE(GradientDescentOptimizer);
76 
82 
84  itkNewMacro(Self);
85 
88 
91 #if !defined(ITK_LEGACY_REMOVE)
92  // We need to expose the enum values at the class level
93  // for backwards compatibility
94  static constexpr StopConditionGradientDescentOptimizerEnum MaximumNumberOfIterations =
95  StopConditionGradientDescentOptimizerEnum::MaximumNumberOfIterations;
96  static constexpr StopConditionGradientDescentOptimizerEnum MetricError =
97  StopConditionGradientDescentOptimizerEnum::MetricError;
98 #endif
99 
101  itkGetConstReferenceMacro(Maximize, bool);
102  itkSetMacro(Maximize, bool);
103  itkBooleanMacro(Maximize);
104  bool
105  GetMinimize() const
106  {
107  return !m_Maximize;
108  }
109  void
110  SetMinimize(bool v)
111  {
112  this->SetMaximize(!v);
113  }
114  void
116  {
117  this->MaximizeOff();
118  }
119  void
121  {
122  this->MaximizeOn();
123  }
125 
127  virtual void
128  AdvanceOneStep();
129 
131  void
132  StartOptimization() override;
133 
136  void
137  ResumeOptimization();
138 
141  void
142  StopOptimization();
143 
145  itkSetMacro(LearningRate, double);
146 
148  itkGetConstReferenceMacro(LearningRate, double);
149 
151  itkSetMacro(NumberOfIterations, SizeValueType);
152 
154  itkGetConstReferenceMacro(NumberOfIterations, SizeValueType);
155 
157  itkGetConstMacro(CurrentIteration, SizeValueType);
158 
160  itkGetConstReferenceMacro(Value, double);
161 
163  itkGetConstReferenceMacro(StopCondition, StopConditionGradientDescentOptimizerEnum);
164  const std::string
165  GetStopConditionDescription() const override;
167 
169  itkGetConstReferenceMacro(Gradient, DerivativeType);
170 
171 protected:
173  ~GradientDescentOptimizer() override = default;
174  void
175  PrintSelf(std::ostream & os, Indent indent) const override;
176 
177  // made protected so subclass can access
179 
180  bool m_Maximize{ false };
181 
182  double m_LearningRate{ 1.0 };
183 
184 private:
185  bool m_Stop{ false };
186  double m_Value{ 0.0 };
188  StopConditionGradientDescentOptimizerEnum::MaximumNumberOfIterations
189  };
190  SizeValueType m_NumberOfIterations{ 100 };
191  SizeValueType m_CurrentIteration{ 0 };
192  std::ostringstream m_StopConditionDescription;
193 };
194 
195 // Define how to print enumeration
196 extern ITKOptimizers_EXPORT std::ostream &
198 
199 } // end namespace itk
200 
201 #endif
itk::uint8_t
::uint8_t uint8_t
Definition: itkIntTypes.h:29
itk::GradientDescentOptimizer::SetMinimize
void SetMinimize(bool v)
Definition: itkGradientDescentOptimizer.h:110
itk::SingleValuedNonLinearOptimizer
This class is a base for the Optimization methods that optimize a single valued function.
Definition: itkSingleValuedNonLinearOptimizer.h:35
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:218
StopConditionGradientDescentOptimizer
itk::GradientDescentOptimizerEnums
Contains all enum classes in the GradientDescentOptimizer class.
Definition: itkGradientDescentOptimizer.h:31
itk::SmartPointer< Self >
itk::GradientDescentOptimizer::MinimizeOff
void MinimizeOff()
Definition: itkGradientDescentOptimizer.h:120
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::GradientDescentOptimizerEnums::StopConditionGradientDescentOptimizer
StopConditionGradientDescentOptimizer
Definition: itkGradientDescentOptimizer.h:37
itk::GradientDescentOptimizer
Implement a gradient descent optimizer.
Definition: itkGradientDescentOptimizer.h:72
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:59
itk::GradientDescentOptimizer::m_StopConditionDescription
std::ostringstream m_StopConditionDescription
Definition: itkGradientDescentOptimizer.h:192
itkIntTypes.h
itk::GradientDescentOptimizerEnums::StopConditionGradientDescentOptimizer::MetricError
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::GradientDescentOptimizer::m_Gradient
DerivativeType m_Gradient
Definition: itkGradientDescentOptimizer.h:178
itk::Array
Array class with size defined at construction time.
Definition: itkArray.h:47
itk::GradientDescentOptimizerEnums::StopConditionGradientDescentOptimizer::MaximumNumberOfIterations
StopCondition
itkSingleValuedNonLinearOptimizer.h
itk::GradientDescentOptimizer::GetMinimize
bool GetMinimize() const
Definition: itkGradientDescentOptimizer.h:105
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
itk::GradientDescentOptimizer::MinimizeOn
void MinimizeOn()
Definition: itkGradientDescentOptimizer.h:115