本文整理汇总了Python中cvxpy.norm方法的典型用法代码示例。如果您正苦于以下问题:Python cvxpy.norm方法的具体用法?Python cvxpy.norm怎么用?Python cvxpy.norm使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cvxpy
的用法示例。
在下文中一共展示了cvxpy.norm方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import norm [as 别名]
def __init__(self):
"""
A large r_scale for a small scale problem will
ead to numerical problems as parameters become excessively small
and (it seems) precision is lost in the dynamics.
"""
self.set_random_initial_state()
self.x_init = np.concatenate(((self.m_wet,), self.r_I_init, self.v_I_init, self.q_B_I_init, self.w_B_init))
self.x_final = np.concatenate(((self.m_dry,), self.r_I_final, self.v_I_final, self.q_B_I_final, self.w_B_final))
self.r_scale = np.linalg.norm(self.r_I_init)
self.m_scale = self.m_wet
# slack variable for linear constraint relaxation
self.s_prime = cvx.Variable((K, 1), nonneg=True)
# slack variable for lossless convexification
# self.gamma = cvx.Variable(K, nonneg=True)
示例2: __init__
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import norm [as 别名]
def __init__(self):
"""
A large r_scale for a small scale problem will
ead to numerical problems as parameters become excessively small
and (it seems) precision is lost in the dynamics.
"""
# self.set_random_initial_state()
self.x_init = np.concatenate(((self.m_wet,), self.r_I_init, self.v_I_init, self.q_B_I_init, self.w_B_init))
self.x_final = np.concatenate(((self.m_dry,), self.r_I_final, self.v_I_final, self.q_B_I_final, self.w_B_final))
self.r_scale = np.linalg.norm(self.r_I_init)
self.m_scale = self.m_wet
# self.nondimensionalize()
示例3: running_example
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import norm [as 别名]
def running_example():
print("running example")
m = 20
n = 10
x = cp.Variable((n, 1))
F = cp.Parameter((m, n))
g = cp.Parameter((m, 1))
lambd = cp.Parameter((1, 1), nonneg=True)
objective_fn = cp.norm(F @ x - g) + lambd * cp.norm(x)
constraints = [x >= 0]
problem = cp.Problem(cp.Minimize(objective_fn), constraints)
assert problem.is_dcp()
assert problem.is_dpp()
print("is_dpp: ", problem.is_dpp())
F_t = torch.randn(m, n, requires_grad=True)
g_t = torch.randn(m, 1, requires_grad=True)
lambd_t = torch.rand(1, 1, requires_grad=True)
layer = CvxpyLayer(problem, parameters=[F, g, lambd], variables=[x])
x_star, = layer(F_t, g_t, lambd_t)
x_star.sum().backward()
print("F_t grad: ", F_t.grad)
print("g_t grad: ", g_t.grad)
示例4: test_simple_batch_socp
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import norm [as 别名]
def test_simple_batch_socp(self):
set_seed(243)
n = 5
m = 1
batch_size = 4
P_sqrt = cp.Parameter((n, n), name='P_sqrt')
q = cp.Parameter((n, 1), name='q')
A = cp.Parameter((m, n), name='A')
b = cp.Parameter((m, 1), name='b')
x = cp.Variable((n, 1), name='x')
objective = 0.5 * cp.sum_squares(P_sqrt @ x) + q.T @ x
constraints = [A@x == b, cp.norm(x) <= 1]
prob = cp.Problem(cp.Minimize(objective), constraints)
prob_tch = CvxpyLayer(prob, [P_sqrt, q, A, b], [x])
P_sqrt_tch = torch.randn(batch_size, n, n, requires_grad=True)
q_tch = torch.randn(batch_size, n, 1, requires_grad=True)
A_tch = torch.randn(batch_size, m, n, requires_grad=True)
b_tch = torch.randn(batch_size, m, 1, requires_grad=True)
torch.autograd.gradcheck(prob_tch, (P_sqrt_tch, q_tch, A_tch, b_tch))
示例5: __init__
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import norm [as 别名]
def __init__(self, m, k, n, complex=False):
if not cvx_available:
raise RuntimeError('Cannot initialize when cvxpy is not available.')
# Initialize parameters and variables
A = cvx.Parameter((m, k), complex=complex)
B = cvx.Parameter((m, n), complex=complex)
l = cvx.Parameter(nonneg=True)
X = cvx.Variable((k, n), complex=complex)
# Create the problem
# CVXPY issue:
# cvx.norm does not work if axis is not 0.
# Workaround:
# use cvx.norm(X.T, 2, axis=0) instead of cvx.norm(X, 2, axis=1)
obj_func = 0.5 * cvx.norm(cvx.matmul(A, X) - B, 'fro')**2 + \
l * cvx.sum(cvx.norm(X.T, 2, axis=0))
self._problem = cvx.Problem(cvx.Minimize(obj_func))
self._A = A
self._B = B
self._l = l
self._X = X
示例6: test_proj_soc
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import norm [as 别名]
def test_proj_soc(self):
import cvxpy as cp
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))
示例7: relax
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import norm [as 别名]
def relax(self):
"""The convex relaxation.
"""
constr = super(Card, self).relax()
return constr + [cvx.norm(self, 1) <= self.k*self.M,
cvx.norm(self, 'inf') <= self.M]
示例8: _project
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import norm [as 别名]
def _project(self, matrix):
if self.R >= cvx.norm(matrix, 2).value >= self.r:
return matrix
elif cvx.norm(matrix, 2).value == 0:
result = np.ones(self.shape)
return self.r*result/cvx.norm(result, 2).value
elif cvx.norm(matrix, 2).value < self.r:
return self.r*matrix/cvx.norm(matrix, 2).value
else:
return self.R*matrix/cvx.norm(matrix, 2).value
示例9: _restrict
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import norm [as 别名]
def _restrict(self, matrix):
# Add restriction that beyond hyperplane at projection onto
# n-sphere of radius r.
return [matrix.T*self >= self.r*cvx.norm(matrix, 2).value]
示例10: relax
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import norm [as 别名]
def relax(self):
"""The convex relaxation.
"""
constr = super(Annulus, self).relax()
return constr + [cvx.norm(self, 2) <= self.R]
示例11: dist
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import norm [as 别名]
def dist(self, matrix):
"""Distance from matrix to projection.
"""
proj_mat = self.project(matrix)
return cvxpy.norm(cvxpy.vec(matrix - proj_mat), 2).value
# Project the matrix into the space defined by the non-convex constraint.
# Returns the updated matrix.
示例12: get_error
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import norm [as 别名]
def get_error(noncvx_vars, eps, rel_eps):
"""The error bound for comparing infeasibility.
"""
error = sum([cvx.norm(cvx.vec(var)) for var in noncvx_vars]).value
return eps + rel_eps*error
示例13: init_z
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import norm [as 别名]
def init_z(self, random):
"""Initializes the value of the replicant variable.
"""
if random:
length = np.random.uniform()
direction = np.random.randn(self.shape)
self.z.value = length*direction/norm(direction, 2).value
else:
self.z.value = np.zeros(self.shape)
示例14: _project
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import norm [as 别名]
def _project(self, matrix):
if np.all(matrix == 0):
result = np.ones(self.shape)
return result/cvx.norm(result, 2).value
else:
return matrix/cvx.norm(matrix, 2).value
# Constrain all entries to be the value in the matrix.
示例15: relax
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import norm [as 别名]
def relax(self):
constr = super(Sphere, self).relax()
return constr + [cvx.norm(self, 2) <= 1]