Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
solver_function.h
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
8#pragma once
9
11#include <momentum/math/types.h>
12#include <momentum/solver/fwd.h>
13
14#include <unordered_map>
15
16namespace momentum {
17
19inline constexpr size_t kJacobianRowAlignment = 8;
20
27[[nodiscard]] constexpr size_t padToSimdAlignment(size_t size) {
28 return (size + kJacobianRowAlignment - 1) & ~(kJacobianRowAlignment - 1);
29}
30
35template <typename T>
37 public:
38 virtual ~SolverFunctionT() = default;
39
44 virtual double getError(const VectorX<T>& parameters) = 0;
45
51 virtual double getGradient(const VectorX<T>& parameters, VectorX<T>& gradient) = 0;
52
63 const VectorX<T>& parameters,
64 MatrixX<T>& jacobian,
65 VectorX<T>& residual,
66 size_t& actualRows);
67
73 virtual void getHessian(const VectorX<T>& parameters, MatrixX<T>& hessian);
74
84 virtual double getJtJR(const VectorX<T>& parameters, MatrixX<T>& jtj, VectorX<T>& jtr);
85
93 virtual double
94 getJtJR_Sparse(const VectorX<T>& parameters, SparseMatrix<T>& jtj, VectorX<T>& jtr);
95
101 virtual void initializeJacobianComputation(const VectorX<T>& parameters) = 0;
102
107 [[nodiscard]] virtual size_t getJacobianBlockCount() const = 0;
108
114 [[nodiscard]] virtual size_t getJacobianBlockSize(size_t blockIndex) const = 0;
115
127 virtual double computeJacobianBlock(
128 const VectorX<T>& parameters,
129 size_t blockIndex,
130 Eigen::Ref<MatrixX<T>> jacobianBlock,
131 Eigen::Ref<VectorX<T>> residualBlock,
132 size_t& actualRows) = 0;
133
139
147 virtual double
148 getSolverDerivatives(const VectorX<T>& parameters, MatrixX<T>& hess, VectorX<T>& grad);
149
154 virtual void updateParameters(VectorX<T>& parameters, const VectorX<T>& gradient) = 0;
155
160 virtual void setEnabledParameters(const ParameterSet& parameterSet);
161
163 [[nodiscard]] size_t getNumParameters() const;
164
166 [[nodiscard]] size_t getActualParameters() const;
167
173 virtual void storeHistory(
174 std::unordered_map<std::string, MatrixX<T>>& history,
175 size_t iteration,
176 size_t maxIterations_);
177
178 protected:
181
186
192};
193
194} // namespace momentum
A dense column-major matrix whose underlying storage capacity grows monotonically and is reused acros...
Abstract base class for optimization objective functions.
double getJacobian(const VectorX< T > &parameters, MatrixX< T > &jacobian, VectorX< T > &residual, size_t &actualRows)
Computes the Jacobian matrix for least squares problems.
size_t getActualParameters() const
Returns the number of parameters currently enabled for optimization.
size_t getNumParameters() const
Returns the total number of parameters in the optimization problem.
virtual double computeJacobianBlock(const VectorX< T > &parameters, size_t blockIndex, Eigen::Ref< MatrixX< T > > jacobianBlock, Eigen::Ref< VectorX< T > > residualBlock, size_t &actualRows)=0
Computes a specific Jacobian block.
virtual double getSolverDerivatives(const VectorX< T > &parameters, MatrixX< T > &hess, VectorX< T > &grad)
Computes derivatives needed by the solver.
virtual double getJtJR_Sparse(const VectorX< T > &parameters, SparseMatrix< T > &jtj, VectorX< T > &jtr)
Computes sparse JᵀJ and JᵀR for large-scale problems.
ResizeableMatrix< float > tResidual_
virtual void updateParameters(VectorX< T > &parameters, const VectorX< T > &gradient)=0
Updates parameters using the computed step direction.
virtual double getError(const VectorX< T > &parameters)=0
Evaluates the objective function at the given parameter values.
virtual void initializeJacobianComputation(const VectorX< T > &parameters)=0
Initializes Jacobian computation for the given parameters.
virtual void getHessian(const VectorX< T > &parameters, MatrixX< T > &hessian)
Computes the Hessian matrix of second derivatives.
virtual double getJtJR(const VectorX< T > &parameters, MatrixX< T > &jtj, VectorX< T > &jtr)
Computes JᵀJ and JᵀR for Gauss-Newton optimization.
virtual void setEnabledParameters(const ParameterSet &parameterSet)
Specifies which parameters should be optimized.
virtual size_t getJacobianBlockCount() const =0
Returns the number of Jacobian blocks available.
virtual void storeHistory(std::unordered_map< std::string, MatrixX< T > > &history, size_t iteration, size_t maxIterations_)
Records solver state for debugging and analysis.
virtual ~SolverFunctionT()=default
virtual double getGradient(const VectorX< T > &parameters, VectorX< T > &gradient)=0
Computes the gradient of the objective function.
virtual void finalizeJacobianComputation()
Optional cleanup after all Jacobian blocks have been computed.
ResizeableMatrix< float > tJacobian_
virtual size_t getJacobianBlockSize(size_t blockIndex) const =0
Returns the maximum number of rows for a specific Jacobian block.
constexpr size_t padToSimdAlignment(size_t size)
Pads a row count to be a multiple of kJacobianRowAlignment for SIMD alignment.
constexpr size_t kJacobianRowAlignment
Alignment for SIMD operations on Jacobian rows (8 floats = 32 bytes for AVX).
Eigen::Matrix< T, Dynamic, Dynamic > MatrixX
Dynamic-size, column-major dense matrix.
Definition types.h:80
Vector< T, Dynamic > VectorX
Dynamic-size column vector. Common return type for solver residuals and parameter vectors.
Definition types.h:207
Eigen::SparseMatrix< T > SparseMatrix
Column-major sparse matrix.
Definition types.h:183
std::bitset< kMaxModelParams > ParameterSet
Bitset for model parameter enablement.
Definition types.h:430