WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
Jacobian.hpp
Go to the documentation of this file.
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <utility>
6
7#include <Eigen/SparseCore>
8#include <wpi/SmallVector.h>
9
15
16namespace sleipnir {
17
18/**
19 * This class calculates the Jacobian of a vector of variables with respect to a
20 * vector of variables.
21 *
22 * The Jacobian is only recomputed if the variable expression is quadratic or
23 * higher order.
24 */
26 public:
27 /**
28 * Constructs a Jacobian object.
29 *
30 * @param variables Vector of variables of which to compute the Jacobian.
31 * @param wrt Vector of variables with respect to which to compute the
32 * Jacobian.
33 */
34 Jacobian(const VariableMatrix& variables, const VariableMatrix& wrt) noexcept
35 : m_variables{std::move(variables)}, m_wrt{std::move(wrt)} {
36 m_profiler.StartSetup();
37
38 for (int row = 0; row < m_wrt.Rows(); ++row) {
39 m_wrt(row).expr->row = row;
40 }
41
42 for (Variable variable : m_variables) {
43 m_graphs.emplace_back(variable.expr);
44 }
45
46 // Reserve triplet space for 99% sparsity
47 m_cachedTriplets.reserve(m_variables.Rows() * m_wrt.Rows() * 0.01);
48
49 for (int row = 0; row < m_variables.Rows(); ++row) {
50 if (m_variables(row).Type() == ExpressionType::kLinear) {
51 // If the row is linear, compute its gradient once here and cache its
52 // triplets. Constant rows are ignored because their gradients have no
53 // nonzero triplets.
54 m_graphs[row].ComputeAdjoints([&](int col, double adjoint) {
55 m_cachedTriplets.emplace_back(row, col, adjoint);
56 });
57 } else if (m_variables(row).Type() > ExpressionType::kLinear) {
58 // If the row is quadratic or nonlinear, add it to the list of nonlinear
59 // rows to be recomputed in Value().
60 m_nonlinearRows.emplace_back(row);
61 }
62 }
63
64 for (int row = 0; row < m_wrt.Rows(); ++row) {
65 m_wrt(row).expr->row = -1;
66 }
67
68 if (m_nonlinearRows.empty()) {
69 m_J.setFromTriplets(m_cachedTriplets.begin(), m_cachedTriplets.end());
70 }
71
72 m_profiler.StopSetup();
73 }
74
75 /**
76 * Returns the Jacobian as a VariableMatrix.
77 *
78 * This is useful when constructing optimization problems with derivatives in
79 * them.
80 */
82 VariableMatrix result{m_variables.Rows(), m_wrt.Rows()};
83
85 wrtVec.reserve(m_wrt.size());
86 for (auto& elem : m_wrt) {
87 wrtVec.emplace_back(elem.expr);
88 }
89
90 for (int row = 0; row < m_variables.Rows(); ++row) {
91 auto grad = m_graphs[row].GenerateGradientTree(wrtVec);
92 for (int col = 0; col < m_wrt.Rows(); ++col) {
93 result(row, col) = Variable{std::move(grad[col])};
94 }
95 }
96
97 return result;
98 }
99
100 /**
101 * Evaluates the Jacobian at wrt's value.
102 */
103 const Eigen::SparseMatrix<double>& Value() {
104 if (m_nonlinearRows.empty()) {
105 return m_J;
106 }
107
108 m_profiler.StartSolve();
109
110 for (auto& graph : m_graphs) {
111 graph.Update();
112 }
113
114 // Copy the cached triplets so triplets added for the nonlinear rows are
115 // thrown away at the end of the function
116 auto triplets = m_cachedTriplets;
117
118 // Compute each nonlinear row of the Jacobian
119 for (int row : m_nonlinearRows) {
120 m_graphs[row].ComputeAdjoints([&](int col, double adjoint) {
121 triplets.emplace_back(row, col, adjoint);
122 });
123 }
124
125 m_J.setFromTriplets(triplets.begin(), triplets.end());
126
127 m_profiler.StopSolve();
128
129 return m_J;
130 }
131
132 /**
133 * Returns the profiler.
134 */
135 Profiler& GetProfiler() { return m_profiler; }
136
137 private:
138 VariableMatrix m_variables;
139 VariableMatrix m_wrt;
140
142
143 Eigen::SparseMatrix<double> m_J{m_variables.Rows(), m_wrt.Rows()};
144
145 // Cached triplets for gradients of linear rows
147
148 // List of row indices for nonlinear rows whose graients will be computed in
149 // Value()
150 wpi::SmallVector<int> m_nonlinearRows;
151
152 Profiler m_profiler;
153};
154
155} // namespace sleipnir
This file defines the SmallVector class.
#define SLEIPNIR_DLLEXPORT
Definition SymbolExports.hpp:34
This class calculates the Jacobian of a vector of variables with respect to a vector of variables.
Definition Jacobian.hpp:25
VariableMatrix Get() const
Returns the Jacobian as a VariableMatrix.
Definition Jacobian.hpp:81
Profiler & GetProfiler()
Returns the profiler.
Definition Jacobian.hpp:135
const Eigen::SparseMatrix< double > & Value()
Evaluates the Jacobian at wrt's value.
Definition Jacobian.hpp:103
Jacobian(const VariableMatrix &variables, const VariableMatrix &wrt) noexcept
Constructs a Jacobian object.
Definition Jacobian.hpp:34
Records the number of profiler measurements (start/stop pairs) and the average duration between each ...
Definition Profiler.hpp:15
An autodiff variable pointing to an expression node.
Definition Variable.hpp:31
A matrix of autodiff variables.
Definition VariableMatrix.hpp:28
int Rows() const
Returns number of rows in the matrix.
Definition VariableMatrix.hpp:753
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition SmallVector.h:1212
reference emplace_back(ArgTypes &&... Args)
Definition SmallVector.h:953
void reserve(size_type N)
Definition SmallVector.h:679
Definition Hessian.hpp:18