1// Ceres Solver - A fast non-linear least squares minimizer 2// Copyright 2010, 2011, 2012 Google Inc. All rights reserved. 3// http://code.google.com/p/ceres-solver/ 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#include "ceres/ceres.h" 32#include "glog/logging.h" 33 34// Data generated using the following octave code. 35// randn('seed', 23497); 36// m = 0.3; 37// c = 0.1; 38// x=[0:0.075:5]; 39// y = exp(m * x + c); 40// noise = randn(size(x)) * 0.2; 41// outlier_noise = rand(size(x)) < 0.05; 42// y_observed = y + noise + outlier_noise; 43// data = [x', y_observed']; 44 45const int kNumObservations = 67; 46const double data[] = { 470.000000e+00, 1.133898e+00, 487.500000e-02, 1.334902e+00, 491.500000e-01, 1.213546e+00, 502.250000e-01, 1.252016e+00, 513.000000e-01, 1.392265e+00, 523.750000e-01, 1.314458e+00, 534.500000e-01, 1.472541e+00, 545.250000e-01, 1.536218e+00, 556.000000e-01, 1.355679e+00, 566.750000e-01, 1.463566e+00, 577.500000e-01, 1.490201e+00, 588.250000e-01, 1.658699e+00, 599.000000e-01, 1.067574e+00, 609.750000e-01, 1.464629e+00, 611.050000e+00, 1.402653e+00, 621.125000e+00, 1.713141e+00, 631.200000e+00, 1.527021e+00, 641.275000e+00, 1.702632e+00, 651.350000e+00, 1.423899e+00, 661.425000e+00, 5.543078e+00, // Outlier point 671.500000e+00, 5.664015e+00, // Outlier point 681.575000e+00, 1.732484e+00, 691.650000e+00, 1.543296e+00, 701.725000e+00, 1.959523e+00, 711.800000e+00, 1.685132e+00, 721.875000e+00, 1.951791e+00, 731.950000e+00, 2.095346e+00, 742.025000e+00, 2.361460e+00, 752.100000e+00, 2.169119e+00, 762.175000e+00, 2.061745e+00, 772.250000e+00, 2.178641e+00, 782.325000e+00, 2.104346e+00, 792.400000e+00, 2.584470e+00, 802.475000e+00, 1.914158e+00, 812.550000e+00, 2.368375e+00, 822.625000e+00, 2.686125e+00, 832.700000e+00, 2.712395e+00, 842.775000e+00, 2.499511e+00, 852.850000e+00, 2.558897e+00, 862.925000e+00, 2.309154e+00, 873.000000e+00, 2.869503e+00, 883.075000e+00, 3.116645e+00, 893.150000e+00, 3.094907e+00, 903.225000e+00, 2.471759e+00, 913.300000e+00, 3.017131e+00, 923.375000e+00, 3.232381e+00, 933.450000e+00, 2.944596e+00, 943.525000e+00, 3.385343e+00, 953.600000e+00, 3.199826e+00, 963.675000e+00, 3.423039e+00, 973.750000e+00, 3.621552e+00, 983.825000e+00, 3.559255e+00, 993.900000e+00, 3.530713e+00, 1003.975000e+00, 3.561766e+00, 1014.050000e+00, 3.544574e+00, 1024.125000e+00, 3.867945e+00, 1034.200000e+00, 4.049776e+00, 1044.275000e+00, 3.885601e+00, 1054.350000e+00, 4.110505e+00, 1064.425000e+00, 4.345320e+00, 1074.500000e+00, 4.161241e+00, 1084.575000e+00, 4.363407e+00, 1094.650000e+00, 4.161576e+00, 1104.725000e+00, 4.619728e+00, 1114.800000e+00, 4.737410e+00, 1124.875000e+00, 4.727863e+00, 1134.950000e+00, 4.669206e+00 114}; 115 116using ceres::AutoDiffCostFunction; 117using ceres::CostFunction; 118using ceres::CauchyLoss; 119using ceres::Problem; 120using ceres::Solve; 121using ceres::Solver; 122 123struct ExponentialResidual { 124 ExponentialResidual(double x, double y) 125 : x_(x), y_(y) {} 126 127 template <typename T> bool operator()(const T* const m, 128 const T* const c, 129 T* residual) const { 130 residual[0] = T(y_) - exp(m[0] * T(x_) + c[0]); 131 return true; 132 } 133 134 private: 135 const double x_; 136 const double y_; 137}; 138 139int main(int argc, char** argv) { 140 google::InitGoogleLogging(argv[0]); 141 142 double m = 0.0; 143 double c = 0.0; 144 145 Problem problem; 146 for (int i = 0; i < kNumObservations; ++i) { 147 CostFunction* cost_function = 148 new AutoDiffCostFunction<ExponentialResidual, 1, 1, 1>( 149 new ExponentialResidual(data[2 * i], data[2 * i + 1])); 150 problem.AddResidualBlock(cost_function, NULL, &m, &c); 151 } 152 153 Solver::Options options; 154 options.linear_solver_type = ceres::DENSE_QR; 155 options.minimizer_progress_to_stdout = true; 156 157 Solver::Summary summary; 158 Solve(options, &problem, &summary); 159 std::cout << summary.BriefReport() << "\n"; 160 std::cout << "Initial m: " << 0.0 << " c: " << 0.0 << "\n"; 161 std::cout << "Final m: " << m << " c: " << c << "\n"; 162 return 0; 163} 164