ITK  5.3.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  * 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 itkLBFGS2Optimizerv4_h
19 #define itkLBFGS2Optimizerv4_h
20 
22 #include "ITKOptimizersv4Export.h"
23 #include <memory>
24 
25 namespace itk
26 {
27 /*** \class LBFGS2Optimizerv4Enums
28  * \brief Scoped Enum classes for LBFGS2Optimizerv4 class
29  * \ingroup ITKOptimizersv4
30  */
32 {
33 public:
34  /***\class LineSearchMethod
35  * \ingroup ITKOptimizersv4
36  * Line search method enum
37  */
39  {
42 
45 
56 
59 
71 
83  };
84 };
85 // Define how to print enumeration
86 extern ITKOptimizersv4_EXPORT std::ostream &
87  operator<<(std::ostream & out, LBFGS2Optimizerv4Enums::LineSearchMethod value);
89 
162 class ITKOptimizersv4_EXPORT LBFGS2Optimizerv4 : public GradientDescentOptimizerv4Template<double>
163 {
165 
166 public:
167  ITK_DISALLOW_COPY_AND_MOVE(LBFGS2Optimizerv4);
168 
170 #if !defined(ITK_LEGACY_REMOVE)
171 
172  static constexpr LineSearchMethodEnum LINESEARCH_DEFAULT = LineSearchMethodEnum::LINESEARCH_DEFAULT;
173  static constexpr LineSearchMethodEnum LINESEARCH_MORETHUENTE = LineSearchMethodEnum::LINESEARCH_MORETHUENTE;
174  static constexpr LineSearchMethodEnum LINESEARCH_BACKTRACKING_ARMIJO =
175  LineSearchMethodEnum::LINESEARCH_BACKTRACKING_ARMIJO;
176  static constexpr LineSearchMethodEnum LINESEARCH_BACKTRACKING = LineSearchMethodEnum::LINESEARCH_BACKTRACKING;
177  static constexpr LineSearchMethodEnum LINESEARCH_BACKTRACKING_WOLFE =
178  LineSearchMethodEnum::LINESEARCH_BACKTRACKING_WOLFE;
179  static constexpr LineSearchMethodEnum LINESEARCH_BACKTRACKING_STRONG_WOLFE =
180  LineSearchMethodEnum::LINESEARCH_BACKTRACKING_STRONG_WOLFE;
181 #endif
182 
188  using PrecisionType = double;
189 
195 
196  using MetricType = Superclass::MetricType;
197  using ParametersType = Superclass::ParametersType;
198  using ScalesType = Superclass::ScalesType;
199 
201  using typename Superclass::StopConditionReturnStringType;
202 
204  itkNewMacro(Self);
205 
208 
210  void
211  StartOptimization(bool doOnlyInitialization = false) override;
212 
216  void
217  ResumeOptimization() override;
218 
219  virtual const StopConditionReturnStringType
220  GetStopConditionDescription() const override;
221 
230  void
231  SetHessianApproximationAccuracy(int m);
232  int
233  GetHessianApproximationAccuracy() const;
235 
244  void
245  SetSolutionAccuracy(PrecisionType epsilon);
247  GetSolutionAccuracy() const;
249 
257  void
258  SetDeltaConvergenceDistance(int nPast);
259  int
260  GetDeltaConvergenceDistance() const;
262 
273  void
274  SetDeltaConvergenceTolerance(PrecisionType tol);
276  GetDeltaConvergenceTolerance() const;
278 
287  void
288  SetMaximumIterations(int maxIterations);
289  int
290  GetMaximumIterations() const;
292 
296  GetNumberOfIterations() const override
297  {
298  return GetMaximumIterations();
299  }
300  void
302  {
303  SetMaximumIterations(static_cast<int>(_arg));
304  }
306 
313  void
314  SetLineSearch(const LineSearchMethodEnum & linesearch);
315  LineSearchMethodEnum
316  GetLineSearch() const;
318 
324  void
325  SetMaximumLineSearchEvaluations(int n);
326  int
327  GetMaximumLineSearchEvaluations() const;
329 
337  void
338  SetMinimumLineSearchStep(PrecisionType step);
339  PrecisionType
340  GetMinimumLineSearchStep() const;
342 
350  void
351  SetMaximumLineSearchStep(PrecisionType step);
352  PrecisionType
353  GetMaximumLineSearchStep() const;
355 
361  void
362  SetLineSearchAccuracy(PrecisionType ftol);
363  PrecisionType
364  GetLineSearchAccuracy() const;
366 
376  void
377  SetWolfeCoefficient(PrecisionType wc);
378  PrecisionType
379  GetWolfeCoefficient() const;
381 
393  void
394  SetLineSearchGradientAccuracy(PrecisionType gtol);
395  PrecisionType
396  GetLineSearchGradientAccuracy() const;
398 
406  void
407  SetMachinePrecisionTolerance(PrecisionType xtol);
408  PrecisionType
409  GetMachinePrecisionTolerance() const;
411 
425  void
426  SetOrthantwiseCoefficient(PrecisionType orthant_c);
427  PrecisionType
428  GetOrthantwiseCoefficient() const;
430 
445  void
446  SetOrthantwiseStart(int start);
447  int
448  GetOrthantwiseStart() const;
450 
458  void
459  SetOrthantwiseEnd(int end);
460  int
461  GetOrthantwiseEnd() const;
463 
465  itkGetConstMacro(CurrentParameterNorm, PrecisionType);
466 
468  itkGetConstMacro(CurrentGradientNorm, PrecisionType);
469 
471  itkGetConstMacro(CurrentStepSize, PrecisionType);
472 
474  itkGetConstMacro(CurrentNumberOfEvaluations, PrecisionType);
475 
480  itkSetMacro(EstimateScalesAtEachIteration, bool);
481  itkGetConstReferenceMacro(EstimateScalesAtEachIteration, bool);
482  itkBooleanMacro(EstimateScalesAtEachIteration);
484 
485 protected:
487  ~LBFGS2Optimizerv4() override;
488  void
489  PrintSelf(std::ostream & os, Indent indent) const override;
490 
491 
493  static int
494  UpdateProgressCallback(void * instance,
495  const PrecisionType * x,
496  const PrecisionType * g,
497  const PrecisionType fx,
498  const PrecisionType xnorm,
499  const PrecisionType gnorm,
500  const PrecisionType step,
501  int n,
502  int k,
503  int ls);
504 
506  int
507  UpdateProgress(const PrecisionType * x,
508  const PrecisionType * g,
509  const PrecisionType fx,
510  const PrecisionType xnorm,
511  const PrecisionType gnorm,
512  const PrecisionType step,
513  int n,
514  int k,
515  int ls);
516 
518  static PrecisionType
519  EvaluateCostCallback(void * instance,
520  const PrecisionType * x,
521  PrecisionType * g,
522  const int n,
523  const PrecisionType step);
524 
525  PrecisionType
526  EvaluateCost(const PrecisionType * x, PrecisionType * g, const int n, const PrecisionType step);
527 
528 private:
529  // Private Implementation (Pimpl), to hide liblbfgs data structures
530  class PrivateImplementationHolder;
531  std::unique_ptr<PrivateImplementationHolder> m_Pimpl;
532 
539 
543  void
544  SetMinimumConvergenceValue(double) override
545  {
546  itkWarningMacro("Not supported. Please use LBFGS specific convergence methods.");
547  };
549  {
550  itkWarningMacro("Not supported. Please use LBFGS specific convergence methods.");
551  };
552  const double &
553  GetConvergenceValue() const override
554  {
555  itkWarningMacro("Not supported. Please use LBFGS specific convergence methods.");
556  static double value = 0;
557  return value;
558  };
560 
561  void
562  AdvanceOneStep() override
563  {
564  itkWarningMacro("LBFGS2Optimizerv4 does not implemenetd single step advance");
565  };
566 };
567 } // end namespace itk
568 #endif
itk::LBFGS2Optimizerv4::m_StatusCode
int m_StatusCode
Definition: itkLBFGS2Optimizerv4.h:538
itk::LBFGS2Optimizerv4Enums::LineSearchMethod::LINESEARCH_BACKTRACKING_STRONG_WOLFE
itk::LBFGS2Optimizerv4::GetConvergenceValue
const double & GetConvergenceValue() const override
Definition: itkLBFGS2Optimizerv4.h:553
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::LBFGS2Optimizerv4::m_EstimateScalesAtEachIteration
bool m_EstimateScalesAtEachIteration
Definition: itkLBFGS2Optimizerv4.h:533
itk::LBFGS2Optimizerv4Enums::LineSearchMethod::LINESEARCH_MORETHUENTE
itk::LBFGS2Optimizerv4Enums
Definition: itkLBFGS2Optimizerv4.h:31
itk::ObjectToObjectOptimizerBaseTemplate< double >::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::LBFGS2Optimizerv4::AdvanceOneStep
void AdvanceOneStep() override
Definition: itkLBFGS2Optimizerv4.h:562
itk::LBFGS2Optimizerv4::ScalesType
Superclass::ScalesType ScalesType
Definition: itkLBFGS2Optimizerv4.h:198
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:59
itk::LBFGS2Optimizerv4::m_CurrentStepSize
double m_CurrentStepSize
Definition: itkLBFGS2Optimizerv4.h:534
itk::LBFGS2Optimizerv4::ParametersType
Superclass::ParametersType ParametersType
Definition: itkLBFGS2Optimizerv4.h:197
itkGradientDescentOptimizerv4.h
itk::LBFGS2Optimizerv4::MetricType
Superclass::MetricType MetricType
Definition: itkLBFGS2Optimizerv4.h:196
itk::LBFGS2Optimizerv4::SetMinimumConvergenceValue
void SetMinimumConvergenceValue(double) override
Definition: itkLBFGS2Optimizerv4.h:544
itk::LBFGS2Optimizerv4Enums::LineSearchMethod
LineSearchMethod
Definition: itkLBFGS2Optimizerv4.h:38
itk::uint8_t
std::uint8_t uint8_t
Definition: itkIntTypes.h:29
itk::LBFGS2Optimizerv4::GetNumberOfIterations
SizeValueType GetNumberOfIterations() const override
Definition: itkLBFGS2Optimizerv4.h:296
itk::LBFGS2Optimizerv4::m_Pimpl
std::unique_ptr< PrivateImplementationHolder > m_Pimpl
Definition: itkLBFGS2Optimizerv4.h:530
itk::LBFGS2Optimizerv4::SetNumberOfIterations
void SetNumberOfIterations(const SizeValueType _arg) override
Definition: itkLBFGS2Optimizerv4.h:301
itk::LBFGS2Optimizerv4::SetConvergenceWindowSize
void SetConvergenceWindowSize(SizeValueType) override
Definition: itkLBFGS2Optimizerv4.h:548
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::LBFGS2Optimizerv4::m_CurrentParameterNorm
double m_CurrentParameterNorm
Definition: itkLBFGS2Optimizerv4.h:535
itk::LBFGS2Optimizerv4
Wrap of the libLBFGS[1] algorithm for use in ITKv4 registration framework. LibLBFGS is a translation ...
Definition: itkLBFGS2Optimizerv4.h:162
itk::LBFGS2Optimizerv4::m_CurrentNumberOfEvaluations
int m_CurrentNumberOfEvaluations
Definition: itkLBFGS2Optimizerv4.h:537
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
itk::LBFGS2Optimizerv4::m_CurrentGradientNorm
double m_CurrentGradientNorm
Definition: itkLBFGS2Optimizerv4.h:536
itk::LBFGS2Optimizerv4::PrecisionType
double PrecisionType
Definition: itkLBFGS2Optimizerv4.h:188