Skip to content

Commit e67a2df

Browse files
committed
Add docstring and comments for hessian related function
1 parent baa153c commit e67a2df

File tree

3 files changed

+90
-5
lines changed

3 files changed

+90
-5
lines changed

Diff for: ifopt_core/include/ifopt/constraint_set.h

+36-1
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,43 @@ class ConstraintSet : public Component {
107107
virtual void FillJacobianBlock(std::string var_set,
108108
Jacobian& jac_block) const = 0;
109109

110-
110+
/**
111+
* @brief The second-order derivatives (Hessians) for these constraints and variables.
112+
*
113+
* This function returns a pair containing two vectors. The first vector holds
114+
* the indices of the corresponding constraints or costs, while the second vector
115+
* contains the Hessian matrices as sparse representations.
116+
*
117+
* Assuming @c n constraints or costs and @c m variables, each Hessian matrix
118+
* in the second vector has dimensions m x m, representing the second-order
119+
* derivatives of a specific constraint or cost function with respect to the
120+
* optimization variables.
121+
*
122+
* This function only combines the user-defined Hessians from
123+
* FillHessianTriplets().
124+
*/
111125
RowIndicesHessiansPair GetHessians() const final;
126+
127+
/**
128+
* @brief Set individual Hessians corresponding to each constraint or cost function.
129+
* @param var_set_list The full set of variables involved in Hessian computation.
130+
* @param hessian_row_index_list Indices of the corresponding constraints or costs.
131+
* @param triplets_list Nonzero elements of each Hessian, stored as triplets.
132+
*
133+
* This function provides the second-order derivatives (Hessians) of constraints
134+
* or costs with respect to the optimization variables. Unlike the Jacobian, Hessians
135+
* involve cross-derivatives between different variable sets, so the full variable
136+
* set is provided.
137+
*
138+
* The Hessians are stored efficiently in triplet form to minimize memory usage,
139+
* as Hessian matrices are typically much sparser than Jacobians. Each entry in
140+
* @c triplets_list corresponds to a specific constraint or cost function, with
141+
* its nonzero elements stored as Eigen triplets.
142+
*
143+
* If a constraint or cost does not require a Hessian, it should simply be omitted
144+
* from @c hessian_row_index_list and @c triplets_list. The missing entries will
145+
* be treated as empty Hessians.
146+
*/
112147
virtual void FillHessianTriplets(std::vector<std::string> var_set_list,
113148
std::vector<int>& hessian_row_index_list,
114149
std::vector<HessianTriplet>& triplets_list) const

Diff for: ifopt_core/include/ifopt/problem.h

+49-4
Original file line numberDiff line numberDiff line change
@@ -218,16 +218,61 @@ class Problem {
218218
Jacobian GetJacobianOfCosts() const;
219219

220220
/**
221-
* @brief Extracts those entries from total hessian (combination of hessian from cost and constraints) that are not zero.
221+
* @brief Computes the nonzero entries of the total Hessian after applying scaling factors.
222+
*
223+
* This function extracts the nonzero elements from the total Hessian, which is
224+
* the combination of the Hessians from both cost functions and constraints, and
225+
* applies the appropriate scaling factors.
226+
*
227+
* The Hessian of the cost function is multiplied by @c obj_factor, while the
228+
* Hessians of the constraints are weighted by their corresponding Lagrange
229+
* multipliers in @c lambda.
230+
*
222231
* @param [in] x The current values of the optimization variables.
223-
* @param [in] obj_factor The scaling multiplier for the hessian of the objective function(cost).
224-
* @param [in] lambda Lagrange Multipliers of the constraints.
225-
* @param [out] values The nonzero derivatives ordered by Eigen::RowMajor.
232+
* @param [in] obj_factor The scaling multiplier for the Hessian of the objective function (cost).
233+
* @param [in] lambda The Lagrange multipliers for the constraints.
234+
* @param [out] values The nonzero second-order derivatives, ordered in @c Eigen::RowMajor format.
226235
*/
227236
void EvalNonzerosOfHessian(const double* x, double obj_factor, const double* lambda, double* values);
228237

238+
/**
239+
* @brief Computes the full Hessian by summing the Hessians of costs and constraints.
240+
*
241+
* This function returns the total Hessian, which combines the second-order
242+
* derivatives of both cost functions and constraints with respect to the
243+
* optimization variables.
244+
*
245+
* The primary purpose of this function is to determine the structure of the
246+
* overall Hessian, including the number and positions of nonzero elements.
247+
* The resulting matrix is stored in sparse format for efficiency.
248+
*/
229249
Hessian GetTotalHessian() const;
250+
251+
/**
252+
* @brief The sparse-matrix representation of the Hessians of the constraints.
253+
*
254+
* This function returns a pair of vectors, where the first vector contains
255+
* the indices of the corresponding constraints, and the second vector holds
256+
* the Hessian matrices in sparse format.
257+
*
258+
* Each Hessian matrix represents the second-order derivatives of a constraint
259+
* with respect to the optimization variables. The Hessians are stored sparsely
260+
* to optimize memory usage, as they are typically very sparse.
261+
*/
230262
RowIndicesHessiansPair GetHessianOfConstraints() const;
263+
264+
265+
/**
266+
* @brief The sparse-matrix representation of the Hessians of the cost functions.
267+
*
268+
* This function returns a pair of vectors, where the first vector contains
269+
* the indices of the corresponding cost functions, and the second vector holds
270+
* the Hessian matrices in sparse format.
271+
*
272+
* Each Hessian matrix represents the second-order derivatives of a cost function
273+
* with respect to the optimization variables. The Hessians are stored sparsely
274+
* to optimize memory usage, as they are typically very sparse.
275+
*/
231276
RowIndicesHessiansPair GetHessianOfCosts() const;
232277

233278
/**

Diff for: ifopt_core/test/ifopt/test_vars_constr_cost.h

+5
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ class ExConstraint : public ConstraintSet {
149149
std::vector<int>& hessian_row_index_list,
150150
std::vector<HessianTriplet>& triplets_list) const override
151151
{
152+
// only the upper triangular elements need to be provided, as the Hessian
153+
// is symmetric and internally only these values are used.
154+
152155
// only 1 constraint in this problem
153156
HessianTriplet constraint1_triplets;
154157
for (int i = 0; i < (int)var_set_list.size(); ++i) {
@@ -188,6 +191,8 @@ class ExCost : public CostTerm {
188191
std::vector<int>& hessian_row_index_list,
189192
std::vector<HessianTriplet>& triplets_list) const override
190193
{
194+
// only the upper triangular elements need to be provided, as the Hessian
195+
// is symmetric and internally only these values are used.
191196
HessianTriplet cost_triplets;
192197
for (int i = 0; i < (int)var_set_list.size(); ++i) {
193198
const std::string& var_set = var_set_list[i];

0 commit comments

Comments
 (0)