/scipy/optimize/tests/test_trustregion.py

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