Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
momentum::SolverFunctionT< T > Class Template Referenceabstract

Abstract base class for optimization objective functions. More...

#include <solver_function.h>

Inheritance diagram for momentum::SolverFunctionT< T >:

Public Member Functions

virtual ~SolverFunctionT ()=default
virtual double getError (const VectorX< T > &parameters)=0
 Evaluates the objective function at the given parameter values.
virtual double getGradient (const VectorX< T > &parameters, VectorX< T > &gradient)=0
 Computes the gradient of the objective function.
double getJacobian (const VectorX< T > &parameters, MatrixX< T > &jacobian, VectorX< T > &residual, size_t &actualRows)
 Computes the Jacobian matrix for least squares problems.
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 double getJtJR_Sparse (const VectorX< T > &parameters, SparseMatrix< T > &jtj, VectorX< T > &jtr)
 Computes sparse JᵀJ and JᵀR for large-scale problems.
virtual void initializeJacobianComputation (const VectorX< T > &parameters)=0
 Initializes Jacobian computation for the given parameters.
virtual size_t getJacobianBlockCount () const =0
 Returns the number of Jacobian blocks available.
virtual size_t getJacobianBlockSize (size_t blockIndex) const =0
 Returns the maximum number of rows for a specific Jacobian block.
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 void finalizeJacobianComputation ()
 Optional cleanup after all Jacobian blocks have been computed.
virtual double getSolverDerivatives (const VectorX< T > &parameters, MatrixX< T > &hess, VectorX< T > &grad)
 Computes derivatives needed by the solver.
virtual void updateParameters (VectorX< T > &parameters, const VectorX< T > &gradient)=0
 Updates parameters using the computed step direction.
virtual void setEnabledParameters (const ParameterSet &parameterSet)
 Specifies which parameters should be optimized.
size_t getNumParameters () const
 Returns the total number of parameters in the optimization problem.
size_t getActualParameters () const
 Returns the number of parameters currently enabled for optimization.
virtual void storeHistory (std::unordered_map< std::string, MatrixX< T > > &history, size_t iteration, size_t maxIterations_)
 Records solver state for debugging and analysis.

Protected Attributes

size_t numParameters_ {}
 Total number of parameters in the optimization problem.
size_t actualParameters_ {}
 Number of parameters currently enabled for optimization.
ResizeableMatrix< T > tJacobian_
 Pre-allocated temporary storage for block-wise JtJ computation.
ResizeableMatrix< T > tResidual_

Detailed Description

template<typename T>
class momentum::SolverFunctionT< T >

Abstract base class for optimization objective functions.

Provides the interface for computing objective function values, gradients, and other derivatives needed by numerical optimization algorithms.

Definition at line 36 of file solver_function.h.

Constructor & Destructor Documentation

◆ ~SolverFunctionT()

template<typename T>
virtual momentum::SolverFunctionT< T >::~SolverFunctionT ( )
virtualdefault

Member Function Documentation

◆ computeJacobianBlock()

template<typename T>
virtual double momentum::SolverFunctionT< T >::computeJacobianBlock ( const VectorX< T > & parameters,
size_t blockIndex,
Eigen::Ref< MatrixX< T > > jacobianBlock,
Eigen::Ref< VectorX< T > > residualBlock,
size_t & actualRows )
pure virtual

Computes a specific Jacobian block.

Must be called after initializeJacobianComputation. The jacobianBlock and residualBlock parameters should be preallocated to the size returned by getJacobianBlockSize.

Parameters
parametersCurrent parameter values
blockIndexIndex of the block to compute (0 to getJacobianBlockCount()-1)
[out]jacobianBlockJacobian for this block (preallocated)
[out]residualBlockResidual for this block (preallocated)
[out]actualRowsNumber of active rows in this block
Returns
Error contribution from this block

Referenced by getJacobian(), and getJtJR().

◆ finalizeJacobianComputation()

