Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
gauss_newton_solver_qr.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
13namespace momentum {
14
15template <typename T>
17 const SolverOptions& options,
18 SolverFunctionT<T>* solverFun)
19 : SolverT<T>(options, solverFun), qrSolver_(0) {
20 // Set default values from GaussNewtonSolverQROptions
23
24 // Update values based on provided options
25 setOptions(options);
26}
27
28template <typename T>
30
31template <typename T>
32std::string_view GaussNewtonSolverQRT<T>::getName() const {
33 return "GaussNewtonQR";
34}
35
36template <typename T>
38
39template <typename T>
42
43 if (const auto* derivedOptions = dynamic_cast<const GaussNewtonSolverQROptions*>(&options)) {
44 regularization_ = derivedOptions->regularization;
45 doLineSearch_ = derivedOptions->doLineSearch;
46 }
47}
48
49template <typename T>
52
53 // Initialize Jacobian computation (sets up SkeletonState, MeshState, etc.)
54 {
55 MT_PROFILE_EVENT("Initialize Jacobian computation");
56 this->solverFunction_->initializeJacobianComputation(this->parameters_);
57 }
58
59 const auto nFullParams = static_cast<Eigen::Index>(this->parameters_.size());
60
61 std::vector<Eigen::Index> enabledParameters;
62 enabledParameters.reserve(nFullParams);
63 for (int iFullParam = 0; iFullParam < nFullParams; ++iFullParam) {
64 if (this->activeParameters_.test(iFullParam)) {
65 enabledParameters.push_back(iFullParam);
66 }
67 }
68
69 const auto nSubsetParams = static_cast<int>(enabledParameters.size());
70
71 if (enabledParameters.empty()) {
72 return;
73 }
74
75 // momentum solves the problem (J^T*J + lambda*I) x = J^T*r;
76 // the QR solver wants the square root of that lambda.
77 qrSolver_.reset(nSubsetParams, std::sqrt(regularization_));
78
79 double error_orig = 0.0;
80 const size_t numBlocks = this->solverFunction_->getJacobianBlockCount();
81
82 for (size_t blockIdx = 0; blockIdx < numBlocks; ++blockIdx) {
83 const size_t blockSize = this->solverFunction_->getJacobianBlockSize(blockIdx);
84 if (blockSize == 0) {
85 continue;
86 }
87
88 const auto paddedBlockSize = padToSimdAlignment(blockSize);
89 const auto rows = static_cast<Eigen::Index>(paddedBlockSize);
90
91 jacobian_.resizeAndSetZero(rows, nFullParams);
92 residual_.resizeAndSetZero(rows);
93
94 size_t usedRows = 0;
95 error_orig += this->solverFunction_->computeJacobianBlock(
96 this->parameters_, blockIdx, jacobian_.mat(), residual_.mat(), usedRows);
97
98 if (usedRows == 0) {
99 continue;
100 }
101
102 qrSolver_.addMutating(
103 ColumnIndexedMatrix<Eigen::MatrixX<T>>(
104 jacobian_.mat().topRows(usedRows), enabledParameters),
105 residual_.mat().topRows(usedRows));
106 }
107
108 // Finalize Jacobian computation
109 {
110 MT_PROFILE_EVENT("Finalize Jacobian computation");
111 this->solverFunction_->finalizeJacobianComputation();
112 }
113
114 this->error_ = error_orig;
115
116 const Eigen::VectorX<T> subsetDelta = qrSolver_.result();
117
118 Eigen::VectorX<T> searchDir = Eigen::VectorX<T>::Zero(nFullParams);
119 for (int iSubsetParam = 0; iSubsetParam < nSubsetParams; ++iSubsetParam) {
120 const auto iFullParam = enabledParameters[iSubsetParam];
121 searchDir(iFullParam) = subsetDelta(iSubsetParam);
122 }
123
124 if (doLineSearch_) {
125 const double innerProd = -qrSolver_.At_times_b().dot(subsetDelta);
126
127 // Line search:
128 const float c_1 = 1e-4f;
129 const float tau = 0.5f;
130 float alpha = 1.0f;
131
132 const Eigen::VectorX<T> parameters_orig = this->parameters_;
133 for (size_t kStep = 0; kStep < 10 && std::fpclassify(alpha) == FP_NORMAL; ++kStep) {
134 // update the this->parameters_
135 this->parameters_ = parameters_orig;
136 this->solverFunction_->updateParameters(this->parameters_, alpha * searchDir);
137
138 const double error_new = this->solverFunction_->getError(this->parameters_);
139
140 if ((error_orig - error_new) >= c_1 * alpha * -innerProd) {
141 break;
142 }
143
144 // Reduce step size:
145 alpha = alpha * tau;
146 }
147 } else {
148 this->solverFunction_->updateParameters(this->parameters_, searchDir);
149 }
150}
151
152template class GaussNewtonSolverQRT<float>;
153template class GaussNewtonSolverQRT<double>;
154
155} // namespace momentum
View over a matrix that exposes a (possibly reordered) subset of its columns.
void initializeSolver() final
Initializes solver state before optimization begins.
GaussNewtonSolverQRT(const SolverOptions &options, SolverFunctionT< T > *solver)
std::string_view getName() const override
Returns the name of the solver implementation.
void doIteration() final
Performs a single iteration of the optimization algorithm.
void setOptions(const SolverOptions &options) final
Updates solver configuration with new options.
Abstract base class for optimization objective functions.
virtual void setOptions(const SolverOptions &options)
Updates solver configuration with new options.
Definition solver.cpp:32
ParameterSet activeParameters_
Bitset indicating which parameters are enabled for optimization.
Definition solver.h:112
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
SolverT(const SolverOptions &options, SolverFunctionT< T > *solver)
Constructor with solver options and function to optimize.
Definition solver.cpp:20
constexpr size_t padToSimdAlignment(size_t size)
Pads a row count to be a multiple of kJacobianRowAlignment for SIMD alignment.
Profiling macros that dispatch to one of three backends, selected at compile time:
#define MT_PROFILE_EVENT(NAME)
Definition profile.h:112
#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.
Gauss-Newton solver with QR decomposition specific options.
Common options for numerical optimization solvers.
Definition solver.h:19