test_lsq_common.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. from __future__ import division, absolute_import, print_function
  2. from numpy.testing import assert_, assert_allclose, assert_equal
  3. from pytest import raises as assert_raises
  4. import numpy as np
  5. from scipy.sparse.linalg import LinearOperator
  6. from scipy.optimize._lsq.common import (
  7. step_size_to_bound, find_active_constraints, make_strictly_feasible,
  8. CL_scaling_vector, intersect_trust_region, build_quadratic_1d,
  9. minimize_quadratic_1d, evaluate_quadratic, reflective_transformation,
  10. left_multiplied_operator, right_multiplied_operator)
  11. class TestBounds(object):
  12. def test_step_size_to_bounds(self):
  13. lb = np.array([-1.0, 2.5, 10.0])
  14. ub = np.array([1.0, 5.0, 100.0])
  15. x = np.array([0.0, 2.5, 12.0])
  16. s = np.array([0.1, 0.0, 0.0])
  17. step, hits = step_size_to_bound(x, s, lb, ub)
  18. assert_equal(step, 10)
  19. assert_equal(hits, [1, 0, 0])
  20. s = np.array([0.01, 0.05, -1.0])
  21. step, hits = step_size_to_bound(x, s, lb, ub)
  22. assert_equal(step, 2)
  23. assert_equal(hits, [0, 0, -1])
  24. s = np.array([10.0, -0.0001, 100.0])
  25. step, hits = step_size_to_bound(x, s, lb, ub)
  26. assert_equal(step, np.array(-0))
  27. assert_equal(hits, [0, -1, 0])
  28. s = np.array([1.0, 0.5, -2.0])
  29. step, hits = step_size_to_bound(x, s, lb, ub)
  30. assert_equal(step, 1.0)
  31. assert_equal(hits, [1, 0, -1])
  32. s = np.zeros(3)
  33. step, hits = step_size_to_bound(x, s, lb, ub)
  34. assert_equal(step, np.inf)
  35. assert_equal(hits, [0, 0, 0])
  36. def test_find_active_constraints(self):
  37. lb = np.array([0.0, -10.0, 1.0])
  38. ub = np.array([1.0, 0.0, 100.0])
  39. x = np.array([0.5, -5.0, 2.0])
  40. active = find_active_constraints(x, lb, ub)
  41. assert_equal(active, [0, 0, 0])
  42. x = np.array([0.0, 0.0, 10.0])
  43. active = find_active_constraints(x, lb, ub)
  44. assert_equal(active, [-1, 1, 0])
  45. active = find_active_constraints(x, lb, ub, rtol=0)
  46. assert_equal(active, [-1, 1, 0])
  47. x = np.array([1e-9, -1e-8, 100 - 1e-9])
  48. active = find_active_constraints(x, lb, ub)
  49. assert_equal(active, [0, 0, 1])
  50. active = find_active_constraints(x, lb, ub, rtol=1.5e-9)
  51. assert_equal(active, [-1, 0, 1])
  52. lb = np.array([1.0, -np.inf, -np.inf])
  53. ub = np.array([np.inf, 10.0, np.inf])
  54. x = np.ones(3)
  55. active = find_active_constraints(x, lb, ub)
  56. assert_equal(active, [-1, 0, 0])
  57. # Handles out-of-bound cases.
  58. x = np.array([0.0, 11.0, 0.0])
  59. active = find_active_constraints(x, lb, ub)
  60. assert_equal(active, [-1, 1, 0])
  61. active = find_active_constraints(x, lb, ub, rtol=0)
  62. assert_equal(active, [-1, 1, 0])
  63. def test_make_strictly_feasible(self):
  64. lb = np.array([-0.5, -0.8, 2.0])
  65. ub = np.array([0.8, 1.0, 3.0])
  66. x = np.array([-0.5, 0.0, 2 + 1e-10])
  67. x_new = make_strictly_feasible(x, lb, ub, rstep=0)
  68. assert_(x_new[0] > -0.5)
  69. assert_equal(x_new[1:], x[1:])
  70. x_new = make_strictly_feasible(x, lb, ub, rstep=1e-4)
  71. assert_equal(x_new, [-0.5 + 1e-4, 0.0, 2 * (1 + 1e-4)])
  72. x = np.array([-0.5, -1, 3.1])
  73. x_new = make_strictly_feasible(x, lb, ub)
  74. assert_(np.all((x_new >= lb) & (x_new <= ub)))
  75. x_new = make_strictly_feasible(x, lb, ub, rstep=0)
  76. assert_(np.all((x_new >= lb) & (x_new <= ub)))
  77. lb = np.array([-1, 100.0])
  78. ub = np.array([1, 100.0 + 1e-10])
  79. x = np.array([0, 100.0])
  80. x_new = make_strictly_feasible(x, lb, ub, rstep=1e-8)
  81. assert_equal(x_new, [0, 100.0 + 0.5e-10])
  82. def test_scaling_vector(self):
  83. lb = np.array([-np.inf, -5.0, 1.0, -np.inf])
  84. ub = np.array([1.0, np.inf, 10.0, np.inf])
  85. x = np.array([0.5, 2.0, 5.0, 0.0])
  86. g = np.array([1.0, 0.1, -10.0, 0.0])
  87. v, dv = CL_scaling_vector(x, g, lb, ub)
  88. assert_equal(v, [1.0, 7.0, 5.0, 1.0])
  89. assert_equal(dv, [0.0, 1.0, -1.0, 0.0])
  90. class TestQuadraticFunction(object):
  91. def setup_method(self):
  92. self.J = np.array([
  93. [0.1, 0.2],
  94. [-1.0, 1.0],
  95. [0.5, 0.2]])
  96. self.g = np.array([0.8, -2.0])
  97. self.diag = np.array([1.0, 2.0])
  98. def test_build_quadratic_1d(self):
  99. s = np.zeros(2)
  100. a, b = build_quadratic_1d(self.J, self.g, s)
  101. assert_equal(a, 0)
  102. assert_equal(b, 0)
  103. a, b = build_quadratic_1d(self.J, self.g, s, diag=self.diag)
  104. assert_equal(a, 0)
  105. assert_equal(b, 0)
  106. s = np.array([1.0, -1.0])
  107. a, b = build_quadratic_1d(self.J, self.g, s)
  108. assert_equal(a, 2.05)
  109. assert_equal(b, 2.8)
  110. a, b = build_quadratic_1d(self.J, self.g, s, diag=self.diag)
  111. assert_equal(a, 3.55)
  112. assert_equal(b, 2.8)
  113. s0 = np.array([0.5, 0.5])
  114. a, b, c = build_quadratic_1d(self.J, self.g, s, diag=self.diag, s0=s0)
  115. assert_equal(a, 3.55)
  116. assert_allclose(b, 2.39)
  117. assert_allclose(c, -0.1525)
  118. def test_minimize_quadratic_1d(self):
  119. a = 5
  120. b = -1
  121. t, y = minimize_quadratic_1d(a, b, 1, 2)
  122. assert_equal(t, 1)
  123. assert_equal(y, a * t**2 + b * t)
  124. t, y = minimize_quadratic_1d(a, b, -2, -1)
  125. assert_equal(t, -1)
  126. assert_equal(y, a * t**2 + b * t)
  127. t, y = minimize_quadratic_1d(a, b, -1, 1)
  128. assert_equal(t, 0.1)
  129. assert_equal(y, a * t**2 + b * t)
  130. c = 10
  131. t, y = minimize_quadratic_1d(a, b, -1, 1, c=c)
  132. assert_equal(t, 0.1)
  133. assert_equal(y, a * t**2 + b * t + c)
  134. def test_evaluate_quadratic(self):
  135. s = np.array([1.0, -1.0])
  136. value = evaluate_quadratic(self.J, self.g, s)
  137. assert_equal(value, 4.85)
  138. value = evaluate_quadratic(self.J, self.g, s, diag=self.diag)
  139. assert_equal(value, 6.35)
  140. s = np.array([[1.0, -1.0],
  141. [1.0, 1.0],
  142. [0.0, 0.0]])
  143. values = evaluate_quadratic(self.J, self.g, s)
  144. assert_allclose(values, [4.85, -0.91, 0.0])
  145. values = evaluate_quadratic(self.J, self.g, s, diag=self.diag)
  146. assert_allclose(values, [6.35, 0.59, 0.0])
  147. class TestTrustRegion(object):
  148. def test_intersect(self):
  149. Delta = 1.0
  150. x = np.zeros(3)
  151. s = np.array([1.0, 0.0, 0.0])
  152. t_neg, t_pos = intersect_trust_region(x, s, Delta)
  153. assert_equal(t_neg, -1)
  154. assert_equal(t_pos, 1)
  155. s = np.array([-1.0, 1.0, -1.0])
  156. t_neg, t_pos = intersect_trust_region(x, s, Delta)
  157. assert_allclose(t_neg, -3**-0.5)
  158. assert_allclose(t_pos, 3**-0.5)
  159. x = np.array([0.5, -0.5, 0])
  160. s = np.array([0, 0, 1.0])
  161. t_neg, t_pos = intersect_trust_region(x, s, Delta)
  162. assert_allclose(t_neg, -2**-0.5)
  163. assert_allclose(t_pos, 2**-0.5)
  164. x = np.ones(3)
  165. assert_raises(ValueError, intersect_trust_region, x, s, Delta)
  166. x = np.zeros(3)
  167. s = np.zeros(3)
  168. assert_raises(ValueError, intersect_trust_region, x, s, Delta)
  169. def test_reflective_transformation():
  170. lb = np.array([-1, -2], dtype=float)
  171. ub = np.array([5, 3], dtype=float)
  172. y = np.array([0, 0])
  173. x, g = reflective_transformation(y, lb, ub)
  174. assert_equal(x, y)
  175. assert_equal(g, np.ones(2))
  176. y = np.array([-4, 4], dtype=float)
  177. x, g = reflective_transformation(y, lb, np.array([np.inf, np.inf]))
  178. assert_equal(x, [2, 4])
  179. assert_equal(g, [-1, 1])
  180. x, g = reflective_transformation(y, np.array([-np.inf, -np.inf]), ub)
  181. assert_equal(x, [-4, 2])
  182. assert_equal(g, [1, -1])
  183. x, g = reflective_transformation(y, lb, ub)
  184. assert_equal(x, [2, 2])
  185. assert_equal(g, [-1, -1])
  186. lb = np.array([-np.inf, -2])
  187. ub = np.array([5, np.inf])
  188. y = np.array([10, 10], dtype=float)
  189. x, g = reflective_transformation(y, lb, ub)
  190. assert_equal(x, [0, 10])
  191. assert_equal(g, [-1, 1])
  192. def test_linear_operators():
  193. A = np.arange(6).reshape((3, 2))
  194. d_left = np.array([-1, 2, 5])
  195. DA = np.diag(d_left).dot(A)
  196. J_left = left_multiplied_operator(A, d_left)
  197. d_right = np.array([5, 10])
  198. AD = A.dot(np.diag(d_right))
  199. J_right = right_multiplied_operator(A, d_right)
  200. x = np.array([-2, 3])
  201. X = -2 * np.arange(2, 8).reshape((2, 3))
  202. xt = np.array([0, -2, 15])
  203. assert_allclose(DA.dot(x), J_left.matvec(x))
  204. assert_allclose(DA.dot(X), J_left.matmat(X))
  205. assert_allclose(DA.T.dot(xt), J_left.rmatvec(xt))
  206. assert_allclose(AD.dot(x), J_right.matvec(x))
  207. assert_allclose(AD.dot(X), J_right.matmat(X))
  208. assert_allclose(AD.T.dot(xt), J_right.rmatvec(xt))