template<typename T>
void momentum::SolverFunctionT< T >::finalizeJacobianComputation ( )
virtual

◆ getActualParameters()

template<typename T>
size_t momentum::SolverFunctionT< T >::getActualParameters ( ) const
nodiscard

Returns the number of parameters currently enabled for optimization.

Definition at line 154 of file solver_function.cpp.

References actualParameters_.

◆ getError()

template<typename T>
virtual double momentum::SolverFunctionT< T >::getError ( const VectorX< T > & parameters)
pure virtual

Evaluates the objective function at the given parameter values.

Parameters
parametersCurrent parameter values
Returns
Objective function value (typically sum of squared errors)

◆ getGradient()

template<typename T>
virtual double momentum::SolverFunctionT< T >::getGradient ( const VectorX< T > & parameters,
VectorX< T > & gradient )
pure virtual

Computes the gradient of the objective function.

Parameters
parametersCurrent parameter values
[out]gradientComputed gradient vector
Returns
Objective function value

◆ getHessian()

template<typename T>
void momentum::SolverFunctionT< T >::getHessian ( const VectorX< T > & parameters,
MatrixX< T > & hessian )
virtual

Computes the Hessian matrix of second derivatives.

Default implementation throws an exception as this is rarely needed

Parameters
parametersCurrent parameter values
[out]hessianComputed Hessian matrix

Definition at line 15 of file solver_function.cpp.

References MT_THROW.

◆ getJacobian()

template<typename T>
double momentum::SolverFunctionT< T >::getJacobian ( const VectorX< T > & parameters,
MatrixX< T > & jacobian,
VectorX< T > & residual,
size_t & actualRows )

Computes the Jacobian matrix for least squares problems.

Default implementation assembles the full Jacobian from blocks using the block-wise interface methods.

Parameters
parametersCurrent parameter values
[out]jacobianJacobian matrix (m×n for m residuals and n parameters)
[out]residualVector of residual values
[out]actualRowsNumber of active residual rows
Returns
Objective function value

Definition at line 22 of file solver_function.cpp.

References computeJacobianBlock(), finalizeJacobianComputation(), getJacobianBlockCount(), getJacobianBlockSize(), initializeJacobianComputation(), and momentum::padToSimdAlignment().

Referenced by momentum::d_modelParams_d_inputs().

◆ getJacobianBlockCount()

template<typename T>
virtual size_t momentum::SolverFunctionT< T >::getJacobianBlockCount ( ) const
nodiscardpure virtual

◆ getJacobianBlockSize()

template<typename T>
virtual size_t momentum::SolverFunctionT< T >::getJacobianBlockSize ( size_t blockIndex) const
nodiscardpure virtual

Returns the maximum number of rows for a specific Jacobian block.

This is the maximum size; actual rows may be fewer (returned via actualRows)

Parameters
blockIndexIndex of the block (0 to getJacobianBlockCount()-1)
Returns
Maximum number of rows for this block

Implemented in momentum::MultiposeSolverFunctionT< T >, momentum::MultiposeSolverFunctionT< double >, momentum::MultiposeSolverFunctionT< float >, momentum::SequenceSolverFunctionT< T >, momentum::SequenceSolverFunctionT< double >, momentum::SequenceSolverFunctionT< float >, momentum::SkeletonSolverFunctionT< T >, momentum::SkeletonSolverFunctionT< double >, and momentum::SkeletonSolverFunctionT< float >.

Referenced by getJacobian(), and getJtJR().

◆ getJtJR()

template<typename T>
double momentum::SolverFunctionT< T >::getJtJR ( const VectorX< T > & parameters,
MatrixX< T > & jtj,
VectorX< T > & jtr )
virtual

Computes JᵀJ and JᵀR for Gauss-Newton optimization.

Default implementation computes these from the Jacobian using the block-wise interface. Derived classes that don't support the block-wise Jacobian interface should override this method to compute JᵀJ and JᵀR directly.

