test_constraint_conversion.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. """
  2. Unit test for constraint conversion
  3. """
  4. import numpy as np
  5. from numpy.testing import (assert_array_almost_equal,
  6. assert_allclose, assert_warns, suppress_warnings)
  7. import pytest
  8. from scipy.optimize import (NonlinearConstraint, LinearConstraint,
  9. OptimizeWarning, minimize, BFGS)
  10. from .test_minimize_constrained import (Maratos, HyperbolicIneq, Rosenbrock,
  11. IneqRosenbrock, EqIneqRosenbrock,
  12. BoundedRosenbrock, Elec)
  13. class TestOldToNew:
  14. x0 = (2, 0)
  15. bnds = ((0, None), (0, None))
  16. method = "trust-constr"
  17. def test_constraint_dictionary_1(self):
  18. fun = lambda x: (x[0] - 1)**2 + (x[1] - 2.5)**2
  19. cons = ({'type': 'ineq', 'fun': lambda x: x[0] - 2 * x[1] + 2},
  20. {'type': 'ineq', 'fun': lambda x: -x[0] - 2 * x[1] + 6},
  21. {'type': 'ineq', 'fun': lambda x: -x[0] + 2 * x[1] + 2})
  22. with suppress_warnings() as sup:
  23. sup.filter(UserWarning, "delta_grad == 0.0")
  24. res = minimize(fun, self.x0, method=self.method,
  25. bounds=self.bnds, constraints=cons)
  26. assert_allclose(res.x, [1.4, 1.7], rtol=1e-4)
  27. assert_allclose(res.fun, 0.8, rtol=1e-4)
  28. def test_constraint_dictionary_2(self):
  29. fun = lambda x: (x[0] - 1)**2 + (x[1] - 2.5)**2
  30. cons = {'type': 'eq',
  31. 'fun': lambda x, p1, p2: p1*x[0] - p2*x[1],
  32. 'args': (1, 1.1),
  33. 'jac': lambda x, p1, p2: np.array([[p1, -p2]])}
  34. with suppress_warnings() as sup:
  35. sup.filter(UserWarning, "delta_grad == 0.0")
  36. res = minimize(fun, self.x0, method=self.method,
  37. bounds=self.bnds, constraints=cons)
  38. assert_allclose(res.x, [1.7918552, 1.62895927])
  39. assert_allclose(res.fun, 1.3857466063348418)
  40. def test_constraint_dictionary_3(self):
  41. fun = lambda x: (x[0] - 1)**2 + (x[1] - 2.5)**2
  42. cons = [{'type': 'ineq', 'fun': lambda x: x[0] - 2 * x[1] + 2},
  43. NonlinearConstraint(lambda x: x[0] - x[1], 0, 0)]
  44. with suppress_warnings() as sup:
  45. sup.filter(UserWarning, "delta_grad == 0.0")
  46. res = minimize(fun, self.x0, method=self.method,
  47. bounds=self.bnds, constraints=cons)
  48. assert_allclose(res.x, [1.75, 1.75], rtol=1e-4)
  49. assert_allclose(res.fun, 1.125, rtol=1e-4)
  50. class TestNewToOld:
  51. def test_multiple_constraint_objects(self):
  52. fun = lambda x: (x[0] - 1)**2 + (x[1] - 2.5)**2 + (x[2] - 0.75)**2
  53. x0 = [2, 0, 1]
  54. coni = [] # only inequality constraints (can use cobyla)
  55. methods = ["slsqp", "cobyla", "trust-constr"]
  56. # mixed old and new
  57. coni.append([{'type': 'ineq', 'fun': lambda x: x[0] - 2 * x[1] + 2},
  58. NonlinearConstraint(lambda x: x[0] - x[1], -1, 1)])
  59. coni.append([LinearConstraint([1, -2, 0], -2, np.inf),
  60. NonlinearConstraint(lambda x: x[0] - x[1], -1, 1)])
  61. coni.append([NonlinearConstraint(lambda x: x[0] - 2 * x[1] + 2, 0, np.inf),
  62. NonlinearConstraint(lambda x: x[0] - x[1], -1, 1)])
  63. for con in coni:
  64. funs = {}
  65. for method in methods:
  66. with suppress_warnings() as sup:
  67. sup.filter(UserWarning)
  68. result = minimize(fun, x0, method=method, constraints=con)
  69. funs[method] = result.fun
  70. assert_allclose(funs['slsqp'], funs['trust-constr'], rtol=1e-4)
  71. assert_allclose(funs['cobyla'], funs['trust-constr'], rtol=1e-4)
  72. def test_individual_constraint_objects(self):
  73. fun = lambda x: (x[0] - 1)**2 + (x[1] - 2.5)**2 + (x[2] - 0.75)**2
  74. x0 = [2, 0, 1]
  75. cone = [] # with equality constraints (can't use cobyla)
  76. coni = [] # only inequality constraints (can use cobyla)
  77. methods = ["slsqp", "cobyla", "trust-constr"]
  78. # nonstandard data types for constraint equality bounds
  79. cone.append(NonlinearConstraint(lambda x: x[0] - x[1], 1, 1))
  80. cone.append(NonlinearConstraint(lambda x: x[0] - x[1], [1.21], [1.21]))
  81. cone.append(NonlinearConstraint(lambda x: x[0] - x[1],
  82. 1.21, np.array([1.21])))
  83. # multiple equalities
  84. cone.append(NonlinearConstraint(
  85. lambda x: [x[0] - x[1], x[1] - x[2]],
  86. 1.21, 1.21)) # two same equalities
  87. cone.append(NonlinearConstraint(
  88. lambda x: [x[0] - x[1], x[1] - x[2]],
  89. [1.21, 1.4], [1.21, 1.4])) # two different equalities
  90. cone.append(NonlinearConstraint(
  91. lambda x: [x[0] - x[1], x[1] - x[2]],
  92. [1.21, 1.21], 1.21)) # equality specified two ways
  93. cone.append(NonlinearConstraint(
  94. lambda x: [x[0] - x[1], x[1] - x[2]],
  95. [1.21, -np.inf], [1.21, np.inf])) # equality + unbounded
  96. # nonstandard data types for constraint inequality bounds
  97. coni.append(NonlinearConstraint(lambda x: x[0] - x[1], 1.21, np.inf))
  98. coni.append(NonlinearConstraint(lambda x: x[0] - x[1], [1.21], np.inf))
  99. coni.append(NonlinearConstraint(lambda x: x[0] - x[1],
  100. 1.21, np.array([np.inf])))
  101. coni.append(NonlinearConstraint(lambda x: x[0] - x[1], -np.inf, -3))
  102. coni.append(NonlinearConstraint(lambda x: x[0] - x[1],
  103. np.array(-np.inf), -3))
  104. # multiple inequalities/equalities
  105. coni.append(NonlinearConstraint(
  106. lambda x: [x[0] - x[1], x[1] - x[2]],
  107. 1.21, np.inf)) # two same inequalities
  108. cone.append(NonlinearConstraint(
  109. lambda x: [x[0] - x[1], x[1] - x[2]],
  110. [1.21, -np.inf], [1.21, 1.4])) # mixed equality/inequality
  111. coni.append(NonlinearConstraint(
  112. lambda x: [x[0] - x[1], x[1] - x[2]],
  113. [1.1, .8], [1.2, 1.4])) # bounded above and below
  114. coni.append(NonlinearConstraint(
  115. lambda x: [x[0] - x[1], x[1] - x[2]],
  116. [-1.2, -1.4], [-1.1, -.8])) # - bounded above and below
  117. # quick check of LinearConstraint class (very little new code to test)
  118. cone.append(LinearConstraint([1, -1, 0], 1.21, 1.21))
  119. cone.append(LinearConstraint([[1, -1, 0], [0, 1, -1]], 1.21, 1.21))
  120. cone.append(LinearConstraint([[1, -1, 0], [0, 1, -1]],
  121. [1.21, -np.inf], [1.21, 1.4]))
  122. for con in coni:
  123. funs = {}
  124. for method in methods:
  125. with suppress_warnings() as sup:
  126. sup.filter(UserWarning)
  127. result = minimize(fun, x0, method=method, constraints=con)
  128. funs[method] = result.fun
  129. assert_allclose(funs['slsqp'], funs['trust-constr'], rtol=1e-3)
  130. assert_allclose(funs['cobyla'], funs['trust-constr'], rtol=1e-3)
  131. for con in cone:
  132. funs = {}
  133. for method in methods[::2]: # skip cobyla
  134. with suppress_warnings() as sup:
  135. sup.filter(UserWarning)
  136. result = minimize(fun, x0, method=method, constraints=con)
  137. funs[method] = result.fun
  138. assert_allclose(funs['slsqp'], funs['trust-constr'], rtol=1e-3)
  139. class TestNewToOldSLSQP:
  140. method = 'slsqp'
  141. elec = Elec(n_electrons=2)
  142. elec.x_opt = np.array([-0.58438468, 0.58438466, 0.73597047,
  143. -0.73597044, 0.34180668, -0.34180667])
  144. brock = BoundedRosenbrock()
  145. brock.x_opt = [0, 0]
  146. list_of_problems = [Maratos(),
  147. HyperbolicIneq(),
  148. Rosenbrock(),
  149. IneqRosenbrock(),
  150. EqIneqRosenbrock(),
  151. elec,
  152. brock
  153. ]
  154. def test_list_of_problems(self):
  155. for prob in self.list_of_problems:
  156. with suppress_warnings() as sup:
  157. sup.filter(UserWarning)
  158. result = minimize(prob.fun, prob.x0,
  159. method=self.method,
  160. bounds=prob.bounds,
  161. constraints=prob.constr)
  162. assert_array_almost_equal(result.x, prob.x_opt, decimal=3)
  163. def test_warn_mixed_constraints(self):
  164. # warns about inefficiency of mixed equality/inequality constraints
  165. fun = lambda x: (x[0] - 1)**2 + (x[1] - 2.5)**2 + (x[2] - 0.75)**2
  166. cons = NonlinearConstraint(lambda x: [x[0]**2 - x[1], x[1] - x[2]],
  167. [1.1, .8], [1.1, 1.4])
  168. bnds = ((0, None), (0, None), (0, None))
  169. with suppress_warnings() as sup:
  170. sup.filter(UserWarning, "delta_grad == 0.0")
  171. assert_warns(OptimizeWarning, minimize, fun, (2, 0, 1),
  172. method=self.method, bounds=bnds, constraints=cons)
  173. def test_warn_ignored_options(self):
  174. # warns about constraint options being ignored
  175. fun = lambda x: (x[0] - 1)**2 + (x[1] - 2.5)**2 + (x[2] - 0.75)**2
  176. x0 = (2, 0, 1)
  177. if self.method == "slsqp":
  178. bnds = ((0, None), (0, None), (0, None))
  179. else:
  180. bnds = None
  181. cons = NonlinearConstraint(lambda x: x[0], 2, np.inf)
  182. res = minimize(fun, x0, method=self.method,
  183. bounds=bnds, constraints=cons)
  184. # no warnings without constraint options
  185. assert_allclose(res.fun, 1)
  186. cons = LinearConstraint([1, 0, 0], 2, np.inf)
  187. res = minimize(fun, x0, method=self.method,
  188. bounds=bnds, constraints=cons)
  189. # no warnings without constraint options
  190. assert_allclose(res.fun, 1)
  191. cons = []
  192. cons.append(NonlinearConstraint(lambda x: x[0]**2, 2, np.inf,
  193. keep_feasible=True))
  194. cons.append(NonlinearConstraint(lambda x: x[0]**2, 2, np.inf,
  195. hess=BFGS()))
  196. cons.append(NonlinearConstraint(lambda x: x[0]**2, 2, np.inf,
  197. finite_diff_jac_sparsity=42))
  198. cons.append(NonlinearConstraint(lambda x: x[0]**2, 2, np.inf,
  199. finite_diff_rel_step=42))
  200. cons.append(LinearConstraint([1, 0, 0], 2, np.inf,
  201. keep_feasible=True))
  202. for con in cons:
  203. assert_warns(OptimizeWarning, minimize, fun, x0,
  204. method=self.method, bounds=bnds, constraints=cons)
  205. class TestNewToOldCobyla:
  206. method = 'cobyla'
  207. list_of_problems = [
  208. Elec(n_electrons=2),
  209. Elec(n_electrons=4),
  210. ]
  211. @pytest.mark.slow
  212. def test_list_of_problems(self):
  213. for prob in self.list_of_problems:
  214. with suppress_warnings() as sup:
  215. sup.filter(UserWarning)
  216. truth = minimize(prob.fun, prob.x0,
  217. method='trust-constr',
  218. bounds=prob.bounds,
  219. constraints=prob.constr)
  220. result = minimize(prob.fun, prob.x0,
  221. method=self.method,
  222. bounds=prob.bounds,
  223. constraints=prob.constr)
  224. assert_allclose(result.fun, truth.fun, rtol=1e-3)