Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
subset_gauss_newton_solver.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
9
12
13#include <gsl/narrow>
14
15namespace momentum {
16
17template <typename T>
19 const SolverOptions& options,
20 SolverFunctionT<T>* solver)
21 : SolverT<T>(options, solver), delta_(this->parameters_.size()) {
22 this->initialized_ = false;
23
24 // Set default values from SubsetGaussNewtonSolverOptions
27
28 // Update values based on provided options
29 setOptions(options);
30 updateEnabledParameters();
31}
32
33template <typename T>
34std::string_view SubsetGaussNewtonSolverT<T>::getName() const {
35 return "SubsetGaussNewton";
36}
37
38template <typename T>
41
42 if (const auto* derivedOptions = dynamic_cast<const SubsetGaussNewtonSolverOptions*>(&options)) {
43 regularization_ = derivedOptions->regularization;
44 doLineSearch_ = derivedOptions->doLineSearch;
45 }
46}
47
48template <typename T>
49void SubsetGaussNewtonSolverT<T>::updateEnabledParameters() {
50 const auto nFullParams = this->parameters_.size();
51 this->enabledParameters_.clear();
52 this->enabledParameters_.reserve(nFullParams);
53 for (int iFullParam = 0; iFullParam < nFullParams; ++iFullParam) {
54 if (this->activeParameters_.test(iFullParam)) {
55 this->enabledParameters_.push_back(iFullParam);
56 }
57 }
58 delta_.setZero();
59
60 const int nSubsetParams = gsl::narrow_cast<int>(this->enabledParameters_.size());
61 this->subsetHessian_.resize(nSubsetParams, nSubsetParams);
62 subsetDelta_.resize(nSubsetParams);
63}
64
65template <typename T>
68 updateEnabledParameters();
69}
70
71template <typename T>
74
75 // get the jacobian and residual
76 size_t jacobianRows = 0;
77 this->error_ = this->solverFunction_->getJacobian(
78 this->parameters_, this->jacobian_, this->residual_, jacobianRows);
79 const double error_orig = this->error_;
80
81 const int nSubsetParams = gsl::narrow_cast<int>(this->enabledParameters_.size());
82
83 // Fill in J for this subset
84
85 // Move the solved columns of the Jacobian into place.
86 // Doing this in-place in the jacobian minimizes the total work.
87 // For the common case where all the "dropped" parameters at the end,
88 // this will effectively be a no-op, since enabledParameters_[iSubsetParam] = iFullParam
89 // for all parameters prior to the dropped ones.
90 for (int iSubsetParam = 0; iSubsetParam < nSubsetParams; ++iSubsetParam) {
91 // Only move columns that need it:
92 if (this->enabledParameters_[iSubsetParam] > iSubsetParam) {
93 this->jacobian_.col(iSubsetParam) =
94 this->jacobian_.col(this->enabledParameters_[iSubsetParam]);
95 }
96 }
97
98 const auto JBlock = this->jacobian_.topLeftCorner(jacobianRows, nSubsetParams);
99
100 // Compute grad = (J^T * r) and hess ~= (J^T * J) for the actual solved subset:
101 // The gradient is just Jt * r but only keeping `jacobianRows` which can be smaller than the
102 // number of rows of the `jacobian` matrix.
103 this->subsetGradient_.noalias() = this->residual_.head(jacobianRows).transpose() * JBlock;
104
105 // The Hessian takes a regularizer on top of Jt * J
106 this->subsetHessian_.template triangularView<Eigen::Lower>() = JBlock.transpose() * JBlock;
107 this->subsetHessian_.diagonal().array() += this->regularization_;
108
109 // LLT only reads the lower triangular part
110 subsetDelta_.noalias() = llt_.compute(this->subsetHessian_).solve(subsetGradient_);
111
112 // We don't need to set this->delta_ to zero because it is already set to zero when
113 // enabledParameters_ is updated.
114 for (int iSubsetParam = 0; iSubsetParam < nSubsetParams; ++iSubsetParam) {
115 const int iFullParam = this->enabledParameters_[iSubsetParam];
116 delta_(iFullParam) = subsetDelta_(iSubsetParam);
117 }
118 // update the parameters
119 if (doLineSearch_) {
120 const double innerProd = -this->subsetGradient_.dot(subsetDelta_);
121
122 // Line search:
123 const float c_1 = 1e-4F;
124 const float tau = 0.5F;
125 float alpha = 1.0F;
126
127 const Eigen::VectorX<T> parameters_orig = this->parameters_;
128 for (size_t kStep = 0; kStep < 10 && std::fpclassify(alpha) == FP_NORMAL; ++kStep) {
129 // update the this->parameters_
130 this->parameters_ = parameters_orig;
131 this->solverFunction_->updateParameters(this->parameters_, alpha * delta_);
132
133 const double error_new = this->solverFunction_->getError(this->parameters_);
134
135 if ((error_orig - error_new) >= c_1 * alpha * -innerProd) {
136 break;
137 }
138
139 // Reduce step size:
140 alpha = alpha * tau;
141 }
142 } else {
143 this->solverFunction_->updateParameters(this->parameters_, delta_);
144 }
145}
146
147template <typename T>
149
152
153} // namespace momentum
Abstract base class for optimization objective functions.
virtual void setOptions(const SolverOptions &options)
Updates solver configuration with new options.
Definition solver.cpp:32
Eigen::VectorX< T > parameters_
Current parameter values.
Definition solver.h:109
double error_
Current objective function value.
Definition solver.h:126
SolverFunctionT< T > * solverFunction_
Function to be optimized.
Definition solver.h:106
virtual void setEnabledParameters(const ParameterSet &parameters)
Specifies which parameters should be optimized.
Definition solver.cpp:40
SolverT(const SolverOptions &options, SolverFunctionT< T > *solver)
Constructor with solver options and function to optimize.
Definition solver.cpp:20
Gauss-Newton solver that optimizes only a selected subset of parameters.
void doIteration() final
Performs one iteration of the Subset Gauss-Newton algorithm.
void setOptions(const SolverOptions &options) final
Updates solver configuration, handling both base and Subset Gauss-Newton specific options.
void initializeSolver() final
Initializes solver state before optimization begins.
std::string_view getName() const override
Returns "SubsetGaussNewton" as the solver name.
void setEnabledParameters(const ParameterSet &parameters) final
Specifies which parameters should be optimized.
SubsetGaussNewtonSolverT(const SolverOptions &options, SolverFunctionT< T > *solver)
Creates a solver with the specified options and function to optimize.
std::bitset< kMaxModelParams > ParameterSet
Bitset for model parameter enablement.
Definition types.h:430
Profiling macros that dispatch to one of three backends, selected at compile time:
#define MT_PROFILE_FUNCTION()
Definition profile.h:110
float regularization
Damping parameter added to Hessian diagonal for numerical stability; see https://en....
bool doLineSearch
Enables backtracking line search to ensure error reduction at each step.
Common options for numerical optimization solvers.
Definition solver.h:19
Extended options for the Subset Gauss-Newton optimization algorithm.