TestDownhillSimplex.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #ifdef NICE_USELIB_CPPUNIT
  2. #include <string>
  3. #include <exception>
  4. #include <map>
  5. #include "TestDownhillSimplex.h"
  6. using namespace std;
  7. using namespace OPTIMIZATION;
  8. const bool verboseStartEnd = true;
  9. // const bool verbose = true;
  10. const bool verbose = false;
  11. CPPUNIT_TEST_SUITE_REGISTRATION( TestDownhillSimplex );
  12. void TestDownhillSimplex::setUp() {
  13. }
  14. void TestDownhillSimplex::tearDown() {
  15. }
  16. //define a simple cost function for one-dimensional or two-dimensional data
  17. class MyCostFunction : public CostFunction
  18. {
  19. public:
  20. MyCostFunction(const int & dim) : CostFunction(dim)
  21. {
  22. }
  23. virtual double evaluate(const OPTIMIZATION::matrix_type & x)
  24. {
  25. double f;
  26. if (verbose)
  27. std::cerr << x.rows() << " x " << x.cols() << std::endl;
  28. if ( x.rows() == 1 )
  29. {
  30. if (verbose)
  31. std::cerr << "current position: " << x(0,0) << std::endl;
  32. //our cost function is f(x) = (x-5)^2
  33. f = pow(x(0,0) - 4.2, 2.0);
  34. if (verbose)
  35. std::cerr << "function value: " << f << std::endl;
  36. }
  37. //two-dimensional data
  38. else {
  39. if (verbose)
  40. std::cerr << "current position: " << x(0,0) << " " << x(1,0) << std::endl;
  41. //our cost function is f(x,y) = (x-4.7)^2 + (y-1.1)^2
  42. f = pow(x(0,0) - 4.7, 2.0) + pow( x(1,0) - 1.1, 2.0 );
  43. if (verbose)
  44. std::cerr << "function value: " << f << std::endl;
  45. }
  46. return f;
  47. }
  48. };
  49. void TestDownhillSimplex::testDHS_1Dim ()
  50. {
  51. if (verboseStartEnd)
  52. std::cerr << "================== TestDownhillSimplex::testDHS_1Dim ===================== " << std::endl;
  53. int dim (1);
  54. CostFunction *func = new MyCostFunction(dim);
  55. //initial guess: 2.0
  56. OPTIMIZATION::matrix_type initialParams (dim, 1, 2.0);
  57. //we search with step-width of 1.0
  58. OPTIMIZATION::matrix_type scales (dim, 1, 1.0);
  59. //setup the OPTIMIZATION:: problem
  60. SimpleOptProblem optProblem ( func, initialParams, scales );
  61. DownhillSimplexOptimizer optimizer;
  62. //actually, this has no effect at all
  63. optimizer.setMaxNumIter(true, 100);
  64. optimizer.optimizeProb ( optProblem );
  65. OPTIMIZATION::matrix_type optimizedParams (optProblem.getAllCurrentParams());
  66. double goal(4.2);
  67. if (verbose)
  68. std::cerr << "1d OPTIMIZATION:: -- result " << optimizedParams(0,0) << " -- goal: " << goal << std::endl;
  69. CPPUNIT_ASSERT_DOUBLES_EQUAL( optimizedParams(0,0), goal, 1e-7 /* tolerance */);
  70. if (verboseStartEnd)
  71. std::cerr << "================== TestDownhillSimplex::testDHS_1Dim done ===================== " << std::endl;
  72. }
  73. void TestDownhillSimplex::testDHS_2Dim()
  74. {
  75. if (verboseStartEnd)
  76. std::cerr << "================== TestDownhillSimplex::testDHS_2Dim ===================== " << std::endl;
  77. int dim (2);
  78. CostFunction *func = new MyCostFunction(dim);
  79. //initial guess: 2.0
  80. OPTIMIZATION::matrix_type initialParams (dim, 1, 2.0);
  81. //we search with step-width of 1.0
  82. OPTIMIZATION::matrix_type scales (dim, 1, 1.0);
  83. //setup the OPTIMIZATION:: problem
  84. SimpleOptProblem optProblem ( func, initialParams, scales );
  85. DownhillSimplexOptimizer optimizer;
  86. //actually, this has no effect at all
  87. optimizer.setMaxNumIter(true, 100);
  88. optimizer.optimizeProb ( optProblem );
  89. OPTIMIZATION::matrix_type optimizedParams (optProblem.getAllCurrentParams());
  90. double goalFirstDim(4.7);
  91. double goalSecondDim(1.1);
  92. if (verbose)
  93. {
  94. std::cerr << "2d OPTIMIZATION:: 1st dim-- result " << optimizedParams(0,0) << " -- goal: " << goalFirstDim << std::endl;
  95. std::cerr << "2d OPTIMIZATION:: 1st dim-- result " << optimizedParams(1,0) << " -- goal: " << goalSecondDim << std::endl;
  96. }
  97. CPPUNIT_ASSERT_DOUBLES_EQUAL( optimizedParams(0,0), goalFirstDim, 1e-7 /* tolerance */);
  98. CPPUNIT_ASSERT_DOUBLES_EQUAL( optimizedParams(1,0), goalSecondDim, 1e-7 /* tolerance */);
  99. if (verboseStartEnd)
  100. std::cerr << "================== TestDownhillSimplex::testDHS_2Dim done ===================== " << std::endl;
  101. }
  102. #endif