blob: 8e0f37e7376f76aab3fb5ff23b4d294909692b0e [file] [log] [blame]
Sameer Agarwalea117042012-08-29 18:18:48 -07001// 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 Agarwalc0fdc972013-02-06 14:31:07 -080031// The National Institute of Standards and Technology has released a
32// set of problems to test non-linear least squares solvers.
Sameer Agarwalea117042012-08-29 18:18:48 -070033//
Sameer Agarwalc0fdc972013-02-06 14:31:07 -080034// More information about the background on these problems and
35// suggested evaluation methodology can be found at:
Sameer Agarwalea117042012-08-29 18:18:48 -070036//
Sameer Agarwalc0fdc972013-02-06 14:31:07 -080037// http://www.itl.nist.gov/div898/strd/nls/nls_info.shtml
Sameer Agarwalea117042012-08-29 18:18:48 -070038//
Sameer Agarwalc0fdc972013-02-06 14:31:07 -080039// 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 Agarwalea117042012-08-29 18:18:48 -070073
74#include <iostream>
Petter Strandmark58560712013-04-07 01:24:13 +020075#include <iterator>
Sameer Agarwalea117042012-08-29 18:18:48 -070076#include <fstream>
77#include "ceres/ceres.h"
Sameer Agarwalea117042012-08-29 18:18:48 -070078#include "gflags/gflags.h"
79#include "glog/logging.h"
80#include "Eigen/Core"
81
82DEFINE_string(nist_data_dir, "", "Directory containing the NIST non-linear"
83 "regression examples");
Sameer Agarwalcbae8562012-09-02 13:50:43 -070084DEFINE_string(trust_region_strategy, "levenberg_marquardt",
85 "Options are: levenberg_marquardt, dogleg");
86DEFINE_string(dogleg, "traditional_dogleg",
87 "Options are: traditional_dogleg, subspace_dogleg");
88DEFINE_string(linear_solver, "dense_qr", "Options are: "
89 "sparse_cholesky, dense_qr, dense_normal_cholesky and"
90 "cgnr");
91DEFINE_string(preconditioner, "jacobi", "Options are: "
92 "identity, jacobi");
93DEFINE_int32(num_iterations, 10000, "Number of iterations");
94DEFINE_bool(nonmonotonic_steps, false, "Trust region algorithm can use"
95 " nonmonotic steps");
Markus Moll58b07a62012-09-07 10:21:17 +020096DEFINE_double(initial_trust_region_radius, 1e4, "Initial trust region radius");
Sameer Agarwalea117042012-08-29 18:18:48 -070097
Sameer Agarwal2c648db2013-03-05 15:20:15 -080098namespace ceres {
99namespace examples {
100
Sameer Agarwalea117042012-08-29 18:18:48 -0700101using Eigen::Dynamic;
102using Eigen::RowMajor;
103typedef Eigen::Matrix<double, Dynamic, 1> Vector;
104typedef Eigen::Matrix<double, Dynamic, Dynamic, RowMajor> Matrix;
105
Sameer Agarwal2c648db2013-03-05 15:20:15 -0800106void 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 Agarwalea117042012-08-29 18:18:48 -0700126bool GetAndSplitLine(std::ifstream& ifs, std::vector<std::string>* pieces) {
127 pieces->clear();
128 char buf[256];
129 ifs.getline(buf, 256);
Sameer Agarwal2c648db2013-03-05 15:20:15 -0800130 SplitStringUsingChar(std::string(buf), ' ', pieces);
Sameer Agarwalea117042012-08-29 18:18:48 -0700131 return true;
132}
133
134void 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 Moll059ad6e2012-09-07 18:51:50 +0200141bool 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 Agarwalea117042012-08-29 18:18:48 -0700149class 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 Agarwalcbae8562012-09-02 13:50:43 -0700197 // Certfied cost
198 SkipLines(ifs, 1);
199 GetAndSplitLine(ifs, &pieces);
200 certified_cost_ = std::atof(pieces[4].c_str()) / 2.0;
201
Sameer Agarwalea117042012-08-29 18:18:48 -0700202 // Read the observations.
Sameer Agarwalcbae8562012-09-02 13:50:43 -0700203 SkipLines(ifs, 18 - kNumParameters);
Sameer Agarwalea117042012-08-29 18:18:48 -0700204 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 Agarwalcbae8562012-09-02 13:50:43 -0700227 double certified_cost() const { return certified_cost_; }
Sameer Agarwalea117042012-08-29 18:18:48 -0700228
229 private:
230 Matrix predictor_;
231 Matrix response_;
232 Matrix initial_parameters_;
233 Matrix final_parameters_;
Sameer Agarwalcbae8562012-09-02 13:50:43 -0700234 double certified_cost_;
Sameer Agarwalea117042012-08-29 18:18:48 -0700235};
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
253NIST_BEGIN(Bennet5)
254 b[0] * pow(b[1] + x, T(-1.0) / b[2])
255NIST_END
256
257// y = b1*(1-exp[-b2*x]) + e
258NIST_BEGIN(BoxBOD)
259 b[0] * (T(1.0) - exp(-b[1] * x))
260NIST_END
261
262// y = exp[-b1*x]/(b2+b3*x) + e
263NIST_BEGIN(Chwirut)
264 exp(-b[0] * x) / (b[1] + b[2] * x)
265NIST_END
266
267// y = b1*x**b2 + e
268NIST_BEGIN(DanWood)
269 b[0] * pow(x, b[1])
270NIST_END
271
272// y = b1*exp( -b2*x ) + b3*exp( -(x-b4)**2 / b5**2 )
273// + b6*exp( -(x-b7)**2 / b8**2 ) + e
274NIST_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))
278NIST_END
279
280// y = b1*exp(-b2*x) + b3*exp(-b4*x) + b5*exp(-b6*x) + e
281NIST_BEGIN(Lanczos)
282 b[0] * exp(-b[1] * x) + b[2] * exp(-b[3] * x) + b[4] * exp(-b[5] * x)
283NIST_END
284
285// y = (b1+b2*x+b3*x**2+b4*x**3) /
286// (1+b5*x+b6*x**2+b7*x**3) + e
287NIST_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)
290NIST_END
291
292// y = (b1 + b2*x + b3*x**2) /
293// (1 + b4*x + b5*x**2) + e
294NIST_BEGIN(Kirby2)
295 (b[0] + b[1] * x + b[2] * x * x) /
296 (T(1.0) + b[3] * x + b[4] * x * x)
297NIST_END
298
299// y = b1*(x**2+x*b2) / (x**2+x*b3+b4) + e
300NIST_BEGIN(MGH09)
301 b[0] * (x * x + x * b[1]) / (x * x + x * b[2] + b[3])
302NIST_END
303
304// y = b1 * exp[b2/(x+b3)] + e
305NIST_BEGIN(MGH10)
306 b[0] * exp(b[1] / (x + b[2]))
307NIST_END
308
309// y = b1 + b2*exp[-x*b4] + b3*exp[-x*b5]
310NIST_BEGIN(MGH17)
311 b[0] + b[1] * exp(-x * b[3]) + b[2] * exp(-x * b[4])
312NIST_END
313
314// y = b1*(1-exp[-b2*x]) + e
315NIST_BEGIN(Misra1a)
316 b[0] * (T(1.0) - exp(-b[1] * x))
317NIST_END
318
319// y = b1 * (1-(1+b2*x/2)**(-2)) + e
320NIST_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)))
322NIST_END
323
324// y = b1 * (1-(1+2*b2*x)**(-.5)) + e
325NIST_BEGIN(Misra1c)
Markus Moll43904882012-08-31 22:54:29 +0200326 b[0] * (T(1.0) - pow(T(1.0) + T(2.0) * b[1] * x, -0.5))
Sameer Agarwalea117042012-08-29 18:18:48 -0700327NIST_END
328
329// y = b1*b2*x*((1+b2*x)**(-1)) + e
330NIST_BEGIN(Misra1d)
331 b[0] * b[1] * x / (T(1.0) + b[1] * x)
332NIST_END
333
334const double kPi = 3.141592653589793238462643383279;
335// pi = 3.141592653589793238462643383279E0
336// y = b1 - b2*x - arctan[b3/(x-b4)]/pi + e
337NIST_BEGIN(Roszman1)
338 b[0] - b[1] * x - atan2(b[2], (x - b[3]))/T(kPi)
339NIST_END
340
341// y = b1 / (1+exp[b2-b3*x]) + e
342NIST_BEGIN(Rat42)
343 b[0] / (T(1.0) + exp(b[1] - b[2] * x))
344NIST_END
345
346// y = b1 / ((1+exp[b2-b3*x])**(1/b4)) + e
347NIST_BEGIN(Rat43)
Sameer Agarwal552f9f82012-08-31 07:27:22 -0700348 b[0] / pow(T(1.0) + exp(b[1] - b[2] * x), T(1.0) / b[3])
Sameer Agarwalea117042012-08-29 18:18:48 -0700349NIST_END
350
351// y = (b1 + b2*x + b3*x**2 + b4*x**3) /
352// (1 + b5*x + b6*x**2 + b7*x**3) + e
353NIST_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)
356NIST_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
361NIST_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])
368NIST_END
369
370// y = (b1/b2) * exp[-0.5*((x-b3)/b2)**2] + e
371NIST_BEGIN(Eckerle4)
372 b[0] / b[1] * exp(T(-0.5) * pow((x - b[2])/b[1], 2))
373NIST_END
374
375struct 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
393template <typename Model, int num_residuals, int num_parameters>
Sameer Agarwal1a89bcc2012-08-30 15:26:17 -0700394int RegressionDriver(const std::string& filename,
Sameer Agarwalea117042012-08-29 18:18:48 -0700395 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 Agarwalc0fdc972013-02-06 14:31:07 -0800403
404 printf("%s\n", filename.c_str());
Sameer Agarwalea117042012-08-29 18:18:48 -0700405
406 // Each NIST problem comes with multiple starting points, so we
407 // construct the problem from scratch for each case and solve it.
Sameer Agarwalc0fdc972013-02-06 14:31:07 -0800408 int num_success = 0;
Sameer Agarwalea117042012-08-29 18:18:48 -0700409 for (int start = 0; start < nist_problem.num_starts(); ++start) {
410 Matrix initial_parameters = nist_problem.initial_parameters(start);
Sameer Agarwalea117042012-08-29 18:18:48 -0700411
Sameer Agarwal552f9f82012-08-31 07:27:22 -0700412 ceres::Problem problem;
Sameer Agarwalea117042012-08-29 18:18:48 -0700413 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 Agarwalc0fdc972013-02-06 14:31:07 -0800422 ceres::Solver::Summary summary;
423 Solve(options, &problem, &summary);
Sameer Agarwalea117042012-08-29 18:18:48 -0700424
Sameer Agarwalc0fdc972013-02-06 14:31:07 -0800425 // 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 Agarwal1a89bcc2012-08-30 15:26:17 -0700442 }
Sameer Agarwalca0ff622012-09-01 14:41:44 -0700443
Sameer Agarwalc0fdc972013-02-06 14:31:07 -0800444 const int kMinNumMatchingDigits = 4;
445 if (log_relative_error >= kMinNumMatchingDigits) {
Sameer Agarwalca0ff622012-09-01 14:41:44 -0700446 ++num_success;
447 }
Sameer Agarwalb329e582012-09-05 10:45:17 -0700448
Sameer Agarwalc0fdc972013-02-06 14:31:07 -0800449 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 Agarwalea117042012-08-29 18:18:48 -0700456 }
Sameer Agarwal1a89bcc2012-08-30 15:26:17 -0700457 return num_success;
458}
459
Sameer Agarwalcbae8562012-09-02 13:50:43 -0700460void 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 Moll58b07a62012-09-07 10:21:17 +0200472 options->initial_trust_region_radius = FLAGS_initial_trust_region_radius;
Sameer Agarwalcbae8562012-09-02 13:50:43 -0700473 options->function_tolerance = 1e-18;
474 options->gradient_tolerance = 1e-18;
475 options->parameter_tolerance = 1e-18;
476}
477
478void 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 Agarwalc0fdc972013-02-06 14:31:07 -0800486 std::cout << "Lower Difficulty\n";
Sameer Agarwal1a89bcc2012-08-30 15:26:17 -0700487 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 Agarwalc0fdc972013-02-06 14:31:07 -0800497 std::cout << "\nMedium Difficulty\n";
Sameer Agarwal1a89bcc2012-08-30 15:26:17 -0700498 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 Agarwalc0fdc972013-02-06 14:31:07 -0800511 std::cout << "\nHigher Difficulty\n";
Sameer Agarwal1a89bcc2012-08-30 15:26:17 -0700512 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 Agarwal552f9f82012-08-31 07:27:22 -0700518
Sameer Agarwal1a89bcc2012-08-30 15:26:17 -0700519 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 Agarwalc0fdc972013-02-06 14:31:07 -0800523 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 Agarwalea117042012-08-29 18:18:48 -0700528}
529
Sameer Agarwal2c648db2013-03-05 15:20:15 -0800530} // namespace examples
531} // namespace ceres
532
Sameer Agarwalea117042012-08-29 18:18:48 -0700533int main(int argc, char** argv) {
534 google::ParseCommandLineFlags(&argc, &argv, true);
535 google::InitGoogleLogging(argv[0]);
Sameer Agarwal2c648db2013-03-05 15:20:15 -0800536 ceres::examples::SolveNISTProblems();
Sameer Agarwalea117042012-08-29 18:18:48 -0700537 return 0;
538};