Sameer Agarwal | ea11704 | 2012-08-29 18:18:48 -0700 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
| 2 | // Copyright 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 | // |
Sameer Agarwal | c0fdc97 | 2013-02-06 14:31:07 -0800 | [diff] [blame] | 31 | // The National Institute of Standards and Technology has released a |
| 32 | // set of problems to test non-linear least squares solvers. |
Sameer Agarwal | ea11704 | 2012-08-29 18:18:48 -0700 | [diff] [blame] | 33 | // |
Sameer Agarwal | c0fdc97 | 2013-02-06 14:31:07 -0800 | [diff] [blame] | 34 | // More information about the background on these problems and |
| 35 | // suggested evaluation methodology can be found at: |
Sameer Agarwal | ea11704 | 2012-08-29 18:18:48 -0700 | [diff] [blame] | 36 | // |
Sameer Agarwal | c0fdc97 | 2013-02-06 14:31:07 -0800 | [diff] [blame] | 37 | // http://www.itl.nist.gov/div898/strd/nls/nls_info.shtml |
Sameer Agarwal | ea11704 | 2012-08-29 18:18:48 -0700 | [diff] [blame] | 38 | // |
Sameer Agarwal | c0fdc97 | 2013-02-06 14:31:07 -0800 | [diff] [blame] | 39 | // The problem data themselves can be found at |
| 40 | // |
| 41 | // http://www.itl.nist.gov/div898/strd/nls/nls_main.shtml |
| 42 | // |
| 43 | // The problems are divided into three levels of difficulty, Easy, |
| 44 | // Medium and Hard. For each problem there are two starting guesses, |
| 45 | // the first one far away from the global minimum and the second |
| 46 | // closer to it. |
| 47 | // |
| 48 | // A problem is considered successfully solved, if every components of |
| 49 | // the solution matches the globally optimal solution in at least 4 |
| 50 | // digits or more. |
| 51 | // |
| 52 | // This dataset was used for an evaluation of Non-linear least squares |
| 53 | // solvers: |
| 54 | // |
| 55 | // P. F. Mondragon & B. Borchers, A Comparison of Nonlinear Regression |
| 56 | // Codes, Journal of Modern Applied Statistical Methods, 4(1):343-351, |
| 57 | // 2005. |
| 58 | // |
| 59 | // The results from Mondragon & Borchers can be summarized as |
| 60 | // Excel Gnuplot GaussFit HBN MinPack |
| 61 | // Average LRE 2.3 4.3 4.0 6.8 4.4 |
| 62 | // Winner 1 5 12 29 12 |
| 63 | // |
| 64 | // Where the row Winner counts, the number of problems for which the |
| 65 | // solver had the highest LRE. |
| 66 | |
| 67 | // In this file, we implement the same evaluation methodology using |
| 68 | // Ceres. Currently using Levenberg-Marquard with DENSE_QR, we get |
| 69 | // |
| 70 | // Excel Gnuplot GaussFit HBN MinPack Ceres |
| 71 | // Average LRE 2.3 4.3 4.0 6.8 4.4 9.4 |
| 72 | // Winner 0 0 5 11 2 41 |
Sameer Agarwal | ea11704 | 2012-08-29 18:18:48 -0700 | [diff] [blame] | 73 | |
| 74 | #include <iostream> |
Petter Strandmark | 5856071 | 2013-04-07 01:24:13 +0200 | [diff] [blame] | 75 | #include <iterator> |
Sameer Agarwal | ea11704 | 2012-08-29 18:18:48 -0700 | [diff] [blame] | 76 | #include <fstream> |
| 77 | #include "ceres/ceres.h" |
Sameer Agarwal | ea11704 | 2012-08-29 18:18:48 -0700 | [diff] [blame] | 78 | #include "gflags/gflags.h" |
| 79 | #include "glog/logging.h" |
| 80 | #include "Eigen/Core" |
| 81 | |
| 82 | DEFINE_string(nist_data_dir, "", "Directory containing the NIST non-linear" |
| 83 | "regression examples"); |
Sameer Agarwal | cbae856 | 2012-09-02 13:50:43 -0700 | [diff] [blame] | 84 | DEFINE_string(trust_region_strategy, "levenberg_marquardt", |
| 85 | "Options are: levenberg_marquardt, dogleg"); |
| 86 | DEFINE_string(dogleg, "traditional_dogleg", |
| 87 | "Options are: traditional_dogleg, subspace_dogleg"); |
| 88 | DEFINE_string(linear_solver, "dense_qr", "Options are: " |
| 89 | "sparse_cholesky, dense_qr, dense_normal_cholesky and" |
| 90 | "cgnr"); |
| 91 | DEFINE_string(preconditioner, "jacobi", "Options are: " |
| 92 | "identity, jacobi"); |
| 93 | DEFINE_int32(num_iterations, 10000, "Number of iterations"); |
| 94 | DEFINE_bool(nonmonotonic_steps, false, "Trust region algorithm can use" |
| 95 | " nonmonotic steps"); |
Markus Moll | 58b07a6 | 2012-09-07 10:21:17 +0200 | [diff] [blame] | 96 | DEFINE_double(initial_trust_region_radius, 1e4, "Initial trust region radius"); |
Sameer Agarwal | ea11704 | 2012-08-29 18:18:48 -0700 | [diff] [blame] | 97 | |
Sameer Agarwal | 2c648db | 2013-03-05 15:20:15 -0800 | [diff] [blame] | 98 | namespace ceres { |
| 99 | namespace examples { |
| 100 | |
Sameer Agarwal | ea11704 | 2012-08-29 18:18:48 -0700 | [diff] [blame] | 101 | using Eigen::Dynamic; |
| 102 | using Eigen::RowMajor; |
| 103 | typedef Eigen::Matrix<double, Dynamic, 1> Vector; |
| 104 | typedef Eigen::Matrix<double, Dynamic, Dynamic, RowMajor> Matrix; |
| 105 | |
Sameer Agarwal | 2c648db | 2013-03-05 15:20:15 -0800 | [diff] [blame] | 106 | void SplitStringUsingChar(const string& full, |
| 107 | const char delim, |
| 108 | vector<string>* result) { |
| 109 | back_insert_iterator< vector<string> > it(*result); |
| 110 | |
| 111 | const char* p = full.data(); |
| 112 | const char* end = p + full.size(); |
| 113 | while (p != end) { |
| 114 | if (*p == delim) { |
| 115 | ++p; |
| 116 | } else { |
| 117 | const char* start = p; |
| 118 | while (++p != end && *p != delim) { |
| 119 | // Skip to the next occurence of the delimiter. |
| 120 | } |
| 121 | *it++ = string(start, p - start); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
Sameer Agarwal | ea11704 | 2012-08-29 18:18:48 -0700 | [diff] [blame] | 126 | bool GetAndSplitLine(std::ifstream& ifs, std::vector<std::string>* pieces) { |
| 127 | pieces->clear(); |
| 128 | char buf[256]; |
| 129 | ifs.getline(buf, 256); |
Sameer Agarwal | 2c648db | 2013-03-05 15:20:15 -0800 | [diff] [blame] | 130 | SplitStringUsingChar(std::string(buf), ' ', pieces); |
Sameer Agarwal | ea11704 | 2012-08-29 18:18:48 -0700 | [diff] [blame] | 131 | return true; |
| 132 | } |
| 133 | |
| 134 | void SkipLines(std::ifstream& ifs, int num_lines) { |
| 135 | char buf[256]; |
| 136 | for (int i = 0; i < num_lines; ++i) { |
| 137 | ifs.getline(buf, 256); |
| 138 | } |
| 139 | } |
| 140 | |
Markus Moll | 059ad6e | 2012-09-07 18:51:50 +0200 | [diff] [blame] | 141 | bool IsSuccessfulTermination(ceres::SolverTerminationType status) { |
| 142 | return |
| 143 | (status == ceres::FUNCTION_TOLERANCE) || |
| 144 | (status == ceres::GRADIENT_TOLERANCE) || |
| 145 | (status == ceres::PARAMETER_TOLERANCE) || |
| 146 | (status == ceres::USER_SUCCESS); |
| 147 | } |
| 148 | |
Sameer Agarwal | ea11704 | 2012-08-29 18:18:48 -0700 | [diff] [blame] | 149 | class NISTProblem { |
| 150 | public: |
| 151 | explicit NISTProblem(const std::string& filename) { |
| 152 | std::ifstream ifs(filename.c_str(), std::ifstream::in); |
| 153 | |
| 154 | std::vector<std::string> pieces; |
| 155 | SkipLines(ifs, 24); |
| 156 | GetAndSplitLine(ifs, &pieces); |
| 157 | const int kNumResponses = std::atoi(pieces[1].c_str()); |
| 158 | |
| 159 | GetAndSplitLine(ifs, &pieces); |
| 160 | const int kNumPredictors = std::atoi(pieces[0].c_str()); |
| 161 | |
| 162 | GetAndSplitLine(ifs, &pieces); |
| 163 | const int kNumObservations = std::atoi(pieces[0].c_str()); |
| 164 | |
| 165 | SkipLines(ifs, 4); |
| 166 | GetAndSplitLine(ifs, &pieces); |
| 167 | const int kNumParameters = std::atoi(pieces[0].c_str()); |
| 168 | SkipLines(ifs, 8); |
| 169 | |
| 170 | // Get the first line of initial and final parameter values to |
| 171 | // determine the number of tries. |
| 172 | GetAndSplitLine(ifs, &pieces); |
| 173 | const int kNumTries = pieces.size() - 4; |
| 174 | |
| 175 | predictor_.resize(kNumObservations, kNumPredictors); |
| 176 | response_.resize(kNumObservations, kNumResponses); |
| 177 | initial_parameters_.resize(kNumTries, kNumParameters); |
| 178 | final_parameters_.resize(1, kNumParameters); |
| 179 | |
| 180 | // Parse the line for parameter b1. |
| 181 | int parameter_id = 0; |
| 182 | for (int i = 0; i < kNumTries; ++i) { |
| 183 | initial_parameters_(i, parameter_id) = std::atof(pieces[i + 2].c_str()); |
| 184 | } |
| 185 | final_parameters_(0, parameter_id) = std::atof(pieces[2 + kNumTries].c_str()); |
| 186 | |
| 187 | // Parse the remaining parameter lines. |
| 188 | for (int parameter_id = 1; parameter_id < kNumParameters; ++parameter_id) { |
| 189 | GetAndSplitLine(ifs, &pieces); |
| 190 | // b2, b3, .... |
| 191 | for (int i = 0; i < kNumTries; ++i) { |
| 192 | initial_parameters_(i, parameter_id) = std::atof(pieces[i + 2].c_str()); |
| 193 | } |
| 194 | final_parameters_(0, parameter_id) = std::atof(pieces[2 + kNumTries].c_str()); |
| 195 | } |
| 196 | |
Sameer Agarwal | cbae856 | 2012-09-02 13:50:43 -0700 | [diff] [blame] | 197 | // Certfied cost |
| 198 | SkipLines(ifs, 1); |
| 199 | GetAndSplitLine(ifs, &pieces); |
| 200 | certified_cost_ = std::atof(pieces[4].c_str()) / 2.0; |
| 201 | |
Sameer Agarwal | ea11704 | 2012-08-29 18:18:48 -0700 | [diff] [blame] | 202 | // Read the observations. |
Sameer Agarwal | cbae856 | 2012-09-02 13:50:43 -0700 | [diff] [blame] | 203 | SkipLines(ifs, 18 - kNumParameters); |
Sameer Agarwal | ea11704 | 2012-08-29 18:18:48 -0700 | [diff] [blame] | 204 | for (int i = 0; i < kNumObservations; ++i) { |
| 205 | GetAndSplitLine(ifs, &pieces); |
| 206 | // Response. |
| 207 | for (int j = 0; j < kNumResponses; ++j) { |
| 208 | response_(i, j) = std::atof(pieces[j].c_str()); |
| 209 | } |
| 210 | |
| 211 | // Predictor variables. |
| 212 | for (int j = 0; j < kNumPredictors; ++j) { |
| 213 | predictor_(i, j) = std::atof(pieces[j + kNumResponses].c_str()); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | Matrix initial_parameters(int start) const { return initial_parameters_.row(start); } |
| 219 | Matrix final_parameters() const { return final_parameters_; } |
| 220 | Matrix predictor() const { return predictor_; } |
| 221 | Matrix response() const { return response_; } |
| 222 | int predictor_size() const { return predictor_.cols(); } |
| 223 | int num_observations() const { return predictor_.rows(); } |
| 224 | int response_size() const { return response_.cols(); } |
| 225 | int num_parameters() const { return initial_parameters_.cols(); } |
| 226 | int num_starts() const { return initial_parameters_.rows(); } |
Sameer Agarwal | cbae856 | 2012-09-02 13:50:43 -0700 | [diff] [blame] | 227 | double certified_cost() const { return certified_cost_; } |
Sameer Agarwal | ea11704 | 2012-08-29 18:18:48 -0700 | [diff] [blame] | 228 | |
| 229 | private: |
| 230 | Matrix predictor_; |
| 231 | Matrix response_; |
| 232 | Matrix initial_parameters_; |
| 233 | Matrix final_parameters_; |
Sameer Agarwal | cbae856 | 2012-09-02 13:50:43 -0700 | [diff] [blame] | 234 | double certified_cost_; |
Sameer Agarwal | ea11704 | 2012-08-29 18:18:48 -0700 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | #define NIST_BEGIN(CostFunctionName) \ |
| 238 | struct CostFunctionName { \ |
| 239 | CostFunctionName(const double* const x, \ |
| 240 | const double* const y) \ |
| 241 | : x_(*x), y_(*y) {} \ |
| 242 | double x_; \ |
| 243 | double y_; \ |
| 244 | template <typename T> \ |
| 245 | bool operator()(const T* const b, T* residual) const { \ |
| 246 | const T y(y_); \ |
| 247 | const T x(x_); \ |
| 248 | residual[0] = y - ( |
| 249 | |
| 250 | #define NIST_END ); return true; }}; |
| 251 | |
| 252 | // y = b1 * (b2+x)**(-1/b3) + e |
| 253 | NIST_BEGIN(Bennet5) |
| 254 | b[0] * pow(b[1] + x, T(-1.0) / b[2]) |
| 255 | NIST_END |
| 256 | |
| 257 | // y = b1*(1-exp[-b2*x]) + e |
| 258 | NIST_BEGIN(BoxBOD) |
| 259 | b[0] * (T(1.0) - exp(-b[1] * x)) |
| 260 | NIST_END |
| 261 | |
| 262 | // y = exp[-b1*x]/(b2+b3*x) + e |
| 263 | NIST_BEGIN(Chwirut) |
| 264 | exp(-b[0] * x) / (b[1] + b[2] * x) |
| 265 | NIST_END |
| 266 | |
| 267 | // y = b1*x**b2 + e |
| 268 | NIST_BEGIN(DanWood) |
| 269 | b[0] * pow(x, b[1]) |
| 270 | NIST_END |
| 271 | |
| 272 | // y = b1*exp( -b2*x ) + b3*exp( -(x-b4)**2 / b5**2 ) |
| 273 | // + b6*exp( -(x-b7)**2 / b8**2 ) + e |
| 274 | NIST_BEGIN(Gauss) |
| 275 | b[0] * exp(-b[1] * x) + |
| 276 | b[2] * exp(-pow((x - b[3])/b[4], 2)) + |
| 277 | b[5] * exp(-pow((x - b[6])/b[7],2)) |
| 278 | NIST_END |
| 279 | |
| 280 | // y = b1*exp(-b2*x) + b3*exp(-b4*x) + b5*exp(-b6*x) + e |
| 281 | NIST_BEGIN(Lanczos) |
| 282 | b[0] * exp(-b[1] * x) + b[2] * exp(-b[3] * x) + b[4] * exp(-b[5] * x) |
| 283 | NIST_END |
| 284 | |
| 285 | // y = (b1+b2*x+b3*x**2+b4*x**3) / |
| 286 | // (1+b5*x+b6*x**2+b7*x**3) + e |
| 287 | NIST_BEGIN(Hahn1) |
| 288 | (b[0] + b[1] * x + b[2] * x * x + b[3] * x * x * x) / |
| 289 | (T(1.0) + b[4] * x + b[5] * x * x + b[6] * x * x * x) |
| 290 | NIST_END |
| 291 | |
| 292 | // y = (b1 + b2*x + b3*x**2) / |
| 293 | // (1 + b4*x + b5*x**2) + e |
| 294 | NIST_BEGIN(Kirby2) |
| 295 | (b[0] + b[1] * x + b[2] * x * x) / |
| 296 | (T(1.0) + b[3] * x + b[4] * x * x) |
| 297 | NIST_END |
| 298 | |
| 299 | // y = b1*(x**2+x*b2) / (x**2+x*b3+b4) + e |
| 300 | NIST_BEGIN(MGH09) |
| 301 | b[0] * (x * x + x * b[1]) / (x * x + x * b[2] + b[3]) |
| 302 | NIST_END |
| 303 | |
| 304 | // y = b1 * exp[b2/(x+b3)] + e |
| 305 | NIST_BEGIN(MGH10) |
| 306 | b[0] * exp(b[1] / (x + b[2])) |
| 307 | NIST_END |
| 308 | |
| 309 | // y = b1 + b2*exp[-x*b4] + b3*exp[-x*b5] |
| 310 | NIST_BEGIN(MGH17) |
| 311 | b[0] + b[1] * exp(-x * b[3]) + b[2] * exp(-x * b[4]) |
| 312 | NIST_END |
| 313 | |
| 314 | // y = b1*(1-exp[-b2*x]) + e |
| 315 | NIST_BEGIN(Misra1a) |
| 316 | b[0] * (T(1.0) - exp(-b[1] * x)) |
| 317 | NIST_END |
| 318 | |
| 319 | // y = b1 * (1-(1+b2*x/2)**(-2)) + e |
| 320 | NIST_BEGIN(Misra1b) |
| 321 | b[0] * (T(1.0) - T(1.0)/ ((T(1.0) + b[1] * x / 2.0) * (T(1.0) + b[1] * x / 2.0))) |
| 322 | NIST_END |
| 323 | |
| 324 | // y = b1 * (1-(1+2*b2*x)**(-.5)) + e |
| 325 | NIST_BEGIN(Misra1c) |
Markus Moll | 4390488 | 2012-08-31 22:54:29 +0200 | [diff] [blame] | 326 | b[0] * (T(1.0) - pow(T(1.0) + T(2.0) * b[1] * x, -0.5)) |
Sameer Agarwal | ea11704 | 2012-08-29 18:18:48 -0700 | [diff] [blame] | 327 | NIST_END |
| 328 | |
| 329 | // y = b1*b2*x*((1+b2*x)**(-1)) + e |
| 330 | NIST_BEGIN(Misra1d) |
| 331 | b[0] * b[1] * x / (T(1.0) + b[1] * x) |
| 332 | NIST_END |
| 333 | |
| 334 | const double kPi = 3.141592653589793238462643383279; |
| 335 | // pi = 3.141592653589793238462643383279E0 |
| 336 | // y = b1 - b2*x - arctan[b3/(x-b4)]/pi + e |
| 337 | NIST_BEGIN(Roszman1) |
| 338 | b[0] - b[1] * x - atan2(b[2], (x - b[3]))/T(kPi) |
| 339 | NIST_END |
| 340 | |
| 341 | // y = b1 / (1+exp[b2-b3*x]) + e |
| 342 | NIST_BEGIN(Rat42) |
| 343 | b[0] / (T(1.0) + exp(b[1] - b[2] * x)) |
| 344 | NIST_END |
| 345 | |
| 346 | // y = b1 / ((1+exp[b2-b3*x])**(1/b4)) + e |
| 347 | NIST_BEGIN(Rat43) |
Sameer Agarwal | 552f9f8 | 2012-08-31 07:27:22 -0700 | [diff] [blame] | 348 | b[0] / pow(T(1.0) + exp(b[1] - b[2] * x), T(1.0) / b[3]) |
Sameer Agarwal | ea11704 | 2012-08-29 18:18:48 -0700 | [diff] [blame] | 349 | NIST_END |
| 350 | |
| 351 | // y = (b1 + b2*x + b3*x**2 + b4*x**3) / |
| 352 | // (1 + b5*x + b6*x**2 + b7*x**3) + e |
| 353 | NIST_BEGIN(Thurber) |
| 354 | (b[0] + b[1] * x + b[2] * x * x + b[3] * x * x * x) / |
| 355 | (T(1.0) + b[4] * x + b[5] * x * x + b[6] * x * x * x) |
| 356 | NIST_END |
| 357 | |
| 358 | // y = b1 + b2*cos( 2*pi*x/12 ) + b3*sin( 2*pi*x/12 ) |
| 359 | // + b5*cos( 2*pi*x/b4 ) + b6*sin( 2*pi*x/b4 ) |
| 360 | // + b8*cos( 2*pi*x/b7 ) + b9*sin( 2*pi*x/b7 ) + e |
| 361 | NIST_BEGIN(ENSO) |
| 362 | b[0] + b[1] * cos(T(2.0 * kPi) * x / T(12.0)) + |
| 363 | b[2] * sin(T(2.0 * kPi) * x / T(12.0)) + |
| 364 | b[4] * cos(T(2.0 * kPi) * x / b[3]) + |
| 365 | b[5] * sin(T(2.0 * kPi) * x / b[3]) + |
| 366 | b[7] * cos(T(2.0 * kPi) * x / b[6]) + |
| 367 | b[8] * sin(T(2.0 * kPi) * x / b[6]) |
| 368 | NIST_END |
| 369 | |
| 370 | // y = (b1/b2) * exp[-0.5*((x-b3)/b2)**2] + e |
| 371 | NIST_BEGIN(Eckerle4) |
| 372 | b[0] / b[1] * exp(T(-0.5) * pow((x - b[2])/b[1], 2)) |
| 373 | NIST_END |
| 374 | |
| 375 | struct Nelson { |
| 376 | public: |
| 377 | Nelson(const double* const x, const double* const y) |
| 378 | : x1_(x[0]), x2_(x[1]), y_(y[0]) {} |
| 379 | |
| 380 | template <typename T> |
| 381 | bool operator()(const T* const b, T* residual) const { |
| 382 | // log[y] = b1 - b2*x1 * exp[-b3*x2] + e |
| 383 | residual[0] = T(log(y_)) - (b[0] - b[1] * T(x1_) * exp(-b[2] * T(x2_))); |
| 384 | return true; |
| 385 | } |
| 386 | |
| 387 | private: |
| 388 | double x1_; |
| 389 | double x2_; |
| 390 | double y_; |
| 391 | }; |
| 392 | |
| 393 | template <typename Model, int num_residuals, int num_parameters> |
Sameer Agarwal | 1a89bcc | 2012-08-30 15:26:17 -0700 | [diff] [blame] | 394 | int RegressionDriver(const std::string& filename, |
Sameer Agarwal | ea11704 | 2012-08-29 18:18:48 -0700 | [diff] [blame] | 395 | const ceres::Solver::Options& options) { |
| 396 | NISTProblem nist_problem(FLAGS_nist_data_dir + filename); |
| 397 | CHECK_EQ(num_residuals, nist_problem.response_size()); |
| 398 | CHECK_EQ(num_parameters, nist_problem.num_parameters()); |
| 399 | |
| 400 | Matrix predictor = nist_problem.predictor(); |
| 401 | Matrix response = nist_problem.response(); |
| 402 | Matrix final_parameters = nist_problem.final_parameters(); |
Sameer Agarwal | c0fdc97 | 2013-02-06 14:31:07 -0800 | [diff] [blame] | 403 | |
| 404 | printf("%s\n", filename.c_str()); |
Sameer Agarwal | ea11704 | 2012-08-29 18:18:48 -0700 | [diff] [blame] | 405 | |
| 406 | // Each NIST problem comes with multiple starting points, so we |
| 407 | // construct the problem from scratch for each case and solve it. |
Sameer Agarwal | c0fdc97 | 2013-02-06 14:31:07 -0800 | [diff] [blame] | 408 | int num_success = 0; |
Sameer Agarwal | ea11704 | 2012-08-29 18:18:48 -0700 | [diff] [blame] | 409 | for (int start = 0; start < nist_problem.num_starts(); ++start) { |
| 410 | Matrix initial_parameters = nist_problem.initial_parameters(start); |
Sameer Agarwal | ea11704 | 2012-08-29 18:18:48 -0700 | [diff] [blame] | 411 | |
Sameer Agarwal | 552f9f8 | 2012-08-31 07:27:22 -0700 | [diff] [blame] | 412 | ceres::Problem problem; |
Sameer Agarwal | ea11704 | 2012-08-29 18:18:48 -0700 | [diff] [blame] | 413 | for (int i = 0; i < nist_problem.num_observations(); ++i) { |
| 414 | problem.AddResidualBlock( |
| 415 | new ceres::AutoDiffCostFunction<Model, num_residuals, num_parameters>( |
| 416 | new Model(predictor.data() + nist_problem.predictor_size() * i, |
| 417 | response.data() + nist_problem.response_size() * i)), |
| 418 | NULL, |
| 419 | initial_parameters.data()); |
| 420 | } |
| 421 | |
Sameer Agarwal | c0fdc97 | 2013-02-06 14:31:07 -0800 | [diff] [blame] | 422 | ceres::Solver::Summary summary; |
| 423 | Solve(options, &problem, &summary); |
Sameer Agarwal | ea11704 | 2012-08-29 18:18:48 -0700 | [diff] [blame] | 424 | |
Sameer Agarwal | c0fdc97 | 2013-02-06 14:31:07 -0800 | [diff] [blame] | 425 | // Compute the LRE by comparing each component of the solution |
| 426 | // with the ground truth, and taking the minimum. |
| 427 | Matrix final_parameters = nist_problem.final_parameters(); |
| 428 | const double kMaxNumSignificantDigits = 11; |
| 429 | double log_relative_error = kMaxNumSignificantDigits + 1; |
| 430 | for (int i = 0; i < num_parameters; ++i) { |
| 431 | const double tmp_lre = |
| 432 | -std::log10(std::fabs(final_parameters(i) - initial_parameters(i)) / |
| 433 | std::fabs(final_parameters(i))); |
| 434 | // The maximum LRE is capped at 11 - the precision at which the |
| 435 | // ground truth is known. |
| 436 | // |
| 437 | // The minimum LRE is capped at 0 - no digits match between the |
| 438 | // computed solution and the ground truth. |
| 439 | log_relative_error = |
| 440 | std::min(log_relative_error, |
| 441 | std::max(0.0, std::min(kMaxNumSignificantDigits, tmp_lre))); |
Sameer Agarwal | 1a89bcc | 2012-08-30 15:26:17 -0700 | [diff] [blame] | 442 | } |
Sameer Agarwal | ca0ff62 | 2012-09-01 14:41:44 -0700 | [diff] [blame] | 443 | |
Sameer Agarwal | c0fdc97 | 2013-02-06 14:31:07 -0800 | [diff] [blame] | 444 | const int kMinNumMatchingDigits = 4; |
| 445 | if (log_relative_error >= kMinNumMatchingDigits) { |
Sameer Agarwal | ca0ff62 | 2012-09-01 14:41:44 -0700 | [diff] [blame] | 446 | ++num_success; |
| 447 | } |
Sameer Agarwal | b329e58 | 2012-09-05 10:45:17 -0700 | [diff] [blame] | 448 | |
Sameer Agarwal | c0fdc97 | 2013-02-06 14:31:07 -0800 | [diff] [blame] | 449 | printf("start: %d status: %s lre: %4.1f initial cost: %e final cost:%e certified cost: %e\n", |
| 450 | start + 1, |
| 451 | log_relative_error < kMinNumMatchingDigits ? "FAILURE" : "SUCCESS", |
| 452 | log_relative_error, |
| 453 | summary.initial_cost, |
| 454 | summary.final_cost, |
| 455 | nist_problem.certified_cost()); |
Sameer Agarwal | ea11704 | 2012-08-29 18:18:48 -0700 | [diff] [blame] | 456 | } |
Sameer Agarwal | 1a89bcc | 2012-08-30 15:26:17 -0700 | [diff] [blame] | 457 | return num_success; |
| 458 | } |
| 459 | |
Sameer Agarwal | cbae856 | 2012-09-02 13:50:43 -0700 | [diff] [blame] | 460 | void SetMinimizerOptions(ceres::Solver::Options* options) { |
| 461 | CHECK(ceres::StringToLinearSolverType(FLAGS_linear_solver, |
| 462 | &options->linear_solver_type)); |
| 463 | CHECK(ceres::StringToPreconditionerType(FLAGS_preconditioner, |
| 464 | &options->preconditioner_type)); |
| 465 | CHECK(ceres::StringToTrustRegionStrategyType( |
| 466 | FLAGS_trust_region_strategy, |
| 467 | &options->trust_region_strategy_type)); |
| 468 | CHECK(ceres::StringToDoglegType(FLAGS_dogleg, &options->dogleg_type)); |
| 469 | |
| 470 | options->max_num_iterations = FLAGS_num_iterations; |
| 471 | options->use_nonmonotonic_steps = FLAGS_nonmonotonic_steps; |
Markus Moll | 58b07a6 | 2012-09-07 10:21:17 +0200 | [diff] [blame] | 472 | options->initial_trust_region_radius = FLAGS_initial_trust_region_radius; |
Sameer Agarwal | cbae856 | 2012-09-02 13:50:43 -0700 | [diff] [blame] | 473 | options->function_tolerance = 1e-18; |
| 474 | options->gradient_tolerance = 1e-18; |
| 475 | options->parameter_tolerance = 1e-18; |
| 476 | } |
| 477 | |
| 478 | void SolveNISTProblems() { |
| 479 | if (FLAGS_nist_data_dir.empty()) { |
| 480 | LOG(FATAL) << "Must specify the directory containing the NIST problems"; |
| 481 | } |
| 482 | |
| 483 | ceres::Solver::Options options; |
| 484 | SetMinimizerOptions(&options); |
| 485 | |
Sameer Agarwal | c0fdc97 | 2013-02-06 14:31:07 -0800 | [diff] [blame] | 486 | std::cout << "Lower Difficulty\n"; |
Sameer Agarwal | 1a89bcc | 2012-08-30 15:26:17 -0700 | [diff] [blame] | 487 | int easy_success = 0; |
| 488 | easy_success += RegressionDriver<Misra1a, 1, 2>("Misra1a.dat", options); |
| 489 | easy_success += RegressionDriver<Chwirut, 1, 3>("Chwirut1.dat", options); |
| 490 | easy_success += RegressionDriver<Chwirut, 1, 3>("Chwirut2.dat", options); |
| 491 | easy_success += RegressionDriver<Lanczos, 1, 6>("Lanczos3.dat", options); |
| 492 | easy_success += RegressionDriver<Gauss, 1, 8>("Gauss1.dat", options); |
| 493 | easy_success += RegressionDriver<Gauss, 1, 8>("Gauss2.dat", options); |
| 494 | easy_success += RegressionDriver<DanWood, 1, 2>("DanWood.dat", options); |
| 495 | easy_success += RegressionDriver<Misra1b, 1, 2>("Misra1b.dat", options); |
| 496 | |
Sameer Agarwal | c0fdc97 | 2013-02-06 14:31:07 -0800 | [diff] [blame] | 497 | std::cout << "\nMedium Difficulty\n"; |
Sameer Agarwal | 1a89bcc | 2012-08-30 15:26:17 -0700 | [diff] [blame] | 498 | int medium_success = 0; |
| 499 | medium_success += RegressionDriver<Kirby2, 1, 5>("Kirby2.dat", options); |
| 500 | medium_success += RegressionDriver<Hahn1, 1, 7>("Hahn1.dat", options); |
| 501 | medium_success += RegressionDriver<Nelson, 1, 3>("Nelson.dat", options); |
| 502 | medium_success += RegressionDriver<MGH17, 1, 5>("MGH17.dat", options); |
| 503 | medium_success += RegressionDriver<Lanczos, 1, 6>("Lanczos1.dat", options); |
| 504 | medium_success += RegressionDriver<Lanczos, 1, 6>("Lanczos2.dat", options); |
| 505 | medium_success += RegressionDriver<Gauss, 1, 8>("Gauss3.dat", options); |
| 506 | medium_success += RegressionDriver<Misra1c, 1, 2>("Misra1c.dat", options); |
| 507 | medium_success += RegressionDriver<Misra1d, 1, 2>("Misra1d.dat", options); |
| 508 | medium_success += RegressionDriver<Roszman1, 1, 4>("Roszman1.dat", options); |
| 509 | medium_success += RegressionDriver<ENSO, 1, 9>("ENSO.dat", options); |
| 510 | |
Sameer Agarwal | c0fdc97 | 2013-02-06 14:31:07 -0800 | [diff] [blame] | 511 | std::cout << "\nHigher Difficulty\n"; |
Sameer Agarwal | 1a89bcc | 2012-08-30 15:26:17 -0700 | [diff] [blame] | 512 | int hard_success = 0; |
| 513 | hard_success += RegressionDriver<MGH09, 1, 4>("MGH09.dat", options); |
| 514 | hard_success += RegressionDriver<Thurber, 1, 7>("Thurber.dat", options); |
| 515 | hard_success += RegressionDriver<BoxBOD, 1, 2>("BoxBOD.dat", options); |
| 516 | hard_success += RegressionDriver<Rat42, 1, 3>("Rat42.dat", options); |
| 517 | hard_success += RegressionDriver<MGH10, 1, 3>("MGH10.dat", options); |
Sameer Agarwal | 552f9f8 | 2012-08-31 07:27:22 -0700 | [diff] [blame] | 518 | |
Sameer Agarwal | 1a89bcc | 2012-08-30 15:26:17 -0700 | [diff] [blame] | 519 | hard_success += RegressionDriver<Eckerle4, 1, 3>("Eckerle4.dat", options); |
| 520 | hard_success += RegressionDriver<Rat43, 1, 4>("Rat43.dat", options); |
| 521 | hard_success += RegressionDriver<Bennet5, 1, 3>("Bennett5.dat", options); |
| 522 | |
Sameer Agarwal | c0fdc97 | 2013-02-06 14:31:07 -0800 | [diff] [blame] | 523 | std::cout << "\n"; |
| 524 | std::cout << "Easy : " << easy_success << "/16\n"; |
| 525 | std::cout << "Medium : " << medium_success << "/22\n"; |
| 526 | std::cout << "Hard : " << hard_success << "/16\n"; |
| 527 | std::cout << "Total : " << easy_success + medium_success + hard_success << "/54\n"; |
Sameer Agarwal | ea11704 | 2012-08-29 18:18:48 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Sameer Agarwal | 2c648db | 2013-03-05 15:20:15 -0800 | [diff] [blame] | 530 | } // namespace examples |
| 531 | } // namespace ceres |
| 532 | |
Sameer Agarwal | ea11704 | 2012-08-29 18:18:48 -0700 | [diff] [blame] | 533 | int main(int argc, char** argv) { |
| 534 | google::ParseCommandLineFlags(&argc, &argv, true); |
| 535 | google::InitGoogleLogging(argv[0]); |
Sameer Agarwal | 2c648db | 2013-03-05 15:20:15 -0800 | [diff] [blame] | 536 | ceres::examples::SolveNISTProblems(); |
Sameer Agarwal | ea11704 | 2012-08-29 18:18:48 -0700 | [diff] [blame] | 537 | return 0; |
| 538 | }; |