test_box_qp.py
Go to the documentation of this file.
1 import numpy as np
2 import pyexotica as exo
3 import unittest
4 from numpy import testing as nptest
5 from scipy.optimize import minimize
6 
7 NUM_TESTS = 1000
8 
9 
11  H,
12  q,
13  b_low,
14  b_high,
15  x_init,
16  threshold_step_acceptance=0.1,
17  max_iterations=100,
18  threshold_gradient_tolerance=1e-5,
19  regularization=1e-12,
20  scipy_method="TNC",
21 ):
23  H,
24  q,
25  b_low,
26  b_high,
27  x_init,
28  threshold_step_acceptance,
29  max_iterations,
30  threshold_gradient_tolerance,
31  regularization,
32  scipy_method,
33  exo.box_qp,
34  )
36  H,
37  q,
38  b_low,
39  b_high,
40  x_init,
41  threshold_step_acceptance,
42  max_iterations,
43  threshold_gradient_tolerance,
44  regularization,
45  scipy_method,
46  exo.box_qp_old,
47  )
48 
49 
51  H,
52  q,
53  b_low,
54  b_high,
55  x_init,
56  threshold_step_acceptance=0.1,
57  max_iterations=100,
58  threshold_gradient_tolerance=1e-5,
59  regularization=0,
60  scipy_method="TNC",
61  box_qp=exo.box_qp,
62 ):
63  sol = box_qp(
64  H,
65  q,
66  b_low,
67  b_high,
68  x_init,
69  threshold_step_acceptance,
70  max_iterations,
71  threshold_gradient_tolerance,
72  regularization,
73  )
74 
75  def cost(x):
76  return 0.5 * np.matmul(np.matmul(x.T, H), x) + np.matmul(q.T, x)
77 
78  # TODO: This is hard-coded for 2D right now!
79  sp_sol = minimize(
80  cost,
81  x_init,
82  method=scipy_method,
83  bounds=[
84  (b_low[0], b_high[0]),
85  (b_low[1], b_high[1]),
86  ],
87  )
88 
89  nptest.assert_allclose(
90  sp_sol.x,
91  sol.x,
92  rtol=1,
93  atol=1e-3,
94  err_msg="BoxQP and SciPy (" + scipy_method + ") differ!",
95  )
96 
97 
98 class TestBoxQP(unittest.TestCase):
99  """Tests BoxQP implementation against SciPy."""
100 
101  def test_zero_q(self):
102  for _ in range(NUM_TESTS):
103  H = np.random.normal(size=(2, 2), loc=0, scale=10)
104  H = np.abs(H)
105  H[0, 1] = H[1, 0] = 0
106 
107  b_low = np.array([-5.0, -5.0])
108  b_high = np.array([5.0, 5.0])
109  x_init = np.random.uniform(low=-5, high=5, size=(2,))
110  q = np.array([0.0, 0.0])
111 
112  # check_boxqp_vs_scipy(H, q, b_low, b_high, x_init, scipy_method='TNC')
113  check_boxqp_vs_scipy(H, q, b_low, b_high, x_init, scipy_method="L-BFGS-B")
114 
115  def test_zero_h(self):
116  for _ in range(NUM_TESTS):
117  H = np.array([[0.0, 0.0], [0.0, 0.0]])
118 
119  b_low = np.array([-5.0, -5.0])
120  b_high = np.array([5.0, 5.0])
121  x_init = np.array([-3.0, 2.0])
122  q = np.random.normal(size=(2, 1), loc=0, scale=10)
123 
124  check_boxqp_vs_scipy(H, q, b_low, b_high, x_init, scipy_method="TNC")
125  check_boxqp_vs_scipy(H, q, b_low, b_high, x_init, scipy_method="L-BFGS-B")
126 
127  def test_big_numbers(self):
128  for _ in range(NUM_TESTS):
129  H = np.random.normal(size=(2, 2), loc=0, scale=10)
130  H = np.abs(H) * 1e20
131  H[0, 1] = H[1, 0] = 0
132 
133  b_low = np.array([-5.0, -5.0])
134  b_high = np.array([5.0, 5.0])
135  x_init = np.array([-3.0, 2.0])
136  q = np.array([0, 0])
137 
138  check_boxqp_vs_scipy(H, q, b_low, b_high, x_init, scipy_method="TNC")
139  check_boxqp_vs_scipy(H, q, b_low, b_high, x_init, scipy_method="L-BFGS-B")
140 
142  for _ in range(NUM_TESTS):
143  H = np.random.normal(size=(2, 2), loc=0, scale=10)
144  H = np.abs(H) * 1e-20
145  H[0, 1] = H[1, 0] = 0
146 
147  b_low = np.array([-5.0, -5.0])
148  b_high = np.array([5.0, 5.0])
149  x_init = np.array([-3.0, 2.0])
150  q = np.array([0, 0])
151 
152  check_boxqp_vs_scipy(H, q, b_low, b_high, x_init, scipy_method="TNC")
153  check_boxqp_vs_scipy(H, q, b_low, b_high, x_init, scipy_method="L-BFGS-B")
154 
155 
156 if __name__ == "__main__":
157  unittest.main()
def test_small_numbers(self)
Definition: test_box_qp.py:141
def check_boxqp_vs_scipy(H, q, b_low, b_high, x_init, threshold_step_acceptance=0.1, max_iterations=100, threshold_gradient_tolerance=1e-5, regularization=1e-12, scipy_method="TNC")
Definition: test_box_qp.py:21
def test_big_numbers(self)
Definition: test_box_qp.py:127
def check_boxqp_vs_scipy_impl(H, q, b_low, b_high, x_init, threshold_step_acceptance=0.1, max_iterations=100, threshold_gradient_tolerance=1e-5, regularization=0, scipy_method="TNC", box_qp=exo.box_qp)
Definition: test_box_qp.py:62


exotica_python
Author(s):
autogenerated on Sat Apr 10 2021 02:35:59