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_proj_exp(self):
np.random.seed(0)
for _ in range(15):
x = np.random.randn(9)
var = cp.Variable(9)
constr = [cp.constraints.ExpCone(var[0], var[1], var[2])]
constr.append(cp.constraints.ExpCone(var[3], var[4], var[5]))
constr.append(cp.constraints.ExpCone(var[6], var[7], var[8]))
obj = cp.Minimize(cp.norm(var[0:3] - x[0:3]) +
cp.norm(var[3:6] - x[3:6]) +
cp.norm(var[6:9] - x[6:9]))
prob = cp.Problem(obj, constr)
prob.solve(solver="SCS", eps=1e-12)
p = cone_lib._proj(x, cone_lib.EXP, dual=False)
np.testing.assert_allclose(p, var.value, atol=1e-6)
# x + Pi_{exp}(-x) = Pi_{exp_dual}(x)
p_dual = cone_lib._proj(x, cone_lib.EXP_DUAL, dual=False)
var = cp.Variable(9)
constr = [cp.constraints.ExpCone(var[0], var[1], var[2])]
constr.append(cp.constraints.ExpCone(var[3], var[4], var[5]))
constr.append(cp.constraints.ExpCone(var[6], var[7], var[8]))
obj = cp.Minimize(cp.norm(var[0:3] + x[0:3]) +
cp.norm(var[3:6] + x[3:6]) +
cp.norm(var[6:9] + x[6:9]))
prob = cp.Problem(obj, constr)
prob.solve(solver="SCS", eps=1e-12)
np.testing.assert_allclose(
p_dual, x + var.value, atol=1e-6)
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]:
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],
def _test_dproj(self, cone, dual, n, x=None, tol=1e-8):
if x is None:
x = np.random.randn(n)
dx = 1e-6 * np.random.randn(n)
proj_x = cone_lib._proj(x, CPP_CONES_TO_SCS[cone.type], dual)
z = cone_lib._proj(x + dx, CPP_CONES_TO_SCS[cone.type], dual)
Dpi = _diffcp.dprojection(x, [cone], dual)
np.testing.assert_allclose(Dpi.matvec(dx), z - proj_x, atol=tol)
Dpi_dense = _diffcp.dprojection_dense(x, [cone], dual)
np.testing.assert_allclose(Dpi_dense @ dx, z - proj_x, atol=tol)
# assure that dense and linear operator are the same.
for i in range(n):
ei = np.zeros(n)
ei[i] = 1.0
np.testing.assert_allclose(Dpi.matvec(ei), Dpi_dense[:, i])
def test_proj_pos(self):
np.random.seed(0)
n = 100
for _ in range(15):
x = np.random.randn(n)
p = cone_lib._proj(x, cone_lib.POS, dual=False)
np.testing.assert_allclose(p, np.maximum(x, 0))
np.testing.assert_allclose(
p, cone_lib._proj(x, cone_lib.POS, dual=True))
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
dim = 3 * exp_dim
np.testing.assert_allclose(proj[offset:offset + dim],
cone_lib._proj(x[offset:offset + dim], cone_lib.EXP, dual=dual))
offset += dim
np.testing.assert_allclose(proj[offset:],
cone_lib._proj(x[offset:], cone_lib.EXP_DUAL, dual=dual))
def test_proj_pos(self):
np.random.seed(0)
n = 100
for _ in range(15):
x = np.random.randn(n)
p = cone_lib._proj(x, cone_lib.POS, dual=False)
np.testing.assert_allclose(p, np.maximum(x, 0))
np.testing.assert_allclose(
p, cone_lib._proj(x, cone_lib.POS, dual=True))
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]:
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: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
dim = 3 * exp_dim
np.testing.assert_allclose(proj[offset:offset + dim],
cone_lib._proj(x[offset:offset + dim], cone_lib.EXP, dual=dual))
offset += dim
np.testing.assert_allclose(proj[offset:],
cone_lib._proj(x[offset:], cone_lib.EXP_DUAL, dual=dual))
def test_proj_soc(self):
np.random.seed(0)
n = 100
for _ in range(15):
x = np.random.randn(n)
z = cp.Variable(n)
objective = cp.Minimize(cp.sum_squares(z - x))
constraints = [cp.norm(z[1:], 2) <= z[0]]
prob = cp.Problem(objective, constraints)
prob.solve(solver="SCS", eps=1e-10)
p = cone_lib._proj(x, cone_lib.SOC, dual=False)
np.testing.assert_allclose(
p, np.array(z.value))
np.testing.assert_allclose(
p, cone_lib._proj(x, cone_lib.SOC, dual=True))