blob: 2e5b1dd590a011248beb59e85995799fe8cb54c1 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
Austin Schuh3de38b02024-06-25 18:25:10 -07002// Copyright 2024 Google Inc. All rights reserved.
Austin Schuh70cc9552019-01-21 19:46:48 -08003// http://ceres-solver.org/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7//
8// * Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13// * Neither the name of Google Inc. nor the names of its contributors may be
14// used to endorse or promote products derived from this software without
15// specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27// POSSIBILITY OF SUCH DAMAGE.
28//
29// Author: sameeragarwal@google.com (Sameer Agarwal)
30// keir@google.m (Keir Mierle)
31//
32// This is the interface through which the least squares solver accesses the
33// residual and Jacobian of the least squares problem. Users are expected to
34// subclass CostFunction to define their own terms in the least squares problem.
35//
36// It is recommended that users define templated residual functors for use as
37// arguments for AutoDiffCostFunction (see autodiff_cost_function.h), instead of
38// directly implementing the CostFunction interface. This often results in both
39// shorter code and faster execution than hand-coded derivatives. However,
40// specialized cases may demand direct implementation of the lower-level
41// CostFunction interface; for example, this is true when calling legacy code
42// which is not templated on numeric types.
43
44#ifndef CERES_PUBLIC_COST_FUNCTION_H_
45#define CERES_PUBLIC_COST_FUNCTION_H_
46
47#include <cstdint>
48#include <vector>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080049
Austin Schuh70cc9552019-01-21 19:46:48 -080050#include "ceres/internal/disable_warnings.h"
Austin Schuh3de38b02024-06-25 18:25:10 -070051#include "ceres/internal/export.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080052
53namespace ceres {
54
55// This class implements the computation of the cost (a.k.a. residual) terms as
56// a function of the input (control) variables, and is the interface for users
57// to describe their least squares problem to Ceres. In other words, this is the
58// modeling layer between users and the Ceres optimizer. The signature of the
59// function (number and sizes of input parameter blocks and number of outputs)
60// is stored in parameter_block_sizes_ and num_residuals_ respectively. User
61// code inheriting from this class is expected to set these two members with the
62// corresponding accessors. This information will be verified by the Problem
63// when added with AddResidualBlock().
64class CERES_EXPORT CostFunction {
65 public:
Austin Schuh3de38b02024-06-25 18:25:10 -070066 CostFunction();
Austin Schuh70cc9552019-01-21 19:46:48 -080067 CostFunction(const CostFunction&) = delete;
Austin Schuh3de38b02024-06-25 18:25:10 -070068 CostFunction& operator=(const CostFunction&) = delete;
Austin Schuh70cc9552019-01-21 19:46:48 -080069
Austin Schuh3de38b02024-06-25 18:25:10 -070070 virtual ~CostFunction();
Austin Schuh70cc9552019-01-21 19:46:48 -080071
72 // Inputs:
73 //
74 // parameters is an array of pointers to arrays containing the
75 // various parameter blocks. parameters has the same number of
76 // elements as parameter_block_sizes_. Parameter blocks are in the
77 // same order as parameter_block_sizes_.i.e.,
78 //
79 // parameters_[i] = double[parameter_block_sizes_[i]]
80 //
81 // Outputs:
82 //
83 // residuals is an array of size num_residuals_.
84 //
85 // jacobians is an array of size parameter_block_sizes_ containing
86 // pointers to storage for jacobian blocks corresponding to each
87 // parameter block. Jacobian blocks are in the same order as
88 // parameter_block_sizes, i.e. jacobians[i], is an
89 // array that contains num_residuals_* parameter_block_sizes_[i]
90 // elements. Each jacobian block is stored in row-major order, i.e.,
91 //
92 // jacobians[i][r*parameter_block_size_[i] + c] =
93 // d residual[r] / d parameters[i][c]
94 //
Austin Schuh3de38b02024-06-25 18:25:10 -070095 // If jacobians is nullptr, then no derivatives are returned; this is
96 // the case when computing cost only. If jacobians[i] is nullptr, then
Austin Schuh70cc9552019-01-21 19:46:48 -080097 // the jacobian block corresponding to the i'th parameter block must
98 // not to be returned.
99 //
100 // The return value indicates whether the computation of the
101 // residuals and/or jacobians was successful or not.
102 //
103 // This can be used to communicate numerical failures in jacobian
104 // computations for instance.
105 //
106 // A more interesting and common use is to impose constraints on the
107 // parameters. If the initial values of the parameter blocks satisfy
108 // the constraints, then returning false whenever the constraints
109 // are not satisfied will prevent the solver from moving into the
110 // infeasible region. This is not a very sophisticated mechanism for
111 // enforcing constraints, but is often good enough.
112 //
113 // Note that it is important that the initial values of the
114 // parameter block must be feasible, otherwise the solver will
115 // declare a numerical problem at iteration 0.
116 virtual bool Evaluate(double const* const* parameters,
117 double* residuals,
118 double** jacobians) const = 0;
119
120 const std::vector<int32_t>& parameter_block_sizes() const {
121 return parameter_block_sizes_;
122 }
123
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800124 int num_residuals() const { return num_residuals_; }
Austin Schuh70cc9552019-01-21 19:46:48 -0800125
126 protected:
Austin Schuh3de38b02024-06-25 18:25:10 -0700127 // Prevent moving through the base class
128 CostFunction(CostFunction&& other) noexcept;
129 CostFunction& operator=(CostFunction&& other) noexcept;
130
Austin Schuh70cc9552019-01-21 19:46:48 -0800131 std::vector<int32_t>* mutable_parameter_block_sizes() {
132 return &parameter_block_sizes_;
133 }
134
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800135 void set_num_residuals(int num_residuals) { num_residuals_ = num_residuals; }
Austin Schuh70cc9552019-01-21 19:46:48 -0800136
137 private:
138 // Cost function signature metadata: number of inputs & their sizes,
139 // number of outputs (residuals).
140 std::vector<int32_t> parameter_block_sizes_;
141 int num_residuals_;
142};
143
144} // namespace ceres
145
146#include "ceres/internal/reenable_warnings.h"
147
148#endif // CERES_PUBLIC_COST_FUNCTION_H_