ITK  5.4.0
Insight Toolkit
itkLBFGS2Optimizerv4.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 itkLBFGS2Optimizerv4_h
19#define itkLBFGS2Optimizerv4_h
20
22#include "ITKOptimizersv4Export.h"
23#include <memory>
24
25#include "lbfgs.h"
26
27namespace itk
28{
29/*** \class LBFGS2Optimizerv4Enums
30 * \brief Scoped Enum classes for LBFGS2Optimizerv4Template class
31 * \ingroup ITKOptimizersv4
32 */
34{
35public:
36 /*** \class LineSearchMethod
37 * \ingroup ITKOptimizersv4
38 * Line search method enum
39 */
40 enum class LineSearchMethod : uint8_t
41 {
43 LINESEARCH_DEFAULT = 0,
44
46 LINESEARCH_MORETHUENTE = 0,
47
57 LINESEARCH_BACKTRACKING_ARMIJO = 1,
58
60 LINESEARCH_BACKTRACKING = 2,
61
72 LINESEARCH_BACKTRACKING_WOLFE = 2,
73
84 LINESEARCH_BACKTRACKING_STRONG_WOLFE = 3,
85 };
86};
87// Define how to print enumeration
88extern ITKOptimizersv4_EXPORT std::ostream &
164template <typename TInternalComputationValueType>
165class ITK_TEMPLATE_EXPORT LBFGS2Optimizerv4Template
166 : public GradientDescentOptimizerv4Template<TInternalComputationValueType>
167{
170public:
171 ITK_DISALLOW_COPY_AND_MOVE(LBFGS2Optimizerv4Template);
172
174#if !defined(ITK_LEGACY_REMOVE)
176 static constexpr LineSearchMethodEnum LINESEARCH_DEFAULT = LineSearchMethodEnum::LINESEARCH_DEFAULT;
177 static constexpr LineSearchMethodEnum LINESEARCH_MORETHUENTE = LineSearchMethodEnum::LINESEARCH_MORETHUENTE;
178 static constexpr LineSearchMethodEnum LINESEARCH_BACKTRACKING_ARMIJO =
179 LineSearchMethodEnum::LINESEARCH_BACKTRACKING_ARMIJO;
180 static constexpr LineSearchMethodEnum LINESEARCH_BACKTRACKING = LineSearchMethodEnum::LINESEARCH_BACKTRACKING;
181 static constexpr LineSearchMethodEnum LINESEARCH_BACKTRACKING_WOLFE =
182 LineSearchMethodEnum::LINESEARCH_BACKTRACKING_WOLFE;
183 static constexpr LineSearchMethodEnum LINESEARCH_BACKTRACKING_STRONG_WOLFE =
184 LineSearchMethodEnum::LINESEARCH_BACKTRACKING_STRONG_WOLFE;
185#endif
186
192 using PrecisionType = double;
193 static_assert(std::is_same<TInternalComputationValueType, double>::value,
194 "LBFGS2Optimizerv4Template only supports double precision");
195
201
202 using MetricType = typename Superclass::MetricType;
203 using ParametersType = typename Superclass::ParametersType;
204 using ScalesType = typename Superclass::ScalesType;
205
207 using typename Superclass::StopConditionReturnStringType;
208
210 itkNewMacro(Self);
211
213 itkOverrideGetNameOfClassMacro(LBFGS2Optimizerv4Template);
214
216 void
217 StartOptimization(bool doOnlyInitialization = false) override;
218
222 void
224
227
236 void
238 int
250 void
263 void
265 int
279 void
293 void
294 SetMaximumIterations(int maxIterations);
295 int
302 GetNumberOfIterations() const override
303 {
304 return GetMaximumIterations();
305 }
306 void
308 {
309 SetMaximumIterations(static_cast<int>(_arg));
310 }
319 void
330 void
332 int
343 void
356 void
367 void
382 void
399 void
412 void
431 void
451 void
453 int
464 void
466 int
471 itkGetConstMacro(CurrentParameterNorm, PrecisionType);
472
474 itkGetConstMacro(CurrentGradientNorm, PrecisionType);
475
477 itkGetConstMacro(CurrentStepSize, PrecisionType);
478
480 itkGetConstMacro(CurrentNumberOfEvaluations, PrecisionType);
481
486 itkSetMacro(EstimateScalesAtEachIteration, bool);
487 itkGetConstReferenceMacro(EstimateScalesAtEachIteration, bool);
488 itkBooleanMacro(EstimateScalesAtEachIteration);
491protected:
494 void
495 PrintSelf(std::ostream & os, Indent indent) const override;
496
497
499 static int
500 UpdateProgressCallback(void * instance,
501 const PrecisionType * x,
502 const PrecisionType * g,
503 const PrecisionType fx,
504 const PrecisionType xnorm,
505 const PrecisionType gnorm,
506 const PrecisionType step,
507 int n,
508 int k,
509 int ls);
510
512 int
514 const PrecisionType * g,
515 const PrecisionType fx,
516 const PrecisionType xnorm,
517 const PrecisionType gnorm,
518 const PrecisionType step,
519 int n,
520 int k,
521 int ls);
522
524 static PrecisionType
525 EvaluateCostCallback(void * instance,
526 const PrecisionType * x,
527 PrecisionType * g,
528 const int n,
529 const PrecisionType step);
530
532 EvaluateCost(const PrecisionType * x, PrecisionType * g, const int n, const PrecisionType step);
533
534private:
535 // Private Implementation (Pimpl), to hide liblbfgs data structures
536 class PrivateImplementationHolder;
537 lbfgs_parameter_t m_Parameters{};
538
539 bool m_EstimateScalesAtEachIteration{};
540 double m_CurrentStepSize{};
541 double m_CurrentParameterNorm{};
542 double m_CurrentGradientNorm{};
543 int m_CurrentNumberOfEvaluations{};
544 int m_StatusCode{};
545
550 {
551 itkWarningMacro("Not supported. Please use LBFGS specific convergence methods.");
552 };
554 {
555 itkWarningMacro("Not supported. Please use LBFGS specific convergence methods.");
556 };
557 const PrecisionType &
558 GetConvergenceValue() const override
559 {
560 itkWarningMacro("Not supported. Please use LBFGS specific convergence methods.");
561 static PrecisionType value{};
562 return value;
563 };
566 void
567 AdvanceOneStep() override
568 {
569 itkWarningMacro("LBFGS2Optimizerv4Template does not implement single step advance");
570 };
571};
572
573
576
577} // end namespace itk
578
579#ifndef ITK_MANUAL_INSTANTIATION
580# include "itkLBFGS2Optimizerv4.hxx"
581#endif
582
583#endif
Control indentation during Print() invocation.
Definition: itkIndent.h:50
Wrap of the libLBFGS[1] algorithm for use in ITKv4 registration framework. LibLBFGS is a translation ...
int GetHessianApproximationAccuracy() const
void SetMachinePrecisionTolerance(PrecisionType xtol)
void SetMaximumLineSearchStep(PrecisionType step)
PrecisionType GetWolfeCoefficient() const
void SetDeltaConvergenceTolerance(PrecisionType tol)
PrecisionType GetDeltaConvergenceTolerance() const
void StartOptimization(bool doOnlyInitialization=false) override
PrecisionType GetMaximumLineSearchStep() const
PrecisionType GetMachinePrecisionTolerance() const
void SetOrthantwiseStart(int start)
LineSearchMethodEnum GetLineSearch() const
PrecisionType GetMinimumLineSearchStep() const
SizeValueType GetNumberOfIterations() const override
void SetHessianApproximationAccuracy(int m)
void SetLineSearchAccuracy(PrecisionType ftol)
void SetDeltaConvergenceDistance(int nPast)
void ResumeOptimization() override
typename Superclass::ParametersType ParametersType
PrecisionType GetLineSearchGradientAccuracy() const
void SetMinimumConvergenceValue(PrecisionType) override
void SetConvergenceWindowSize(SizeValueType) override
typename Superclass::MetricType MetricType
int GetMaximumLineSearchEvaluations() const
PrecisionType GetLineSearchAccuracy() const
void SetLineSearchGradientAccuracy(PrecisionType gtol)
void SetNumberOfIterations(const SizeValueType _arg) override
typename Superclass::ScalesType ScalesType
const PrecisionType & GetConvergenceValue() const override
PrecisionType GetSolutionAccuracy() const
void PrintSelf(std::ostream &os, Indent indent) const override
PrecisionType GetOrthantwiseCoefficient() const
static int UpdateProgressCallback(void *instance, const PrecisionType *x, const PrecisionType *g, const PrecisionType fx, const PrecisionType xnorm, const PrecisionType gnorm, const PrecisionType step, int n, int k, int ls)
void SetLineSearch(const LineSearchMethodEnum &linesearch)
void SetMaximumIterations(int maxIterations)
void SetSolutionAccuracy(PrecisionType epsilon)
void SetOrthantwiseCoefficient(PrecisionType orthant_c)
void SetMinimumLineSearchStep(PrecisionType step)
void SetWolfeCoefficient(PrecisionType wc)
PrecisionType EvaluateCost(const PrecisionType *x, PrecisionType *g, const int n, const PrecisionType step)
int UpdateProgress(const PrecisionType *x, const PrecisionType *g, const PrecisionType fx, const PrecisionType xnorm, const PrecisionType gnorm, const PrecisionType step, int n, int k, int ls)
void SetMaximumLineSearchEvaluations(int n)
virtual const StopConditionReturnStringType GetStopConditionDescription() const override
static PrecisionType EvaluateCostCallback(void *instance, const PrecisionType *x, PrecisionType *g, const int n, const PrecisionType step)
Light weight base class for most itk classes.
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:216
unsigned long SizeValueType
Definition: itkIntTypes.h:83