コード例 #1
0
ファイル: tests.py プロジェクト: spenrich/diffcp
    def test_threading(self):
        np.random.seed(0)
        m = 20
        n = 10
        As, bs, cs, cone_dicts = [], [], [], []
        results = []

        for _ in range(50):
            A, b, c, cone_dims = utils.least_squares_eq_scs_data(m, n)
            As += [A]
            bs += [b]
            cs += [c]
            cone_dicts += [cone_dims]
            results.append(cone_prog.solve_and_derivative(A, b, c, cone_dims))

        for n_jobs in [1, -1]:
            xs, ys, ss, _, DT_batch = cone_prog.solve_and_derivative_batch(
                As, bs, cs, cone_dicts, n_jobs_forward=n_jobs, n_jobs_backward=n_jobs)

            for i in range(50):
                np.testing.assert_allclose(results[i][0], xs[i])
                np.testing.assert_allclose(results[i][1], ys[i])
                np.testing.assert_allclose(results[i][2], ss[i])
            
            dAs, dbs, dcs = DT_batch(xs, ys, ss)
            for i in range(50):
                dA, db, dc = results[i][-1](results[i][0], results[i][1], results[i][2])
                np.testing.assert_allclose(dA.todense(), dAs[i].todense())
                np.testing.assert_allclose(dbs[i], db)
                np.testing.assert_allclose(dcs[i], dc)
コード例 #2
0
    def test_threading(self):
        m = 20
        n = 10
        As, bs, cs, cone_dicts = [], [], [], []
        results = []

        serial_time = 0.0
        for _ in range(50):
            A, b, c, cone_dims = utils.least_squares_eq_scs_data(m, n)
            As += [A]
            bs += [b]
            cs += [c]
            cone_dicts += [cone_dims]
            tic = time.time()
            results.append(cone_prog.solve_and_derivative(A, b, c, cone_dims))
            toc = time.time()
            serial_time += toc - tic

        tic = time.time()
        results_thread = cone_prog.solve_and_derivative_batch(
            As, bs, cs, cone_dicts)
        toc = time.time()
        parallel_time = toc - tic

        self.assertTrue(parallel_time < serial_time)

        for i in range(50):
            np.testing.assert_allclose(results[i][0], results_thread[i][0])
            np.testing.assert_allclose(results[i][1], results_thread[i][1])
            np.testing.assert_allclose(results[i][2], results_thread[i][2])
コード例 #3
0
def conic_transform_solve_batch(Qs, qs, Gs, hs, As, bs, cp_sol=cp.SCS, n_jobs=4):

    results = np.array([__single_cvxprob_formulate(Q, q, G, h, A, b, sol_opt=cp_sol) \
                        for Q, q, G, h, A, b in zip(Qs, qs, Gs, hs, As, bs)])
    As_ = results[:, 0]
    bs_ = results[:, 1]
    cs_ = results[:, 2]
    cons_dims_list = results[:, 3]

    res = diffcp_cprog.solve_and_derivative_batch(As_, bs_, cs_, cons_dims_list, n_jobs=n_jobs, eps=1e-5)
    res = np.array(res)

    x_sols_batch =res[:, 0]
    y_sols_batch =res[:, 1]
    s_sols_batch =res[:, 2]
    derivative_batch =res[:, 3]
    adjoint_derivative = res[:, 4]

    return  x_sols_batch, y_sols_batch, s_sols_batch, derivative_batch, adjoint_derivative, As_, bs_, cs_