ITK  5.1.0
Insight Toolkit
itkGradientDescentOptimizer.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 itkGradientDescentOptimizer_h
19 #define itkGradientDescentOptimizer_h
20 
21 #include "itkIntTypes.h"
23 #include "ITKOptimizersExport.h"
24 #include <string>
25 namespace itk
26 {
52 class ITKOptimizers_EXPORT GradientDescentOptimizer : public SingleValuedNonLinearOptimizer
53 {
54 public:
55  ITK_DISALLOW_COPY_AND_ASSIGN(GradientDescentOptimizer);
56 
62 
64  itkNewMacro(Self);
65 
67  itkTypeMacro(GradientDescentOptimizer, SingleValuedNonLinearOptimizer);
68 
74  {
76  MetricError
77  };
78 #if !defined(ITK_LEGACY_REMOVE) || defined(ITK_WRAPPING) /* castxml 'enum class' workaround */
79  // We need to expose the enum values at the class level
80  // for backwards compatibility
82  static constexpr StopConditionType MetricError = StopConditionType::MetricError;
83 #endif
84 
86  itkGetConstReferenceMacro(Maximize, bool);
87  itkSetMacro(Maximize, bool);
88  itkBooleanMacro(Maximize);
89  bool
90  GetMinimize() const
91  {
92  return !m_Maximize;
93  }
94  void
95  SetMinimize(bool v)
96  {
97  this->SetMaximize(!v);
98  }
99  void
101  {
102  this->MaximizeOff();
103  }
104  void
106  {
107  this->MaximizeOn();
108  }
110 
112  virtual void
113  AdvanceOneStep();
114 
116  void
117  StartOptimization() override;
118 
121  void
122  ResumeOptimization();
123 
126  void
127  StopOptimization();
128 
130  itkSetMacro(LearningRate, double);
131 
133  itkGetConstReferenceMacro(LearningRate, double);
134 
136  itkSetMacro(NumberOfIterations, SizeValueType);
137 
139  itkGetConstReferenceMacro(NumberOfIterations, SizeValueType);
140 
142  itkGetConstMacro(CurrentIteration, SizeValueType);
143 
145  itkGetConstReferenceMacro(Value, double);
146 
148  itkGetConstReferenceMacro(StopCondition, StopConditionType);
149  const std::string
150  GetStopConditionDescription() const override;
152 
154  itkGetConstReferenceMacro(Gradient, DerivativeType);
155 
156 protected:
158  ~GradientDescentOptimizer() override = default;
159  void
160  PrintSelf(std::ostream & os, Indent indent) const override;
161 
162  // made protected so subclass can access
163  DerivativeType m_Gradient;
164 
165  bool m_Maximize{ false };
166 
167  double m_LearningRate{ 1.0 };
168 
169 private:
170  bool m_Stop{ false };
171  double m_Value{ 0.0 };
173  SizeValueType m_NumberOfIterations{ 100 };
174  SizeValueType m_CurrentIteration{ 0 };
175  std::ostringstream m_StopConditionDescription;
176 };
177 
178 // Define how to print enumeration
179 extern ITKOptimizers_EXPORT std::ostream &
180  operator<<(std::ostream & out, const GradientDescentOptimizer::StopConditionType value);
181 
182 } // end namespace itk
183 
184 #endif
itk::uint8_t
::uint8_t uint8_t
Definition: itkIntTypes.h:29
itk::GradientDescentOptimizer::SetMinimize
void SetMinimize(bool v)
Definition: itkGradientDescentOptimizer.h:95
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:211
itk::SmartPointer< Self >
itk::GradientDescentOptimizer::MinimizeOff
void MinimizeOff()
Definition: itkGradientDescentOptimizer.h:105
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::GradientDescentOptimizer
Implement a gradient descent optimizer.
Definition: itkGradientDescentOptimizer.h:52
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:175
itk::GradientDescentOptimizer::StopConditionType
StopConditionType
Definition: itkGradientDescentOptimizer.h:73
itkIntTypes.h
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkArray.h:26
itk::GradientDescentOptimizer::m_Gradient
DerivativeType m_Gradient
Definition: itkGradientDescentOptimizer.h:163
StopConditionType
Definition: itkSPSAOptimizer.h:59
itk::MRFStopType::MaximumNumberOfIterations
itkSingleValuedNonLinearOptimizer.h
itk::GradientDescentOptimizer::GetMinimize
bool GetMinimize() const
Definition: itkGradientDescentOptimizer.h:90
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
itk::GradientDescentOptimizer::MinimizeOn
void MinimizeOn()
Definition: itkGradientDescentOptimizer.h:100