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 
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 
199 
200  using MetricType = typename Superclass::MetricType;
201  using ParametersType = typename Superclass::ParametersType;
202  using ScalesType = typename Superclass::ScalesType;
203 
205  using typename Superclass::StopConditionReturnStringType;
206 
208  itkNewMacro(Self);
209 
212 
214  void
215  StartOptimization(bool doOnlyInitialization = false) override;
216 
220  void
221  ResumeOptimization() override;
222 
223  virtual const StopConditionReturnStringType
224  GetStopConditionDescription() const override;
225 
234  void
235  SetHessianApproximationAccuracy(int m);
236  int
237  GetHessianApproximationAccuracy() const;
248  void
249  SetSolutionAccuracy(PrecisionType epsilon);
251  GetSolutionAccuracy() const;
261  void
262  SetDeltaConvergenceDistance(int nPast);
263  int
264  GetDeltaConvergenceDistance() const;
277  void
278  SetDeltaConvergenceTolerance(PrecisionType tol);
280  GetDeltaConvergenceTolerance() const;
291  void
292  SetMaximumIterations(int maxIterations);
293  int
294  GetMaximumIterations() const;
300  GetNumberOfIterations() const override
301  {
302  return GetMaximumIterations();
303  }
304  void
306  {
307  SetMaximumIterations(static_cast<int>(_arg));
308  }
317  void
318  SetLineSearch(const LineSearchMethodEnum & linesearch);
319  LineSearchMethodEnum
320  GetLineSearch() const;
328  void
329  SetMaximumLineSearchEvaluations(int n);
330  int
331  GetMaximumLineSearchEvaluations() const;
341  void
342  SetMinimumLineSearchStep(PrecisionType step);
343  PrecisionType
344  GetMinimumLineSearchStep() const;
354  void
355  SetMaximumLineSearchStep(PrecisionType step);
356  PrecisionType
357  GetMaximumLineSearchStep() const;
365  void
366  SetLineSearchAccuracy(PrecisionType ftol);
367  PrecisionType
368  GetLineSearchAccuracy() const;
380  void
381  SetWolfeCoefficient(PrecisionType wc);
382  PrecisionType
383  GetWolfeCoefficient() const;
397  void
398  SetLineSearchGradientAccuracy(PrecisionType gtol);
399  PrecisionType
400  GetLineSearchGradientAccuracy() const;
410  void
411  SetMachinePrecisionTolerance(PrecisionType xtol);
412  PrecisionType
413  GetMachinePrecisionTolerance() const;
429  void
430  SetOrthantwiseCoefficient(PrecisionType orthant_c);
431  PrecisionType
432  GetOrthantwiseCoefficient() const;
449  void
450  SetOrthantwiseStart(int start);
451  int
452  GetOrthantwiseStart() const;
462  void
463  SetOrthantwiseEnd(int end);
464  int
465  GetOrthantwiseEnd() const;
469  itkGetConstMacro(CurrentParameterNorm, PrecisionType);
470 
472  itkGetConstMacro(CurrentGradientNorm, PrecisionType);
473 
475  itkGetConstMacro(CurrentStepSize, PrecisionType);
476 
478  itkGetConstMacro(CurrentNumberOfEvaluations, PrecisionType);
479 
484  itkSetMacro(EstimateScalesAtEachIteration, bool);
485  itkGetConstReferenceMacro(EstimateScalesAtEachIteration, bool);
486  itkBooleanMacro(EstimateScalesAtEachIteration);
489 protected:
491  ~LBFGS2Optimizerv4Template() override;
492  void
493  PrintSelf(std::ostream & os, Indent indent) const override;
494 
495 
497  static int
498  UpdateProgressCallback(void * instance,
499  const PrecisionType * x,
500  const PrecisionType * g,
501  const PrecisionType fx,
502  const PrecisionType xnorm,
503  const PrecisionType gnorm,
504  const PrecisionType step,
505  int n,
506  int k,
507  int ls);
508 
510  int
511  UpdateProgress(const PrecisionType * x,
512  const PrecisionType * g,
513  const PrecisionType fx,
514  const PrecisionType xnorm,
515  const PrecisionType gnorm,
516  const PrecisionType step,
517  int n,
518  int k,
519  int ls);
520 
522  static PrecisionType
523  EvaluateCostCallback(void * instance,
524  const PrecisionType * x,
525  PrecisionType * g,
526  const int n,
527  const PrecisionType step);
528 
529  PrecisionType
530  EvaluateCost(const PrecisionType * x, PrecisionType * g, const int n, const PrecisionType step);
531 
532 private:
533  // Private Implementation (Pimpl), to hide liblbfgs data structures
534  class PrivateImplementationHolder;
535  lbfgs_parameter_t m_Parameters{};
536 
537  bool m_EstimateScalesAtEachIteration{};
538  double m_CurrentStepSize{};
539  double m_CurrentParameterNorm{};
540  double m_CurrentGradientNorm{};
541  int m_CurrentNumberOfEvaluations{};
542  int m_StatusCode{};
543 
547  void
548  SetMinimumConvergenceValue(double) override
549  {
550  itkWarningMacro("Not supported. Please use LBFGS specific convergence methods.");
551  };
553  {
554  itkWarningMacro("Not supported. Please use LBFGS specific convergence methods.");
555  };
556  const double &
557  GetConvergenceValue() const override
558  {
559  itkWarningMacro("Not supported. Please use LBFGS specific convergence methods.");
560  static double value = 0;
561  return value;
562  };
565  void
566  AdvanceOneStep() override
567  {
568  itkWarningMacro("LBFGS2Optimizerv4Template does not implement single step advance");
569  };
570 };
571 
572 
575 
576 } // end namespace itk
577 
578 #ifndef ITK_MANUAL_INSTANTIATION
579 # include "itkLBFGS2Optimizerv4.hxx"
580 #endif
581 
582 #endif
itk::LBFGS2Optimizerv4Template::SetConvergenceWindowSize
void SetConvergenceWindowSize(SizeValueType) override
Definition: itkLBFGS2Optimizerv4.h:552
itk::LBFGS2Optimizerv4Enums::LineSearchMethod::LINESEARCH_BACKTRACKING_STRONG_WOLFE
itk::LBFGS2Optimizerv4Enums::LineSearchMethod::LINESEARCH_BACKTRACKING_WOLFE
itk::GradientDescentOptimizerv4Template
Gradient descent optimizer.
Definition: itkGradientDescentOptimizerv4.h:78
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:216
itk::LBFGS2Optimizerv4Template::ScalesType
typename Superclass::ScalesType ScalesType
Definition: itkLBFGS2Optimizerv4.h:202
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::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:300
itk::LBFGS2Optimizerv4Template::SetNumberOfIterations
void SetNumberOfIterations(const SizeValueType _arg) override
Definition: itkLBFGS2Optimizerv4.h:305
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
itkOverrideGetNameOfClassMacro
itkOverrideGetNameOfClassMacro(AddImageFilter)
itk::LBFGS2Optimizerv4Template::ParametersType
typename Superclass::ParametersType ParametersType
Definition: itkLBFGS2Optimizerv4.h:201
itk::LBFGS2Optimizerv4Template::AdvanceOneStep
void AdvanceOneStep() override
Definition: itkLBFGS2Optimizerv4.h:566
itkGradientDescentOptimizerv4.h
itk::LBFGS2Optimizerv4Template::MetricType
typename Superclass::MetricType MetricType
Definition: itkLBFGS2Optimizerv4.h:200
itk::LBFGS2Optimizerv4Enums::LineSearchMethod
LineSearchMethod
Definition: itkLBFGS2Optimizerv4.h:40
itk::LBFGS2Optimizerv4Template::SetMinimumConvergenceValue
void SetMinimumConvergenceValue(double) override
Definition: itkLBFGS2Optimizerv4.h:548
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::LBFGS2Optimizerv4Template::GetConvergenceValue
const double & GetConvergenceValue() const override
Definition: itkLBFGS2Optimizerv4.h:557
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83