def test_inactive_constraints(self, optimization_variables):
     l, Q, P = optimization_variables
     C = np.eye(len(l))
     x0 = np.zeros(len(l))
     d = 1e3 * np.ones(C.shape[0])
     x = optimization_methods._active_set_QP(l, Q, C, d, x0)
     x_sp = optimize_scipy(l, Q, C, d)
     assert np.linalg.norm(C.dot(x) <= d)
     assert np.isclose(objective(l, Q, 0, x), objective(l, Q, 0, x_sp),
                       rtol=1e-4, atol=1e-4)
    def test_remove_constraint(self, optimization_variables):
        optimum = np.array([2.5, 3, 1])
        Q = np.array([[8, 0, 0], [0, 4, 0], [0, 0, 1]])
        l = Q.dot(optimum)
        C = np.vstack([np.eye(len(l)), -np.eye(len(l))])
        x0 = 2 * np.ones(len(l))
        d = 2 * np.ones(C.shape[0])
        x = optimization_methods._active_set_QP(l, Q, C, d, x0)

        x_sp = optimize_scipy(l, Q, C, d)
        assert np.linalg.norm(C.dot(x) <= d + 1e-4)
        assert np.isclose(objective(l, Q, 0, x), objective(l, Q, 0, x_sp),
                          rtol=1e-4, atol=1e-4)
 def test_equality_constraints(self, optimization_variables):
     l, Q, P = optimization_variables
     C = np.vstack([np.eye(len(l)), -np.eye(len(l))])
     A = np.ones((1, len(l)))
     b = np.zeros(1)
     x0 = np.zeros(len(l))
     d = 0.3 * np.ones(C.shape[0])
     x = optimization_methods._active_set_QP(l, Q, C, d, x0, A=A, b=b)
     x_sp = optimize_scipy(l, Q, C, d, A, b)
     assert np.isclose(A.dot(x), b)
     assert np.linalg.norm(C.dot(x) <= d + 1e-4)
     assert np.isclose(objective(l, Q, 0, x), objective(l, Q, 0, x_sp),
                       rtol=1e-4, atol=1e-4)