blob: 49d605ea2d6ff0d92b97d6afcb80e35b477045d3 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
Austin Schuh1d1e6ea2020-12-23 21:56:30 -08002// Copyright 2019 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
31#ifndef CERES_PUBLIC_GRADIENT_PROBLEM_H_
32#define CERES_PUBLIC_GRADIENT_PROBLEM_H_
33
34#include <memory>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080035
36#include "ceres/first_order_function.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080037#include "ceres/internal/port.h"
38#include "ceres/local_parameterization.h"
39
40namespace ceres {
41
42class FirstOrderFunction;
43
44// Instances of GradientProblem represent general non-linear
45// optimization problems that must be solved using just the value of
46// the objective function and its gradient. Unlike the Problem class,
47// which can only be used to model non-linear least squares problems,
48// instances of GradientProblem not restricted in the form of the
49// objective function.
50//
51// Structurally GradientProblem is a composition of a
52// FirstOrderFunction and optionally a LocalParameterization.
53//
54// The FirstOrderFunction is responsible for evaluating the cost and
55// gradient of the objective function.
56//
57// The LocalParameterization is responsible for going back and forth
58// between the ambient space and the local tangent space. (See
59// local_parameterization.h for more details). When a
60// LocalParameterization is not provided, then the tangent space is
61// assumed to coincide with the ambient Euclidean space that the
62// gradient vector lives in.
63//
64// Example usage:
65//
66// The following demonstrate the problem construction for Rosenbrock's function
67//
68// f(x,y) = (1-x)^2 + 100(y - x^2)^2;
69//
70// class Rosenbrock : public ceres::FirstOrderFunction {
71// public:
72// virtual ~Rosenbrock() {}
73//
74// virtual bool Evaluate(const double* parameters,
75// double* cost,
76// double* gradient) const {
77// const double x = parameters[0];
78// const double y = parameters[1];
79//
80// cost[0] = (1.0 - x) * (1.0 - x) + 100.0 * (y - x * x) * (y - x * x);
81// if (gradient != NULL) {
82// gradient[0] = -2.0 * (1.0 - x) - 200.0 * (y - x * x) * 2.0 * x;
83// gradient[1] = 200.0 * (y - x * x);
84// }
85// return true;
86// };
87//
88// virtual int NumParameters() const { return 2; };
89// };
90//
91// ceres::GradientProblem problem(new Rosenbrock());
92class CERES_EXPORT GradientProblem {
93 public:
94 // Takes ownership of the function.
95 explicit GradientProblem(FirstOrderFunction* function);
96
97 // Takes ownership of the function and the parameterization.
98 GradientProblem(FirstOrderFunction* function,
99 LocalParameterization* parameterization);
100
101 int NumParameters() const;
102 int NumLocalParameters() const;
103
104 // This call is not thread safe.
105 bool Evaluate(const double* parameters, double* cost, double* gradient) const;
106 bool Plus(const double* x, const double* delta, double* x_plus_delta) const;
107
108 private:
109 std::unique_ptr<FirstOrderFunction> function_;
110 std::unique_ptr<LocalParameterization> parameterization_;
111 std::unique_ptr<double[]> scratch_;
112};
113
Austin Schuh70cc9552019-01-21 19:46:48 -0800114} // namespace ceres
115
116#endif // CERES_PUBLIC_GRADIENT_PROBLEM_H_