test_tnc.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. """
  2. Unit tests for TNC optimization routine from tnc.py
  3. """
  4. import pytest
  5. from numpy.testing import assert_allclose, assert_equal
  6. import numpy as np
  7. from math import pow
  8. from scipy import optimize
  9. class TestTnc:
  10. """TNC non-linear optimization.
  11. These tests are taken from Prof. K. Schittkowski's test examples
  12. for constrained non-linear programming.
  13. http://www.uni-bayreuth.de/departments/math/~kschittkowski/home.htm
  14. """
  15. def setup_method(self):
  16. # options for minimize
  17. self.opts = {'disp': False, 'maxfun': 200}
  18. # objective functions and Jacobian for each test
  19. def f1(self, x, a=100.0):
  20. return a * pow((x[1] - pow(x[0], 2)), 2) + pow(1.0 - x[0], 2)
  21. def g1(self, x, a=100.0):
  22. dif = [0, 0]
  23. dif[1] = 2 * a * (x[1] - pow(x[0], 2))
  24. dif[0] = -2.0 * (x[0] * (dif[1] - 1.0) + 1.0)
  25. return dif
  26. def fg1(self, x, a=100.0):
  27. return self.f1(x, a), self.g1(x, a)
  28. def f3(self, x):
  29. return x[1] + pow(x[1] - x[0], 2) * 1.0e-5
  30. def g3(self, x):
  31. dif = [0, 0]
  32. dif[0] = -2.0 * (x[1] - x[0]) * 1.0e-5
  33. dif[1] = 1.0 - dif[0]
  34. return dif
  35. def fg3(self, x):
  36. return self.f3(x), self.g3(x)
  37. def f4(self, x):
  38. return pow(x[0] + 1.0, 3) / 3.0 + x[1]
  39. def g4(self, x):
  40. dif = [0, 0]
  41. dif[0] = pow(x[0] + 1.0, 2)
  42. dif[1] = 1.0
  43. return dif
  44. def fg4(self, x):
  45. return self.f4(x), self.g4(x)
  46. def f5(self, x):
  47. return np.sin(x[0] + x[1]) + pow(x[0] - x[1], 2) - \
  48. 1.5 * x[0] + 2.5 * x[1] + 1.0
  49. def g5(self, x):
  50. dif = [0, 0]
  51. v1 = np.cos(x[0] + x[1])
  52. v2 = 2.0*(x[0] - x[1])
  53. dif[0] = v1 + v2 - 1.5
  54. dif[1] = v1 - v2 + 2.5
  55. return dif
  56. def fg5(self, x):
  57. return self.f5(x), self.g5(x)
  58. def f38(self, x):
  59. return (100.0 * pow(x[1] - pow(x[0], 2), 2) +
  60. pow(1.0 - x[0], 2) + 90.0 * pow(x[3] - pow(x[2], 2), 2) +
  61. pow(1.0 - x[2], 2) + 10.1 * (pow(x[1] - 1.0, 2) +
  62. pow(x[3] - 1.0, 2)) +
  63. 19.8 * (x[1] - 1.0) * (x[3] - 1.0)) * 1.0e-5
  64. def g38(self, x):
  65. dif = [0, 0, 0, 0]
  66. dif[0] = (-400.0 * x[0] * (x[1] - pow(x[0], 2)) -
  67. 2.0 * (1.0 - x[0])) * 1.0e-5
  68. dif[1] = (200.0 * (x[1] - pow(x[0], 2)) + 20.2 * (x[1] - 1.0) +
  69. 19.8 * (x[3] - 1.0)) * 1.0e-5
  70. dif[2] = (- 360.0 * x[2] * (x[3] - pow(x[2], 2)) -
  71. 2.0 * (1.0 - x[2])) * 1.0e-5
  72. dif[3] = (180.0 * (x[3] - pow(x[2], 2)) + 20.2 * (x[3] - 1.0) +
  73. 19.8 * (x[1] - 1.0)) * 1.0e-5
  74. return dif
  75. def fg38(self, x):
  76. return self.f38(x), self.g38(x)
  77. def f45(self, x):
  78. return 2.0 - x[0] * x[1] * x[2] * x[3] * x[4] / 120.0
  79. def g45(self, x):
  80. dif = [0] * 5
  81. dif[0] = - x[1] * x[2] * x[3] * x[4] / 120.0
  82. dif[1] = - x[0] * x[2] * x[3] * x[4] / 120.0
  83. dif[2] = - x[0] * x[1] * x[3] * x[4] / 120.0
  84. dif[3] = - x[0] * x[1] * x[2] * x[4] / 120.0
  85. dif[4] = - x[0] * x[1] * x[2] * x[3] / 120.0
  86. return dif
  87. def fg45(self, x):
  88. return self.f45(x), self.g45(x)
  89. # tests
  90. # minimize with method=TNC
  91. def test_minimize_tnc1(self):
  92. x0, bnds = [-2, 1], ([-np.inf, None], [-1.5, None])
  93. xopt = [1, 1]
  94. iterx = [] # to test callback
  95. res = optimize.minimize(self.f1, x0, method='TNC', jac=self.g1,
  96. bounds=bnds, options=self.opts,
  97. callback=iterx.append)
  98. assert_allclose(res.fun, self.f1(xopt), atol=1e-8)
  99. assert_equal(len(iterx), res.nit)
  100. def test_minimize_tnc1b(self):
  101. x0, bnds = np.array([-2, 1]), ([-np.inf, None], [-1.5, None])
  102. xopt = [1, 1]
  103. x = optimize.minimize(self.f1, x0, method='TNC',
  104. bounds=bnds, options=self.opts).x
  105. assert_allclose(self.f1(x), self.f1(xopt), atol=1e-4)
  106. def test_minimize_tnc1c(self):
  107. x0, bnds = [-2, 1], ([-np.inf, None],[-1.5, None])
  108. xopt = [1, 1]
  109. x = optimize.minimize(self.fg1, x0, method='TNC',
  110. jac=True, bounds=bnds,
  111. options=self.opts).x
  112. assert_allclose(self.f1(x), self.f1(xopt), atol=1e-8)
  113. def test_minimize_tnc2(self):
  114. x0, bnds = [-2, 1], ([-np.inf, None], [1.5, None])
  115. xopt = [-1.2210262419616387, 1.5]
  116. x = optimize.minimize(self.f1, x0, method='TNC',
  117. jac=self.g1, bounds=bnds,
  118. options=self.opts).x
  119. assert_allclose(self.f1(x), self.f1(xopt), atol=1e-8)
  120. def test_minimize_tnc3(self):
  121. x0, bnds = [10, 1], ([-np.inf, None], [0.0, None])
  122. xopt = [0, 0]
  123. x = optimize.minimize(self.f3, x0, method='TNC',
  124. jac=self.g3, bounds=bnds,
  125. options=self.opts).x
  126. assert_allclose(self.f3(x), self.f3(xopt), atol=1e-8)
  127. def test_minimize_tnc4(self):
  128. x0,bnds = [1.125, 0.125], [(1, None), (0, None)]
  129. xopt = [1, 0]
  130. x = optimize.minimize(self.f4, x0, method='TNC',
  131. jac=self.g4, bounds=bnds,
  132. options=self.opts).x
  133. assert_allclose(self.f4(x), self.f4(xopt), atol=1e-8)
  134. def test_minimize_tnc5(self):
  135. x0, bnds = [0, 0], [(-1.5, 4),(-3, 3)]
  136. xopt = [-0.54719755119659763, -1.5471975511965976]
  137. x = optimize.minimize(self.f5, x0, method='TNC',
  138. jac=self.g5, bounds=bnds,
  139. options=self.opts).x
  140. assert_allclose(self.f5(x), self.f5(xopt), atol=1e-8)
  141. def test_minimize_tnc38(self):
  142. x0, bnds = np.array([-3, -1, -3, -1]), [(-10, 10)]*4
  143. xopt = [1]*4
  144. x = optimize.minimize(self.f38, x0, method='TNC',
  145. jac=self.g38, bounds=bnds,
  146. options=self.opts).x
  147. assert_allclose(self.f38(x), self.f38(xopt), atol=1e-8)
  148. def test_minimize_tnc45(self):
  149. x0, bnds = [2] * 5, [(0, 1), (0, 2), (0, 3), (0, 4), (0, 5)]
  150. xopt = [1, 2, 3, 4, 5]
  151. x = optimize.minimize(self.f45, x0, method='TNC',
  152. jac=self.g45, bounds=bnds,
  153. options=self.opts).x
  154. assert_allclose(self.f45(x), self.f45(xopt), atol=1e-8)
  155. # fmin_tnc
  156. def test_tnc1(self):
  157. fg, x, bounds = self.fg1, [-2, 1], ([-np.inf, None], [-1.5, None])
  158. xopt = [1, 1]
  159. x, nf, rc = optimize.fmin_tnc(fg, x, bounds=bounds, args=(100.0, ),
  160. messages=optimize._tnc.MSG_NONE,
  161. maxfun=200)
  162. assert_allclose(self.f1(x), self.f1(xopt), atol=1e-8,
  163. err_msg="TNC failed with status: " +
  164. optimize._tnc.RCSTRINGS[rc])
  165. def test_tnc1b(self):
  166. x, bounds = [-2, 1], ([-np.inf, None], [-1.5, None])
  167. xopt = [1, 1]
  168. x, nf, rc = optimize.fmin_tnc(self.f1, x, approx_grad=True,
  169. bounds=bounds,
  170. messages=optimize._tnc.MSG_NONE,
  171. maxfun=200)
  172. assert_allclose(self.f1(x), self.f1(xopt), atol=1e-4,
  173. err_msg="TNC failed with status: " +
  174. optimize._tnc.RCSTRINGS[rc])
  175. def test_tnc1c(self):
  176. x, bounds = [-2, 1], ([-np.inf, None], [-1.5, None])
  177. xopt = [1, 1]
  178. x, nf, rc = optimize.fmin_tnc(self.f1, x, fprime=self.g1,
  179. bounds=bounds,
  180. messages=optimize._tnc.MSG_NONE,
  181. maxfun=200)
  182. assert_allclose(self.f1(x), self.f1(xopt), atol=1e-8,
  183. err_msg="TNC failed with status: " +
  184. optimize._tnc.RCSTRINGS[rc])
  185. def test_tnc2(self):
  186. fg, x, bounds = self.fg1, [-2, 1], ([-np.inf, None], [1.5, None])
  187. xopt = [-1.2210262419616387, 1.5]
  188. x, nf, rc = optimize.fmin_tnc(fg, x, bounds=bounds,
  189. messages=optimize._tnc.MSG_NONE,
  190. maxfun=200)
  191. assert_allclose(self.f1(x), self.f1(xopt), atol=1e-8,
  192. err_msg="TNC failed with status: " +
  193. optimize._tnc.RCSTRINGS[rc])
  194. def test_tnc3(self):
  195. fg, x, bounds = self.fg3, [10, 1], ([-np.inf, None], [0.0, None])
  196. xopt = [0, 0]
  197. x, nf, rc = optimize.fmin_tnc(fg, x, bounds=bounds,
  198. messages=optimize._tnc.MSG_NONE,
  199. maxfun=200)
  200. assert_allclose(self.f3(x), self.f3(xopt), atol=1e-8,
  201. err_msg="TNC failed with status: " +
  202. optimize._tnc.RCSTRINGS[rc])
  203. def test_tnc4(self):
  204. fg, x, bounds = self.fg4, [1.125, 0.125], [(1, None), (0, None)]
  205. xopt = [1, 0]
  206. x, nf, rc = optimize.fmin_tnc(fg, x, bounds=bounds,
  207. messages=optimize._tnc.MSG_NONE,
  208. maxfun=200)
  209. assert_allclose(self.f4(x), self.f4(xopt), atol=1e-8,
  210. err_msg="TNC failed with status: " +
  211. optimize._tnc.RCSTRINGS[rc])
  212. def test_tnc5(self):
  213. fg, x, bounds = self.fg5, [0, 0], [(-1.5, 4),(-3, 3)]
  214. xopt = [-0.54719755119659763, -1.5471975511965976]
  215. x, nf, rc = optimize.fmin_tnc(fg, x, bounds=bounds,
  216. messages=optimize._tnc.MSG_NONE,
  217. maxfun=200)
  218. assert_allclose(self.f5(x), self.f5(xopt), atol=1e-8,
  219. err_msg="TNC failed with status: " +
  220. optimize._tnc.RCSTRINGS[rc])
  221. def test_tnc38(self):
  222. fg, x, bounds = self.fg38, np.array([-3, -1, -3, -1]), [(-10, 10)]*4
  223. xopt = [1]*4
  224. x, nf, rc = optimize.fmin_tnc(fg, x, bounds=bounds,
  225. messages=optimize._tnc.MSG_NONE,
  226. maxfun=200)
  227. assert_allclose(self.f38(x), self.f38(xopt), atol=1e-8,
  228. err_msg="TNC failed with status: " +
  229. optimize._tnc.RCSTRINGS[rc])
  230. def test_tnc45(self):
  231. fg, x, bounds = self.fg45, [2] * 5, [(0, 1), (0, 2), (0, 3),
  232. (0, 4), (0, 5)]
  233. xopt = [1, 2, 3, 4, 5]
  234. x, nf, rc = optimize.fmin_tnc(fg, x, bounds=bounds,
  235. messages=optimize._tnc.MSG_NONE,
  236. maxfun=200)
  237. assert_allclose(self.f45(x), self.f45(xopt), atol=1e-8,
  238. err_msg="TNC failed with status: " +
  239. optimize._tnc.RCSTRINGS[rc])
  240. def test_raising_exceptions(self):
  241. # tnc was ported to cython from hand-crafted cpython code
  242. # check that Exception handling works.
  243. def myfunc(x):
  244. raise RuntimeError("myfunc")
  245. def myfunc1(x):
  246. return optimize.rosen(x)
  247. def callback(x):
  248. raise ValueError("callback")
  249. with pytest.raises(RuntimeError):
  250. optimize.minimize(myfunc, [0, 1], method="TNC")
  251. with pytest.raises(ValueError):
  252. optimize.minimize(
  253. myfunc1, [0, 1], method="TNC", callback=callback
  254. )
  255. def test_callback_shouldnt_affect_minimization(self):
  256. # gh14879. The output of a TNC minimization was different depending
  257. # on whether a callback was used or not. The two should be equivalent.
  258. # The issue was that TNC was unscaling/scaling x, and this process was
  259. # altering x in the process. Now the callback uses an unscaled
  260. # temporary copy of x.
  261. def callback(x):
  262. pass
  263. fun = optimize.rosen
  264. bounds = [(0, 10)] * 4
  265. x0 = [1, 2, 3, 4.]
  266. res = optimize.minimize(
  267. fun, x0, bounds=bounds, method="TNC", options={"maxfun": 1000}
  268. )
  269. res2 = optimize.minimize(
  270. fun, x0, bounds=bounds, method="TNC", options={"maxfun": 1000},
  271. callback=callback
  272. )
  273. assert_allclose(res2.x, res.x)
  274. assert_allclose(res2.fun, res.fun)
  275. assert_equal(res2.nfev, res.nfev)