Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
sequence_solver_base.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
13
14#include <algorithm>
15#include <memory>
16#include <vector>
17
18namespace momentum {
19
20namespace detail {
21
22template <typename ErrorFunctionType>
23size_t computeJacobianSize(const std::vector<std::shared_ptr<ErrorFunctionType>>& functions) {
24 size_t result = 0;
25 for (const auto& f : functions) {
26 if (f->getWeight() <= 0) {
27 continue;
28 }
29 result += f->getJacobianSize();
30 }
31 return result;
32}
33
34template <typename T>
35std::vector<Eigen::Index> buildSequenceColumnIndices(
37 size_t bandwidth) {
38 std::vector<Eigen::Index> result;
39 result.reserve(bandwidth * fn->getPerFrameParameterIndices().size());
40 for (size_t iSubFrame = 0; iSubFrame < bandwidth; ++iSubFrame) {
41 std::transform(
42 fn->getPerFrameParameterIndices().begin(),
44 std::back_inserter(result),
45 [offset = iSubFrame * (*fn->getParameterTransform()).numAllModelParameters()](
46 Eigen::Index kParam) -> Eigen::Index { return kParam + offset; });
47 }
48
49 return result;
50}
51
52} // namespace detail
53
57 float regularization = 0.05f;
58
60 bool doLineSearch = false;
61
63 bool multithreaded = false;
64
66 bool progressBar = false;
67
69
70 /* implicit */ SequenceSolverOptionsBase(const SolverOptions& baseOptions)
71 : SolverOptions(baseOptions) {
72 // Empty
73 }
74};
75
77template <typename T>
78class SequenceSolverBaseT : public SolverT<T> {
79 public:
81 : SolverT<T>(options, function) {
82 setOptions(options);
83 }
84
85 void setOptions(const SolverOptions& options) override {
87
88 if (const auto* sequenceOptions = dynamic_cast<const SequenceSolverOptionsBase*>(&options)) {
89 regularization_ = sequenceOptions->regularization;
90 doLineSearch_ = sequenceOptions->doLineSearch;
91 multithreaded_ = sequenceOptions->multithreaded;
92 progressBar_ = sequenceOptions->progressBar;
93 }
94 }
95
96 // "numParameters" has a different meaning here than in Solver.
97 // For Solver class, numParameters = ParameterTransform.numAllModelParameters() * numFrames; but
98 // it means #variables here. To hack around this discrepancy, we update numParameters when a new
99 // set of variables are defined.
100 // XXX To be fixed as part of T118191244
101 void setEnabledParameters(const ParameterSet& parameters) final {
103 this->numParameters_ = this->solverFunction_->getNumParameters();
104 }
105
106 protected:
107 float regularization_ = 0.05f;
108 bool doLineSearch_ = false;
109 bool multithreaded_ = false;
110 bool progressBar_ = false;
111
112 // bandwidth in frames:
113 size_t bandwidth_ = 0;
114};
115
118
119} // namespace momentum
Shared base class for sequence solvers.
SequenceSolverBaseT(const SolverOptions &options, SequenceSolverFunctionT< T > *function)
void setEnabledParameters(const ParameterSet &parameters) final
Specifies which parameters should be optimized.
void setOptions(const SolverOptions &options) override
Updates solver configuration with new options.
const std::vector< Eigen::Index > & getPerFrameParameterIndices() const
const ParameterTransformT< T > * getParameterTransform() const
virtual void setOptions(const SolverOptions &options)
Updates solver configuration with new options.
Definition solver.cpp:32
SolverFunctionT< float > * solverFunction_
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
size_t computeJacobianSize(const std::vector< std::shared_ptr< ErrorFunctionType > > &functions)
std::vector< Eigen::Index > buildSequenceColumnIndices(const SequenceSolverFunctionT< T > *fn, size_t bandwidth)
SequenceSolverBaseT< double > SequenceSolverBased
Definition fwd.h:291
SequenceSolverBaseT< float > SequenceSolverBase
Definition fwd.h:290
std::bitset< kMaxModelParams > ParameterSet
Bitset for model parameter enablement.
Definition types.h:430
Shared options for sequence solvers.
bool progressBar
Flag to enable a progress bar during optimization.
bool doLineSearch
Flag to enable line search during optimization.
SequenceSolverOptionsBase(const SolverOptions &baseOptions)
float regularization
Regularization parameter used by the linearized sequence solve.
bool multithreaded
Flag to enable multithreaded sequence processing.
Common options for numerical optimization solvers.
Definition solver.h:19