Esempio n. 1
0
def test_data_keys():
    data, cone, _ = ex.simple_socp()
    work = scs.Workspace(data, cone)

    assert 'A' not in work.data

    assert set(work.data.keys()) == set(['b','c'])
Esempio n. 2
0
    def __init__(self, prob, x_vars, **settings):
        """ Forms the proximal problem, stuffs the appropriate SCS matrices,
        and stores the array/matrix data.
        After initialization, doesn't depend on CVXPY in any way.

        Parameters
        ----------
        prob: CVXPY problem
        x_vars: dict
            Dict of the CVXPY Variables we want to prox on. Keys give the names
            of the variables as they'll be referred to in the input to the prox

        """
        self.settings = self.default_settings()
        self.update_settings(**settings)

        self._info = {}
        with DictTimer(_cvxpytime, self._info):
            data, self._indmap, self._solmap = stuffed_prox(prob, x_vars)

        with DictTimer(_outer_setup_time, self._info):
            self._work = cyscs.Workspace(data, data['dims'], **self.settings)

        self._bc = dict(b=data['b'], c=data['c'])

        self._warm_start = None
Esempio n. 3
0
def test_xys():
    data, cone = ex.simple_lp()
    m, n = data['A'].shape
    work = scs.Workspace(data, cone)

    with pytest.raises(ValueError):
        ws = dict(x=np.ones(n + 1), y=np.ones(m), s=np.ones(m))
        work.solve(warm_start=ws)

    with pytest.raises(ValueError):
        ws = dict(x=np.ones(n), y=np.ones(m + 1), s=np.ones(m))
        work.solve(warm_start=ws)

    with pytest.raises(ValueError):
        ws = dict(x=np.ones(n), y=np.ones(m), s=np.ones(m + 1))
        work.solve(warm_start=ws)

    ws = dict(x=np.ones(n), y=np.ones(m), s=np.ones(m))
    work.solve(warm_start=ws)

    # no value error because we copy the values to an existing float64 numpy array
    ws = dict(x=np.ones(n, dtype=np.float32), y=np.ones(m), s=np.ones(m))
    work.solve(warm_start=ws)

    # no value error because we copy the list values to an existing float64 array
    ws = dict(x=[1] * n, y=np.ones(m), s=np.ones(m))
    work.solve(warm_start=ws)
Esempio n. 4
0
def test_A():
    data, cone, true_x = ex.simple_socp()
    work = scs.Workspace(data, cone)

    # corrupt the original data (but SCS should have made an internal copy, so this is ok)
    data['A'][:] = 3

    sol = work.solve(eps=1e-6)

    assert np.allclose(sol['x'], true_x)

    # now, solving on corrupted data shouldn't work
    work = scs.Workspace(data, cone)

    sol = work.solve(eps=1e-6)

    assert not np.allclose(sol['x'], true_x)
Esempio n. 5
0
def test_settings_change():
    data, cone, _ = ex.simple_socp()
    work = scs.Workspace(data, cone)

    assert work.settings['eps'] == 1e-3

    work.solve(eps=1e-6)

    assert work.settings['eps'] == 1e-6
Esempio n. 6
0
def test_b_len_workspace2():
    data, cone = ex.simple_lp()
    work = scs.Workspace(data, cone)

    b = data['b']
    b = np.append(b, b)
    data['b'] = b

    with pytest.raises(ValueError):
        work.solve(new_bc=data)
Esempio n. 7
0
def test_c_len_workspace2():
    data, cone = ex.simple_lp()
    work = scs.Workspace(data, cone)

    c = data['c']
    c = np.append(c, c)
    data['c'] = c

    with pytest.raises(ValueError):
        work.solve(new_bc=data)
Esempio n. 8
0
def test_fixed_settings():
    data, cone, _ = ex.simple_socp()
    work = scs.Workspace(data, cone)

    expected_fixed = set(['normalize', 'use_indirect', 'scale', 'rho_x'])

    assert set(work.fixed.keys()) == expected_fixed

    with pytest.raises(Exception):
        work.settings['rho_x'] = 3.14159
        # should raise an exception because we changed a fixed setting
        work.solve()
Esempio n. 9
0
def test_workspace():
    data, cone = ex.simple_lp()
    m, n = data['A'].shape
    work = scs.Workspace(data, cone)

    ws = dict(x=np.zeros(n), y=np.zeros(m), s=np.zeros(m))

    sol = work.solve(warm_start=ws)

    # make sure sol and ws contain *different* numpy arrays
    assert any(ws['x'] != sol['x'])
    assert any(ws['y'] != sol['y'])
    assert any(ws['s'] != sol['s'])
Esempio n. 10
0
def test_settings():
    expected_keys = set(['normalize', 'use_indirect', 'scale', 'verbose',
                        'eps', 'cg_rate', 'max_iters', 'alpha', 'rho_x'])

    data, cone, _ = ex.simple_socp()
    work = scs.Workspace(data, cone)

    assert 'warm_start' not in work.settings
    assert set(work.settings.keys()) == expected_keys

    work.solve()

    assert 'warm_start' not in work.settings
    assert set(work.settings.keys()) == expected_keys
Esempio n. 11
0
def test_warm_start():
    # if warm-starting, the input warm-start vector should not be modified
    data, cone, true_x = ex.simple_socp()
    work = scs.Workspace(data, cone)

    sol = work.solve(eps=1e-2)

    assert np.linalg.norm(sol['x'] - true_x) > 1e-3

    sol2 = work.solve(warm_start=sol, eps=1e-9)

    assert np.linalg.norm(sol2['x'] - true_x) <= 1e-9

    assert np.linalg.norm(sol['x'] - sol2['x']) > 0
    assert sol['x'] is not sol2['x']
Esempio n. 12
0
def test_b_len_workspace():
    data, cone = ex.simple_lp()
    work = scs.Workspace(data, cone)

    b = data['b']
    b = np.append(b, b)

    # should go without error
    work.solve()

    # also should go on without error
    data['b'] = b
    work.solve()

    with pytest.raises(ValueError):
        work.data['b'] = b
        work.solve()
Esempio n. 13
0
def test_c_len_workspace():
    data, cone = ex.simple_lp()
    work = scs.Workspace(data, cone)

    c = data['c']
    c = np.append(c, c)

    # should go without error
    work.solve()

    # also should go on without error
    data['c'] = c
    work.solve()

    with pytest.raises(ValueError):
        work.data['c'] = c
        work.solve()
Esempio n. 14
0
def test_many_iter_ecp():
    # warm starting with a solution at a lower tolerance should reduce
    # the number of iterations needed
    data, cone = ex.many_iter_ecp()

    # intially takes ~920 iters for eps 1e-4
    work = scs.Workspace(data, cone, eps=1e-4)
    sol = work.solve()
    assert sol['info']['iter'] >= 800

    # ~640 for eps 1e-3
    sol = work.solve(eps=1e-3)
    assert 500 <= sol['info']['iter'] <= 700

    # use 1e-3 sol as warm start for 1e-4
    # extra digit only takes ~280 iters more
    sol = work.solve(warm_start = sol, eps=1e-4)
    assert sol['info']['iter'] < 300
Esempio n. 15
0
def test_cache():
    data, cone = ex.many_iter_ecp()

    work = scs.Workspace(data, cone)

    sol = work.solve()