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
27namespace itk
28{
29/* Necessary forward declaration see below for definition */
39// Forward reference because of circular dependencies
40template <typename TInternalVnlOptimizerType>
41class ITK_TEMPLATE_EXPORT LBFGSOptimizerBaseHelperv4;
42
75template <typename TInternalVnlOptimizerType>
77{
78public:
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
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
127
128 itkGetConstMacro(MaximumNumberOfFunctionEvaluations, unsigned int);
129
135 virtual void
137
138 itkGetConstMacro(GradientConvergenceTolerance, double);
139
143
144protected:
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
171template <typename TInternalVnlOptimizerType>
172class ITK_TEMPLATE_EXPORT LBFGSOptimizerBaseHelperv4 : public TInternalVnlOptimizerType
173{
174public:
176 using Superclass = TInternalVnlOptimizerType;
177
179 : TInternalVnlOptimizerType(f)
180 , m_ItkObj(itkObj)
181 {}
182
183protected:
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
216namespace itk
217{
218
219ITK_GCC_PRAGMA_DIAG_PUSH()
220ITK_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
228ITK_GCC_PRAGMA_DIAG_POP()
229
230} // end namespace itk
231#undef ITKOptimizersv4_EXPORT_EXPLICIT
232#endif
Wrapper helper around vnl_lbfgsb.
Control indentation during Print() invocation.
Definition: itkIndent.h:50
Wrapper helper around vnl optimizer.
LBFGSOptimizerBaseHelperv4(vnl_cost_function &f, LBFGSOptimizerBasev4< TInternalVnlOptimizerType > *itkObj)
LBFGSOptimizerBasev4< TInternalVnlOptimizerType > * m_ItkObj
TInternalVnlOptimizerType Superclass
Abstract base for vnl lbfgs algorithm optimizers in ITKv4 registration framework.
Superclass::StopConditionDescriptionType StopConditionDescriptionType
void SetMetric(MetricType *metric) override
Superclass::CostFunctionAdaptorType CostFunctionAdaptorType
virtual void SetTrace(bool flag)
Superclass::StopConditionReturnStringType StopConditionReturnStringType
InternalOptimizerType * GetOptimizer()
virtual void SetGradientConvergenceTolerance(double f)
Superclass::ScalesType ScalesType
~LBFGSOptimizerBasev4() override=default
Superclass::MetricType MetricType
Superclass::ParametersType ParametersType
void PrintSelf(std::ostream &os, Indent indent) const override
void StartOptimization(bool doOnlyInitialization=false) override
virtual void SetMaximumNumberOfFunctionEvaluations(unsigned int n)
std::unique_ptr< InternalOptimizerType > InternalOptimizerAutoPointer
StopConditionReturnStringType GetStopConditionDescription() const override
Light weight base class for most itk classes.
This is a base for the ITKv4 Optimization methods using the vnl library.
#define ITKOptimizersv4_EXPORT_EXPLICIT
static constexpr double e
Definition: itkMath.h:56
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
template class ITKOptimizersv4_EXPORT_EXPLICIT LBFGSOptimizerBasev4< vnl_lbfgsb >
class ITK_TEMPLATE_EXPORT LBFGSOptimizerBaseHelperv4
template class ITKOptimizersv4_EXPORT_EXPLICIT LBFGSOptimizerBasev4< vnl_lbfgs >