How to use the diffcp.cones.parse_cone_dict_cpp 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
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]:
                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 / diffcp / cone_program.py View on Github external
if raise_on_error:
            raise SolverError("Solver scs returned status %s" % status)
        else:
            result["D"] = None
            result["DT"] = None
            return result

    x = result["x"]
    y = result["y"]
    s = result["s"]

    # pre-compute quantities for the derivative
    m, n = A.shape
    N = m + n + 1
    cones = cone_lib.parse_cone_dict(cone_dict)
    cones_parsed = cone_lib.parse_cone_dict_cpp(cones)
    z = (x, y - s, np.array([1]))
    u, v, w = z

    Q = sparse.bmat([
        [None, A.T, np.expand_dims(c, - 1)],
        [-A, None, np.expand_dims(b, -1)],
        [-np.expand_dims(c, -1).T, -np.expand_dims(b, -1).T, None]
    ])

    D_proj_dual_cone = _diffcp.dprojection(v, cones_parsed, True)
    if mode == "dense":
        Q_dense = Q.todense()
        M = _diffcp.M_dense(Q_dense, cones_parsed, u, v, w)
        MT = M.T
    else:
        M = _diffcp.M_operator(Q, cones_parsed, u, v, w)