def test_dpi(self): np.random.seed(0) for _ in range(10): zero_dim = np.random.randint(1, 10) pos_dim = np.random.randint(1, 10) soc_dim = [ np.random.randint(1, 10) for _ in range(np.random.randint(1, 10)) ] psd_dim = [ np.random.randint(1, 10) for _ in range(np.random.randint(1, 10)) ] exp_dim = np.random.randint(3, 18) cones = [(cone_lib.ZERO, zero_dim), (cone_lib.POS, pos_dim), (cone_lib.SOC, soc_dim), (cone_lib.PSD, psd_dim), (cone_lib.EXP, exp_dim), (cone_lib.EXP_DUAL, exp_dim)] size = zero_dim + pos_dim + sum(soc_dim) + sum( [cone_lib.vec_psd_dim(d) for d in psd_dim]) + 2 * 3 * exp_dim x = np.random.randn(size) for dual in [False, True]: cone_list_cpp = cone_lib.parse_cone_dict_cpp(cones) proj_x = cone_lib.pi(x, cones, dual=dual) dx = 1e-7 * np.random.randn(size) z = cone_lib.pi(x + dx, cones, dual=dual) Dpi = _diffcp.dprojection(x, cone_list_cpp, dual) np.testing.assert_allclose(Dpi.matvec(dx), z - proj_x, atol=1e-6) Dpi = _diffcp.dprojection_dense(x, cone_list_cpp, dual) np.testing.assert_allclose(Dpi @ dx, z - proj_x, atol=1e-6)
def test_dpi(self): for _ in range(10): zero_dim = np.random.randint(1, 10) pos_dim = np.random.randint(1, 10) soc_dim = [ np.random.randint(1, 10) for _ in range(np.random.randint(1, 10)) ] psd_dim = [ np.random.randint(1, 10) for _ in range(np.random.randint(1, 10)) ] exp_dim = np.random.randint(3, 18) exp_dim -= (exp_dim % 3) cones = [(cone_lib.ZERO, zero_dim), (cone_lib.POS, pos_dim), (cone_lib.SOC, soc_dim), (cone_lib.PSD, psd_dim), (cone_lib.EXP, exp_dim)] size = zero_dim + pos_dim + sum(soc_dim) + sum( [cone_lib.vec_psd_dim(d) for d in psd_dim]) + exp_dim x = np.random.randn(size) for dual in [False, True]: Dpi = cone_lib.dpi(x, cones, dual=dual) proj_x = cone_lib.pi(x, cones, dual=dual) dx = 1e-6 * np.random.randn(size) z = cone_lib.pi(x + dx, cones, dual=dual) np.testing.assert_allclose(Dpi @ dx, z - proj_x, atol=1e-3, rtol=1e-4)
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, solve_method="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, solve_method="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 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)])
def test_pi(self): for _ in range(10): zero_dim = np.random.randint(1, 10) pos_dim = np.random.randint(1, 10) soc_dim = [ np.random.randint(1, 10) for _ in range(np.random.randint(1, 10)) ] psd_dim = [ np.random.randint(1, 10) for _ in range(np.random.randint(1, 10)) ] exp_dim = np.random.randint(3, 18) exp_dim -= (exp_dim % 3) cones = [(cone_lib.ZERO, zero_dim), (cone_lib.POS, pos_dim), (cone_lib.SOC, soc_dim), (cone_lib.PSD, psd_dim), (cone_lib.EXP, exp_dim)] size = zero_dim + pos_dim + sum(soc_dim) + sum( [cone_lib.vec_psd_dim(d) for d in psd_dim]) + exp_dim x = np.random.randn(size) for dual in [False, True]: proj = cone_lib.pi(x, cones, dual=dual) offset = 0 np.testing.assert_allclose( proj[:zero_dim], cone_lib._proj(x[:zero_dim], cone_lib.ZERO, dual=dual)) offset += zero_dim np.testing.assert_allclose( proj[offset:offset + pos_dim], cone_lib._proj(x[offset:offset + pos_dim], cone_lib.POS, dual=dual)) offset += pos_dim for dim in soc_dim: np.testing.assert_allclose( proj[offset:offset + dim], cone_lib._proj(x[offset:offset + dim], cone_lib.SOC, dual=dual)) offset += dim for dim in psd_dim: dim = cone_lib.vec_psd_dim(dim) np.testing.assert_allclose( proj[offset:offset + dim], cone_lib._proj(x[offset:offset + dim], cone_lib.PSD, dual=dual)) offset += dim np.testing.assert_allclose( proj[offset:], cone_lib._proj(x[offset:], cone_lib.EXP, dual=dual))