OptimizationTest.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzTest/AzTest.h>
  9. #include <NumericalMethods/Optimization.h>
  10. #include <Optimization/SolverBFGS.h>
  11. #include <Optimization/LineSearch.h>
  12. #include <Optimization/Constants.h>
  13. #include <Optimization/Utilities.h>
  14. #include <Tests/Environment.h>
  15. namespace NumericalMethods::Optimization
  16. {
  17. // The Rosenbrock function is a function commonly used to test optimization routines because it has a very long, narrow
  18. // valley
  19. struct
  20. {
  21. double a = 1.0;
  22. double b = 100.0;
  23. } RosenbrockConstants;
  24. class TestFunctionRosenbrock
  25. : public Optimization::Function
  26. {
  27. public:
  28. TestFunctionRosenbrock() = default;
  29. private:
  30. virtual AZ::u32 GetDimension() const override
  31. {
  32. return 2;
  33. }
  34. virtual AZ::Outcome<double, FunctionOutcome> ExecuteImpl(const AZStd::vector<double>& x) const override
  35. {
  36. return AZ::Success((RosenbrockConstants.a - x[0]) * (RosenbrockConstants.a - x[0]) +
  37. RosenbrockConstants.b * (x[1] - x[0] * x[0]) * (x[1] - x[0] * x[0]));
  38. }
  39. };
  40. VectorVariable TestFunctionRosenbrockGradient(const VectorVariable p)
  41. {
  42. double x = p[0];
  43. double y = p[1];
  44. VectorVariable gradient(2);
  45. gradient[0] = -2.0 * (RosenbrockConstants.a - x) - 4.0 * RosenbrockConstants.b * x * (y - x * x);
  46. gradient[1] = 2.0 * RosenbrockConstants.b * (y - x * x);
  47. return gradient;
  48. }
  49. TEST(OptimizationTest, FunctionValue_RosenbrockFunction_CorrectValues)
  50. {
  51. TestFunctionRosenbrock testFunctionRosenbrock;
  52. EXPECT_NEAR(FunctionValue(testFunctionRosenbrock, VectorVariable::CreateFromVector({ 1.0, 1.0 })), 0.0, 1e-3);
  53. EXPECT_NEAR(FunctionValue(testFunctionRosenbrock, VectorVariable::CreateFromVector({ 3.0, 5.0 })), 1604.0, 1e-3);
  54. EXPECT_NEAR(FunctionValue(testFunctionRosenbrock, VectorVariable::CreateFromVector({ -2.0, 4.0 })), 9.0, 1e-3);
  55. EXPECT_NEAR(FunctionValue(testFunctionRosenbrock, VectorVariable::CreateFromVector({ -3.0, 7.0 })), 416.0, 1e-3);
  56. EXPECT_NEAR(FunctionValue(testFunctionRosenbrock, VectorVariable::CreateFromVector({ 0.0, 5.0 })), 2501.0, 1e-3);
  57. EXPECT_NEAR(FunctionValue(testFunctionRosenbrock, VectorVariable::CreateFromVector({ 4.0, 0.0 })), 25609.0, 1e-3);
  58. }
  59. TEST(OptimizationTest, Gradient_RosenbrockFunction_CorrectGradient)
  60. {
  61. TestFunctionRosenbrock testFunctionRosenbrock;
  62. VectorVariable x(2);
  63. for (double x0 = -5.0; x0 < 6.0; x0 += 2.0)
  64. {
  65. for (double x1 = -5.0; x1 < 6.0; x1 += 2.0)
  66. {
  67. x[0] = x0;
  68. x[1] = x1;
  69. VectorVariable gradient = Gradient(testFunctionRosenbrock, x);
  70. VectorVariable expectedGradient = TestFunctionRosenbrockGradient(x);
  71. ExpectClose(gradient, expectedGradient, 1e-3);
  72. }
  73. }
  74. }
  75. TEST(OptimizationTest, DirectionalDerivative_RosenbrockFunction_CorrectDerivative)
  76. {
  77. TestFunctionRosenbrock testFunctionRosenbrock;
  78. VectorVariable x(2);
  79. x[0] = 3.0;
  80. x[1] = -4.0;
  81. VectorVariable direction(2);
  82. for (double d0 = -5.0; d0 < 6.0; d0 += 2.0)
  83. {
  84. for (double d1 = -5.0; d1 < 6.0; d1 += 2.0)
  85. {
  86. direction[0] = d0;
  87. direction[1] = d1;
  88. double directionalDerivative = DirectionalDerivative(testFunctionRosenbrock, x, direction);
  89. double expectedDirectionalDerivative = TestFunctionRosenbrockGradient(x).Dot(direction);
  90. EXPECT_NEAR(directionalDerivative, expectedDirectionalDerivative, 1e-3);
  91. }
  92. }
  93. }
  94. TEST(OptimizationTest, CubicMinimum_KnownCubic_CorrectMinimum)
  95. {
  96. double expectedMinimum = 3.0;
  97. double otherRoot = -7.0;
  98. double a = 0.0;
  99. double f_a = (a - expectedMinimum) * (a - expectedMinimum) * (a - otherRoot);
  100. double df_a = 2.0 * (a - expectedMinimum) * (a - otherRoot) + (a - expectedMinimum) * (a - expectedMinimum);
  101. double b = 5.0;
  102. double f_b = (b - expectedMinimum) * (b - expectedMinimum) * (b - otherRoot);
  103. double c = -3.0;
  104. double f_c = (c - expectedMinimum) * (c - expectedMinimum) * (c - otherRoot);
  105. double calculatedMinimum = CubicMinimum(a, f_a, df_a, b, f_b, c, f_c);
  106. EXPECT_NEAR(calculatedMinimum, expectedMinimum, 1e-3);
  107. }
  108. TEST(OptimizationTest, QuadraticMinimum_KnownQuadratic_CorrectMinimum)
  109. {
  110. double expectedMinimum = 2.0;
  111. double a = -1.0;
  112. double f_a = 5.0 * (a - expectedMinimum) * (a - expectedMinimum) + 7.0;
  113. double df_a = 10.0 * (a - expectedMinimum);
  114. double b = 1.0;
  115. double f_b = 5.0 * (b - expectedMinimum) * (b - expectedMinimum) + 7.0;
  116. double calculatedMinimum = QuadraticMinimum(a, f_a, df_a, b, f_b);
  117. EXPECT_NEAR(calculatedMinimum, expectedMinimum, 1e-3);
  118. }
  119. TEST(OptimizationTest, ValidateStepSize_ValidateStepSize_CorrectResult)
  120. {
  121. EXPECT_TRUE(ValidateStepSize(0.5, 0.0, 1.0, 0.1));
  122. EXPECT_FALSE(ValidateStepSize(0.05, 0.0, 1.0, 0.1));
  123. EXPECT_FALSE(ValidateStepSize(-0.5, 0.0, 1.0, 0.1));
  124. EXPECT_FALSE(ValidateStepSize(1.5, 0.0, 1.0, 0.1));
  125. EXPECT_TRUE(ValidateStepSize(1.5, 2.0, -1.0, 0.05));
  126. EXPECT_FALSE(ValidateStepSize(std::numeric_limits<double>::quiet_NaN(), 2.0, 0.0, 0.1));
  127. EXPECT_FALSE(ValidateStepSize(std::numeric_limits<double>::infinity(), -1.0, 3.0, 0.2));
  128. }
  129. TEST(OptimizationTest, LineSearch_SelectStepSizeFromInterval_SatisfiesWolfeConditions)
  130. {
  131. TestFunctionRosenbrock testFunctionRosenbrock;
  132. VectorVariable x0 = VectorVariable::CreateFromVector({ 7.0, 7.0 });
  133. VectorVariable searchDirection = VectorVariable::CreateFromVector({ -1.0, -1.0 });
  134. double alpha0 = 0.0;
  135. double alpha1 = 20.0;
  136. double f_alpha0 = FunctionValue(testFunctionRosenbrock, x0);
  137. double f_alpha1 = FunctionValue(testFunctionRosenbrock, x0 + alpha1 * searchDirection);
  138. double df_alpha0 = DirectionalDerivative(testFunctionRosenbrock, x0, searchDirection);
  139. double f_x0 = f_alpha0;
  140. double df_x0 = df_alpha0;
  141. LineSearchResult lineSearchResult = SelectStepSizeFromInterval(alpha0, alpha1, f_alpha0, f_alpha1, df_alpha0,
  142. testFunctionRosenbrock, x0, searchDirection, f_x0, df_x0, WolfeConditionsC1, WolfeConditionsC2);
  143. EXPECT_TRUE(lineSearchResult.m_outcome == LineSearchOutcome::Success);
  144. // check that the Wolfe conditions are satisfied by the returned step size
  145. EXPECT_TRUE(lineSearchResult.m_functionValue < f_x0 + WolfeConditionsC1 * df_x0 * lineSearchResult.m_stepSize);
  146. EXPECT_TRUE(fabs(lineSearchResult.m_derivativeValue) <= -WolfeConditionsC2 * df_x0);
  147. }
  148. TEST(OptimizationTest, LineSearch_VariousSearchDirections_SatisfiesWolfeCondition)
  149. {
  150. TestFunctionRosenbrock testFunctionRosenbrock;
  151. VectorVariable x0 = VectorVariable::CreateFromVector({ 7.0, 7.0 });
  152. AZStd::vector<AZStd::vector<double>> searchVectors = { { -1.0, -1.0 }, { -0.1, -0.2 }, { -8.0, -9.0 } };
  153. for (const auto& searchVector : searchVectors)
  154. {
  155. VectorVariable searchDirection = VectorVariable::CreateFromVector(searchVector);
  156. double f_x0 = FunctionValue(testFunctionRosenbrock, x0);
  157. double df_x0 = DirectionalDerivative(testFunctionRosenbrock, x0, searchDirection);
  158. LineSearchResult lineSearchResult = LineSearchWolfe(testFunctionRosenbrock, x0, f_x0, searchDirection);
  159. EXPECT_TRUE(lineSearchResult.m_outcome == LineSearchOutcome::Success);
  160. // check that the Wolfe conditions are satisfied by the returned step size
  161. EXPECT_TRUE(lineSearchResult.m_functionValue < f_x0 + WolfeConditionsC1 * df_x0 * lineSearchResult.m_stepSize);
  162. EXPECT_TRUE(fabs(lineSearchResult.m_derivativeValue) <= -WolfeConditionsC2 * df_x0);
  163. }
  164. }
  165. TEST(OptimizationTest, MinimizeBFGS_RosenbrockFunction_CorrectMinimum)
  166. {
  167. TestFunctionRosenbrock testFunctionRosenbrock;
  168. AZStd::vector<double> xInitial = { -7.0, 11.0 };
  169. SolverResult solverResult = MinimizeBFGS(testFunctionRosenbrock, xInitial);
  170. AZStd::vector<double> xExpected = { 1.0, 1.0 };
  171. ExpectClose(solverResult.m_xValues, xExpected, 1e-3);
  172. }
  173. } // namespace NumericalMethods::Optimization