How to use the diffcp.utils 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_ecos_solve(self):
        np.random.seed(0)
        m = 20
        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)
github cvxgrp / diffcp / tests.py View on Github external
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 / prof.py View on Github external
import cvxpy as cp
import numpy as np
from scipy import sparse
from scipy.sparse import linalg as splinalg
import time

import diffcp.cone_program as cone_prog
import diffcp.cones as cone_lib
import diffcp.utils as utils


m = 100
n = 50

A, b, c, cone_dims = utils.least_squares_eq_scs_data(m, n)
for mode in ["lsqr", "dense"]:
    x, y, s, derivative, adjoint_derivative = cone_prog.solve_and_derivative(
        A, b, c, cone_dims, eps=1e-10, mode=mode)

    dA = utils.get_random_like(
        A, lambda n: np.random.normal(0, 1e-2, size=n))
    db = np.random.normal(0, 1e-2, size=b.size)
    dc = np.random.normal(0, 1e-2, size=c.size)

    derivative_time = 0.0
    for _ in range(10):
        tic = time.time()
        dx, dy, ds = derivative(dA, db, dc)
        toc = time.time()
        derivative_time += (toc - tic) / 10
github cvxgrp / diffcp / examples / batch_ecos.py View on Github external
import diffcp
import utils
import IPython as ipy
import time
import numpy as np

m = 100
n = 50

batch_size = 16
n_jobs = 1

As, bs, cs, Ks = [], [], [], []
for _ in range(batch_size):
    A, b, c, K = diffcp.utils.least_squares_eq_scs_data(m, n)
    As += [A]
    bs += [b]
    cs += [c]
    Ks += [K]


def time_function(f, N=1):
    result = []
    for i in range(N):
        tic = time.time()
        f()
        toc = time.time()
        result += [toc - tic]
    return np.mean(result), np.std(result)

for n_jobs in range(1, 8):