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 
10 def check_boxqp_vs_scipy(H, q, b_low, b_high, x_init,
11  threshold_step_acceptance=0.1,
12  max_iterations=100,
13  threshold_gradient_tolerance=1e-5,
14  regularization=1e-12,
15  scipy_method='TNC'):
16  check_boxqp_vs_scipy_impl(H, q, b_low, b_high, x_init, threshold_step_acceptance, max_iterations, threshold_gradient_tolerance, regularization, scipy_method, exo.box_qp)
17  check_boxqp_vs_scipy_impl(H, q, b_low, b_high, x_init, threshold_step_acceptance, max_iterations, threshold_gradient_tolerance, regularization, scipy_method, exo.box_qp_old)
18 
19 
20 def check_boxqp_vs_scipy_impl(H, q, b_low, b_high, x_init,
21  threshold_step_acceptance=0.1,
22  max_iterations=100,
23  threshold_gradient_tolerance=1e-5,
24  regularization=0,
25  scipy_method='TNC',
26  box_qp=exo.box_qp):
27  sol = box_qp(H, q, b_low, b_high, x_init, threshold_step_acceptance, max_iterations, threshold_gradient_tolerance, regularization)
28 
29  def cost(x):
30  return .5 * np.matmul(np.matmul(x.T, H), x) + np.matmul(q.T, x)
31 
32  # TODO: This is hard-coded for 2D right now!
33  sp_sol = minimize(cost, x_init, method=scipy_method, bounds=[
34  (b_low[0], b_high[0]),
35  (b_low[1], b_high[1]),
36  ])
37 
38  nptest.assert_allclose(sp_sol.x, sol.x, rtol=1, atol=1e-3, err_msg="BoxQP and SciPy (" + scipy_method + ") differ!")
39 
40 
41 class TestBoxQP(unittest.TestCase):
42  """Tests BoxQP implementation against scipy."""
43 
44  def test_zero_q(self):
45  for _ in range(NUM_TESTS):
46  H = np.random.normal(
47  size=(2, 2), loc=0, scale=10
48  )
49  H = np.abs(H)
50  H[0, 1] = H[1,0] = 0
51 
52  b_low = np.array([-5., -5.])
53  b_high = np.array([5., 5.])
54  x_init = np.random.uniform(low=-5, high=5, size=(2,))
55  q = np.array([0.0, 0.0])
56 
57  #check_boxqp_vs_scipy(H, q, b_low, b_high, x_init, scipy_method='TNC')
58  check_boxqp_vs_scipy(H, q, b_low, b_high, x_init, scipy_method='L-BFGS-B')
59 
60  def test_zero_h(self):
61  for _ in range(NUM_TESTS):
62  H = np.array([[0.,0.], [0.,0.]])
63 
64  b_low = np.array([-5., -5.])
65  b_high = np.array([5., 5.])
66  x_init = np.array([-3., 2.])
67  q = np.random.normal(size=(2,1), loc=0, scale=10)
68 
69  check_boxqp_vs_scipy(H, q, b_low, b_high, x_init, scipy_method='TNC')
70  check_boxqp_vs_scipy(H, q, b_low, b_high, x_init, scipy_method='L-BFGS-B')
71 
72  def test_big_numbers(self):
73  for _ in range(NUM_TESTS):
74  H = np.random.normal(
75  size=(2, 2), loc=0, scale=10
76  )
77  H = np.abs(H) * 1e20
78  H[0, 1] = H[1,0] = 0
79 
80  b_low = np.array([-5., -5.])
81  b_high = np.array([5., 5.])
82  x_init = np.array([-3., 2.])
83  q = np.array([0, 0])
84 
85  check_boxqp_vs_scipy(H, q, b_low, b_high, x_init, scipy_method='TNC')
86  check_boxqp_vs_scipy(H, q, b_low, b_high, x_init, scipy_method='L-BFGS-B')
87 
88  def test_small_numbers(self):
89  for _ in range(NUM_TESTS):
90  H = np.random.normal(
91  size=(2, 2), loc=0, scale=10
92  )
93  H = np.abs(H) * 1e-20
94  H[0, 1] = H[1,0] = 0
95 
96  b_low = np.array([-5., -5.])
97  b_high = np.array([5., 5.])
98  x_init = np.array([-3., 2.])
99  q = np.array([0, 0])
100 
101  check_boxqp_vs_scipy(H, q, b_low, b_high, x_init, scipy_method='TNC')
102  check_boxqp_vs_scipy(H, q, b_low, b_high, x_init, scipy_method='L-BFGS-B')
103 
104 if __name__ == '__main__':
105  unittest.main()
def test_small_numbers(self)
Definition: test_box_qp.py:88
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:26
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:15
def test_big_numbers(self)
Definition: test_box_qp.py:72


exotica_python
Author(s):
autogenerated on Mon Feb 22 2021 03:33:27