Exemple #1
0
def test_feasible(use_indirect):
    for i in range(num_feas):
        data, p_star = tools.gen_feasible(K, n=m // 3, density=0.1)

        sol = scs.solve(data, K, use_indirect=use_indirect, **opts)
        assert_almost_equal(np.dot(data['c'], sol['x']), p_star, decimal=2)
        assert_almost_equal(np.dot(-data['b'], sol['y']), p_star, decimal=2)
Exemple #2
0
def test_feasible(use_indirect):
    for i in range(num_feas):
        data, p_star = tools.gen_feasible(K, n=m // 3, density=0.1)
        solver = scs.SCS(data, K, use_indirect=use_indirect, **opts)
        sol = solver.solve()
        assert_almost_equal(np.dot(data["c"], sol["x"]), p_star, decimal=2)
        assert_almost_equal(np.dot(-data["b"], sol["y"]), p_star, decimal=2)
def test_solve_feasible(use_indirect, gpu):
    data, p_star = tools.gen_feasible(K, n=m // 3, density=0.01)

    sol = scs.solve(data, K, use_indirect=use_indirect, gpu=gpu, **params)
    x = sol['x']
    y = sol['y']
    print('p*  = ', p_star)
    print('pri error = ', (np.dot(data['c'], x) - p_star) / p_star)
    print('dual error = ', (-np.dot(data['b'], y) - p_star) / p_star)
def test_feasible():
  for i in range(num_feas):
    data, p_star = tools.gen_feasible(K, n=m // 3, density=0.1)

    sol = scs.solve(data, K, use_indirect=False, **opts)
    yield check_solution, np.dot(data['c'], sol['x']), p_star
    yield check_solution, np.dot(-data['b'], sol['y']), p_star

    sol = scs.solve(data, K, use_indirect=True, **opts)
    yield check_solution, np.dot(data['c'], sol['x']), p_star
    yield check_solution, np.dot(-data['b'], sol['y']), p_star
Exemple #5
0
def test_solve_feasible(use_indirect, gpu):
    data, p_star = tools.gen_feasible(K, n=m // 3, density=0.1)

    sol = scs.solve(data, K, use_indirect=use_indirect, gpu=gpu, **params)
    x = sol['x']
    y = sol['y']
    s = sol['s']
    np.testing.assert_almost_equal(np.dot(data['c'], x), p_star, decimal=3)
    np.testing.assert_almost_equal(np.dot(data['c'], x), p_star, decimal=3)
    np.testing.assert_array_less(np.linalg.norm(data['A'] @ x - data['b'] + s),
                                 1e-3)
    np.testing.assert_array_less(np.linalg.norm(data['A'].T @ y + data['c']),
                                 1e-3)
    np.testing.assert_almost_equal(s.T @ y, 0.)
    np.testing.assert_almost_equal(s, tools.proj_cone(s, K), decimal=4)
    np.testing.assert_almost_equal(y, tools.proj_dual_cone(y, K), decimal=4)
Exemple #6
0
def test_solve_feasible(use_indirect, gpu):
    data, p_star = tools.gen_feasible(K, n=m // 3, density=0.1)
    solver = scs.SCS(data, K, use_indirect=use_indirect, gpu=gpu, **params)
    sol = solver.solve()
    x = sol["x"]
    y = sol["y"]
    s = sol["s"]
    np.testing.assert_almost_equal(np.dot(data["c"], x), p_star, decimal=3)
    np.testing.assert_almost_equal(np.dot(data["c"], x), p_star, decimal=3)
    np.testing.assert_array_less(np.linalg.norm(data["A"] @ x - data["b"] + s),
                                 1e-3)
    np.testing.assert_array_less(np.linalg.norm(data["A"].T @ y + data["c"]),
                                 1e-3)
    np.testing.assert_almost_equal(s.T @ y, 0.0)
    np.testing.assert_almost_equal(s, tools.proj_cone(s, K), decimal=4)
    np.testing.assert_almost_equal(y, tools.proj_dual_cone(y, K), decimal=4)
Exemple #7
0
def solve_feasible(use_indirect, gpu):
    # cone:
    K = {
        'f': 10,
        'l': 15,
        'q': [5, 10, 0, 1],
        's': [3, 4, 0, 0, 1],
        'ep': 10,
        'ed': 10,
        'p': [-0.25, 0.5, 0.75, -0.33]
    }
    m = tools.get_scs_cone_dims(K)
    data, p_star = tools.gen_feasible(K, n=m // 3, density=0.01)
    params = {'normalize': True, 'scale': 5, 'cg_rate': 2}

    sol = scs.solve(data, K, use_indirect=use_indirect, gpu=gpu, **params)
    x = sol['x']
    y = sol['y']
    print('p*  = ', p_star)
    print('pri error = ', (np.dot(data['c'], x) - p_star) / p_star)
    print('dual error = ', (-np.dot(data['b'], y) - p_star) / p_star)
Exemple #8
0
def solve_feasible(use_indirect, gpu):
    # cone:
    K = {
        "f": 10,
        "l": 15,
        "q": [5, 10, 0, 1],
        "s": [3, 4, 0, 0, 1],
        "ep": 10,
        "ed": 10,
        "p": [-0.25, 0.5, 0.75, -0.33],
    }
    m = tools.get_scs_cone_dims(K)
    data, p_star = tools.gen_feasible(K, n=m // 3, density=0.01)
    params = {"normalize": True, "scale": 5}

    sol = scs.solve(data, K, use_indirect=use_indirect, gpu=gpu, **params)
    x = sol["x"]
    y = sol["y"]
    print("p*  = ", p_star)
    print("pri error = ", (np.dot(data["c"], x) - p_star) / p_star)
    print("dual error = ", (-np.dot(data["b"], y) - p_star) / p_star)
Exemple #9
0
def test_python_linsys():
    global Msolve, A, nz_cone, ncon_cone

    for i in range(num_probs):
        data, p_star = tools.gen_feasible(K, n=m // 3, density=0.1)

        A = data['A']
        ncon_cone, nz_cone = A.shape
        Msolve = None

        def init_lin_sys_work_cb(rho):
            global Msolve, nz_cone, ncon_cone, A
            M = sp.bmat([[rho * sp.eye(nz_cone), A.T], [A,
                                                        -sp.eye(ncon_cone)]])
            Msolve = sla.factorized(M)

        def solve_lin_sys_cb(b, s, i):
            global Msolve
            b[:] = Msolve(b)

        def accum_by_a_cb(x, y):
            global A
            y += A.dot(x)

        def accum_by_atrans_cb(x, y):
            global A
            y += A.T.dot(x)

        def normalize_a_cb(boundaries, scale):
            global A, ncon_cone, nz_cone

            D_all = np.ones(ncon_cone)
            E_all = np.ones(nz_cone)

            min_scale, max_scale = (1e-4, 1e4)
            n_passes = 10

            for i in range(n_passes):
                D = np.sqrt(sla.norm(A, float('inf'), axis=1))
                E = np.sqrt(sla.norm(A, float('inf'), axis=0))
                D[D < min_scale] = 1.0
                E[E < min_scale] = 1.0
                D[D > max_scale] = max_scale
                E[E > max_scale] = max_scale
                start = boundaries[0]
                for delta in boundaries[1:]:
                    D[start:start + delta] = D[start:start + delta].mean()
                    start += delta
                A = sp.diags(1 / D).dot(A).dot(sp.diags(1 / E))
                D_all *= D
                E_all *= E

            mean_row_norm = sla.norm(A, 2, axis=1).mean()
            mean_col_norm = sla.norm(A, 2, axis=0).mean()
            A *= scale

            return D_all, E_all, mean_row_norm, mean_col_norm

        def un_normalize_a_cb(D, E, scale):
            global A
            A = sp.diags(D).dot(A).dot(sp.diags(E)) / scale

        sol = scs.solve(
            data,
            K,
            verbose=False,
            use_indirect=False,
            normalize=False,
            linsys_cbs=(
                init_lin_sys_work_cb,
                solve_lin_sys_cb,
                accum_by_a_cb,
                accum_by_atrans_cb,
                normalize_a_cb,
                un_normalize_a_cb,
            ),
            max_iters=int(1e5),
            eps=1e-5,
        )

        yield check_solution, np.dot(data['c'], sol['x']), p_star
        yield check_solution, np.dot(-data['b'], sol['y']), p_star

        sol = scs.solve(
            data,
            K,
            verbose=False,
            use_indirect=False,
            normalize=True,
            linsys_cbs=(
                init_lin_sys_work_cb,
                solve_lin_sys_cb,
                accum_by_a_cb,
                accum_by_atrans_cb,
                normalize_a_cb,
                un_normalize_a_cb,
            ),
            max_iters=int(1e5),
            eps=1e-5,
        )

        yield check_solution, np.dot(data['c'], sol['x']), p_star
        yield check_solution, np.dot(-data['b'], sol['y']), p_star