Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_threading(self):
np.random.seed(0)
m = 20
n = 10
As, bs, cs, cone_dicts = [], [], [], []
results = []
for _ in range(50):
A, b, c, cone_dims = utils.least_squares_eq_scs_data(m, n)
As += [A]
bs += [b]
cs += [c]
cone_dicts += [cone_dims]
results.append(cone_prog.solve_and_derivative(A, b, c, cone_dims))
for n_jobs in [1, -1]:
xs, ys, ss, _, DT_batch = cone_prog.solve_and_derivative_batch(
As, bs, cs, cone_dicts, n_jobs_forward=n_jobs, n_jobs_backward=n_jobs)
for i in range(50):
np.testing.assert_allclose(results[i][0], xs[i])
np.testing.assert_allclose(results[i][1], ys[i])
np.testing.assert_allclose(results[i][2], ss[i])
dAs, dbs, dcs = DT_batch(xs, ys, ss)
for i in range(50):
dA, db, dc = results[
i][-1](results[i][0], results[i][1], results[i][2])
np.testing.assert_allclose(dA.todense(), dAs[i].todense())
np.testing.assert_allclose(dbs[i], db)
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)
def test_warm_start(self):
np.random.seed(0)
m = 20
n = 10
A, b, c, cone_dims = utils.least_squares_eq_scs_data(m, n)
x, y, s, _, _ = cone_prog.solve_and_derivative(
A, b, c, cone_dims, eps=1e-11, solver="SCS")
x_p, y_p, s_p, _, _ = cone_prog.solve_and_derivative(
A, b, c, cone_dims, warm_start=(x, y, s), max_iters=1, solver="SCS")
np.testing.assert_allclose(x, x_p, atol=1e-7)
np.testing.assert_allclose(y, y_p, atol=1e-7)
np.testing.assert_allclose(s, s_p, atol=1e-7)
x_pert, y_pert, s_pert, _, _ = cone_prog.solve_and_derivative(
A + dA, b + db, c + dc, cone_dims, eps=1e-10, solver="SCS")
np.testing.assert_allclose(x_pert - x, dx, atol=1e-8)
np.testing.assert_allclose(y_pert - y, dy, atol=1e-8)
np.testing.assert_allclose(s_pert - s, ds, atol=1e-8)
x, y, s, derivative, adjoint_derivative = cone_prog.solve_and_derivative(
A, b, c, cone_dims, eps=1e-10, mode=mode, solver="SCS")
objective = c.T @ x
dA, db, dc = adjoint_derivative(
c, np.zeros(y.size), np.zeros(s.size))
x_pert, _, _, _, _ = cone_prog.solve_and_derivative(
A + 1e-6 * dA, b + 1e-6 * db, c + 1e-6 * dc, cone_dims, eps=1e-10, solver="SCS")
objective_pert = c.T @ x_pert
np.testing.assert_allclose(
objective_pert - objective,
1e-6 * dA.multiply(dA).sum() + 1e-6 * db@db + 1e-6 * dc@dc, atol=1e-8)
dA = utils.get_random_like(
A, lambda n: np.random.normal(0, 1e-6, size=n))
db = np.random.normal(0, 1e-6, size=b.size)
dc = np.random.normal(0, 1e-6, size=c.size)
dx, dy, ds = derivative(dA, db, dc)
x_pert, y_pert, s_pert, _, _ = cone_prog.solve_and_derivative(
A + dA, b + db, c + dc, cone_dims, eps=1e-10, solver="SCS")
np.testing.assert_allclose(x_pert - x, dx, atol=1e-8)
np.testing.assert_allclose(y_pert - y, dy, atol=1e-8)
np.testing.assert_allclose(s_pert - s, ds, atol=1e-8)
x, y, s, derivative, adjoint_derivative = cone_prog.solve_and_derivative(
A, b, c, cone_dims, eps=1e-10, mode=mode, solver="SCS")
objective = c.T @ x
dA, db, dc = adjoint_derivative(
c, np.zeros(y.size), np.zeros(s.size))
x_pert, _, _, _, _ = cone_prog.solve_and_derivative(
A + 1e-6 * dA, b + 1e-6 * db, c + 1e-6 * dc, cone_dims, eps=1e-10, solver="SCS")
objective_pert = c.T @ x_pert
np.testing.assert_allclose(
objective_pert - objective,
1e-6 * dA.multiply(dA).sum() + 1e-6 * db@db + 1e-6 * dc@dc, atol=1e-8)
def test_infeasible(self):
np.random.seed(0)
c = np.ones(1)
b = np.array([1.0, -1.0])
A = sparse.csc_matrix(np.ones((2, 1)))
cone_dims = {"f": 2}
with self.assertRaises(cone_prog.SolverError, msg='Solver scs returned status.*'):
cone_prog.solve_and_derivative(A, b, c, cone_dims)