test_trustregion_krylov.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. """
  2. Unit tests for Krylov space trust-region subproblem solver.
  3. """
  4. import numpy as np
  5. from scipy.optimize._trlib import (get_trlib_quadratic_subproblem)
  6. from numpy.testing import (assert_,
  7. assert_almost_equal,
  8. assert_equal, assert_array_almost_equal)
  9. KrylovQP = get_trlib_quadratic_subproblem(tol_rel_i=1e-8, tol_rel_b=1e-6)
  10. KrylovQP_disp = get_trlib_quadratic_subproblem(tol_rel_i=1e-8, tol_rel_b=1e-6,
  11. disp=True)
  12. class TestKrylovQuadraticSubproblem:
  13. def test_for_the_easy_case(self):
  14. # `H` is chosen such that `g` is not orthogonal to the
  15. # eigenvector associated with the smallest eigenvalue.
  16. H = np.array([[1.0, 0.0, 4.0],
  17. [0.0, 2.0, 0.0],
  18. [4.0, 0.0, 3.0]])
  19. g = np.array([5.0, 0.0, 4.0])
  20. # Trust Radius
  21. trust_radius = 1.0
  22. # Solve Subproblem
  23. subprob = KrylovQP(x=0,
  24. fun=lambda x: 0,
  25. jac=lambda x: g,
  26. hess=lambda x: None,
  27. hessp=lambda x, y: H.dot(y))
  28. p, hits_boundary = subprob.solve(trust_radius)
  29. assert_array_almost_equal(p, np.array([-1.0, 0.0, 0.0]))
  30. assert_equal(hits_boundary, True)
  31. # check kkt satisfaction
  32. assert_almost_equal(
  33. np.linalg.norm(H.dot(p) + subprob.lam * p + g),
  34. 0.0)
  35. # check trust region constraint
  36. assert_almost_equal(np.linalg.norm(p), trust_radius)
  37. trust_radius = 0.5
  38. p, hits_boundary = subprob.solve(trust_radius)
  39. assert_array_almost_equal(p,
  40. np.array([-0.46125446, 0., -0.19298788]))
  41. assert_equal(hits_boundary, True)
  42. # check kkt satisfaction
  43. assert_almost_equal(
  44. np.linalg.norm(H.dot(p) + subprob.lam * p + g),
  45. 0.0)
  46. # check trust region constraint
  47. assert_almost_equal(np.linalg.norm(p), trust_radius)
  48. def test_for_the_hard_case(self):
  49. # `H` is chosen such that `g` is orthogonal to the
  50. # eigenvector associated with the smallest eigenvalue.
  51. H = np.array([[1.0, 0.0, 4.0],
  52. [0.0, 2.0, 0.0],
  53. [4.0, 0.0, 3.0]])
  54. g = np.array([0.0, 2.0, 0.0])
  55. # Trust Radius
  56. trust_radius = 1.0
  57. # Solve Subproblem
  58. subprob = KrylovQP(x=0,
  59. fun=lambda x: 0,
  60. jac=lambda x: g,
  61. hess=lambda x: None,
  62. hessp=lambda x, y: H.dot(y))
  63. p, hits_boundary = subprob.solve(trust_radius)
  64. assert_array_almost_equal(p, np.array([0.0, -1.0, 0.0]))
  65. # check kkt satisfaction
  66. assert_almost_equal(
  67. np.linalg.norm(H.dot(p) + subprob.lam * p + g),
  68. 0.0)
  69. # check trust region constraint
  70. assert_almost_equal(np.linalg.norm(p), trust_radius)
  71. trust_radius = 0.5
  72. p, hits_boundary = subprob.solve(trust_radius)
  73. assert_array_almost_equal(p, np.array([0.0, -0.5, 0.0]))
  74. # check kkt satisfaction
  75. assert_almost_equal(
  76. np.linalg.norm(H.dot(p) + subprob.lam * p + g),
  77. 0.0)
  78. # check trust region constraint
  79. assert_almost_equal(np.linalg.norm(p), trust_radius)
  80. def test_for_interior_convergence(self):
  81. H = np.array([[1.812159, 0.82687265, 0.21838879, -0.52487006, 0.25436988],
  82. [0.82687265, 2.66380283, 0.31508988, -0.40144163, 0.08811588],
  83. [0.21838879, 0.31508988, 2.38020726, -0.3166346, 0.27363867],
  84. [-0.52487006, -0.40144163, -0.3166346, 1.61927182, -0.42140166],
  85. [0.25436988, 0.08811588, 0.27363867, -0.42140166, 1.33243101]])
  86. g = np.array([0.75798952, 0.01421945, 0.33847612, 0.83725004, -0.47909534])
  87. trust_radius = 1.1
  88. # Solve Subproblem
  89. subprob = KrylovQP(x=0,
  90. fun=lambda x: 0,
  91. jac=lambda x: g,
  92. hess=lambda x: None,
  93. hessp=lambda x, y: H.dot(y))
  94. p, hits_boundary = subprob.solve(trust_radius)
  95. # check kkt satisfaction
  96. assert_almost_equal(
  97. np.linalg.norm(H.dot(p) + subprob.lam * p + g),
  98. 0.0)
  99. assert_array_almost_equal(p, [-0.68585435, 0.1222621, -0.22090999,
  100. -0.67005053, 0.31586769])
  101. assert_array_almost_equal(hits_boundary, False)
  102. def test_for_very_close_to_zero(self):
  103. H = np.array([[0.88547534, 2.90692271, 0.98440885, -0.78911503, -0.28035809],
  104. [2.90692271, -0.04618819, 0.32867263, -0.83737945, 0.17116396],
  105. [0.98440885, 0.32867263, -0.87355957, -0.06521957, -1.43030957],
  106. [-0.78911503, -0.83737945, -0.06521957, -1.645709, -0.33887298],
  107. [-0.28035809, 0.17116396, -1.43030957, -0.33887298, -1.68586978]])
  108. g = np.array([0, 0, 0, 0, 1e-6])
  109. trust_radius = 1.1
  110. # Solve Subproblem
  111. subprob = KrylovQP(x=0,
  112. fun=lambda x: 0,
  113. jac=lambda x: g,
  114. hess=lambda x: None,
  115. hessp=lambda x, y: H.dot(y))
  116. p, hits_boundary = subprob.solve(trust_radius)
  117. # check kkt satisfaction
  118. assert_almost_equal(
  119. np.linalg.norm(H.dot(p) + subprob.lam * p + g),
  120. 0.0)
  121. # check trust region constraint
  122. assert_almost_equal(np.linalg.norm(p), trust_radius)
  123. assert_array_almost_equal(p, [0.06910534, -0.01432721,
  124. -0.65311947, -0.23815972,
  125. -0.84954934])
  126. assert_array_almost_equal(hits_boundary, True)
  127. def test_disp(self, capsys):
  128. H = -np.eye(5)
  129. g = np.array([0, 0, 0, 0, 1e-6])
  130. trust_radius = 1.1
  131. subprob = KrylovQP_disp(x=0,
  132. fun=lambda x: 0,
  133. jac=lambda x: g,
  134. hess=lambda x: None,
  135. hessp=lambda x, y: H.dot(y))
  136. p, hits_boundary = subprob.solve(trust_radius)
  137. out, err = capsys.readouterr()
  138. assert_(out.startswith(' TR Solving trust region problem'), repr(out))