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