WPILibC++ 2027.0.0-alpha-2
Loading...
Searching...
No Matches
hessian.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>
9
15
16namespace slp {
17
18/**
19 * This class calculates the Hessian of a variable with respect to a vector of
20 * variables.
21 *
22 * The gradient tree is cached so subsequent Hessian calculations are faster,
23 * and the Hessian is only recomputed if the variable expression is nonlinear.
24 *
25 * @tparam UpLo Which part of the Hessian to compute (Lower or Lower | Upper).
26 */
27template <int UpLo>
28 requires(UpLo == Eigen::Lower) || (UpLo == (Eigen::Lower | Eigen::Upper))
30 public:
31 /**
32 * Constructs a Hessian object.
33 *
34 * @param variable Variable of which to compute the Hessian.
35 * @param wrt Variable with respect to which to compute the Hessian.
36 */
37 Hessian(Variable variable, Variable wrt) noexcept
38 : Hessian{std::move(variable), VariableMatrix{std::move(wrt)}} {}
39
40 /**
41 * Constructs a Hessian object.
42 *
43 * @param variable Variable of which to compute the Hessian.
44 * @param wrt Vector of variables with respect to which to compute the
45 * Hessian.
46 */
47 Hessian(Variable variable, SleipnirMatrixLike auto wrt) noexcept
48 : m_variables{detail::AdjointExpressionGraph{variable}
50 m_wrt{wrt} {
51 // Initialize column each expression's adjoint occupies in the Jacobian
52 for (size_t col = 0; col < m_wrt.size(); ++col) {
53 m_wrt[col].expr->col = col;
54 }
55
56 for (auto& variable : m_variables) {
57 m_graphs.emplace_back(variable);
58 }
59
60 // Reset col to -1
61 for (auto& node : m_wrt) {
62 node.expr->col = -1;
63 }
64
65 for (int row = 0; row < m_variables.rows(); ++row) {
66 if (m_variables[row].expr == nullptr) {
67 continue;
68 }
69
70 if (m_variables[row].type() == ExpressionType::LINEAR) {
71 // If the row is linear, compute its gradient once here and cache its
72 // triplets. Constant rows are ignored because their gradients have no
73 // nonzero triplets.
74 m_graphs[row].append_adjoint_triplets(m_cached_triplets, row, m_wrt);
75 } else if (m_variables[row].type() > ExpressionType::LINEAR) {
76 // If the row is quadratic or nonlinear, add it to the list of nonlinear
77 // rows to be recomputed in Value().
78 m_nonlinear_rows.emplace_back(row);
79 }
80 }
81
82 if (m_nonlinear_rows.empty()) {
83 m_H.setFromTriplets(m_cached_triplets.begin(), m_cached_triplets.end());
84 if constexpr (UpLo == Eigen::Lower) {
85 m_H = m_H.triangularView<Eigen::Lower>();
86 }
87 }
88 }
89
90 /**
91 * Returns the Hessian as a VariableMatrix.
92 *
93 * This is useful when constructing optimization problems with derivatives in
94 * them.
95 *
96 * @return The Hessian as a VariableMatrix.
97 */
99 VariableMatrix result{VariableMatrix::empty, m_variables.rows(),
100 m_wrt.rows()};
101
102 for (int row = 0; row < m_variables.rows(); ++row) {
103 auto grad = m_graphs[row].generate_gradient_tree(m_wrt);
104 for (int col = 0; col < m_wrt.rows(); ++col) {
105 if (grad[col].expr != nullptr) {
106 result(row, col) = std::move(grad[col]);
107 } else {
108 result(row, col) = Variable{0.0};
109 }
110 }
111 }
112
113 return result;
114 }
115
116 /**
117 * Evaluates the Hessian at wrt's value.
118 *
119 * @return The Hessian at wrt's value.
120 */
121 const Eigen::SparseMatrix<double>& value() {
122 if (m_nonlinear_rows.empty()) {
123 return m_H;
124 }
125
126 for (auto& graph : m_graphs) {
127 graph.update_values();
128 }
129
130 // Copy the cached triplets so triplets added for the nonlinear rows are
131 // thrown away at the end of the function
132 auto triplets = m_cached_triplets;
133
134 // Compute each nonlinear row of the Hessian
135 for (int row : m_nonlinear_rows) {
136 m_graphs[row].append_adjoint_triplets(triplets, row, m_wrt);
137 }
138
139 if (!triplets.empty()) {
140 m_H.setFromTriplets(triplets.begin(), triplets.end());
141 if constexpr (UpLo == Eigen::Lower) {
142 m_H = m_H.triangularView<Eigen::Lower>();
143 }
144 } else {
145 // setFromTriplets() is a no-op on empty triplets, so explicitly zero out
146 // the storage
147 m_H.setZero();
148 }
149
150 return m_H;
151 }
152
153 private:
154 VariableMatrix m_variables;
155 VariableMatrix m_wrt;
156
158
159 Eigen::SparseMatrix<double> m_H{m_variables.rows(), m_wrt.rows()};
160
161 // Cached triplets for gradients of linear rows
163
164 // List of row indices for nonlinear rows whose graients will be computed in
165 // Value()
166 gch::small_vector<int> m_nonlinear_rows;
167};
168
169} // namespace slp
This class calculates the Hessian of a variable with respect to a vector of variables.
Definition hessian.hpp:29
Hessian(Variable variable, SleipnirMatrixLike auto wrt) noexcept
Constructs a Hessian object.
Definition hessian.hpp:47
Hessian(Variable variable, Variable wrt) noexcept
Constructs a Hessian object.
Definition hessian.hpp:37
VariableMatrix get() const
Returns the Hessian as a VariableMatrix.
Definition hessian.hpp:98
const Eigen::SparseMatrix< double > & value()
Evaluates the Hessian at wrt's value.
Definition hessian.hpp:121
An autodiff variable pointing to an expression node.
Definition variable.hpp:40
A matrix of autodiff variables.
Definition variable_matrix.hpp:29
int rows() const
Returns the number of rows in the matrix.
Definition variable_matrix.hpp:914
static constexpr empty_t empty
Designates an uninitialized VariableMatrix.
Definition variable_matrix.hpp:39
This class is an adaptor type that performs value updates of an expression's adjoint graph.
Definition adjoint_expression_graph.hpp:21
VariableMatrix generate_gradient_tree(const VariableMatrix &wrt) const
Returns the variable's gradient tree.
Definition adjoint_expression_graph.hpp:52
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition SmallVector.h:1198
Definition concepts.hpp:30
Definition expression_graph.hpp:11
@ LINEAR
The expression is composed of linear and lower-order operators.
#define SLEIPNIR_DLLEXPORT
Definition symbol_exports.hpp:34