How to use the diffcp.cones.pi function in diffcp

To help you get started, we’ve selected a few diffcp examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cvxgrp / diffcp / tests.py View on Github external
np.testing.assert_allclose(s, cone_lib.pi(
            s, cone_lib.parse_cone_dict(cone_dims), dual=False), atol=1e-8)
        np.testing.assert_allclose(y, cone_lib.pi(
            y, cone_lib.parse_cone_dict(cone_dims), dual=True), atol=1e-8)

        x = cp.Variable(10)
        prob = cp.Problem(cp.Minimize(cp.sum_squares(np.random.randn(5, 10) @ x) + np.random.randn(10) @ x), [cp.norm2(x) <= 1, np.random.randn(2, 10) @ x == np.random.randn(2)])
        A, b, c, cone_dims = utils.scs_data_from_cvxpy_problem(prob)
        x, y, s, derivative, adjoint_derivative = cone_prog.solve_and_derivative(
            A, b, c, cone_dims, solver="ECOS")

        # check optimality conditions
        np.testing.assert_allclose(A @ x + s, b, atol=1e-8)
        np.testing.assert_allclose(A.T @ y + c, 0, atol=1e-8)
        np.testing.assert_allclose(s @ y, 0, atol=1e-8)
        np.testing.assert_allclose(s, cone_lib.pi(
            s, cone_lib.parse_cone_dict(cone_dims), dual=False), atol=1e-8)
        np.testing.assert_allclose(y, cone_lib.pi(
            y, cone_lib.parse_cone_dict(cone_dims), dual=True), atol=1e-8)
github cvxgrp / diffcp / tests.py View on Github external
for _ in range(10):
            zero_dim = np.random.randint(1, 10)
            pos_dim = np.random.randint(1, 10)
            soc_dim = [np.random.randint(1, 10) for _ in range(
                np.random.randint(1, 10))]
            psd_dim = [np.random.randint(1, 10) for _ in range(
                np.random.randint(1, 10))]
            exp_dim = np.random.randint(3, 18)
            cones = [(cone_lib.ZERO, zero_dim), (cone_lib.POS, pos_dim),
                     (cone_lib.SOC, soc_dim), (cone_lib.PSD, psd_dim),
                     (cone_lib.EXP, exp_dim), (cone_lib.EXP_DUAL, exp_dim)]
            size = zero_dim + pos_dim + sum(soc_dim) + sum(
                [cone_lib.vec_psd_dim(d) for d in psd_dim]) + 2 * 3 * exp_dim
            x = np.random.randn(size)
            for dual in [False, True]:
                proj = cone_lib.pi(x, cones, dual=dual)

                offset = 0
                np.testing.assert_allclose(proj[:zero_dim],
                                           cone_lib._proj(x[:zero_dim], cone_lib.ZERO, dual=dual))
                offset += zero_dim

                np.testing.assert_allclose(proj[offset:offset + pos_dim],
                                           cone_lib._proj(x[offset:offset + pos_dim], cone_lib.POS,
                                                          dual=dual))
                offset += pos_dim

                for dim in soc_dim:
                    np.testing.assert_allclose(proj[offset:offset + dim],
                                               cone_lib._proj(x[offset:offset + dim], cone_lib.SOC,
                                                              dual=dual))
                    offset += dim
github cvxgrp / diffcp / tests.py View on Github external
np.random.randint(1, 10))]
            psd_dim = [np.random.randint(1, 10) for _ in range(
                np.random.randint(1, 10))]
            exp_dim = np.random.randint(3, 18)
            cones = [(cone_lib.ZERO, zero_dim), (cone_lib.POS, pos_dim),
                     (cone_lib.SOC, soc_dim), (cone_lib.PSD, psd_dim),
                     (cone_lib.EXP, exp_dim), (cone_lib.EXP_DUAL, exp_dim)]
            size = zero_dim + pos_dim + sum(soc_dim) + sum(
                [cone_lib.vec_psd_dim(d) for d in psd_dim]) + 2 * 3 * exp_dim
            x = np.random.randn(size)

            for dual in [False, True]:
                cone_list_cpp = cone_lib.parse_cone_dict_cpp(cones)
                proj_x = cone_lib.pi(x, cones, dual=dual)
                dx = 1e-7 * np.random.randn(size)
                z = cone_lib.pi(x + dx, cones, dual=dual)

                Dpi = _diffcp.dprojection(x, cone_list_cpp, dual)
                np.testing.assert_allclose(
                    Dpi.matvec(dx), z - proj_x, atol=1e-6)

                Dpi = _diffcp.dprojection_dense(x, cone_list_cpp, dual)
                np.testing.assert_allclose(Dpi @ dx, z - proj_x, atol=1e-6)
github cvxgrp / diffcp / tests.py View on Github external
pos_dim = np.random.randint(1, 10)
            soc_dim = [np.random.randint(1, 10) for _ in range(
                np.random.randint(1, 10))]
            psd_dim = [np.random.randint(1, 10) for _ in range(
                np.random.randint(1, 10))]
            exp_dim = np.random.randint(3, 18)
            cones = [(cone_lib.ZERO, zero_dim), (cone_lib.POS, pos_dim),
                     (cone_lib.SOC, soc_dim), (cone_lib.PSD, psd_dim),
                     (cone_lib.EXP, exp_dim), (cone_lib.EXP_DUAL, exp_dim)]
            size = zero_dim + pos_dim + sum(soc_dim) + sum(
                [cone_lib.vec_psd_dim(d) for d in psd_dim]) + 2 * 3 * exp_dim
            x = np.random.randn(size)

            for dual in [False, True]:
                cone_list_cpp = cone_lib.parse_cone_dict_cpp(cones)
                proj_x = cone_lib.pi(x, cones, dual=dual)
                dx = 1e-7 * np.random.randn(size)
                z = cone_lib.pi(x + dx, cones, dual=dual)

                Dpi = _diffcp.dprojection(x, cone_list_cpp, dual)
                np.testing.assert_allclose(
                    Dpi.matvec(dx), z - proj_x, atol=1e-6)

                Dpi = _diffcp.dprojection_dense(x, cone_list_cpp, dual)
                np.testing.assert_allclose(Dpi @ dx, z - proj_x, atol=1e-6)