How to use the diffcp.cones 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
def _test_dproj(self, cone, dual, n, x=None, tol=1e-8):
        if x is None:
            x = np.random.randn(n)
        dx = 1e-6 * np.random.randn(n)
        proj_x = cone_lib._proj(x, CPP_CONES_TO_SCS[cone.type], dual)
        z = cone_lib._proj(x + dx, CPP_CONES_TO_SCS[cone.type], dual)

        Dpi = _diffcp.dprojection(x, [cone], dual)
        np.testing.assert_allclose(Dpi.matvec(dx), z - proj_x, atol=tol)

        Dpi_dense = _diffcp.dprojection_dense(x, [cone], dual)
        np.testing.assert_allclose(Dpi_dense @ dx, z - proj_x, atol=tol)

        # assure that dense and linear operator are the same.
        for i in range(n):
            ei = np.zeros(n)
            ei[i] = 1.0
            np.testing.assert_allclose(Dpi.matvec(ei), Dpi_dense[:, i])
github cvxgrp / diffcp / tests.py View on Github external
n = 10

        A, b, c, cone_dims = utils.least_squares_eq_scs_data(m, n)
        cone_dims.pop("q")
        cone_dims.pop("s")
        cone_dims.pop("ep")
        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)

        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 / diffcp / cone_program.py View on Github external
def pi(z, cones):
    """Projection onto R^n x K^* x R_+

    `cones` represents a convex cone K, and K^* is its dual cone.
    """
    u, v, w = z
    return np.concatenate(
        [u, cone_lib.pi(v, cones, dual=True), np.maximum(w, 0)])