Unfit  3.1.1
Data fitting and optimization software
LevenbergMarquardt.hpp
1 // Unfit: Data fitting and optimization software
2 //
3 // Copyright (C) 2012- Dr Martin Buist & Dr Alberto Corrias
4 // Contacts: martin.buist _at_ nus.edu.sg; alberto _at_ nus.edu.sg
5 //
6 // See the 'Contributors' file for a list of those who have contributed
7 // to this work.
8 //
9 // This program is free software: you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 3 of the License, or
12 // (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program. If not, see <http://www.gnu.org/licenses/>.
21 //
22 #ifndef UNFIT_INCLUDE_LEVENBERGMARQUARDT_HPP_
23 #define UNFIT_INCLUDE_LEVENBERGMARQUARDT_HPP_
24 
25 #include <vector>
26 #include "Bounds.hpp"
27 #include "GenericCostFunction.hpp"
28 #include "GenericOptimizer.hpp"
29 #include "Matrix.hpp"
30 #include "Options.hpp"
31 
32 namespace Unfit
33 {
50 {
52  friend class TestLevenbergMarquardt;
53  public:
58 
64  virtual ~LevenbergMarquardt() {}
65 
90  int FindMin(GenericCostFunction &cost_function,
91  std::vector<double> &coordinates) override;
92 
97  void Reset() override;
98 
99  private:
111  std::vector<double> LoopUntilWithinBounds(
112  const std::vector<double> &point, std::vector<double> &hlm);
113 
125  std::vector<double> CalculateResiduals(GenericCostFunction &cost_function,
126  const std::vector<double> &coordinates);
127 
141  void FindJacobian(GenericCostFunction &cost_function,
142  const std::vector<double> &residuals,
143  const std::vector<double> &coordinates, bool one_sided_difference = true);
144 
157  void BroydenUpdate(const std::vector<double> &residuals_new,
158  const std::vector<double> &residuals, const std::vector<double> &hlm,
159  Matrix &jacobianT);
160 
163  // MB: I need to have a look to see if it is much slower if we just make the
164  // jacobian locally each time we need it, rather than storing it as a member
165  // variable (as it is probably cleaner - more pure).
167  std::size_t dimensions_;
169  std::size_t observation_size_;
171  double stored_cost_;
172 };
173 
174 } // namespace Unfit
175 
176 #endif
std::vector< double > CalculateResiduals(GenericCostFunction &cost_function, const std::vector< double > &coordinates)
Definition: LevenbergMarquardt.cpp:59
void BroydenUpdate(const std::vector< double > &residuals_new, const std::vector< double > &residuals, const std::vector< double > &hlm, Matrix &jacobianT)
Definition: LevenbergMarquardt.cpp:267
A class to implement the Levenberg Marquardt optimization method.
Definition: LevenbergMarquardt.hpp:49
LevenbergMarquardt()
Definition: LevenbergMarquardt.cpp:39
Definition: GenericOptimizer.hpp:41
Definition: Matrix.hpp:33
Matrix jacobian_
Definition: LevenbergMarquardt.hpp:162
Definition: Bounds.hpp:27
double stored_cost_
Definition: LevenbergMarquardt.hpp:171
virtual ~LevenbergMarquardt()
Definition: LevenbergMarquardt.hpp:64
Definition: GenericCostFunction.hpp:36
Definition: TestLevenbergMarquardt.cpp:40
void FindJacobian(GenericCostFunction &cost_function, const std::vector< double > &residuals, const std::vector< double > &coordinates, bool one_sided_difference=true)
Definition: LevenbergMarquardt.cpp:198
void Reset() override
Definition: LevenbergMarquardt.cpp:49
int FindMin(GenericCostFunction &cost_function, std::vector< double > &coordinates) override
Implements the Levenberd-Marquardt optimization method.
Definition: LevenbergMarquardt.cpp:66
std::vector< double > LoopUntilWithinBounds(const std::vector< double > &point, std::vector< double > &hlm)
Definition: LevenbergMarquardt.cpp:245
std::size_t dimensions_
Definition: LevenbergMarquardt.hpp:167
std::size_t observation_size_
Definition: LevenbergMarquardt.hpp:169