svZeroDSolver
Loading...
Searching...
No Matches
LevenbergMarquardtOptimizer.h
Go to the documentation of this file.
1// Copyright (c) Stanford University, The Regents of the University of
2// California, and others.
3//
4// All Rights Reserved.
5//
6// See Copyright-SimVascular.txt for additional details.
7//
8// Permission is hereby granted, free of charge, to any person obtaining
9// a copy of this software and associated documentation files (the
10// "Software"), to deal in the Software without restriction, including
11// without limitation the rights to use, copy, modify, merge, publish,
12// distribute, sublicense, and/or sell copies of the Software, and to
13// permit persons to whom the Software is furnished to do so, subject
14// to the following conditions:
15//
16// The above copyright notice and this permission notice shall be included
17// in all copies or substantial portions of the Software.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHdouble HOLDERS AND CONTRIBUTORS "AS
20// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUdouble NOdouble
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENdouble SHALL THE COPYRIGHdouble
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUdouble NOdouble
25// LIMITED TO, PROCUREMENdouble OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
27// OF LIABILITY, WHETHER IN CONTRACT, STRICdouble LIABILITY, OR TORdouble
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUdouble OF THE USE OF
29// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30/**
31 * @file LevenbergMarquardtOptimizer.h
32 * @brief opt::LevenbergMarquardtOptimizer source file
33 */
34#ifndef SVZERODSOLVER_OPTIMIZE_LEVENBERGMARQUARDT_HPP_
35#define SVZERODSOLVER_OPTIMIZE_LEVENBERGMARQUARDT_HPP_
36
37#include <Eigen/Dense>
38#include <Eigen/Sparse>
39
40#include "Model.h"
41
42/**
43 * @brief Levenberg-Marquardt optimization class
44 *
45 * The 0D residual (assuming no time-dependency in parameters) is
46 *
47 * \f[
48 * \boldsymbol{r}(\boldsymbol{\alpha}, \boldsymbol{y}, \boldsymbol{\dot{y}}) =
49 * \boldsymbol{E}(\boldsymbol{\alpha}, \boldsymbol{y}) \cdot
50 * \dot{\boldsymbol{y}}+\boldsymbol{F}(\boldsymbol{\alpha}, \boldsymbol{y})
51 * \cdot \boldsymbol{y}+\boldsymbol{c}(\boldsymbol{\alpha}, \boldsymbol{y}) \f]
52 *
53 * with solution vector \f$\boldsymbol{y} \in \mathbb{R}^{N}\f$ (flow and
54 * pressure at nodes), LPN parameters \f$\boldsymbol{\alpha} \in
55 * \mathbb{R}^{P}\f$, system matrices \f$\boldsymbol{E},\boldsymbol{F} \in
56 * \mathbb{R}^{NxN}\f$, and system vector \f$\boldsymbol{c} \in
57 * \mathbb{R}^{N}\f$.
58 *
59 * The least squares problem can be formulated as
60 *
61 * \f[
62 * \min _\alpha S, \quad \mathrm { with } \quad S=\sum_i^D
63 * r_i^2\left(\boldsymbol{\alpha}, y_i, \dot{y}_i\right) \f]
64 *
65 * with given solution vectors \f$\boldsymbol{y}\f$, \f$\boldsymbol{\dot{y}}\f$
66 * at all datapoints \f$D\f$. The parameter vector is iteratively improved
67 * according to
68 *
69 * \f[
70 * \boldsymbol{\alpha}^{i+1}=\boldsymbol{\alpha}^{i}+\Delta
71 * \boldsymbol{\alpha}^{i+1} \f]
72 *
73 * wherein the increment \f$\Delta \boldsymbol{\alpha}^{i+1} \f$ is determined
74 * by solving the following system:
75 *
76 * \f[
77 * \left[\mathbf{J}^{\mathrm{T}} \mathbf{J}+\lambda
78 * \operatorname{diag}\left(\mathbf{J}^{\mathrm{T}} \mathbf{J}\right)\right]^{i}
79 * \cdot \Delta \boldsymbol{\alpha}^{i+1}=-\left[\mathbf{J}^{\mathrm{T}}
80 * \mathbf{r}\right]^{i}, \quad \lambda^{i}=\lambda^{i-1}
81 * \cdot\left\|\left[\mathbf{J}^{\mathrm{T}} \mathbf{r}\right]^{i}\right\|_2
82 * /\left\|\left[\mathbf{J}^{\mathrm{T}} \mathbf{r}\right]^{i-1}\right\|_2. \f]
83 *
84 * The algorithm terminates when the following tolerance thresholds are reached
85 *
86 * \f[
87 * \left\|\left[\mathbf{J}^{\mathrm{T}}
88 * \mathbf{r}\right]^{\mathrm{i}}\right\|_2<\operatorname{tol}_{\text {grad
89 * }}^\alpha \text { and }\left\|\Delta
90 * \boldsymbol{\alpha}^{\mathrm{i}+1}\right\|_2<\mathrm{tol}_{\text {inc
91 * }}^\alpha, \f]
92 *
93 * The Jacobian is derived from the residual as
94 *
95 * \f[
96 * J = \frac{\partial \boldsymbol{r}}{\partial \boldsymbol{\alpha}} =
97 * \frac{\partial \mathbf{E}}{\partial \boldsymbol{\alpha}} \cdot
98 * \dot{\mathbf{y}}+\frac{\partial \mathbf{F}}{\partial \boldsymbol{\alpha}}
99 * \cdot \mathbf{y}+\frac{\partial \mathbf{c}}{\partial \boldsymbol{\alpha}} \f]
100 *
101 *
102 */
104 public:
105 /**
106 * @brief Construct a new LevenbergMarquardtOptimizer object
107 *
108 * @param model The 0D model
109 * @param num_obs Number of observations in optimization
110 * @param num_params Number of parameters in optimization
111 * @param lambda0 Initial damping factor
112 * @param tol_grad Gradient tolerance
113 * @param tol_inc Parameter increment tolerance
114 * @param max_iter Maximum iterations
115 */
116 LevenbergMarquardtOptimizer(Model* model, int num_obs, int num_params,
117 double lambda0, double tol_grad, double tol_inc,
118 int max_iter);
119
120 /**
121 * @brief Run the optimization algorithm
122 *
123 * @param alpha Initial parameter vector alpha
124 * @param y_obs Matrix (num_obs x n) with all observations for y
125 * @param dy_obs Matrix (num_obs x n) with all observations for dy
126 * @return Eigen::Matrix<double, Eigen::Dynamic, 1> Optimized parameter vector
127 * alpha
128 */
129 Eigen::Matrix<double, Eigen::Dynamic, 1> run(
130 Eigen::Matrix<double, Eigen::Dynamic, 1> alpha,
131 std::vector<std::vector<double>>& y_obs,
132 std::vector<std::vector<double>>& dy_obs);
133
134 private:
135 Eigen::SparseMatrix<double> jacobian;
136 Eigen::Matrix<double, Eigen::Dynamic, 1> residual;
137 Eigen::Matrix<double, Eigen::Dynamic, 1> delta;
138 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> mat;
139 Eigen::Matrix<double, Eigen::Dynamic, 1> vec;
140 Model* model;
141 double lambda;
142
143 int num_obs;
144 int num_params;
145 int num_eqns;
146 int num_vars;
147 int num_dpoints;
148
149 double tol_grad;
150 double tol_inc;
151 int max_iter;
152
153 void update_gradient(Eigen::Matrix<double, Eigen::Dynamic, 1>& alpha,
154 std::vector<std::vector<double>>& y_obs,
155 std::vector<std::vector<double>>& dy_obs);
156
157 void update_delta(bool first_step);
158};
159
160#endif // SVZERODSOLVER_OPTIMIZE_LEVENBERGMARQUARDT_HPP_
model::Model source file
Levenberg-Marquardt optimization class.
Definition LevenbergMarquardtOptimizer.h:103
LevenbergMarquardtOptimizer(Model *model, int num_obs, int num_params, double lambda0, double tol_grad, double tol_inc, int max_iter)
Construct a new LevenbergMarquardtOptimizer object.
Definition LevenbergMarquardtOptimizer.cpp:35
Eigen::Matrix< double, Eigen::Dynamic, 1 > run(Eigen::Matrix< double, Eigen::Dynamic, 1 > alpha, std::vector< std::vector< double > > &y_obs, std::vector< std::vector< double > > &dy_obs)
Run the optimization algorithm.
Definition LevenbergMarquardtOptimizer.cpp:56
Model of 0D elements.
Definition Model.h:75