G+Smo  25.01.0
Geometry + Simulation Modules
 
Loading...
Searching...
No Matches
gsLevenbergMarquardt.h
Go to the documentation of this file.
1
10#pragma once
11
13#include <gsIO/gsOptionList.h>
16#define Eigen gsEigen
17#include "lsqcpp.h"
18#undef Eigen
19
20
21namespace gismo
22{
23
24template<typename T>
25struct gsLevenbergMarquardtObjective
26{
27 typedef gsEigen::Matrix<T, gsEigen::Dynamic, 1> Vector;
28 typedef gsEigen::Matrix<T, gsEigen::Dynamic, gsEigen::Dynamic> Matrix;
29
30 gsLevenbergMarquardtObjective(gsOptProblem<T>* objective)
31 :
32 obj(objective)
33 { }
34
35 gsLevenbergMarquardtObjective()
36 :
37 obj(nullptr)
38 { }
39
40 T operator()(const Vector &xval, Vector &fval, Matrix &jacobian) const
41 {
42 gsAsConstVector<T> xvec(xval.data(),xval.size());
43 const T val = obj->evalObj(xvec);
44 fval.resize(1);
45 fval(0,0) = val;
46
47 jacobian.resize(1, obj->numDesignVars());
48 gsAsVector<T> gvec(jacobian.data(), jacobian.size());
49 obj->gradObj_into(xvec,gvec);
50 return val;
51 }
52
53 gsOptProblem<T> * obj;
54};
55
66template<typename T = real_t,
67 typename StepSize=lsq::BarzilaiBorwein<T>,
68 typename Callback=lsq::NoCallback<T>,
69 typename FiniteDifferences=lsq::CentralDifferences<T> >
71{
72 using Base = gsOptimizer<T>;
73
74 typedef typename lsq::LevenbergMarquardt<T, gsLevenbergMarquardtObjective<T>>::Result Result;
75
76public:
78 :
79 Base(problem),
80 m_solver()
81 {
82 this->defaultOptions();
83 gsLevenbergMarquardtObjective<T> obj(m_op);
84 m_solver.setErrorFunction(obj);
85 }
86
87
88public:
89 // const gsMatrix<T> & lambda() const { return m_lambda; }
90
91 void minimize(const gsMatrix<T> &initialGuess)
92 {
93 m_result = m_solver.minimize(initialGuess);
94 m_numIterations = m_result.iterations;
95 m_finalObjective = m_result.fval.value(); //scalar assumed
96 m_curDesign = m_result.xval;
97 }
98
99 Result result() { return m_result; };
100
101protected:
102 void defaultOptions()
103 {
104 Base::defaultOptions();
105 m_options.addReal("MinGradLen","Minimal gradient length",1e-9);
106 m_options.addReal("MinStepLen","Minimal step length",1e-9);
107 }
108
109 void getOptions()
110 {
111 Base::getOptions();
112 m_solver.setMaxIterations(m_maxIterations);
113 m_solver.setMinGradientLength( m_options.getReal("MinGradLen") ); // same notation as gsHLBFGS
114 m_solver.setMinStepLength( m_options.getReal("MinStepLen") ); // same notation as gsHLBFGS
115 m_solver.setVerbosity(m_verbose);
116
117 // Set the minimum least squares error.
118 m_solver.setMinError(0);
119
120 // Set the parameters of the step method (Levenberg Marquardt).
121 //m_solver.setMethodParameters({1.0, 2.0, 0.5, 100});
122 }
123
124public:
125
126 void solve(const gsMatrix<T> &initialGuess)
127 {
128 this->getOptions();
129 this->minimize(initialGuess);
130 }
131
132protected:
133 using Base::m_op;
134 using Base::m_numIterations;
135 using Base::m_finalObjective;
136 using Base::m_curDesign;
137 using Base::m_options;
138 using Base::m_verbose;
139 using Base::m_maxIterations;
140
141 using Base::defaultOptions;
142 using Base::getOptions;
143
144 Result m_result;
145
146protected:
147 T m_minGradientLength;
148
149 lsq::LevenbergMarquardt<T, gsLevenbergMarquardtObjective<T> > m_solver;
150
151};
152
153
154// using gsLevenbergMarquardt = lsq::LevenbergMarquardt<T, Objective, StepSize, Callback, FiniteDifferences>;
155
156} //namespace gismo
This class describes the gradient descent method.
Definition gsLevenbergMarquardt.h:71
gsOptionList m_options
Options.
Definition gsOptimizer.h:108
gsMatrix< T > m_curDesign
Current design variables (and starting point )
Definition gsOptimizer.h:105
A matrix with arbitrary coefficient type and fixed or dynamic size.
Definition gsMatrix.h:41
Class defining an optimization problem.
Definition gsOptProblem.h:25
Class defining an optimizer.
Definition gsOptimizer.h:28
gsOptionList m_options
Options.
Definition gsOptimizer.h:108
gsMatrix< T > m_curDesign
Current design variables (and starting point )
Definition gsOptimizer.h:105
Real getReal(const std::string &label) const
Reads value for option label from options.
Definition gsOptionList.cpp:44
void addReal(const std::string &label, const std::string &desc, const Real &value)
Adds a option named label, with description desc and value value.
Definition gsOptionList.cpp:211
This is the main header file that collects wrappers of Eigen for linear algebra.
Provides declaration of an optimization problem.
This file is part of the G+Smo library.
Provides a list of labeled parameters/options that can be set and accessed easily.
The G+Smo namespace, containing all definitions for the library.