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
25namespace itk
26{
32{
33public:
37 enum class StopConditionSPSAOptimizer : uint8_t
38 {
39 Unknown,
40 MaximumNumberOfIterations,
41 BelowTolerance,
42 MetricError
43 };
44};
45// Define how to print enumeration
46extern ITKOptimizers_EXPORT std::ostream &
47 operator<<(std::ostream & out, const SPSAOptimizerEnums::StopConditionSPSAOptimizer value);
65class ITKOptimizers_EXPORT SPSAOptimizer : public SingleValuedNonLinearOptimizer
66{
67public:
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
93 virtual void
95
97 void
99
102 void
104
107 void
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
202 {
203 return !m_Maximize;
204 }
205 void
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
267
268protected:
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
297
302 virtual double
304
311 virtual void
312 GenerateDelta(const unsigned int spaceDimension);
313
318 virtual void
319 ComputeGradient(const ParametersType & parameters, DerivativeType & gradient);
320
321private:
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
340extern 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
Array class with size defined at construction time.
Definition: itkArray.h:48
Control indentation during Print() invocation.
Definition: itkIndent.h:50
Light weight base class for most itk classes.
Contains all enum classes used by SPSAOptimizer class.
An optimizer based on simultaneous perturbation...
double Geta() const
void StartOptimization() override
bool GetMinimize() const
SPSAOptimizerEnums::StopConditionSPSAOptimizer StopConditionSPSAOptimizerEnum
void PrintSelf(std::ostream &os, Indent indent) const override
virtual void GenerateDelta(const unsigned int spaceDimension)
virtual double Compute_a(SizeValueType k) const
double Getc() const
~SPSAOptimizer() override=default
void SetMinimize(bool v)
void Setc(double c)
virtual void AdvanceOneStep()
virtual void ComputeGradient(const ParametersType &parameters, DerivativeType &gradient)
virtual double Compute_c(SizeValueType k) const
std::string GetStopConditionDescription() const override
virtual void GuessParameters(SizeValueType numberOfGradientEstimates, double initialStepSize)
virtual MeasureType GetValue() const
virtual MeasureType GetValue(const ParametersType &parameters) const
void Seta(double a)
This class is a base for the Optimization methods that optimize a single valued function.
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)
unsigned long SizeValueType
Definition: itkIntTypes.h:86