28#include <ElementsKernel/Exception.h>
29#include <ElementsKernel/Logging.h>
33#ifndef LEVMAR_WORKAREA_MAX_SIZE
34#define LEVMAR_WORKAREA_MAX_SIZE size_t(2ul<<30)
42 std::cerr <<
" ||e||_2 at initial p: " << info[0] <<
'\n';
43 std::cerr <<
" ||e||_2: " << info[1] <<
'\n';
44 std::cerr <<
" ||J^T e||_inf: " << info[2] <<
'\n';
45 std::cerr <<
" ||Dp||_2: " << info[3] <<
'\n';
46 std::cerr <<
" mu/max[J^T J]_ii: " << info[4] <<
'\n';
47 std::cerr <<
" # iterations: " << info[5] <<
'\n';
48 switch ((
int) info[6]) {
50 std::cerr <<
" stopped by small gradient J^T e\n";
59 std::cerr <<
" singular matrix. Restart from current p with increased mu\n";
62 std::cerr <<
" no further error reduction is possible. Restart with increased mu\n";
65 std::cerr <<
" stopped by small ||e||_2\n";
68 std::cerr <<
" stopped by invalid (i.e. NaN or Inf) func values; a user error\n";
71 std::cerr <<
" # function evaluations: " << info[7] <<
'\n';
72 std::cerr <<
" # Jacobian evaluations: " << info[8] <<
'\n';
73 std::cerr <<
" # linear systems solved: " << info[9] <<
"\n\n";
92 switch (
static_cast<int>(info[6])) {
104 double epsilon2,
double epsilon3,
double delta)
105 :
m_itmax{itmax},
m_opts{tau, epsilon1, epsilon2, epsilon3, delta} {
106#ifdef LINSOLVERS_RETAIN_MEMORY
107 logger.warn() <<
"Using a non thread safe levmar! Parallelism will be reduced.";
114#ifdef LINSOLVERS_RETAIN_MEMORY
125 auto adata =
std::tie(parameter_manager, residual_estimator);
128 auto levmar_res_func = [](
double *p,
double *hx, int, int,
void *extra) {
129#ifdef LINSOLVERS_RETAIN_MEMORY
130 levmar_mutex.unlock();
133 auto* extra_ptr = (
decltype(adata)*)extra;
139#ifdef LINSOLVERS_RETAIN_MEMORY
154#ifdef LINSOLVERS_RETAIN_MEMORY
164 workarea.
reset(
new double[workarea_size]);
170 if (workarea ==
nullptr) {
180 auto res = dlevmar_dif(levmar_res_func,
189 covariance_matrix.
data(),
194#ifdef LINSOLVERS_RETAIN_MEMORY
195 levmar_mutex.unlock();
#define LEVMAR_WORKAREA_MAX_SIZE
static Logging getLogger(const std::string &name="")
Class responsible for managing the parameters the least square engine minimizes.
void updateEngineValues(DoubleIter new_values_iter)
Updates the managed parameters with the given engine values.
std::vector< double > convertCovarianceMatrixToWorldSpace(std::vector< double > covariance_matrix) const
std::size_t numberOfParameters()
Returns the number of parameters managed by the manager.
void getEngineValues(DoubleIter output_iter) const
Returns the engine values of the managed parameters.
LeastSquareSummary solveProblem(EngineParameterManager ¶meter_manager, ResidualEstimator &residual_estimator) override
virtual ~LevmarEngine()
Destructor.
std::vector< double > m_opts
LevmarEngine(size_t itmax=1000, double tau=1E-3, double epsilon1=1E-8, double epsilon2=1E-8, double epsilon3=1E-8, double delta=1E-4)
Constructs a new instance of the engine.
Provides to the LeastSquareEngine the residual values.
std::size_t numberOfResiduals() const
void populateResiduals(DoubleIter output_iter) const
static LeastSquareSummary::StatusFlag getStatusFlag(int ret)
static std::shared_ptr< LeastSquareEngine > createLevmarEngine(unsigned max_iterations)
static LeastSquareEngineManager::StaticEngine levmar_engine
static Elements::Logging logger
Class containing the summary information of solving a least square minimization problem.
StatusFlag status_flag
Flag indicating if the minimization was successful.
boost::any underlying_framework_info
size_t iteration_no
The number of iterations.
float duration
Runtime (in seconds)
std::vector< double > parameter_sigmas
1-sigma margin of error for all the parameters
int engine_stop_reason
Engine-specific reason for stopping the fitting.