2 import pyexotica
as exo
4 from numpy
import testing
as nptest
5 from scipy.optimize
import minimize
16 threshold_step_acceptance=0.1,
18 threshold_gradient_tolerance=1e-5,
28 threshold_step_acceptance,
30 threshold_gradient_tolerance,
41 threshold_step_acceptance,
43 threshold_gradient_tolerance,
56 threshold_step_acceptance=0.1,
58 threshold_gradient_tolerance=1e-5,
69 threshold_step_acceptance,
71 threshold_gradient_tolerance,
76 return 0.5 * np.matmul(np.matmul(x.T, H), x) + np.matmul(q.T, x)
84 (b_low[0], b_high[0]),
85 (b_low[1], b_high[1]),
89 nptest.assert_allclose(
94 err_msg=
"BoxQP and SciPy (" + scipy_method +
") differ!",
99 """Tests BoxQP implementation against SciPy.""" 102 for _
in range(NUM_TESTS):
103 H = np.random.normal(size=(2, 2), loc=0, scale=10)
105 H[0, 1] = H[1, 0] = 0
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])
116 for _
in range(NUM_TESTS):
117 H = np.array([[0.0, 0.0], [0.0, 0.0]])
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)
128 for _
in range(NUM_TESTS):
129 H = np.random.normal(size=(2, 2), loc=0, scale=10)
131 H[0, 1] = H[1, 0] = 0
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])
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
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])
156 if __name__ ==
"__main__":
def test_small_numbers(self)
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")
def test_big_numbers(self)
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)