10ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Ceres Solver - A fast non-linear least squares minimizer 20ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Copyright 2010, 2011, 2012 Google Inc. All rights reserved. 30ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// http://code.google.com/p/ceres-solver/ 40ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 50ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Redistribution and use in source and binary forms, with or without 60ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// modification, are permitted provided that the following conditions are met: 70ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 80ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions of source code must retain the above copyright notice, 90ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// this list of conditions and the following disclaimer. 100ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions in binary form must reproduce the above copyright notice, 110ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// this list of conditions and the following disclaimer in the documentation 120ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// and/or other materials provided with the distribution. 130ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Neither the name of Google Inc. nor the names of its contributors may be 140ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// used to endorse or promote products derived from this software without 150ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// specific prior written permission. 160ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 170ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 180ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 190ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 200ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 210ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 220ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 230ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 240ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 250ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 260ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 270ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// POSSIBILITY OF SUCH DAMAGE. 280ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 290ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Author: keir@google.com (Keir Mierle) 300ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 310ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Minimize 0.5 (10 - x)^2 using jacobian matrix computed using 320ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// numeric differentiation. 330ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 340ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include <vector> 350ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/ceres.h" 360ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "gflags/gflags.h" 370ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "glog/logging.h" 380ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 390ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongusing ceres::NumericDiffCostFunction; 400ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongusing ceres::CENTRAL; 410ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongusing ceres::CostFunction; 420ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongusing ceres::Problem; 430ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongusing ceres::Solver; 440ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongusing ceres::Solve; 450ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 461d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// A cost functor that implements the residual r = 10 - x. 471d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberlingclass QuadraticCostFunctor { 480ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong public: 491d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling bool operator()(const double* const x, double* residual) const { 501d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling residual[0] = 10.0 - x[0]; 510ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong return true; 520ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong } 530ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}; 540ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 550ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongint main(int argc, char** argv) { 560ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong google::ParseCommandLineFlags(&argc, &argv, true); 570ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong google::InitGoogleLogging(argv[0]); 580ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 590ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // The variable to solve for with its initial value. 600ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong double initial_x = 5.0; 610ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong double x = initial_x; 620ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 630ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // Set up the only cost function (also known as residual). This uses 640ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // numeric differentiation to obtain the derivative (jacobian). 650ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong CostFunction* cost = 661d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling new NumericDiffCostFunction<QuadraticCostFunctor, CENTRAL, 1, 1> ( 671d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling new QuadraticCostFunctor); 680ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 690ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // Build the problem. 700ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong Problem problem; 710ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong problem.AddResidualBlock(cost, NULL, &x); 720ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 730ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // Run the solver! 740ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong Solver::Options options; 750ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong options.max_num_iterations = 10; 760ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong options.linear_solver_type = ceres::DENSE_QR; 770ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong options.minimizer_progress_to_stdout = true; 780ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong Solver::Summary summary; 790ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong Solve(options, &problem, &summary); 800ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong std::cout << summary.BriefReport() << "\n"; 810ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong std::cout << "x : " << initial_x 820ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong << " -> " << x << "\n"; 830ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong return 0; 840ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong} 85