test_cobyqa.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import numpy as np
  2. import pytest
  3. import threading
  4. from numpy.testing import assert_allclose, assert_equal
  5. from scipy.optimize import (
  6. Bounds,
  7. LinearConstraint,
  8. NonlinearConstraint,
  9. OptimizeResult,
  10. minimize,
  11. )
  12. class TestCOBYQA:
  13. def setup_method(self):
  14. self.x0 = [4.95, 0.66]
  15. self.options = {'maxfev': 100}
  16. @staticmethod
  17. def fun(x, c=1.0):
  18. return x[0]**2 + c * abs(x[1])**3
  19. @staticmethod
  20. def con(x):
  21. return x[0]**2 + x[1]**2 - 25.0
  22. def test_minimize_simple(self):
  23. class Callback:
  24. def __init__(self):
  25. self.lock = threading.Lock()
  26. self.n_calls = 0
  27. def __call__(self, x):
  28. assert isinstance(x, np.ndarray)
  29. with self.lock:
  30. self.n_calls += 1
  31. class CallbackNewSyntax:
  32. def __init__(self):
  33. self.lock = threading.Lock()
  34. self.n_calls = 0
  35. def __call__(self, intermediate_result):
  36. assert isinstance(intermediate_result, OptimizeResult)
  37. with self.lock:
  38. self.n_calls += 1
  39. x0 = [4.95, 0.66]
  40. callback = Callback()
  41. callback_new_syntax = CallbackNewSyntax()
  42. # Minimize with method='cobyqa'.
  43. constraints = NonlinearConstraint(self.con, 0.0, 0.0)
  44. sol = minimize(
  45. self.fun,
  46. x0,
  47. method='cobyqa',
  48. constraints=constraints,
  49. callback=callback,
  50. options=self.options,
  51. )
  52. sol_new = minimize(
  53. self.fun,
  54. x0,
  55. method='cobyqa',
  56. constraints=constraints,
  57. callback=callback_new_syntax,
  58. options=self.options,
  59. )
  60. solution = [np.sqrt(25.0 - 4.0 / 9.0), 2.0 / 3.0]
  61. assert_allclose(sol.x, solution, atol=1e-4)
  62. assert sol.success, sol.message
  63. assert sol.maxcv < 1e-8, sol
  64. assert sol.nfev <= 100, sol
  65. assert sol.fun < self.fun(solution) + 1e-3, sol
  66. assert sol.nfev == callback.n_calls, \
  67. "Callback is not called exactly once for every function eval."
  68. assert_equal(sol.x, sol_new.x)
  69. assert sol_new.success, sol_new.message
  70. assert sol.fun == sol_new.fun
  71. assert sol.maxcv == sol_new.maxcv
  72. assert sol.nfev == sol_new.nfev
  73. assert sol.nit == sol_new.nit
  74. assert sol_new.nfev == callback_new_syntax.n_calls, \
  75. "Callback is not called exactly once for every function eval."
  76. def test_minimize_bounds(self):
  77. def fun_check_bounds(x):
  78. assert np.all(bounds.lb <= x) and np.all(x <= bounds.ub)
  79. return self.fun(x)
  80. # Case where the bounds are not active at the solution.
  81. bounds = Bounds([4.5, 0.6], [5.0, 0.7])
  82. constraints = NonlinearConstraint(self.con, 0.0, 0.0)
  83. sol = minimize(
  84. fun_check_bounds,
  85. self.x0,
  86. method='cobyqa',
  87. bounds=bounds,
  88. constraints=constraints,
  89. options=self.options,
  90. )
  91. solution = [np.sqrt(25.0 - 4.0 / 9.0), 2.0 / 3.0]
  92. assert_allclose(sol.x, solution, atol=1e-4)
  93. assert sol.success, sol.message
  94. assert sol.maxcv < 1e-8, sol
  95. assert np.all(bounds.lb <= sol.x) and np.all(sol.x <= bounds.ub), sol
  96. assert sol.nfev <= 100, sol
  97. assert sol.fun < self.fun(solution) + 1e-3, sol
  98. # Case where the bounds are active at the solution.
  99. bounds = Bounds([5.0, 0.6], [5.5, 0.65])
  100. sol = minimize(
  101. fun_check_bounds,
  102. self.x0,
  103. method='cobyqa',
  104. bounds=bounds,
  105. constraints=constraints,
  106. options=self.options,
  107. )
  108. assert not sol.success, sol.message
  109. assert sol.maxcv > 0.35, sol
  110. assert np.all(bounds.lb <= sol.x) and np.all(sol.x <= bounds.ub), sol
  111. assert sol.nfev <= 100, sol
  112. def test_minimize_linear_constraints(self):
  113. constraints = LinearConstraint([1.0, 1.0], 1.0, 1.0)
  114. sol = minimize(
  115. self.fun,
  116. self.x0,
  117. method='cobyqa',
  118. constraints=constraints,
  119. options=self.options,
  120. )
  121. solution = [(4 - np.sqrt(7)) / 3, (np.sqrt(7) - 1) / 3]
  122. assert_allclose(sol.x, solution, atol=1e-4)
  123. assert sol.success, sol.message
  124. assert sol.maxcv < 1e-8, sol
  125. assert sol.nfev <= 100, sol
  126. assert sol.fun < self.fun(solution) + 1e-3, sol
  127. def test_minimize_args(self):
  128. constraints = NonlinearConstraint(self.con, 0.0, 0.0)
  129. sol = minimize(
  130. self.fun,
  131. self.x0,
  132. args=(2.0,),
  133. method='cobyqa',
  134. constraints=constraints,
  135. options=self.options,
  136. )
  137. solution = [np.sqrt(25.0 - 4.0 / 36.0), 2.0 / 6.0]
  138. assert_allclose(sol.x, solution, atol=1e-4)
  139. assert sol.success, sol.message
  140. assert sol.maxcv < 1e-8, sol
  141. assert sol.nfev <= 100, sol
  142. assert sol.fun < self.fun(solution, 2.0) + 1e-3, sol
  143. def test_minimize_array(self):
  144. def fun_array(x, dim):
  145. f = np.array(self.fun(x))
  146. return np.reshape(f, (1,) * dim)
  147. # The argument fun can return an array with a single element.
  148. bounds = Bounds([4.5, 0.6], [5.0, 0.7])
  149. constraints = NonlinearConstraint(self.con, 0.0, 0.0)
  150. sol = minimize(
  151. self.fun,
  152. self.x0,
  153. method='cobyqa',
  154. bounds=bounds,
  155. constraints=constraints,
  156. options=self.options,
  157. )
  158. for dim in [0, 1, 2]:
  159. sol_array = minimize(
  160. fun_array,
  161. self.x0,
  162. args=(dim,),
  163. method='cobyqa',
  164. bounds=bounds,
  165. constraints=constraints,
  166. options=self.options,
  167. )
  168. assert_equal(sol.x, sol_array.x)
  169. assert sol_array.success, sol_array.message
  170. assert sol.fun == sol_array.fun
  171. assert sol.maxcv == sol_array.maxcv
  172. assert sol.nfev == sol_array.nfev
  173. assert sol.nit == sol_array.nit
  174. # The argument fun cannot return an array with more than one element.
  175. with pytest.raises(TypeError):
  176. minimize(
  177. lambda x: np.array([self.fun(x), self.fun(x)]),
  178. self.x0,
  179. method='cobyqa',
  180. bounds=bounds,
  181. constraints=constraints,
  182. options=self.options,
  183. )
  184. def test_minimize_maxfev(self):
  185. constraints = NonlinearConstraint(self.con, 0.0, 0.0)
  186. options = {'maxfev': 2}
  187. sol = minimize(
  188. self.fun,
  189. self.x0,
  190. method='cobyqa',
  191. constraints=constraints,
  192. options=options,
  193. )
  194. assert not sol.success, sol.message
  195. assert sol.nfev <= 2, sol
  196. def test_minimize_maxiter(self):
  197. constraints = NonlinearConstraint(self.con, 0.0, 0.0)
  198. options = {'maxiter': 2}
  199. sol = minimize(
  200. self.fun,
  201. self.x0,
  202. method='cobyqa',
  203. constraints=constraints,
  204. options=options,
  205. )
  206. assert not sol.success, sol.message
  207. assert sol.nit <= 2, sol
  208. def test_minimize_f_target(self):
  209. constraints = NonlinearConstraint(self.con, 0.0, 0.0)
  210. sol_ref = minimize(
  211. self.fun,
  212. self.x0,
  213. method='cobyqa',
  214. constraints=constraints,
  215. options=self.options,
  216. )
  217. options = dict(self.options)
  218. options['f_target'] = sol_ref.fun
  219. sol = minimize(
  220. self.fun,
  221. self.x0,
  222. method='cobyqa',
  223. constraints=constraints,
  224. options=options,
  225. )
  226. assert sol.success, sol.message
  227. assert sol.maxcv < 1e-8, sol
  228. assert sol.nfev <= sol_ref.nfev, sol
  229. assert sol.fun <= sol_ref.fun, sol