コード例 #1
0
    def test_polish_random(self):

        # Random QP problem
        sp.random.seed(6)

        self.n = 30
        self.m = 50
        Pt = sparse.random(self.n, self.n)
        self.P = Pt.T @ Pt
        self.q = np.random.randn(self.n)
        self.A = sparse.csc_matrix(np.random.randn(self.m, self.n))
        self.l = -3 + np.random.randn(self.m)
        self.u = 3 + np.random.randn(self.m)
        self.model = osqp.OSQP()
        self.model.setup(P=self.P,
                         q=self.q,
                         A=self.A,
                         l=self.l,
                         u=self.u,
                         **self.opts)

        # Solve problem
        res = self.model.solve()

        x_sol, y_sol, obj_sol = solve_high_accuracy(self.P, self.q, self.A,
                                                    self.l, self.u)
        # Assert close
        nptest.assert_allclose(res.x, x_sol, rtol=rel_tol, atol=abs_tol)
        nptest.assert_allclose(res.y, y_sol, rtol=rel_tol, atol=abs_tol)
        nptest.assert_almost_equal(res.info.obj_val,
                                   obj_sol,
                                   decimal=decimal_tol)
コード例 #2
0
    def test_polish_unconstrained(self):

        # Unconstrained QP problem
        sp.random.seed(4)

        self.n = 30
        self.m = 0
        P = sparse.diags(np.random.rand(self.n)) + 0.2 * sparse.eye(self.n)
        self.P = P.tocsc()
        self.q = np.random.randn(self.n)
        self.A = sparse.csc_matrix((self.m, self.n))
        self.l = np.array([])
        self.u = np.array([])
        self.model = osqp.OSQP()
        self.model.setup(P=self.P,
                         q=self.q,
                         A=self.A,
                         l=self.l,
                         u=self.u,
                         **self.opts)

        # Solve problem
        res = self.model.solve()

        x_sol, _, obj_sol = solve_high_accuracy(self.P, self.q, self.A, self.l,
                                                self.u)
        # Assert close
        nptest.assert_allclose(res.x, x_sol, rtol=rel_tol, atol=abs_tol)
        nptest.assert_almost_equal(res.info.obj_val,
                                   obj_sol,
                                   decimal=decimal_tol)
コード例 #3
0
    def test_polish_simple(self):

        # Simple QP problem
        self.P = sparse.diags([11., 0.], format='csc')
        self.q = np.array([3, 4])
        self.A = sparse.csc_matrix([[-1, 0], [0, -1], [-1, -3], [2, 5], [3,
                                                                         4]])
        self.u = np.array([0, 0, -15, 100, 80])
        self.l = -1e05 * np.ones(len(self.u))
        self.n = self.P.shape[0]
        self.m = self.A.shape[0]
        self.model = osqp.OSQP()
        self.model.setup(P=self.P,
                         q=self.q,
                         A=self.A,
                         l=self.l,
                         u=self.u,
                         **self.opts)

        # Solve problem
        res = self.model.solve()

        x_sol, y_sol, obj_sol = solve_high_accuracy(self.P, self.q, self.A,
                                                    self.l, self.u)
        # Assert close
        nptest.assert_allclose(res.x, x_sol, rtol=rel_tol, atol=abs_tol)
        nptest.assert_allclose(res.y, y_sol, rtol=rel_tol, atol=abs_tol)
        nptest.assert_almost_equal(res.info.obj_val,
                                   obj_sol,
                                   decimal=decimal_tol)
コード例 #4
0
    def test_basic_QP(self):
        # Solve problem
        res = self.model.solve()

        x_sol, y_sol, obj_sol = solve_high_accuracy(self.P, self.q, self.A,
                                                    self.l, self.u)
        # Assert close
        nptest.assert_allclose(res.x, x_sol, rtol=rel_tol, atol=abs_tol)
        nptest.assert_allclose(res.y, y_sol, rtol=rel_tol, atol=abs_tol)
        nptest.assert_almost_equal(
            res.info.obj_val, obj_sol, decimal=decimal_tol)
コード例 #5
0
    def test_update_P_allind(self):
        # Update matrix P
        Px = self.P_triu_new.data
        self.model.update(Px=Px)
        res = self.model.solve()

        x_sol, y_sol, obj_sol = solve_high_accuracy(self.P_new, self.q, self.A,
                                                    self.l, self.u)
        # Assert close
        nptest.assert_allclose(res.x, x_sol, rtol=rel_tol, atol=abs_tol)
        nptest.assert_allclose(res.y, y_sol, rtol=rel_tol, atol=abs_tol)
        nptest.assert_almost_equal(res.info.obj_val,
                                   obj_sol,
                                   decimal=decimal_tol)
コード例 #6
0
    def test_update_u(self):
        # Update lower bound
        u_new = 1000 * np.ones(self.m)
        self.model.update(u=u_new)
        res = self.model.solve()

        x_sol, y_sol, obj_sol = solve_high_accuracy(self.P, self.q, self.A,
                                                    self.l, u_new)

        # Assert close
        nptest.assert_allclose(res.x, x_sol, rtol=rel_tol, atol=abs_tol)
        nptest.assert_allclose(res.y, y_sol, rtol=rel_tol, atol=abs_tol)
        nptest.assert_almost_equal(
            res.info.obj_val, obj_sol, decimal=decimal_tol)
コード例 #7
0
    def test_update_q(self):
        # Update linear cost
        q_new = np.array([10, 20])
        self.model.update(q=q_new)
        res = self.model.solve()

        x_sol, y_sol, obj_sol = solve_high_accuracy(self.P, q_new, self.A,
                                                    self.l, self.u)

        # Assert close
        nptest.assert_allclose(res.x, x_sol, rtol=rel_tol, atol=abs_tol)
        nptest.assert_allclose(res.y, y_sol, rtol=rel_tol, atol=abs_tol)
        nptest.assert_almost_equal(
            res.info.obj_val, obj_sol, decimal=decimal_tol)
コード例 #8
0
    def test_update_A(self):
        # Update matrix A
        Ax = self.A_new.data
        Ax_idx = np.arange(self.A_new.nnz)
        self.model.update(Ax=Ax, Ax_idx=Ax_idx)
        res = self.model.solve()

        x_sol, y_sol, obj_sol = solve_high_accuracy(self.P, self.q, self.A_new,
                                                    self.l, self.u)
        # Assert close
        nptest.assert_allclose(res.x, x_sol, rtol=rel_tol, atol=abs_tol)
        nptest.assert_allclose(res.y, y_sol, rtol=rel_tol, atol=abs_tol)
        nptest.assert_almost_equal(res.info.obj_val,
                                   obj_sol,
                                   decimal=decimal_tol)
コード例 #9
0
    def test_update_P_A_indP(self):
        # Update matrices P and A
        Px = self.P_triu_new.data
        Px_idx = np.arange(self.P_triu_new.nnz)
        Ax = self.A_new.data
        self.model.update(Px=Px, Px_idx=Px_idx, Ax=Ax)
        res = self.model.solve()

        x_sol, y_sol, obj_sol = solve_high_accuracy(self.P_new, self.q,
                                                    self.A_new, self.l, self.u)
        # Assert close
        nptest.assert_allclose(res.x, x_sol, rtol=rel_tol, atol=abs_tol)
        nptest.assert_allclose(res.y, y_sol, rtol=rel_tol, atol=abs_tol)
        nptest.assert_almost_equal(res.info.obj_val,
                                   obj_sol,
                                   decimal=decimal_tol)