ITK  6.0.0
Insight Toolkit
itkSPSAOptimizer.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 itkSPSAOptimizer_h
19 #define itkSPSAOptimizer_h
20 
23 #include "ITKOptimizersExport.h"
24 
25 namespace itk
26 {
32 {
33 public:
37  enum class StopConditionSPSAOptimizer : uint8_t
38  {
39  Unknown,
43  };
44 };
45 // Define how to print enumeration
46 extern ITKOptimizers_EXPORT std::ostream &
47  operator<<(std::ostream & out, const SPSAOptimizerEnums::StopConditionSPSAOptimizer value);
65 class ITKOptimizers_EXPORT SPSAOptimizer : public SingleValuedNonLinearOptimizer
66 {
67 public:
68  ITK_DISALLOW_COPY_AND_MOVE(SPSAOptimizer);
69 
75 
77  itkNewMacro(Self);
78 
80  itkOverrideGetNameOfClassMacro(SPSAOptimizer);
81 
83 #if !defined(ITK_LEGACY_REMOVE)
84  // We need to expose the enum values at the class level
85  // for backwards compatibility
86  static constexpr StopConditionSPSAOptimizerEnum Unknown = StopConditionSPSAOptimizerEnum::Unknown;
87  static constexpr StopConditionSPSAOptimizerEnum MaximumNumberOfIterations =
88  StopConditionSPSAOptimizerEnum::MaximumNumberOfIterations;
89  static constexpr StopConditionSPSAOptimizerEnum BelowTolerance = StopConditionSPSAOptimizerEnum::BelowTolerance;
90  static constexpr StopConditionSPSAOptimizerEnum MetricError = StopConditionSPSAOptimizerEnum::MetricError;
91 #endif
92 
93  virtual void
94  AdvanceOneStep();
95 
97  void
98  StartOptimization() override;
99 
102  void
103  ResumeOptimization();
104 
107  void
108  StopOptimization();
109 
111  virtual MeasureType
112  GetValue() const;
113 
115  virtual MeasureType
116  GetValue(const ParametersType & parameters) const;
117 
131  virtual void
132  GuessParameters(SizeValueType numberOfGradientEstimates, double initialStepSize);
133 
135  itkGetConstMacro(CurrentIteration, SizeValueType);
136 
139 
141  itkGetConstMacro(LearningRate, double);
142 
144  itkGetConstMacro(GradientMagnitude, double);
145 
147  itkGetConstReferenceMacro(Gradient, DerivativeType);
148 
150  itkSetMacro(Sa, double);
151  itkGetConstMacro(Sa, double);
152  // For backward compatibility
153  void
154  Seta(double a)
155  {
156  SetSa(a);
157  }
158  double
159  Geta() const
160  {
161  return GetSa();
162  }
166  itkSetMacro(Sc, double);
167  itkGetConstMacro(Sc, double);
168  // For backward compatibility
169  void
170  Setc(double c)
171  {
172  SetSc(c);
173  }
174  double
175  Getc() const
176  {
177  return GetSc();
178  }
182  itkSetMacro(A, double);
183  itkGetConstMacro(A, double);
187  itkSetMacro(Alpha, double);
188  itkGetConstMacro(Alpha, double);
192  itkSetMacro(Gamma, double);
193  itkGetConstMacro(Gamma, double);
197  itkGetConstMacro(Maximize, bool);
198  itkSetMacro(Maximize, bool);
199  itkBooleanMacro(Maximize);
200  bool
201  GetMinimize() const
202  {
203  return !m_Maximize;
204  }
205  void
206  SetMinimize(bool v)
207  {
208  this->SetMaximize(!v);
209  }
210  void
212  {
213  this->MaximizeOff();
214  }
215  void
217  {
218  this->MaximizeOn();
219  }
227  itkSetMacro(NumberOfPerturbations, SizeValueType);
228  itkGetConstMacro(NumberOfPerturbations, SizeValueType);
242  itkGetConstMacro(StateOfConvergence, double);
243 
245  itkSetMacro(StateOfConvergenceDecayRate, double);
246  itkGetConstMacro(StateOfConvergenceDecayRate, double);
250  itkSetMacro(MinimumNumberOfIterations, SizeValueType);
251  itkGetConstMacro(MinimumNumberOfIterations, SizeValueType);
255  itkSetMacro(MaximumNumberOfIterations, SizeValueType);
256  itkGetConstMacro(MaximumNumberOfIterations, SizeValueType);
260  itkSetMacro(Tolerance, double);
261  itkGetConstMacro(Tolerance, double);
265  std::string
266  GetStopConditionDescription() const override;
267 
268 protected:
269  SPSAOptimizer();
270  ~SPSAOptimizer() override = default;
271 
273  void
274  PrintSelf(std::ostream & os, Indent indent) const override;
275 
277  DerivativeType m_Gradient{};
278 
279  double m_LearningRate{};
280 
281  DerivativeType m_Delta{};
282 
283  bool m_Stop{ false };
284 
286 
287  double m_StateOfConvergence{};
288 
289  SizeValueType m_CurrentIteration{};
290 
293 
295  virtual double
296  Compute_a(SizeValueType k) const;
297 
302  virtual double
303  Compute_c(SizeValueType k) const;
304 
311  virtual void
312  GenerateDelta(const unsigned int spaceDimension);
313 
318  virtual void
319  ComputeGradient(const ParametersType & parameters, DerivativeType & gradient);
320 
321 private:
323  SizeValueType m_MinimumNumberOfIterations{};
324  SizeValueType m_MaximumNumberOfIterations{};
325  double m_StateOfConvergenceDecayRate{};
326  double m_Tolerance{};
327  bool m_Maximize{};
328  double m_GradientMagnitude{};
329  SizeValueType m_NumberOfPerturbations{};
330 
332  double m_Sa{};
333  double m_Sc{};
334  double m_A{};
335  double m_Alpha{};
336  double m_Gamma{};
337 }; // end class SPSAOptimizer
338 
339 // Define how to print enumeration
340 extern ITKOptimizers_EXPORT std::ostream &
341  operator<<(std::ostream & out, const SPSAOptimizer::StopConditionSPSAOptimizerEnum value);
342 
343 } // end namespace itk
344 
345 #endif // end #ifndef itkSPSAOptimizer_h
itk::OptimizerParameters< double >
itk::SingleValuedNonLinearOptimizer
This class is a base for the Optimization methods that optimize a single valued function.
Definition: itkSingleValuedNonLinearOptimizer.h:35
itk::SPSAOptimizer::MinimizeOff
void MinimizeOff()
Definition: itkSPSAOptimizer.h:216
itk::SPSAOptimizer::GetMinimize
bool GetMinimize() const
Definition: itkSPSAOptimizer.h:201
itk::SPSAOptimizer::Getc
double Getc() const
Definition: itkSPSAOptimizer.h:175
itk::SPSAOptimizer::Geta
double Geta() const
Definition: itkSPSAOptimizer.h:159
itk::SPSAOptimizerEnums::StopConditionSPSAOptimizer::MetricError
itk::SPSAOptimizerEnums::StopConditionSPSAOptimizer::Unknown
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::SPSAOptimizer::Seta
void Seta(double a)
Definition: itkSPSAOptimizer.h:154
itk::SPSAOptimizer::SetMinimize
void SetMinimize(bool v)
Definition: itkSPSAOptimizer.h:206
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:55
itk::SPSAOptimizerEnums::StopConditionSPSAOptimizer
StopConditionSPSAOptimizer
Definition: itkSPSAOptimizer.h:37
itk::SPSAOptimizerEnums::StopConditionSPSAOptimizer::MaximumNumberOfIterations
itkMersenneTwisterRandomVariateGenerator.h
itk::SPSAOptimizer::MinimizeOn
void MinimizeOn()
Definition: itkSPSAOptimizer.h:211
itk::SPSAOptimizerEnums::StopConditionSPSAOptimizer::BelowTolerance
itk::SPSAOptimizerEnums
Contains all enum classes used by SPSAOptimizer class.
Definition: itkSPSAOptimizer.h:31
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::SPSAOptimizer::StopConditionSPSAOptimizerEnum
SPSAOptimizerEnums::StopConditionSPSAOptimizer StopConditionSPSAOptimizerEnum
Definition: itkSPSAOptimizer.h:82
itk::SPSAOptimizer
An optimizer based on simultaneous perturbation...
Definition: itkSPSAOptimizer.h:65
StopConditionSPSAOptimizer
StopCondition
itkSingleValuedNonLinearOptimizer.h
itk::SPSAOptimizer::Setc
void Setc(double c)
Definition: itkSPSAOptimizer.h:170
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:86