test__shgo.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. import logging
  2. import numpy
  3. import pytest
  4. from pytest import raises as assert_raises, warns
  5. from scipy.optimize import shgo
  6. from scipy.optimize._shgo import SHGO
  7. class StructTestFunction(object):
  8. def __init__(self, bounds, expected_x, expected_fun=None,
  9. expected_xl=None, expected_funl=None):
  10. self.bounds = bounds
  11. self.expected_x = expected_x
  12. self.expected_fun = expected_fun
  13. self.expected_xl = expected_xl
  14. self.expected_funl = expected_funl
  15. def wrap_constraints(g):
  16. cons = []
  17. if g is not None:
  18. if (type(g) is not tuple) and (type(g) is not list):
  19. g = (g,)
  20. else:
  21. pass
  22. for g in g:
  23. cons.append({'type': 'ineq',
  24. 'fun': g})
  25. cons = tuple(cons)
  26. else:
  27. cons = None
  28. return cons
  29. class StructTest1(StructTestFunction):
  30. def f(self, x):
  31. return x[0] ** 2 + x[1] ** 2
  32. def g(x):
  33. return -(numpy.sum(x, axis=0) - 6.0)
  34. cons = wrap_constraints(g)
  35. test1_1 = StructTest1(bounds=[(-1, 6), (-1, 6)],
  36. expected_x=[0, 0])
  37. test1_2 = StructTest1(bounds=[(0, 1), (0, 1)],
  38. expected_x=[0, 0])
  39. test1_3 = StructTest1(bounds=[(None, None), (None, None)],
  40. expected_x=[0, 0])
  41. class StructTest2(StructTestFunction):
  42. """
  43. Scalar function with several minima to test all minimiser retrievals
  44. """
  45. def f(self, x):
  46. return (x - 30) * numpy.sin(x)
  47. def g(x):
  48. return 58 - numpy.sum(x, axis=0)
  49. cons = wrap_constraints(g)
  50. test2_1 = StructTest2(bounds=[(0, 60)],
  51. expected_x=[1.53567906],
  52. expected_fun=-28.44677132,
  53. # Important: test that funl return is in the correct order
  54. expected_xl=numpy.array([[1.53567906],
  55. [55.01782167],
  56. [7.80894889],
  57. [48.74797493],
  58. [14.07445705],
  59. [42.4913859],
  60. [20.31743841],
  61. [36.28607535],
  62. [26.43039605],
  63. [30.76371366]]),
  64. expected_funl=numpy.array([-28.44677132, -24.99785984,
  65. -22.16855376, -18.72136195,
  66. -15.89423937, -12.45154942,
  67. -9.63133158, -6.20801301,
  68. -3.43727232, -0.46353338])
  69. )
  70. test2_2 = StructTest2(bounds=[(0, 4.5)],
  71. expected_x=[1.53567906],
  72. expected_fun=[-28.44677132],
  73. expected_xl=numpy.array([[1.53567906]]),
  74. expected_funl=numpy.array([-28.44677132])
  75. )
  76. class StructTest3(StructTestFunction):
  77. """
  78. Hock and Schittkowski 18 problem (HS18). Hoch and Schittkowski (1981)
  79. http://www.ai7.uni-bayreuth.de/test_problem_coll.pdf
  80. Minimize: f = 0.01 * (x_1)**2 + (x_2)**2
  81. Subject to: x_1 * x_2 - 25.0 >= 0,
  82. (x_1)**2 + (x_2)**2 - 25.0 >= 0,
  83. 2 <= x_1 <= 50,
  84. 0 <= x_2 <= 50.
  85. Approx. Answer:
  86. f([(250)**0.5 , (2.5)**0.5]) = 5.0
  87. """
  88. def f(self, x):
  89. return 0.01 * (x[0]) ** 2 + (x[1]) ** 2
  90. def g1(x):
  91. return x[0] * x[1] - 25.0
  92. def g2(x):
  93. return x[0] ** 2 + x[1] ** 2 - 25.0
  94. g = (g1, g2)
  95. cons = wrap_constraints(g)
  96. test3_1 = StructTest3(bounds=[(2, 50), (0, 50)],
  97. expected_x=[250 ** 0.5, 2.5 ** 0.5],
  98. expected_fun=5.0
  99. )
  100. class StructTest4(StructTestFunction):
  101. """
  102. Hock and Schittkowski 11 problem (HS11). Hoch and Schittkowski (1981)
  103. NOTE: Did not find in original reference to HS collection, refer to
  104. Henderson (2015) problem 7 instead. 02.03.2016
  105. """
  106. def f(self, x):
  107. return ((x[0] - 10) ** 2 + 5 * (x[1] - 12) ** 2 + x[2] ** 4
  108. + 3 * (x[3] - 11) ** 2 + 10 * x[4] ** 6 + 7 * x[5] ** 2 + x[
  109. 6] ** 4
  110. - 4 * x[5] * x[6] - 10 * x[5] - 8 * x[6]
  111. )
  112. def g1(x):
  113. return -(2 * x[0] ** 2 + 3 * x[1] ** 4 + x[2] + 4 * x[3] ** 2
  114. + 5 * x[4] - 127)
  115. def g2(x):
  116. return -(7 * x[0] + 3 * x[1] + 10 * x[2] ** 2 + x[3] - x[4] - 282.0)
  117. def g3(x):
  118. return -(23 * x[0] + x[1] ** 2 + 6 * x[5] ** 2 - 8 * x[6] - 196)
  119. def g4(x):
  120. return -(4 * x[0] ** 2 + x[1] ** 2 - 3 * x[0] * x[1] + 2 * x[2] ** 2
  121. + 5 * x[5] - 11 * x[6])
  122. g = (g1, g2, g3, g4)
  123. cons = wrap_constraints(g)
  124. test4_1 = StructTest4(bounds=[(-10, 10), ] * 7,
  125. expected_x=[2.330499, 1.951372, -0.4775414,
  126. 4.365726, -0.6244870, 1.038131, 1.594227],
  127. expected_fun=680.6300573
  128. )
  129. class StructTest5(StructTestFunction):
  130. def f(self, x):
  131. return (-(x[1] + 47.0)
  132. * numpy.sin(numpy.sqrt(abs(x[0] / 2.0 + (x[1] + 47.0))))
  133. - x[0] * numpy.sin(numpy.sqrt(abs(x[0] - (x[1] + 47.0))))
  134. )
  135. g = None
  136. cons = wrap_constraints(g)
  137. test5_1 = StructTest5(bounds=[(-512, 512), (-512, 512)],
  138. expected_fun=[-959.64066272085051],
  139. expected_x=[512., 404.23180542])
  140. class StructTestLJ(StructTestFunction):
  141. """
  142. LennardJones objective function. Used to test symmetry constraints settings.
  143. """
  144. def f(self, x, *args):
  145. self.N = args[0]
  146. k = int(self.N / 3)
  147. s = 0.0
  148. for i in range(k - 1):
  149. for j in range(i + 1, k):
  150. a = 3 * i
  151. b = 3 * j
  152. xd = x[a] - x[b]
  153. yd = x[a + 1] - x[b + 1]
  154. zd = x[a + 2] - x[b + 2]
  155. ed = xd * xd + yd * yd + zd * zd
  156. ud = ed * ed * ed
  157. if ed > 0.0:
  158. s += (1.0 / ud - 2.0) / ud
  159. return s
  160. g = None
  161. cons = wrap_constraints(g)
  162. N = 6
  163. boundsLJ = list(zip([-4.0] * 6, [4.0] * 6))
  164. testLJ = StructTestLJ(bounds=boundsLJ,
  165. expected_fun=[-1.0],
  166. expected_x=[-2.71247337e-08,
  167. -2.71247337e-08,
  168. -2.50000222e+00,
  169. -2.71247337e-08,
  170. -2.71247337e-08,
  171. -1.50000222e+00]
  172. )
  173. class StructTestTable(StructTestFunction):
  174. def f(self, x):
  175. if x[0] == 3.0 and x[1] == 3.0:
  176. return 50
  177. else:
  178. return 100
  179. g = None
  180. cons = wrap_constraints(g)
  181. test_table = StructTestTable(bounds=[(-10, 10), (-10, 10)],
  182. expected_fun=[50],
  183. expected_x=[3.0, 3.0])
  184. class StructTestInfeasible(StructTestFunction):
  185. """
  186. Test function with no feasible domain.
  187. """
  188. def f(self, x, *args):
  189. return x[0] ** 2 + x[1] ** 2
  190. def g1(x):
  191. return x[0] + x[1] - 1
  192. def g2(x):
  193. return -(x[0] + x[1] - 1)
  194. def g3(x):
  195. return -x[0] + x[1] - 1
  196. def g4(x):
  197. return -(-x[0] + x[1] - 1)
  198. g = (g1, g2, g3, g4)
  199. cons = wrap_constraints(g)
  200. test_infeasible = StructTestInfeasible(bounds=[(2, 50), (-1, 1)],
  201. expected_fun=None,
  202. expected_x=None
  203. )
  204. def run_test(test, args=(), test_atol=1e-5, n=100, iters=None,
  205. callback=None, minimizer_kwargs=None, options=None,
  206. sampling_method='sobol'):
  207. res = shgo(test.f, test.bounds, args=args, constraints=test.cons,
  208. n=n, iters=iters, callback=callback,
  209. minimizer_kwargs=minimizer_kwargs, options=options,
  210. sampling_method=sampling_method)
  211. logging.info(res)
  212. if test.expected_x is not None:
  213. numpy.testing.assert_allclose(res.x, test.expected_x,
  214. rtol=test_atol,
  215. atol=test_atol)
  216. # (Optional tests)
  217. if test.expected_fun is not None:
  218. numpy.testing.assert_allclose(res.fun,
  219. test.expected_fun,
  220. atol=test_atol)
  221. if test.expected_xl is not None:
  222. numpy.testing.assert_allclose(res.xl,
  223. test.expected_xl,
  224. atol=test_atol)
  225. if test.expected_funl is not None:
  226. numpy.testing.assert_allclose(res.funl,
  227. test.expected_funl,
  228. atol=test_atol)
  229. return
  230. # Base test functions:
  231. class TestShgoSobolTestFunctions(object):
  232. """
  233. Global optimisation tests with Sobol sampling:
  234. """
  235. # Sobol algorithm
  236. def test_f1_1_sobol(self):
  237. """Multivariate test function 1:
  238. x[0]**2 + x[1]**2 with bounds=[(-1, 6), (-1, 6)]"""
  239. run_test(test1_1)
  240. def test_f1_2_sobol(self):
  241. """Multivariate test function 1:
  242. x[0]**2 + x[1]**2 with bounds=[(0, 1), (0, 1)]"""
  243. run_test(test1_2)
  244. def test_f1_3_sobol(self):
  245. """Multivariate test function 1:
  246. x[0]**2 + x[1]**2 with bounds=[(None, None),(None, None)]"""
  247. run_test(test1_3)
  248. def test_f2_1_sobol(self):
  249. """Univariate test function on
  250. f(x) = (x - 30) * sin(x) with bounds=[(0, 60)]"""
  251. run_test(test2_1)
  252. def test_f2_2_sobol(self):
  253. """Univariate test function on
  254. f(x) = (x - 30) * sin(x) bounds=[(0, 4.5)]"""
  255. run_test(test2_2)
  256. def test_f3_sobol(self):
  257. """NLP: Hock and Schittkowski problem 18"""
  258. run_test(test3_1)
  259. @pytest.mark.slow
  260. def test_f4_sobol(self):
  261. """NLP: (High dimensional) Hock and Schittkowski 11 problem (HS11)"""
  262. # run_test(test4_1, n=500)
  263. # run_test(test4_1, n=800)
  264. options = {'infty_constraints': False}
  265. run_test(test4_1, n=990, options=options)
  266. def test_f5_1_sobol(self):
  267. """NLP: Eggholder, multimodal"""
  268. run_test(test5_1, n=30)
  269. def test_f5_2_sobol(self):
  270. """NLP: Eggholder, multimodal"""
  271. # run_test(test5_1, n=60, iters=5)
  272. run_test(test5_1, n=60, iters=5)
  273. # def test_t911(self):
  274. # """1D tabletop function"""
  275. # run_test(test11_1)
  276. class TestShgoSimplicialTestFunctions(object):
  277. """
  278. Global optimisation tests with Simplicial sampling:
  279. """
  280. def test_f1_1_simplicial(self):
  281. """Multivariate test function 1:
  282. x[0]**2 + x[1]**2 with bounds=[(-1, 6), (-1, 6)]"""
  283. run_test(test1_1, n=1, sampling_method='simplicial')
  284. def test_f1_2_simplicial(self):
  285. """Multivariate test function 1:
  286. x[0]**2 + x[1]**2 with bounds=[(0, 1), (0, 1)]"""
  287. run_test(test1_2, n=1, sampling_method='simplicial')
  288. def test_f1_3_simplicial(self):
  289. """Multivariate test function 1: x[0]**2 + x[1]**2
  290. with bounds=[(None, None),(None, None)]"""
  291. run_test(test1_3, n=1, sampling_method='simplicial')
  292. def test_f2_1_simplicial(self):
  293. """Univariate test function on
  294. f(x) = (x - 30) * sin(x) with bounds=[(0, 60)]"""
  295. options = {'minimize_every_iter': False}
  296. run_test(test2_1, iters=7, options=options,
  297. sampling_method='simplicial')
  298. def test_f2_2_simplicial(self):
  299. """Univariate test function on
  300. f(x) = (x - 30) * sin(x) bounds=[(0, 4.5)]"""
  301. run_test(test2_2, n=1, sampling_method='simplicial')
  302. def test_f3_simplicial(self):
  303. """NLP: Hock and Schittkowski problem 18"""
  304. run_test(test3_1, n=1, sampling_method='simplicial')
  305. @pytest.mark.slow
  306. def test_f4_simplicial(self):
  307. """NLP: (High dimensional) Hock and Schittkowski 11 problem (HS11)"""
  308. run_test(test4_1, n=1, sampling_method='simplicial')
  309. def test_lj_symmetry(self):
  310. """LJ: Symmetry constrained test function"""
  311. options = {'symmetry': True,
  312. 'disp': True}
  313. args = (6,) # No. of atoms
  314. run_test(testLJ, args=args, n=None,
  315. options=options, iters=4,
  316. sampling_method='simplicial')
  317. # Argument test functions
  318. class TestShgoArguments(object):
  319. def test_1_1_simpl_iter(self):
  320. """Iterative simplicial sampling on TestFunction 1 (multivariate)"""
  321. run_test(test1_2, n=None, iters=2, sampling_method='simplicial')
  322. def test_1_2_simpl_iter(self):
  323. """Iterative simplicial on TestFunction 2 (univariate)"""
  324. options = {'minimize_every_iter': False}
  325. run_test(test2_1, n=None, iters=7, options=options,
  326. sampling_method='simplicial')
  327. def test_2_1_sobol_iter(self):
  328. """Iterative Sobol sampling on TestFunction 1 (multivariate)"""
  329. run_test(test1_2, n=None, iters=1, sampling_method='sobol')
  330. def test_2_2_sobol_iter(self):
  331. """Iterative Sobol sampling on TestFunction 2 (univariate)"""
  332. res = shgo(test2_1.f, test2_1.bounds, constraints=test2_1.cons,
  333. n=None, iters=1, sampling_method='sobol')
  334. numpy.testing.assert_allclose(res.x, test2_1.expected_x, rtol=1e-5,
  335. atol=1e-5)
  336. numpy.testing.assert_allclose(res.fun, test2_1.expected_fun, atol=1e-5)
  337. def test_3_1_disp_simplicial(self):
  338. """Iterative sampling on TestFunction 1 and 2 (multi and univariate)"""
  339. def callback_func(x):
  340. print("Local minimization callback test")
  341. for test in [test1_1, test2_1]:
  342. res = shgo(test.f, test.bounds, iters=1,
  343. sampling_method='simplicial',
  344. callback=callback_func, options={'disp': True})
  345. res = shgo(test.f, test.bounds, n=1, sampling_method='simplicial',
  346. callback=callback_func, options={'disp': True})
  347. def test_3_2_disp_sobol(self):
  348. """Iterative sampling on TestFunction 1 and 2 (multi and univariate)"""
  349. def callback_func(x):
  350. print("Local minimization callback test")
  351. for test in [test1_1, test2_1]:
  352. res = shgo(test.f, test.bounds, iters=1, sampling_method='sobol',
  353. callback=callback_func, options={'disp': True})
  354. res = shgo(test.f, test.bounds, n=1, sampling_method='simplicial',
  355. callback=callback_func, options={'disp': True})
  356. @pytest.mark.slow
  357. def test_4_1_known_f_min(self):
  358. """Test known function minima stopping criteria"""
  359. # Specify known function value
  360. options = {'f_min': test4_1.expected_fun,
  361. 'f_tol': 1e-6,
  362. 'minimize_every_iter': True}
  363. # TODO: Make default n higher for faster tests
  364. run_test(test4_1, n=None, test_atol=1e-5, options=options,
  365. sampling_method='simplicial')
  366. @pytest.mark.slow
  367. def test_4_2_known_f_min(self):
  368. """Test Global mode limiting local evalutions"""
  369. options = { # Specify known function value
  370. 'f_min': test4_1.expected_fun,
  371. 'f_tol': 1e-6,
  372. # Specify number of local iterations to perform
  373. 'minimize_every_iter': True,
  374. 'local_iter': 1}
  375. run_test(test4_1, n=None, test_atol=1e-5, options=options,
  376. sampling_method='simplicial')
  377. @pytest.mark.slow
  378. def test_4_3_known_f_min(self):
  379. """Test Global mode limiting local evalutions"""
  380. options = { # Specify known function value
  381. 'f_min': test4_1.expected_fun,
  382. 'f_tol': 1e-6,
  383. # Specify number of local iterations to perform+
  384. 'minimize_every_iter': True,
  385. 'local_iter': 1,
  386. 'infty_constraints': False}
  387. run_test(test4_1, n=300, test_atol=1e-5, options=options,
  388. sampling_method='sobol')
  389. def test_4_4_known_f_min(self):
  390. """Test Global mode limiting local evalutions for 1D funcs"""
  391. options = { # Specify known function value
  392. 'f_min': test2_1.expected_fun,
  393. 'f_tol': 1e-6,
  394. # Specify number of local iterations to perform+
  395. 'minimize_every_iter': True,
  396. 'local_iter': 1,
  397. 'infty_constraints': False}
  398. res = shgo(test2_1.f, test2_1.bounds, constraints=test2_1.cons,
  399. n=None, iters=None, options=options,
  400. sampling_method='sobol')
  401. numpy.testing.assert_allclose(res.x, test2_1.expected_x, rtol=1e-5,
  402. atol=1e-5)
  403. def test_5_1_simplicial_argless(self):
  404. """Test Default simplicial sampling settings on TestFunction 1"""
  405. res = shgo(test1_1.f, test1_1.bounds, constraints=test1_1.cons)
  406. numpy.testing.assert_allclose(res.x, test1_1.expected_x, rtol=1e-5,
  407. atol=1e-5)
  408. def test_5_2_sobol_argless(self):
  409. """Test Default sobol sampling settings on TestFunction 1"""
  410. res = shgo(test1_1.f, test1_1.bounds, constraints=test1_1.cons,
  411. sampling_method='sobol')
  412. numpy.testing.assert_allclose(res.x, test1_1.expected_x, rtol=1e-5,
  413. atol=1e-5)
  414. def test_6_1_simplicial_max_iter(self):
  415. """Test that maximum iteration option works on TestFunction 3"""
  416. options = {'max_iter': 2}
  417. res = shgo(test3_1.f, test3_1.bounds, constraints=test3_1.cons,
  418. options=options, sampling_method='simplicial')
  419. numpy.testing.assert_allclose(res.x, test3_1.expected_x, rtol=1e-5,
  420. atol=1e-5)
  421. numpy.testing.assert_allclose(res.fun, test3_1.expected_fun, atol=1e-5)
  422. def test_6_2_simplicial_min_iter(self):
  423. """Test that maximum iteration option works on TestFunction 3"""
  424. options = {'min_iter': 2}
  425. res = shgo(test3_1.f, test3_1.bounds, constraints=test3_1.cons,
  426. options=options, sampling_method='simplicial')
  427. numpy.testing.assert_allclose(res.x, test3_1.expected_x, rtol=1e-5,
  428. atol=1e-5)
  429. numpy.testing.assert_allclose(res.fun, test3_1.expected_fun, atol=1e-5)
  430. def test_7_1_minkwargs(self):
  431. """Test the minimizer_kwargs arguments for solvers with constraints"""
  432. # Test solvers
  433. for solver in ['COBYLA', 'SLSQP']:
  434. # Note that passing global constraints to SLSQP is tested in other
  435. # unittests which run test4_1 normally
  436. minimizer_kwargs = {'method': solver,
  437. 'constraints': test3_1.cons}
  438. print("Solver = {}".format(solver))
  439. print("=" * 100)
  440. run_test(test3_1, n=100, test_atol=1e-3,
  441. minimizer_kwargs=minimizer_kwargs, sampling_method='sobol')
  442. def test_7_2_minkwargs(self):
  443. """Test the minimizer_kwargs default inits"""
  444. minimizer_kwargs = {'ftol': 1e-5}
  445. options = {'disp': True} # For coverage purposes
  446. SHGOc = SHGO(test3_1.f, test3_1.bounds, constraints=test3_1.cons[0],
  447. minimizer_kwargs=minimizer_kwargs, options=options)
  448. def test_7_3_minkwargs(self):
  449. """Test minimizer_kwargs arguments for solvers without constraints"""
  450. for solver in ['Nelder-Mead', 'Powell', 'CG', 'BFGS', 'Newton-CG',
  451. 'L-BFGS-B', 'TNC', 'dogleg', 'trust-ncg', 'trust-exact',
  452. 'trust-krylov']:
  453. def jac(x):
  454. return numpy.array([2 * x[0], 2 * x[1]]).T
  455. def hess(x):
  456. return numpy.array([[2, 0], [0, 2]])
  457. minimizer_kwargs = {'method': solver,
  458. 'jac': jac,
  459. 'hess': hess}
  460. logging.info("Solver = {}".format(solver))
  461. logging.info("=" * 100)
  462. run_test(test1_1, n=100, test_atol=1e-3,
  463. minimizer_kwargs=minimizer_kwargs, sampling_method='sobol')
  464. def test_8_homology_group_diff(self):
  465. options = {'minhgrd': 1,
  466. 'minimize_every_iter': True}
  467. run_test(test1_1, n=None, iters=None, options=options,
  468. sampling_method='simplicial')
  469. def test_9_cons_g(self):
  470. """Test single function constraint passing"""
  471. SHGOc = SHGO(test3_1.f, test3_1.bounds, constraints=test3_1.cons[0])
  472. def test_10_finite_time(self):
  473. """Test single function constraint passing"""
  474. options = {'maxtime': 1e-15}
  475. res = shgo(test1_1.f, test1_1.bounds, n=1, iters=None,
  476. options=options, sampling_method='sobol')
  477. def test_11_f_min_time(self):
  478. """Test to cover the case where f_lowest == 0"""
  479. options = {'maxtime': 1e-15,
  480. 'f_min': 0.0}
  481. res = shgo(test1_2.f, test1_2.bounds, n=1, iters=None,
  482. options=options, sampling_method='sobol')
  483. def test_12_sobol_inf_cons(self):
  484. """Test to cover the case where f_lowest == 0"""
  485. options = {'maxtime': 1e-15,
  486. 'f_min': 0.0}
  487. res = shgo(test1_2.f, test1_2.bounds, n=1, iters=None,
  488. options=options, sampling_method='sobol')
  489. def test_13_high_sobol(self):
  490. """Test init of high-dimensional sobol sequences"""
  491. def f(x):
  492. return 0
  493. bounds = [(None, None), ] * 41
  494. SHGOc = SHGO(f, bounds)
  495. SHGOc.sobol_points(2, 50)
  496. def test_14_local_iter(self):
  497. """Test limited local iterations for a pseudo-global mode"""
  498. options = {'local_iter': 4}
  499. run_test(test5_1, n=30, options=options)
  500. def test_15_min_every_iter(self):
  501. """Test minimize every iter options and cover function cache"""
  502. options = {'minimize_every_iter': True}
  503. run_test(test1_1, n=1, iters=7, options=options,
  504. sampling_method='sobol')
  505. # Failure test functions
  506. class TestShgoFailures(object):
  507. def test_1_maxiter(self):
  508. """Test failure on insufficient iterations"""
  509. options = {'maxiter': 2}
  510. res = shgo(test4_1.f, test4_1.bounds, n=2, iters=None,
  511. options=options, sampling_method='sobol')
  512. numpy.testing.assert_equal(False, res.success)
  513. numpy.testing.assert_equal(4, res.nfev)
  514. def test_2_sampling(self):
  515. """Rejection of unknown sampling method"""
  516. assert_raises(ValueError, shgo, test1_1.f, test1_1.bounds,
  517. sampling_method='not_Sobol')
  518. def test_3_1_no_min_pool_sobol(self):
  519. """Check that the routine stops when no minimiser is found
  520. after maximum specified function evaluations"""
  521. options = {'maxfev': 10,
  522. 'disp': True}
  523. res = shgo(test_table.f, test_table.bounds, n=3, options=options,
  524. sampling_method='sobol')
  525. numpy.testing.assert_equal(False, res.success)
  526. # numpy.testing.assert_equal(9, res.nfev)
  527. numpy.testing.assert_equal(12, res.nfev)
  528. def test_3_2_no_min_pool_simplicial(self):
  529. """Check that the routine stops when no minimiser is found
  530. after maximum specified sampling evaluations"""
  531. options = {'maxev': 10,
  532. 'disp': True}
  533. res = shgo(test_table.f, test_table.bounds, n=3, options=options,
  534. sampling_method='simplicial')
  535. numpy.testing.assert_equal(False, res.success)
  536. def test_4_1_bound_err(self):
  537. """Specified bounds ub > lb"""
  538. bounds = [(6, 3), (3, 5)]
  539. assert_raises(ValueError, shgo, test1_1.f, bounds)
  540. def test_4_2_bound_err(self):
  541. """Specified bounds are of the form (lb, ub)"""
  542. bounds = [(3, 5, 5), (3, 5)]
  543. assert_raises(ValueError, shgo, test1_1.f, bounds)
  544. def test_5_1_1_infeasible_sobol(self):
  545. """Ensures the algorithm terminates on infeasible problems
  546. after maxev is exceeded. Use infty constraints option"""
  547. options = {'maxev': 100,
  548. 'disp': True}
  549. res = shgo(test_infeasible.f, test_infeasible.bounds,
  550. constraints=test_infeasible.cons, n=100, options=options,
  551. sampling_method='sobol')
  552. numpy.testing.assert_equal(False, res.success)
  553. def test_5_1_2_infeasible_sobol(self):
  554. """Ensures the algorithm terminates on infeasible problems
  555. after maxev is exceeded. Do not use infty constraints option"""
  556. options = {'maxev': 100,
  557. 'disp': True,
  558. 'infty_constraints': False}
  559. res = shgo(test_infeasible.f, test_infeasible.bounds,
  560. constraints=test_infeasible.cons, n=100, options=options,
  561. sampling_method='sobol')
  562. numpy.testing.assert_equal(False, res.success)
  563. def test_5_2_infeasible_simplicial(self):
  564. """Ensures the algorithm terminates on infeasible problems
  565. after maxev is exceeded."""
  566. options = {'maxev': 1000,
  567. 'disp': False}
  568. res = shgo(test_infeasible.f, test_infeasible.bounds,
  569. constraints=test_infeasible.cons, n=100, options=options,
  570. sampling_method='simplicial')
  571. numpy.testing.assert_equal(False, res.success)
  572. def test_6_1_lower_known_f_min(self):
  573. """Test Global mode limiting local evalutions with f* too high"""
  574. options = { # Specify known function value
  575. 'f_min': test2_1.expected_fun + 2.0,
  576. 'f_tol': 1e-6,
  577. # Specify number of local iterations to perform+
  578. 'minimize_every_iter': True,
  579. 'local_iter': 1,
  580. 'infty_constraints': False}
  581. args = (test2_1.f, test2_1.bounds)
  582. kwargs = {'constraints': test2_1.cons,
  583. 'n': None,
  584. 'iters': None,
  585. 'options': options,
  586. 'sampling_method': 'sobol'
  587. }
  588. warns(UserWarning, shgo, *args, **kwargs)