test_trustregion.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. """
  2. Unit tests for trust-region optimization routines.
  3. To run it in its simplest form::
  4. nosetests test_optimize.py
  5. """
  6. from __future__ import division, print_function, absolute_import
  7. import numpy as np
  8. from scipy.optimize import (minimize, rosen, rosen_der, rosen_hess,
  9. rosen_hess_prod)
  10. from numpy.testing import assert_, assert_equal, assert_allclose
  11. class Accumulator:
  12. """ This is for testing callbacks."""
  13. def __init__(self):
  14. self.count = 0
  15. self.accum = None
  16. def __call__(self, x):
  17. self.count += 1
  18. if self.accum is None:
  19. self.accum = np.array(x)
  20. else:
  21. self.accum += x
  22. class TestTrustRegionSolvers(object):
  23. def setup_method(self):
  24. self.x_opt = [1.0, 1.0]
  25. self.easy_guess = [2.0, 2.0]
  26. self.hard_guess = [-1.2, 1.0]
  27. def test_dogleg_accuracy(self):
  28. # test the accuracy and the return_all option
  29. x0 = self.hard_guess
  30. r = minimize(rosen, x0, jac=rosen_der, hess=rosen_hess, tol=1e-8,
  31. method='dogleg', options={'return_all': True},)
  32. assert_allclose(x0, r['allvecs'][0])
  33. assert_allclose(r['x'], r['allvecs'][-1])
  34. assert_allclose(r['x'], self.x_opt)
  35. def test_dogleg_callback(self):
  36. # test the callback mechanism and the maxiter and return_all options
  37. accumulator = Accumulator()
  38. maxiter = 5
  39. r = minimize(rosen, self.hard_guess, jac=rosen_der, hess=rosen_hess,
  40. callback=accumulator, method='dogleg',
  41. options={'return_all': True, 'maxiter': maxiter},)
  42. assert_equal(accumulator.count, maxiter)
  43. assert_equal(len(r['allvecs']), maxiter+1)
  44. assert_allclose(r['x'], r['allvecs'][-1])
  45. assert_allclose(sum(r['allvecs'][1:]), accumulator.accum)
  46. def test_solver_concordance(self):
  47. # Assert that dogleg uses fewer iterations than ncg on the Rosenbrock
  48. # test function, although this does not necessarily mean
  49. # that dogleg is faster or better than ncg even for this function
  50. # and especially not for other test functions.
  51. f = rosen
  52. g = rosen_der
  53. h = rosen_hess
  54. for x0 in (self.easy_guess, self.hard_guess):
  55. r_dogleg = minimize(f, x0, jac=g, hess=h, tol=1e-8,
  56. method='dogleg', options={'return_all': True})
  57. r_trust_ncg = minimize(f, x0, jac=g, hess=h, tol=1e-8,
  58. method='trust-ncg',
  59. options={'return_all': True})
  60. r_trust_krylov = minimize(f, x0, jac=g, hess=h, tol=1e-8,
  61. method='trust-krylov',
  62. options={'return_all': True})
  63. r_ncg = minimize(f, x0, jac=g, hess=h, tol=1e-8,
  64. method='newton-cg', options={'return_all': True})
  65. r_iterative = minimize(f, x0, jac=g, hess=h, tol=1e-8,
  66. method='trust-exact',
  67. options={'return_all': True})
  68. assert_allclose(self.x_opt, r_dogleg['x'])
  69. assert_allclose(self.x_opt, r_trust_ncg['x'])
  70. assert_allclose(self.x_opt, r_trust_krylov['x'])
  71. assert_allclose(self.x_opt, r_ncg['x'])
  72. assert_allclose(self.x_opt, r_iterative['x'])
  73. assert_(len(r_dogleg['allvecs']) < len(r_ncg['allvecs']))
  74. def test_trust_ncg_hessp(self):
  75. for x0 in (self.easy_guess, self.hard_guess, self.x_opt):
  76. r = minimize(rosen, x0, jac=rosen_der, hessp=rosen_hess_prod,
  77. tol=1e-8, method='trust-ncg')
  78. assert_allclose(self.x_opt, r['x'])
  79. def test_trust_ncg_start_in_optimum(self):
  80. r = minimize(rosen, x0=self.x_opt, jac=rosen_der, hess=rosen_hess,
  81. tol=1e-8, method='trust-ncg')
  82. assert_allclose(self.x_opt, r['x'])
  83. def test_trust_krylov_start_in_optimum(self):
  84. r = minimize(rosen, x0=self.x_opt, jac=rosen_der, hess=rosen_hess,
  85. tol=1e-8, method='trust-krylov')
  86. assert_allclose(self.x_opt, r['x'])
  87. def test_trust_exact_start_in_optimum(self):
  88. r = minimize(rosen, x0=self.x_opt, jac=rosen_der, hess=rosen_hess,
  89. tol=1e-8, method='trust-exact')
  90. assert_allclose(self.x_opt, r['x'])