ITK  6.0.0
Insight Toolkit
itkLBFGSOptimizerBasev4.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 itkLBFGSOptimizerBasev4_h
19 #define itkLBFGSOptimizerBasev4_h
20 
22 #include "vnl/algo/vnl_lbfgs.h"
23 #include "vnl/algo/vnl_lbfgsb.h"
24 #include <memory>
25 #include "ITKOptimizersv4Export.h"
26 
27 namespace itk
28 {
29 /* Necessary forward declaration see below for definition */
39 // Forward reference because of circular dependencies
40 template <typename TInternalVnlOptimizerType>
41 class ITK_TEMPLATE_EXPORT LBFGSOptimizerBaseHelperv4;
42 
75 template <typename TInternalVnlOptimizerType>
77 {
78 public:
79  ITK_DISALLOW_COPY_AND_MOVE(LBFGSOptimizerBasev4);
80 
86 
88  itkOverrideGetNameOfClassMacro(LBFGSOptimizerBasev4);
89 
90  using MetricType = Superclass::MetricType;
91  using ParametersType = Superclass::ParametersType;
92  using ScalesType = Superclass::ScalesType;
93 
95  using StopConditionReturnStringType = Superclass::StopConditionReturnStringType;
96 
98  using StopConditionDescriptionType = Superclass::StopConditionDescriptionType;
99 
102 
105  GetOptimizer();
106 
108  void
109  StartOptimization(bool doOnlyInitialization = false) override;
110 
112  void
113  SetMetric(MetricType * metric) override;
114 
118  virtual void
119  SetTrace(bool flag);
120 
121  itkGetConstMacro(Trace, bool);
122  itkBooleanMacro(Trace);
123 
125  virtual void
126  SetMaximumNumberOfFunctionEvaluations(unsigned int n);
127 
128  itkGetConstMacro(MaximumNumberOfFunctionEvaluations, unsigned int);
129 
135  virtual void
136  SetGradientConvergenceTolerance(double f);
137 
138  itkGetConstMacro(GradientConvergenceTolerance, double);
139 
142  GetStopConditionDescription() const override;
143 
144 protected:
146  ~LBFGSOptimizerBasev4() override = default;
147  void
148  PrintSelf(std::ostream & os, Indent indent) const override;
149 
150  using CostFunctionAdaptorType = Superclass::CostFunctionAdaptorType;
151 
152  bool m_OptimizerInitialized{ false };
153 
154  using InternalOptimizerAutoPointer = std::unique_ptr<InternalOptimizerType>;
156 
157  mutable std::ostringstream m_StopConditionDescription{};
158 
159  bool m_Trace{ false };
160  unsigned int m_MaximumNumberOfFunctionEvaluations{ 2000 };
161  double m_GradientConvergenceTolerance{ 1e-5 };
162  double m_InfinityNormOfProjectedGradient{ 0.0 };
163  double m_CostFunctionConvergenceFactor{ 1e+7 };
164 
165  // give the helper access to member variables, to update iteration
166  // counts, etc.
167  friend class LBFGSOptimizerBaseHelperv4<TInternalVnlOptimizerType>;
169 };
170 
171 template <typename TInternalVnlOptimizerType>
172 class ITK_TEMPLATE_EXPORT LBFGSOptimizerBaseHelperv4 : public TInternalVnlOptimizerType
173 {
174 public:
176  using Superclass = TInternalVnlOptimizerType;
177 
179  : TInternalVnlOptimizerType(f)
180  , m_ItkObj(itkObj)
181  {}
182 
183 protected:
185 
187  bool
188  report_iter() override;
189 };
190 
191 } // end namespace itk
192 
193 #ifndef ITK_MANUAL_INSTANTIATION
194 # include "itkLBFGSOptimizerBasev4.hxx"
195 #endif
196 
197 #endif
198 
200 #ifndef ITK_TEMPLATE_EXPLICIT_LBFGSOptimizerBasev4
201 // Explicit instantiation is required to ensure correct dynamic_cast
202 // behavior across shared libraries.
203 //
204 // IMPORTANT: Since within the same compilation unit,
205 // ITK_TEMPLATE_EXPLICIT_<classname> defined and undefined states
206 // need to be considered. This code *MUST* be *OUTSIDE* the header
207 // guards.
208 //
209 #if defined(ITKOptimizersv4_EXPORTS)
210 // We are building this library
211 # define ITKOptimizersv4_EXPORT_EXPLICIT ITK_FORWARD_EXPORT
212 #else
213 // We are using this library
214 # define ITKOptimizersv4_EXPORT_EXPLICIT ITKOptimizersv4_EXPORT
215 #endif
216 namespace itk
217 {
218 
219 ITK_GCC_PRAGMA_DIAG_PUSH()
220 ITK_GCC_PRAGMA_DIAG(ignored "-Wattributes")
221 
222 #if defined(_MSC_VER)
223 # pragma warning(disable : 4661) // no suitable definition provided for explicit template instantiation request
224 #endif
227 
228 ITK_GCC_PRAGMA_DIAG_POP()
229 
230 } // end namespace itk
231 #undef ITKOptimizersv4_EXPORT_EXPLICIT
232 #endif
itk::LBFGSOptimizerBaseHelperv4
Wrapper helper around vnl optimizer.
Definition: itkLBFGSOptimizerBasev4.h:172
itk::LBFGSOptimizerBaseHelperv4
class ITK_TEMPLATE_EXPORT LBFGSOptimizerBaseHelperv4
Definition: itkLBFGSOptimizerBasev4.h:41
itk::LBFGSOptimizerBasev4< vnl_lbfgsb >
template class ITKOptimizersv4_EXPORT_EXPLICIT LBFGSOptimizerBasev4< vnl_lbfgsb >
itk::SingleValuedNonLinearVnlOptimizerv4::MetricType
Superclass::MetricType MetricType
Definition: itkSingleValuedNonLinearVnlOptimizerv4.h:60
itk::LBFGSOptimizerBasev4< vnl_lbfgs >
template class ITKOptimizersv4_EXPORT_EXPLICIT LBFGSOptimizerBasev4< vnl_lbfgs >
itk::LBFGSOptimizerBasev4< vnl_lbfgsb >::InternalOptimizerAutoPointer
std::unique_ptr< InternalOptimizerType > InternalOptimizerAutoPointer
Definition: itkLBFGSOptimizerBasev4.h:154
itk::SingleValuedNonLinearVnlOptimizerv4::ScalesType
Superclass::ScalesType ScalesType
Definition: itkSingleValuedNonLinearVnlOptimizerv4.h:63
itk::LBFGSOptimizerBaseHelperv4::m_ItkObj
LBFGSOptimizerBasev4< TInternalVnlOptimizerType > * m_ItkObj
Definition: itkLBFGSOptimizerBasev4.h:184
itkSingleValuedNonLinearVnlOptimizerv4.h
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::SingleValuedNonLinearVnlOptimizerv4
This is a base for the ITKv4 Optimization methods using the vnl library.
Definition: itkSingleValuedNonLinearVnlOptimizerv4.h:40
itk::SingleValuedNonLinearVnlOptimizerv4::StopConditionReturnStringType
Superclass::StopConditionReturnStringType StopConditionReturnStringType
Definition: itkSingleValuedNonLinearVnlOptimizerv4.h:66
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:55
itk::SingleValuedNonLinearVnlOptimizerv4::StopConditionDescriptionType
Superclass::StopConditionDescriptionType StopConditionDescriptionType
Definition: itkSingleValuedNonLinearVnlOptimizerv4.h:69
itk::LBFGSOptimizerBaseHelperv4::LBFGSOptimizerBaseHelperv4
LBFGSOptimizerBaseHelperv4(vnl_cost_function &f, LBFGSOptimizerBasev4< TInternalVnlOptimizerType > *itkObj)
Definition: itkLBFGSOptimizerBasev4.h:178
LBFGSBOptimizerHelperv4
Wrapper helper around vnl_lbfgsb.
itk::SingleValuedVnlCostFunctionAdaptorv4
This class is an Adaptor that allows to pass itk::ObjectToObjectMetricBase objects to vnl_optimizers ...
Definition: itkSingleValuedVnlCostFunctionAdaptorv4.h:37
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
ITKOptimizersv4_EXPORT_EXPLICIT
#define ITKOptimizersv4_EXPORT_EXPLICIT
Definition: itkLBFGSOptimizerBasev4.h:214
itk::Math::e
static constexpr double e
Definition: itkMath.h:56
itk::SingleValuedNonLinearVnlOptimizerv4::ParametersType
Superclass::ParametersType ParametersType
Definition: itkSingleValuedNonLinearVnlOptimizerv4.h:62
itk::LBFGSOptimizerBaseHelperv4::Superclass
TInternalVnlOptimizerType Superclass
Definition: itkLBFGSOptimizerBasev4.h:176
itk::LBFGSOptimizerBasev4
Abstract base for vnl lbfgs algorithm optimizers in ITKv4 registration framework.
Definition: itkLBFGSOptimizerBasev4.h:76