2012-04-30 23:09:08 -07:00
|
|
|
// Ceres Solver - A fast non-linear least squares minimizer
|
2023-09-19 15:29:34 -07:00
|
|
|
// Copyright 2023 Google Inc. All rights reserved.
|
2015-03-17 22:30:16 -07:00
|
|
|
// http://ceres-solver.org/
|
2012-04-30 23:09:08 -07:00
|
|
|
//
|
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
|
//
|
|
|
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
|
// * Neither the name of Google Inc. nor the names of its contributors may be
|
|
|
|
|
// used to endorse or promote products derived from this software without
|
|
|
|
|
// specific prior written permission.
|
|
|
|
|
//
|
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
//
|
|
|
|
|
// Author: keir@google.com (Keir Mierle)
|
|
|
|
|
//
|
|
|
|
|
// Minimize 0.5 (10 - x)^2 using jacobian matrix computed using
|
|
|
|
|
// numeric differentiation.
|
|
|
|
|
|
2024-07-07 10:24:18 -07:00
|
|
|
#include "absl/log/initialize.h"
|
2012-04-30 23:09:08 -07:00
|
|
|
#include "ceres/ceres.h"
|
|
|
|
|
|
2013-01-21 13:05:01 -08:00
|
|
|
// A cost functor that implements the residual r = 10 - x.
|
2013-02-06 14:31:07 -08:00
|
|
|
struct CostFunctor {
|
2013-01-21 13:05:01 -08:00
|
|
|
bool operator()(const double* const x, double* residual) const {
|
|
|
|
|
residual[0] = 10.0 - x[0];
|
2012-04-30 23:09:08 -07:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2024-07-07 10:24:18 -07:00
|
|
|
absl::InitializeLog();
|
2012-04-30 23:09:08 -07:00
|
|
|
|
2013-02-06 14:31:07 -08:00
|
|
|
// The variable to solve for with its initial value. It will be
|
|
|
|
|
// mutated in place by the solver.
|
|
|
|
|
double x = 0.5;
|
|
|
|
|
const double initial_x = x;
|
2012-04-30 23:09:08 -07:00
|
|
|
|
|
|
|
|
// Build the problem.
|
2023-09-27 14:30:58 -07:00
|
|
|
ceres::Problem problem;
|
2013-02-06 14:31:07 -08:00
|
|
|
|
|
|
|
|
// Set up the only cost function (also known as residual). This uses
|
|
|
|
|
// numeric differentiation to obtain the derivative (jacobian).
|
2023-09-27 14:30:58 -07:00
|
|
|
ceres::CostFunction* cost_function =
|
|
|
|
|
new ceres::NumericDiffCostFunction<CostFunctor, ceres::CENTRAL, 1, 1>(
|
|
|
|
|
new CostFunctor);
|
2022-01-18 16:26:47 -08:00
|
|
|
problem.AddResidualBlock(cost_function, nullptr, &x);
|
2012-04-30 23:09:08 -07:00
|
|
|
|
|
|
|
|
// Run the solver!
|
2023-09-27 14:30:58 -07:00
|
|
|
ceres::Solver::Options options;
|
2012-04-30 23:09:08 -07:00
|
|
|
options.minimizer_progress_to_stdout = true;
|
2023-09-27 14:30:58 -07:00
|
|
|
ceres::Solver::Summary summary;
|
|
|
|
|
ceres::Solve(options, &problem, &summary);
|
2013-02-06 14:31:07 -08:00
|
|
|
|
2012-04-30 23:09:08 -07:00
|
|
|
std::cout << summary.BriefReport() << "\n";
|
2020-09-08 17:51:32 +02:00
|
|
|
std::cout << "x : " << initial_x << " -> " << x << "\n";
|
2012-04-30 23:09:08 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|