ITK  5.2.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  * 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 itkSPSAOptimizer_h
19 #define itkSPSAOptimizer_h
20 
23 #include "ITKOptimizersExport.h"
24 
25 namespace itk
26 {
32 {
33 public:
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 
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  }
164 
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  }
180 
182  itkSetMacro(A, double);
183  itkGetConstMacro(A, double);
185 
187  itkSetMacro(Alpha, double);
188  itkGetConstMacro(Alpha, double);
190 
192  itkSetMacro(Gamma, double);
193  itkGetConstMacro(Gamma, double);
195 
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  }
221 
227  itkSetMacro(NumberOfPerturbations, SizeValueType);
228  itkGetConstMacro(NumberOfPerturbations, SizeValueType);
230 
242  itkGetConstMacro(StateOfConvergence, double);
243 
245  itkSetMacro(StateOfConvergenceDecayRate, double);
246  itkGetConstMacro(StateOfConvergenceDecayRate, double);
248 
250  itkSetMacro(MinimumNumberOfIterations, SizeValueType);
251  itkGetConstMacro(MinimumNumberOfIterations, SizeValueType);
253 
255  itkSetMacro(MaximumNumberOfIterations, SizeValueType);
256  itkGetConstMacro(MaximumNumberOfIterations, SizeValueType);
258 
260  itkSetMacro(Tolerance, double);
261  itkGetConstMacro(Tolerance, double);
263 
265  const 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 
278 
280 
282 
283  bool m_Stop{ false };
284 
286 
288 
290 
293 
295  virtual double
296  Compute_a(SizeValueType k) const;
297 
302  virtual double
303  Compute_c(SizeValueType k) const;
304 
306  virtual void
307  GenerateDelta(const unsigned int spaceDimension);
308 
313  virtual void
314  ComputeGradient(const ParametersType & parameters, DerivativeType & gradient);
315 
316 private:
321  double m_Tolerance;
325 
327  double m_Sa;
328  double m_Sc;
329  double m_A;
330  double m_Alpha;
331  double m_Gamma;
332 }; // end class SPSAOptimizer
333 
334 // Define how to print enumeration
335 extern ITKOptimizers_EXPORT std::ostream &
336  operator<<(std::ostream & out, const SPSAOptimizer::StopConditionSPSAOptimizerEnum value);
337 
338 } // end namespace itk
339 
340 #endif // end #ifndef itkSPSAOptimizer_h
itk::uint8_t
::uint8_t uint8_t
Definition: itkIntTypes.h:29
itk::OptimizerParameters< double >
itk::SPSAOptimizer::m_LearningRate
double m_LearningRate
Definition: itkSPSAOptimizer.h:279
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::m_A
double m_A
Definition: itkSPSAOptimizer.h:329
itk::SPSAOptimizer::GetMinimize
bool GetMinimize() const
Definition: itkSPSAOptimizer.h:201
itk::SPSAOptimizer::m_Alpha
double m_Alpha
Definition: itkSPSAOptimizer.h:330
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:218
itk::SPSAOptimizer::m_Gradient
DerivativeType m_Gradient
Definition: itkSPSAOptimizer.h:277
itk::SPSAOptimizer::m_StopCondition
StopConditionSPSAOptimizerEnum m_StopCondition
Definition: itkSPSAOptimizer.h:285
itk::SPSAOptimizer::Getc
double Getc() const
Definition: itkSPSAOptimizer.h:175
itk::SPSAOptimizer::m_Tolerance
double m_Tolerance
Definition: itkSPSAOptimizer.h:321
itk::SPSAOptimizer::Geta
double Geta() const
Definition: itkSPSAOptimizer.h:159
itk::SPSAOptimizer::m_MaximumNumberOfIterations
SizeValueType m_MaximumNumberOfIterations
Definition: itkSPSAOptimizer.h:319
itk::SPSAOptimizerEnums::StopConditionSPSAOptimizer::MetricError
itk::SPSAOptimizerEnums::StopConditionSPSAOptimizer::Unknown
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::SPSAOptimizer::m_Sa
double m_Sa
Definition: itkSPSAOptimizer.h:327
itk::SPSAOptimizer::m_Generator
Statistics::MersenneTwisterRandomVariateGenerator::Pointer m_Generator
Definition: itkSPSAOptimizer.h:292
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:59
itk::SPSAOptimizer::m_MinimumNumberOfIterations
SizeValueType m_MinimumNumberOfIterations
Definition: itkSPSAOptimizer.h:318
itk::SPSAOptimizerEnums::StopConditionSPSAOptimizer
StopConditionSPSAOptimizer
Definition: itkSPSAOptimizer.h:37
itk::SPSAOptimizer::m_Maximize
bool m_Maximize
Definition: itkSPSAOptimizer.h:322
itk::SPSAOptimizerEnums::StopConditionSPSAOptimizer::MaximumNumberOfIterations
itk::SPSAOptimizer::m_NumberOfPerturbations
SizeValueType m_NumberOfPerturbations
Definition: itkSPSAOptimizer.h:324
itkMersenneTwisterRandomVariateGenerator.h
itk::SPSAOptimizer::MinimizeOn
void MinimizeOn()
Definition: itkSPSAOptimizer.h:211
itk::SPSAOptimizerEnums::StopConditionSPSAOptimizer::BelowTolerance
itk::SPSAOptimizer::m_CurrentIteration
SizeValueType m_CurrentIteration
Definition: itkSPSAOptimizer.h:289
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: itkAnnulusOperator.h:24
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
An optimizer based on simultaneous perturbation...
Definition: itkSPSAOptimizer.h:65
StopConditionSPSAOptimizer
itk::SPSAOptimizer::m_StateOfConvergence
double m_StateOfConvergence
Definition: itkSPSAOptimizer.h:287
itk::SPSAOptimizer::m_Delta
DerivativeType m_Delta
Definition: itkSPSAOptimizer.h:281
itk::SPSAOptimizer::m_GradientMagnitude
double m_GradientMagnitude
Definition: itkSPSAOptimizer.h:323
StopCondition
itkSingleValuedNonLinearOptimizer.h
itk::SPSAOptimizer::m_Sc
double m_Sc
Definition: itkSPSAOptimizer.h:328
itk::SPSAOptimizer::Setc
void Setc(double c)
Definition: itkSPSAOptimizer.h:170
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
itk::SPSAOptimizer::m_Gamma
double m_Gamma
Definition: itkSPSAOptimizer.h:331
itk::SPSAOptimizer::m_StateOfConvergenceDecayRate
double m_StateOfConvergenceDecayRate
Definition: itkSPSAOptimizer.h:320