ITK  6.0.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 
27 namespace itk
28 {
29 /*** \class LBFGS2Optimizerv4Enums
30  * \brief Scoped Enum classes for LBFGS2Optimizerv4Template class
31  * \ingroup ITKOptimizersv4
32  */
34 {
35 public:
36  /*** \class LineSearchMethod
37  * \ingroup ITKOptimizersv4
38  * Line search method enum
39  */
40  enum class LineSearchMethod : uint8_t
41  {
44 
47 
58 
61 
73 
85  };
86 };
87 // Define how to print enumeration
88 extern ITKOptimizersv4_EXPORT std::ostream &
89  operator<<(std::ostream & out, LBFGS2Optimizerv4Enums::LineSearchMethod value);
164 template <typename TInternalComputationValueType>
165 class ITK_TEMPLATE_EXPORT LBFGS2Optimizerv4Template
166  : public GradientDescentOptimizerv4Template<TInternalComputationValueType>
167 {
170 public:
171  ITK_DISALLOW_COPY_AND_MOVE(LBFGS2Optimizerv4Template);
172 
174 #if !defined(ITK_LEGACY_REMOVE)
175 
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
223  ResumeOptimization() override;
224 
226  GetStopConditionDescription() const override;
227 
236  void
237  SetHessianApproximationAccuracy(int m);
238  int
239  GetHessianApproximationAccuracy() const;
250  void
251  SetSolutionAccuracy(PrecisionType epsilon);
253  GetSolutionAccuracy() const;
263  void
264  SetDeltaConvergenceDistance(int nPast);
265  int
266  GetDeltaConvergenceDistance() const;
279  void
280  SetDeltaConvergenceTolerance(PrecisionType tol);
282  GetDeltaConvergenceTolerance() const;
293  void
294  SetMaximumIterations(int maxIterations);
295  int
296  GetMaximumIterations() const;
302  GetNumberOfIterations() const override
303  {
304  return GetMaximumIterations();
305  }
306  void
308  {
309  SetMaximumIterations(static_cast<int>(_arg));
310  }
319  void
320  SetLineSearch(const LineSearchMethodEnum & linesearch);
321  LineSearchMethodEnum
322  GetLineSearch() const;
330  void
331  SetMaximumLineSearchEvaluations(int n);
332  int
333  GetMaximumLineSearchEvaluations() const;
343  void
344  SetMinimumLineSearchStep(PrecisionType step);
345  PrecisionType
346  GetMinimumLineSearchStep() const;
356  void
357  SetMaximumLineSearchStep(PrecisionType step);
358  PrecisionType
359  GetMaximumLineSearchStep() const;
367  void
368  SetLineSearchAccuracy(PrecisionType ftol);
369  PrecisionType
370  GetLineSearchAccuracy() const;
382  void
383  SetWolfeCoefficient(PrecisionType wc);
384  PrecisionType
385  GetWolfeCoefficient() const;
399  void
400  SetLineSearchGradientAccuracy(PrecisionType gtol);
401  PrecisionType
402  GetLineSearchGradientAccuracy() const;
412  void
413  SetMachinePrecisionTolerance(PrecisionType xtol);
414  PrecisionType
415  GetMachinePrecisionTolerance() const;
431  void
432  SetOrthantwiseCoefficient(PrecisionType orthant_c);
433  PrecisionType
434  GetOrthantwiseCoefficient() const;
451  void
452  SetOrthantwiseStart(int start);
453  int
454  GetOrthantwiseStart() const;
464  void
465  SetOrthantwiseEnd(int end);
466  int
467  GetOrthantwiseEnd() const;
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);
491 protected:
493  ~LBFGS2Optimizerv4Template() override;
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
513  UpdateProgress(const PrecisionType * x,
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 
531  PrecisionType
532  EvaluateCost(const PrecisionType * x, PrecisionType * g, const int n, const PrecisionType step);
533 
534 private:
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 
549  void
551  {
552  itkWarningMacro("Not supported. Please use LBFGS specific convergence methods.");
553  }
554  void
556  {
557  itkWarningMacro("Not supported. Please use LBFGS specific convergence methods.");
558  }
559  const PrecisionType &
560  GetConvergenceValue() const override
561  {
562  itkWarningMacro("Not supported. Please use LBFGS specific convergence methods.");
563  static const PrecisionType value{};
564  return value;
565  }
568  void
569  AdvanceOneStep() override
570  {
571  itkWarningMacro("LBFGS2Optimizerv4Template does not implement single step advance");
572  }
573 };
574 
575 
578 
579 } // end namespace itk
580 
581 #ifndef ITK_MANUAL_INSTANTIATION
582 # include "itkLBFGS2Optimizerv4.hxx"
583 #endif
584 
585 #endif
itk::LBFGS2Optimizerv4Template::SetConvergenceWindowSize
void SetConvergenceWindowSize(SizeValueType) override
Definition: itkLBFGS2Optimizerv4.h:555
itk::LBFGS2Optimizerv4Enums::LineSearchMethod::LINESEARCH_BACKTRACKING_STRONG_WOLFE
itk::LBFGS2Optimizerv4Enums::LineSearchMethod::LINESEARCH_BACKTRACKING_WOLFE
itk::GradientDescentOptimizerv4Template
Gradient descent optimizer.
Definition: itkGradientDescentOptimizerv4.h:78
itk::LBFGS2Optimizerv4Template::ScalesType
typename Superclass::ScalesType ScalesType
Definition: itkLBFGS2Optimizerv4.h:204
itk::LBFGS2Optimizerv4Enums::LineSearchMethod::LINESEARCH_MORETHUENTE
itk::LBFGS2Optimizerv4Enums
Definition: itkLBFGS2Optimizerv4.h:33
itk::LBFGS2Optimizerv4Template::PrecisionType
double PrecisionType
Definition: itkLBFGS2Optimizerv4.h:192
itk::ObjectToObjectOptimizerBaseTemplate::StopConditionReturnStringType
std::string StopConditionReturnStringType
Definition: itkObjectToObjectOptimizerBase.h:148
itk::LBFGS2Optimizerv4Enums::LineSearchMethod::LINESEARCH_DEFAULT
itk::operator<<
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)
itk::LBFGS2Optimizerv4Enums::LineSearchMethod::LINESEARCH_BACKTRACKING_ARMIJO
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::LBFGS2Optimizerv4Enums::LineSearchMethod::LINESEARCH_BACKTRACKING
itk::LBFGS2Optimizerv4Template::GetNumberOfIterations
SizeValueType GetNumberOfIterations() const override
Definition: itkLBFGS2Optimizerv4.h:302
itk::LBFGS2Optimizerv4Template::SetNumberOfIterations
void SetNumberOfIterations(const SizeValueType _arg) override
Definition: itkLBFGS2Optimizerv4.h:307
itk::LBFGS2Optimizerv4Template::SetMinimumConvergenceValue
void SetMinimumConvergenceValue(PrecisionType) override
Definition: itkLBFGS2Optimizerv4.h:550
itk::LBFGS2Optimizerv4Template
Wrap of the libLBFGS[1] algorithm for use in ITKv4 registration framework. LibLBFGS is a translation ...
Definition: itkLBFGS2Optimizerv4.h:165
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:55
itk::LBFGS2Optimizerv4Template::ParametersType
typename Superclass::ParametersType ParametersType
Definition: itkLBFGS2Optimizerv4.h:203
itk::LBFGS2Optimizerv4Template::AdvanceOneStep
void AdvanceOneStep() override
Definition: itkLBFGS2Optimizerv4.h:569
itk::LBFGS2Optimizerv4Template::GetConvergenceValue
const PrecisionType & GetConvergenceValue() const override
Definition: itkLBFGS2Optimizerv4.h:560
itkGradientDescentOptimizerv4.h
itk::LBFGS2Optimizerv4Template::MetricType
typename Superclass::MetricType MetricType
Definition: itkLBFGS2Optimizerv4.h:202
itk::LBFGS2Optimizerv4Enums::LineSearchMethod
LineSearchMethod
Definition: itkLBFGS2Optimizerv4.h:40
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:86