Parameters
parametersCurrent parameter values
[out]jtjApproximated Hessian matrix (JᵀJ)
[out]jtrGradient vector (JᵀR)
Returns
Objective function value

Definition at line 74 of file solver_function.cpp.

References actualParameters_, computeJacobianBlock(), finalizeJacobianComputation(), getJacobianBlockCount(), getJacobianBlockSize(), initializeJacobianComputation(), momentum::padToSimdAlignment(), tJacobian_, and tResidual_.

Referenced by getSolverDerivatives().

◆ getJtJR_Sparse()

template<typename T>
double momentum::SolverFunctionT< T >::getJtJR_Sparse ( const VectorX< T > & parameters,
SparseMatrix< T > & jtj,
VectorX< T > & jtr )
virtual

Computes sparse JᵀJ and JᵀR for large-scale problems.

Default implementation returns 0.0 and must be overridden for sparse optimization

Parameters
parametersCurrent parameter values
[out]jtjSparse approximated Hessian matrix
[out]jtrGradient vector
Returns
Objective function value

Definition at line 124 of file solver_function.cpp.

◆ getNumParameters()

template<typename T>
size_t momentum::SolverFunctionT< T >::getNumParameters ( ) const
nodiscard

Returns the total number of parameters in the optimization problem.

Definition at line 149 of file solver_function.cpp.

References numParameters_.

◆ getSolverDerivatives()

template<typename T>
double momentum::SolverFunctionT< T >::getSolverDerivatives ( const VectorX< T > & parameters,
MatrixX< T > & hess,
VectorX< T > & grad )
virtual

Computes derivatives needed by the solver.

Default implementation calls getJtJR

Parameters
parametersCurrent parameter values
[out]hessHessian matrix or approximation
[out]gradGradient vector
Returns
Objective function value

Definition at line 137 of file solver_function.cpp.

References getJtJR().

◆ initializeJacobianComputation()

template<typename T>
virtual void momentum::SolverFunctionT< T >::initializeJacobianComputation ( const VectorX< T > & parameters)
pure virtual

Initializes Jacobian computation for the given parameters.

Handles expensive one-time setup that can be reused across multiple blocks (e.g., constructing SkeletonState). Must be called before iterating over blocks.

Parameters
parametersCurrent parameter values

Referenced by getJacobian(), and getJtJR().

◆ setEnabledParameters()

template<typename T>
void momentum::SolverFunctionT< T >::setEnabledParameters ( const ParameterSet & parameterSet)
virtual

◆ storeHistory()

template<typename T>
void momentum::SolverFunctionT< T >::storeHistory ( std::unordered_map< std::string, MatrixX< T > > & history,
size_t iteration,
size_t maxIterations_ )
virtual

Records solver state for debugging and analysis.

Parameters
[in,out]historyMap to store iteration data
iterationCurrent iteration number
maxIterationsMaximum number of iterations

Definition at line 159 of file solver_function.cpp.

◆ updateParameters()

template<typename T>
virtual void momentum::SolverFunctionT< T >::updateParameters ( VectorX< T > & parameters,
const VectorX< T > & gradient )
pure virtual

Updates parameters using the computed step direction.

Parameters
[in,out]parametersCurrent parameters, updated in-place
gradientStep direction (typically the negative gradient)

Member Data Documentation

◆ actualParameters_

◆ numParameters_

◆ tJacobian_

template<typename T>
ResizeableMatrix<T> momentum::SolverFunctionT< T >::tJacobian_
protected

Pre-allocated temporary storage for block-wise JtJ computation.

Uses ResizeableMatrix to avoid reallocations when size fluctuates

Definition at line 190 of file solver_function.h.

Referenced by getJtJR().

◆ tResidual_

template<typename T>
ResizeableMatrix<T> momentum::SolverFunctionT< T >::tResidual_
protected

Definition at line 191 of file solver_function.h.

Referenced by getJtJR().


The documentation for this class was generated from the following files: