more_garbow_hillstrom.cc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2015 Google Inc. All rights reserved.
  3. // http://ceres-solver.org/
  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. // Test problems from the paper
  32. //
  33. // Testing Unconstrained Optimization Software
  34. // Jorge J. More, Burton S. Garbow and Kenneth E. Hillstrom
  35. // ACM Transactions on Mathematical Software, 7(1), pp. 17-41, 1981
  36. //
  37. // A subset of these problems were augmented with bounds and used for
  38. // testing bounds constrained optimization algorithms by
  39. //
  40. // A Trust Region Approach to Linearly Constrained Optimization
  41. // David M. Gay
  42. // Numerical Analysis (Griffiths, D.F., ed.), pp. 72-105
  43. // Lecture Notes in Mathematics 1066, Springer Verlag, 1984.
  44. //
  45. // The latter paper is behind a paywall. We obtained the bounds on the
  46. // variables and the function values at the global minimums from
  47. //
  48. // http://www.mat.univie.ac.at/~neum/glopt/bounds.html
  49. //
  50. // A problem is considered solved if of the log relative error of its
  51. // objective function is at least 4.
  52. #include <cmath>
  53. #include <iostream> // NOLINT
  54. #include <sstream> // NOLINT
  55. #include <string>
  56. #include "ceres/ceres.h"
  57. #include "gflags/gflags.h"
  58. #include "glog/logging.h"
  59. DEFINE_string(problem, "all", "Which problem to solve");
  60. DEFINE_bool(use_numeric_diff, false,
  61. "Use numeric differentiation instead of automatic "
  62. "differentiation.");
  63. DEFINE_string(numeric_diff_method, "ridders", "When using numeric "
  64. "differentiation, selects algorithm. Options are: central, "
  65. "forward, ridders.");
  66. DEFINE_int32(ridders_extrapolations, 3, "Maximal number of extrapolations in "
  67. "Ridders' method.");
  68. namespace ceres {
  69. namespace examples {
  70. const double kDoubleMax = std::numeric_limits<double>::max();
  71. static void SetNumericDiffOptions(ceres::NumericDiffOptions* options) {
  72. options->max_num_ridders_extrapolations = FLAGS_ridders_extrapolations;
  73. }
  74. #define BEGIN_MGH_PROBLEM(name, num_parameters, num_residuals) \
  75. struct name { \
  76. static const int kNumParameters = num_parameters; \
  77. static const double initial_x[kNumParameters]; \
  78. static const double lower_bounds[kNumParameters]; \
  79. static const double upper_bounds[kNumParameters]; \
  80. static const double constrained_optimal_cost; \
  81. static const double unconstrained_optimal_cost; \
  82. static CostFunction* Create() { \
  83. if (FLAGS_use_numeric_diff) { \
  84. ceres::NumericDiffOptions options; \
  85. SetNumericDiffOptions(&options); \
  86. if (FLAGS_numeric_diff_method == "central") { \
  87. return new NumericDiffCostFunction<name, \
  88. ceres::CENTRAL, \
  89. num_residuals, \
  90. num_parameters>( \
  91. new name, ceres::TAKE_OWNERSHIP, num_residuals, options); \
  92. } else if (FLAGS_numeric_diff_method == "forward") { \
  93. return new NumericDiffCostFunction<name, \
  94. ceres::FORWARD, \
  95. num_residuals, \
  96. num_parameters>( \
  97. new name, ceres::TAKE_OWNERSHIP, num_residuals, options); \
  98. } else if (FLAGS_numeric_diff_method == "ridders") { \
  99. return new NumericDiffCostFunction<name, \
  100. ceres::RIDDERS, \
  101. num_residuals, \
  102. num_parameters>( \
  103. new name, ceres::TAKE_OWNERSHIP, num_residuals, options); \
  104. } else { \
  105. LOG(ERROR) << "Invalid numeric diff method specified"; \
  106. return NULL; \
  107. } \
  108. } else { \
  109. return new AutoDiffCostFunction<name, \
  110. num_residuals, \
  111. num_parameters>(new name); \
  112. } \
  113. } \
  114. template <typename T> \
  115. bool operator()(const T* const x, T* residual) const {
  116. #define END_MGH_PROBLEM return true; } }; // NOLINT
  117. // Rosenbrock function.
  118. BEGIN_MGH_PROBLEM(TestProblem1, 2, 2)
  119. const T x1 = x[0];
  120. const T x2 = x[1];
  121. residual[0] = 10.0 * (x2 - x1 * x1);
  122. residual[1] = 1.0 - x1;
  123. END_MGH_PROBLEM;
  124. const double TestProblem1::initial_x[] = {-1.2, 1.0};
  125. const double TestProblem1::lower_bounds[] = {-kDoubleMax, -kDoubleMax};
  126. const double TestProblem1::upper_bounds[] = {kDoubleMax, kDoubleMax};
  127. const double TestProblem1::constrained_optimal_cost =
  128. std::numeric_limits<double>::quiet_NaN();
  129. const double TestProblem1::unconstrained_optimal_cost = 0.0;
  130. // Freudenstein and Roth function.
  131. BEGIN_MGH_PROBLEM(TestProblem2, 2, 2)
  132. const T x1 = x[0];
  133. const T x2 = x[1];
  134. residual[0] = -13.0 + x1 + ((5.0 - x2) * x2 - 2.0) * x2;
  135. residual[1] = -29.0 + x1 + ((x2 + 1.0) * x2 - 14.0) * x2;
  136. END_MGH_PROBLEM;
  137. const double TestProblem2::initial_x[] = {0.5, -2.0};
  138. const double TestProblem2::lower_bounds[] = {-kDoubleMax, -kDoubleMax};
  139. const double TestProblem2::upper_bounds[] = {kDoubleMax, kDoubleMax};
  140. const double TestProblem2::constrained_optimal_cost =
  141. std::numeric_limits<double>::quiet_NaN();
  142. const double TestProblem2::unconstrained_optimal_cost = 0.0;
  143. // Powell badly scaled function.
  144. BEGIN_MGH_PROBLEM(TestProblem3, 2, 2)
  145. const T x1 = x[0];
  146. const T x2 = x[1];
  147. residual[0] = 10000.0 * x1 * x2 - 1.0;
  148. residual[1] = exp(-x1) + exp(-x2) - 1.0001;
  149. END_MGH_PROBLEM;
  150. const double TestProblem3::initial_x[] = {0.0, 1.0};
  151. const double TestProblem3::lower_bounds[] = {0.0, 1.0};
  152. const double TestProblem3::upper_bounds[] = {1.0, 9.0};
  153. const double TestProblem3::constrained_optimal_cost = 0.15125900e-9;
  154. const double TestProblem3::unconstrained_optimal_cost = 0.0;
  155. // Brown badly scaled function.
  156. BEGIN_MGH_PROBLEM(TestProblem4, 2, 3)
  157. const T x1 = x[0];
  158. const T x2 = x[1];
  159. residual[0] = x1 - 1000000.0;
  160. residual[1] = x2 - 0.000002;
  161. residual[2] = x1 * x2 - 2.0;
  162. END_MGH_PROBLEM;
  163. const double TestProblem4::initial_x[] = {1.0, 1.0};
  164. const double TestProblem4::lower_bounds[] = {0.0, 0.00003};
  165. const double TestProblem4::upper_bounds[] = {1000000.0, 100.0};
  166. const double TestProblem4::constrained_optimal_cost = 0.78400000e3;
  167. const double TestProblem4::unconstrained_optimal_cost = 0.0;
  168. // Beale function.
  169. BEGIN_MGH_PROBLEM(TestProblem5, 2, 3)
  170. const T x1 = x[0];
  171. const T x2 = x[1];
  172. residual[0] = 1.5 - x1 * (1.0 - x2);
  173. residual[1] = 2.25 - x1 * (1.0 - x2 * x2);
  174. residual[2] = 2.625 - x1 * (1.0 - x2 * x2 * x2);
  175. END_MGH_PROBLEM;
  176. const double TestProblem5::initial_x[] = {1.0, 1.0};
  177. const double TestProblem5::lower_bounds[] = {0.6, 0.5};
  178. const double TestProblem5::upper_bounds[] = {10.0, 100.0};
  179. const double TestProblem5::constrained_optimal_cost = 0.0;
  180. const double TestProblem5::unconstrained_optimal_cost = 0.0;
  181. // Jennrich and Sampson function.
  182. BEGIN_MGH_PROBLEM(TestProblem6, 2, 10)
  183. const T x1 = x[0];
  184. const T x2 = x[1];
  185. for (int i = 1; i <= 10; ++i) {
  186. residual[i - 1] = 2.0 + 2.0 * i -
  187. (exp(static_cast<double>(i) * x1) +
  188. exp(static_cast<double>(i) * x2));
  189. }
  190. END_MGH_PROBLEM;
  191. const double TestProblem6::initial_x[] = {1.0, 1.0};
  192. const double TestProblem6::lower_bounds[] = {-kDoubleMax, -kDoubleMax};
  193. const double TestProblem6::upper_bounds[] = {kDoubleMax, kDoubleMax};
  194. const double TestProblem6::constrained_optimal_cost =
  195. std::numeric_limits<double>::quiet_NaN();
  196. const double TestProblem6::unconstrained_optimal_cost = 124.362;
  197. // Helical valley function.
  198. BEGIN_MGH_PROBLEM(TestProblem7, 3, 3)
  199. const T x1 = x[0];
  200. const T x2 = x[1];
  201. const T x3 = x[2];
  202. const T theta = (0.5 / M_PI) * atan(x2 / x1) + (x1 > 0.0 ? 0.0 : 0.5);
  203. residual[0] = 10.0 * (x3 - 10.0 * theta);
  204. residual[1] = 10.0 * (sqrt(x1 * x1 + x2 * x2) - 1.0);
  205. residual[2] = x3;
  206. END_MGH_PROBLEM;
  207. const double TestProblem7::initial_x[] = {-1.0, 0.0, 0.0};
  208. const double TestProblem7::lower_bounds[] = {-100.0, -1.0, -1.0};
  209. const double TestProblem7::upper_bounds[] = {0.8, 1.0, 1.0};
  210. const double TestProblem7::constrained_optimal_cost = 0.99042212;
  211. const double TestProblem7::unconstrained_optimal_cost = 0.0;
  212. // Bard function
  213. BEGIN_MGH_PROBLEM(TestProblem8, 3, 15)
  214. const T x1 = x[0];
  215. const T x2 = x[1];
  216. const T x3 = x[2];
  217. double y[] = {0.14, 0.18, 0.22, 0.25,
  218. 0.29, 0.32, 0.35, 0.39, 0.37, 0.58,
  219. 0.73, 0.96, 1.34, 2.10, 4.39};
  220. for (int i = 1; i <=15; ++i) {
  221. const double u = static_cast<double>(i);
  222. const double v = static_cast<double>(16 - i);
  223. const double w = static_cast<double>(std::min(i, 16 - i));
  224. residual[i - 1] = y[i - 1] - (x1 + u / (v * x2 + w * x3));
  225. }
  226. END_MGH_PROBLEM;
  227. const double TestProblem8::initial_x[] = {1.0, 1.0, 1.0};
  228. const double TestProblem8::lower_bounds[] = {
  229. -kDoubleMax, -kDoubleMax, -kDoubleMax};
  230. const double TestProblem8::upper_bounds[] = {
  231. kDoubleMax, kDoubleMax, kDoubleMax};
  232. const double TestProblem8::constrained_optimal_cost =
  233. std::numeric_limits<double>::quiet_NaN();
  234. const double TestProblem8::unconstrained_optimal_cost = 8.21487e-3;
  235. // Gaussian function.
  236. BEGIN_MGH_PROBLEM(TestProblem9, 3, 15)
  237. const T x1 = x[0];
  238. const T x2 = x[1];
  239. const T x3 = x[2];
  240. const double y[] = {0.0009, 0.0044, 0.0175, 0.0540, 0.1295, 0.2420, 0.3521,
  241. 0.3989,
  242. 0.3521, 0.2420, 0.1295, 0.0540, 0.0175, 0.0044, 0.0009};
  243. for (int i = 0; i < 15; ++i) {
  244. const double t_i = (8.0 - i - 1.0) / 2.0;
  245. residual[i] = x1 * exp(-x2 * (t_i - x3) * (t_i - x3) / 2.0) - y[i];
  246. }
  247. END_MGH_PROBLEM;
  248. const double TestProblem9::initial_x[] = {0.4, 1.0, 0.0};
  249. const double TestProblem9::lower_bounds[] = {0.398, 1.0, -0.5};
  250. const double TestProblem9::upper_bounds[] = {4.2, 2.0, 0.1};
  251. const double TestProblem9::constrained_optimal_cost = 0.11279300e-7;
  252. const double TestProblem9::unconstrained_optimal_cost = 0.112793e-7;
  253. // Meyer function.
  254. BEGIN_MGH_PROBLEM(TestProblem10, 3, 16)
  255. const T x1 = x[0];
  256. const T x2 = x[1];
  257. const T x3 = x[2];
  258. const double y[] = {34780, 28610, 23650, 19630, 16370, 13720, 11540, 9744,
  259. 8261, 7030, 6005, 5147, 4427, 3820, 3307, 2872};
  260. for (int i = 0; i < 16; ++i) {
  261. const double ti = 45.0 + 5.0 * (i + 1);
  262. residual[i] = x1 * exp(x2 / (ti + x3)) - y[i];
  263. }
  264. END_MGH_PROBLEM
  265. const double TestProblem10::initial_x[] = {0.02, 4000, 250};
  266. const double TestProblem10::lower_bounds[] = {
  267. -kDoubleMax, -kDoubleMax, -kDoubleMax};
  268. const double TestProblem10::upper_bounds[] = {
  269. kDoubleMax, kDoubleMax, kDoubleMax};
  270. const double TestProblem10::constrained_optimal_cost =
  271. std::numeric_limits<double>::quiet_NaN();
  272. const double TestProblem10::unconstrained_optimal_cost = 87.9458;
  273. // Gulf research and development function
  274. BEGIN_MGH_PROBLEM(TestProblem11, 3, 100)
  275. const T x1 = x[0];
  276. const T x2 = x[1];
  277. const T x3 = x[2];
  278. for (int i = 1; i <= 100; ++i) {
  279. const double ti = static_cast<double>(i) / 100.0;
  280. const double yi = 25.0 + pow(-50.0 * log(ti), 2.0 / 3.0);
  281. residual[i - 1] = exp(-pow(abs((yi * 100.0 * i) * x2), x3) / x1) - ti;
  282. }
  283. END_MGH_PROBLEM
  284. const double TestProblem11::initial_x[] = {5.0, 2.5, 0.15};
  285. const double TestProblem11::lower_bounds[] = {1e-16, 0.0, 0.0};
  286. const double TestProblem11::upper_bounds[] = {10.0, 10.0, 10.0};
  287. const double TestProblem11::constrained_optimal_cost = 0.58281431e-4;
  288. const double TestProblem11::unconstrained_optimal_cost = 0.0;
  289. // Box three-dimensional function.
  290. BEGIN_MGH_PROBLEM(TestProblem12, 3, 3)
  291. const T x1 = x[0];
  292. const T x2 = x[1];
  293. const T x3 = x[2];
  294. const double t1 = 0.1;
  295. const double t2 = 0.2;
  296. const double t3 = 0.3;
  297. residual[0] = exp(-t1 * x1) - exp(-t1 * x2) - x3 * (exp(-t1) - exp(-10.0 * t1));
  298. residual[1] = exp(-t2 * x1) - exp(-t2 * x2) - x3 * (exp(-t2) - exp(-10.0 * t2));
  299. residual[2] = exp(-t3 * x1) - exp(-t3 * x2) - x3 * (exp(-t3) - exp(-10.0 * t3));
  300. END_MGH_PROBLEM
  301. const double TestProblem12::initial_x[] = {0.0, 10.0, 20.0};
  302. const double TestProblem12::lower_bounds[] = {0.0, 5.0, 0.0};
  303. const double TestProblem12::upper_bounds[] = {2.0, 9.5, 20.0};
  304. const double TestProblem12::constrained_optimal_cost = 0.30998153e-5;
  305. const double TestProblem12::unconstrained_optimal_cost = 0.0;
  306. // Powell Singular function.
  307. BEGIN_MGH_PROBLEM(TestProblem13, 4, 4)
  308. const T x1 = x[0];
  309. const T x2 = x[1];
  310. const T x3 = x[2];
  311. const T x4 = x[3];
  312. residual[0] = x1 + 10.0 * x2;
  313. residual[1] = sqrt(5.0) * (x3 - x4);
  314. residual[2] = (x2 - 2.0 * x3) * (x2 - 2.0 * x3);
  315. residual[3] = sqrt(10.0) * (x1 - x4) * (x1 - x4);
  316. END_MGH_PROBLEM
  317. const double TestProblem13::initial_x[] = {3.0, -1.0, 0.0, 1.0};
  318. const double TestProblem13::lower_bounds[] = {
  319. -kDoubleMax, -kDoubleMax, -kDoubleMax};
  320. const double TestProblem13::upper_bounds[] = {
  321. kDoubleMax, kDoubleMax, kDoubleMax};
  322. const double TestProblem13::constrained_optimal_cost =
  323. std::numeric_limits<double>::quiet_NaN();
  324. const double TestProblem13::unconstrained_optimal_cost = 0.0;
  325. // Wood function.
  326. BEGIN_MGH_PROBLEM(TestProblem14, 4, 6)
  327. const T x1 = x[0];
  328. const T x2 = x[1];
  329. const T x3 = x[2];
  330. const T x4 = x[3];
  331. residual[0] = 10.0 * (x2 - x1 * x1);
  332. residual[1] = 1.0 - x1;
  333. residual[2] = sqrt(90.0) * (x4 - x3 * x3);
  334. residual[3] = 1.0 - x3;
  335. residual[4] = sqrt(10.0) * (x2 + x4 - 2.0);
  336. residual[5] = 1.0 / sqrt(10.0) * (x2 - x4);
  337. END_MGH_PROBLEM;
  338. const double TestProblem14::initial_x[] = {-3.0, -1.0, -3.0, -1.0};
  339. const double TestProblem14::lower_bounds[] = {-100.0, -100.0, -100.0, -100.0};
  340. const double TestProblem14::upper_bounds[] = {0.0, 10.0, 100.0, 100.0};
  341. const double TestProblem14::constrained_optimal_cost = 0.15567008e1;
  342. const double TestProblem14::unconstrained_optimal_cost = 0.0;
  343. // Kowalik and Osborne function.
  344. BEGIN_MGH_PROBLEM(TestProblem15, 4, 11)
  345. const T x1 = x[0];
  346. const T x2 = x[1];
  347. const T x3 = x[2];
  348. const T x4 = x[3];
  349. const double y[] = {0.1957, 0.1947, 0.1735, 0.1600, 0.0844, 0.0627,
  350. 0.0456, 0.0342, 0.0323, 0.0235, 0.0246};
  351. const double u[] = {4.0, 2.0, 1.0, 0.5, 0.25, 0.167, 0.125, 0.1,
  352. 0.0833, 0.0714, 0.0625};
  353. for (int i = 0; i < 11; ++i) {
  354. residual[i] = y[i] - x1 * (u[i] * u[i] + u[i] * x2) / (u[i] * u[i] + u[i] * x3 + x4);
  355. }
  356. END_MGH_PROBLEM;
  357. const double TestProblem15::initial_x[] = {0.25, 0.39, 0.415, 0.39};
  358. const double TestProblem15::lower_bounds[] = {
  359. -kDoubleMax, -kDoubleMax, -kDoubleMax, -kDoubleMax};
  360. const double TestProblem15::upper_bounds[] = {
  361. kDoubleMax, kDoubleMax, kDoubleMax, kDoubleMax};
  362. const double TestProblem15::constrained_optimal_cost =
  363. std::numeric_limits<double>::quiet_NaN();
  364. const double TestProblem15::unconstrained_optimal_cost = 3.07505e-4;
  365. // Brown and Dennis function.
  366. BEGIN_MGH_PROBLEM(TestProblem16, 4, 20)
  367. const T x1 = x[0];
  368. const T x2 = x[1];
  369. const T x3 = x[2];
  370. const T x4 = x[3];
  371. for (int i = 0; i < 20; ++i) {
  372. const double ti = static_cast<double>(i + 1) / 5.0;
  373. residual[i] = (x1 + ti * x2 - exp(ti)) * (x1 + ti * x2 - exp(ti)) +
  374. (x3 + x4 * sin(ti) - cos(ti)) * (x3 + x4 * sin(ti) - cos(ti));
  375. }
  376. END_MGH_PROBLEM;
  377. const double TestProblem16::initial_x[] = {25.0, 5.0, -5.0, -1.0};
  378. const double TestProblem16::lower_bounds[] = {-10.0, 0.0, -100.0, -20.0};
  379. const double TestProblem16::upper_bounds[] = {100.0, 15.0, 0.0, 0.2};
  380. const double TestProblem16::constrained_optimal_cost = 0.88860479e5;
  381. const double TestProblem16::unconstrained_optimal_cost = 85822.2;
  382. // Osborne 1 function.
  383. BEGIN_MGH_PROBLEM(TestProblem17, 5, 33)
  384. const T x1 = x[0];
  385. const T x2 = x[1];
  386. const T x3 = x[2];
  387. const T x4 = x[3];
  388. const T x5 = x[4];
  389. const double y[] = {0.844, 0.908, 0.932, 0.936, 0.925, 0.908, 0.881, 0.850, 0.818,
  390. 0.784, 0.751, 0.718, 0.685, 0.658, 0.628, 0.603, 0.580, 0.558,
  391. 0.538, 0.522, 0.506, 0.490, 0.478, 0.467, 0.457, 0.448, 0.438,
  392. 0.431, 0.424, 0.420, 0.414, 0.411, 0.406};
  393. for (int i = 0; i < 33; ++i) {
  394. const double ti = 10.0 * i;
  395. residual[i] = y[i] - (x1 + x2 * exp(-ti * x4) + x3 * exp(-ti * x5));
  396. }
  397. END_MGH_PROBLEM;
  398. const double TestProblem17::initial_x[] = {0.5, 1.5, -1.0, 0.01, 0.02};
  399. const double TestProblem17::lower_bounds[] = {
  400. -kDoubleMax, -kDoubleMax, -kDoubleMax, -kDoubleMax};
  401. const double TestProblem17::upper_bounds[] = {
  402. kDoubleMax, kDoubleMax, kDoubleMax, kDoubleMax};
  403. const double TestProblem17::constrained_optimal_cost =
  404. std::numeric_limits<double>::quiet_NaN();
  405. const double TestProblem17::unconstrained_optimal_cost = 5.46489e-5;
  406. // Biggs EXP6 function.
  407. BEGIN_MGH_PROBLEM(TestProblem18, 6, 13)
  408. const T x1 = x[0];
  409. const T x2 = x[1];
  410. const T x3 = x[2];
  411. const T x4 = x[3];
  412. const T x5 = x[4];
  413. const T x6 = x[5];
  414. for (int i = 0; i < 13; ++i) {
  415. const double ti = 0.1 * (i + 1.0);
  416. const double yi = exp(-ti) - 5.0 * exp(-10.0 * ti) + 3.0 * exp(-4.0 * ti);
  417. residual[i] =x3 * exp(-ti * x1) - x4 * exp(-ti * x2) + x6 * exp(-ti * x5) - yi;
  418. }
  419. END_MGH_PROBLEM
  420. const double TestProblem18::initial_x[] = {1.0, 2.0, 1.0, 1.0, 1.0, 1.0};
  421. const double TestProblem18::lower_bounds[] = {0.0, 0.0, 0.0, 1.0, 0.0, 0.0};
  422. const double TestProblem18::upper_bounds[] = {2.0, 8.0, 1.0, 7.0, 5.0, 5.0};
  423. const double TestProblem18::constrained_optimal_cost = 0.53209865e-3;
  424. const double TestProblem18::unconstrained_optimal_cost = 0.0;
  425. // Osborne 2 function.
  426. BEGIN_MGH_PROBLEM(TestProblem19, 11, 65)
  427. const T x1 = x[0];
  428. const T x2 = x[1];
  429. const T x3 = x[2];
  430. const T x4 = x[3];
  431. const T x5 = x[4];
  432. const T x6 = x[5];
  433. const T x7 = x[6];
  434. const T x8 = x[7];
  435. const T x9 = x[8];
  436. const T x10 = x[9];
  437. const T x11 = x[10];
  438. const double y[] = {1.366, 1.191, 1.112, 1.013, 0.991,
  439. 0.885, 0.831, 0.847, 0.786, 0.725,
  440. 0.746, 0.679, 0.608, 0.655, 0.616,
  441. 0.606, 0.602, 0.626, 0.651, 0.724,
  442. 0.649, 0.649, 0.694, 0.644, 0.624,
  443. 0.661, 0.612, 0.558, 0.533, 0.495,
  444. 0.500, 0.423, 0.395, 0.375, 0.372,
  445. 0.391, 0.396, 0.405, 0.428, 0.429,
  446. 0.523, 0.562, 0.607, 0.653, 0.672,
  447. 0.708, 0.633, 0.668, 0.645, 0.632,
  448. 0.591, 0.559, 0.597, 0.625, 0.739,
  449. 0.710, 0.729, 0.720, 0.636, 0.581,
  450. 0.428, 0.292, 0.162, 0.098, 0.054};
  451. for (int i = 0; i < 65; ++i) {
  452. const double ti = static_cast<double>(i) / 10.0;
  453. residual[i] = y[i] - (x1 * exp(-(ti * x5)) +
  454. x2 * exp(-(ti - x9) * (ti - x9) * x6) +
  455. x3 * exp(-(ti - x10) * (ti - x10) * x7) +
  456. x4 * exp(-(ti - x11) * (ti - x11) * x8));
  457. }
  458. END_MGH_PROBLEM;
  459. const double TestProblem19::initial_x[] = {1.3, 0.65, 0.65, 0.7, 0.6,
  460. 3.0, 5.0, 7.0, 2.0, 4.5, 5.5};
  461. const double TestProblem19::lower_bounds[] = {
  462. -kDoubleMax, -kDoubleMax, -kDoubleMax, -kDoubleMax};
  463. const double TestProblem19::upper_bounds[] = {
  464. kDoubleMax, kDoubleMax, kDoubleMax, kDoubleMax};
  465. const double TestProblem19::constrained_optimal_cost =
  466. std::numeric_limits<double>::quiet_NaN();
  467. const double TestProblem19::unconstrained_optimal_cost = 4.01377e-2;
  468. #undef BEGIN_MGH_PROBLEM
  469. #undef END_MGH_PROBLEM
  470. template<typename TestProblem> bool Solve(bool is_constrained, int trial) {
  471. double x[TestProblem::kNumParameters];
  472. for (int i = 0; i < TestProblem::kNumParameters; ++i) {
  473. x[i] = pow(10, trial) * TestProblem::initial_x[i];
  474. }
  475. Problem problem;
  476. problem.AddResidualBlock(TestProblem::Create(), NULL, x);
  477. double optimal_cost = TestProblem::unconstrained_optimal_cost;
  478. if (is_constrained) {
  479. for (int i = 0; i < TestProblem::kNumParameters; ++i) {
  480. problem.SetParameterLowerBound(x, i, TestProblem::lower_bounds[i]);
  481. problem.SetParameterUpperBound(x, i, TestProblem::upper_bounds[i]);
  482. }
  483. optimal_cost = TestProblem::constrained_optimal_cost;
  484. }
  485. Solver::Options options;
  486. options.parameter_tolerance = 1e-18;
  487. options.function_tolerance = 1e-18;
  488. options.gradient_tolerance = 1e-18;
  489. options.max_num_iterations = 1000;
  490. options.linear_solver_type = DENSE_QR;
  491. Solver::Summary summary;
  492. Solve(options, &problem, &summary);
  493. const double kMinLogRelativeError = 4.0;
  494. const double log_relative_error = -std::log10(
  495. std::abs(2.0 * summary.final_cost - optimal_cost) /
  496. (optimal_cost > 0.0 ? optimal_cost : 1.0));
  497. const bool success = log_relative_error >= kMinLogRelativeError;
  498. LOG(INFO) << "Expected : " << optimal_cost
  499. << " actual: " << 2.0 * summary.final_cost
  500. << " " << success
  501. << " in " << summary.total_time_in_seconds
  502. << " seconds";
  503. return success;
  504. }
  505. } // namespace examples
  506. } // namespace ceres
  507. int main(int argc, char** argv) {
  508. CERES_GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true);
  509. google::InitGoogleLogging(argv[0]);
  510. using ceres::examples::Solve;
  511. int unconstrained_problems = 0;
  512. int unconstrained_successes = 0;
  513. int constrained_problems = 0;
  514. int constrained_successes = 0;
  515. std::stringstream ss;
  516. #define UNCONSTRAINED_SOLVE(n) \
  517. ss << "Unconstrained Problem " << n << " : "; \
  518. if (FLAGS_problem == #n || FLAGS_problem == "all") { \
  519. unconstrained_problems += 3; \
  520. if (Solve<ceres::examples::TestProblem##n>(false, 0)) { \
  521. unconstrained_successes += 1; \
  522. ss << "Yes "; \
  523. } else { \
  524. ss << "No "; \
  525. } \
  526. if (Solve<ceres::examples::TestProblem##n>(false, 1)) { \
  527. unconstrained_successes += 1; \
  528. ss << "Yes "; \
  529. } else { \
  530. ss << "No "; \
  531. } \
  532. if (Solve<ceres::examples::TestProblem##n>(false, 2)) { \
  533. unconstrained_successes += 1; \
  534. ss << "Yes "; \
  535. } else { \
  536. ss << "No "; \
  537. } \
  538. } \
  539. ss << std::endl;
  540. UNCONSTRAINED_SOLVE(1);
  541. UNCONSTRAINED_SOLVE(2);
  542. UNCONSTRAINED_SOLVE(3);
  543. UNCONSTRAINED_SOLVE(4);
  544. UNCONSTRAINED_SOLVE(5);
  545. UNCONSTRAINED_SOLVE(6);
  546. UNCONSTRAINED_SOLVE(7);
  547. UNCONSTRAINED_SOLVE(8);
  548. UNCONSTRAINED_SOLVE(9);
  549. UNCONSTRAINED_SOLVE(10);
  550. UNCONSTRAINED_SOLVE(11);
  551. UNCONSTRAINED_SOLVE(12);
  552. UNCONSTRAINED_SOLVE(13);
  553. UNCONSTRAINED_SOLVE(14);
  554. UNCONSTRAINED_SOLVE(15);
  555. UNCONSTRAINED_SOLVE(16);
  556. UNCONSTRAINED_SOLVE(17);
  557. UNCONSTRAINED_SOLVE(18);
  558. UNCONSTRAINED_SOLVE(19);
  559. ss << "Unconstrained : "
  560. << unconstrained_successes
  561. << "/"
  562. << unconstrained_problems << std::endl;
  563. #define CONSTRAINED_SOLVE(n) \
  564. ss << "Constrained Problem " << n << " : "; \
  565. if (FLAGS_problem == #n || FLAGS_problem == "all") { \
  566. constrained_problems += 1; \
  567. if (Solve<ceres::examples::TestProblem##n>(true, 0)) { \
  568. constrained_successes += 1; \
  569. ss << "Yes "; \
  570. } else { \
  571. ss << "No "; \
  572. } \
  573. } \
  574. ss << std::endl;
  575. CONSTRAINED_SOLVE(3);
  576. CONSTRAINED_SOLVE(4);
  577. CONSTRAINED_SOLVE(5);
  578. CONSTRAINED_SOLVE(7);
  579. CONSTRAINED_SOLVE(9);
  580. CONSTRAINED_SOLVE(11);
  581. CONSTRAINED_SOLVE(12);
  582. CONSTRAINED_SOLVE(14);
  583. CONSTRAINED_SOLVE(16);
  584. CONSTRAINED_SOLVE(18);
  585. ss << "Constrained : "
  586. << constrained_successes
  587. << "/"
  588. << constrained_problems << std::endl;
  589. std::cout << ss.str();
  590. return 0;
  591. }