本文整理汇总了Python中cvxpy.sum_squares方法的典型用法代码示例。如果您正苦于以下问题:Python cvxpy.sum_squares方法的具体用法?Python cvxpy.sum_squares怎么用?Python cvxpy.sum_squares使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cvxpy
的用法示例。
在下文中一共展示了cvxpy.sum_squares方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ball_con
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import sum_squares [as 别名]
def ball_con():
# print(f'--- {sys._getframe().f_code.co_name} ---')
print('ball con')
npr.seed(0)
n = 2
A = cp.Parameter((n, n))
z = cp.Parameter(n)
p = cp.Parameter(n)
x = cp.Variable(n)
t = cp.Variable(n)
obj = cp.Minimize(0.5 * cp.sum_squares(x - p))
# TODO automate introduction of variables.
cons = [0.5 * cp.sum_squares(A * t) <= 1, t == (x - z)]
prob = cp.Problem(obj, cons)
L = npr.randn(n, n)
A.value = L.T
z.value = npr.randn(n)
p.value = npr.randn(n)
prob.solve(solver=cp.SCS)
print(x.value)
示例2: relu
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import sum_squares [as 别名]
def relu():
# print(f'--- {sys._getframe().f_code.co_name} ---')
print('relu')
npr.seed(0)
n = 4
_x = cp.Parameter(n)
_y = cp.Variable(n)
obj = cp.Minimize(cp.sum_squares(_y - _x))
cons = [_y >= 0]
prob = cp.Problem(obj, cons)
_x.value = npr.randn(n)
prob.solve(solver=cp.SCS)
print(_y.value)
示例3: test_simple_batch_socp
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import sum_squares [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))
示例4: test_shared_parameter
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import sum_squares [as 别名]
def test_shared_parameter(self):
set_seed(243)
m, n = 10, 5
A = cp.Parameter((m, n))
x = cp.Variable(n)
b1 = np.random.randn(m)
b2 = np.random.randn(m)
prob1 = cp.Problem(cp.Minimize(cp.sum_squares(A @ x - b1)))
layer1 = CvxpyLayer(prob1, parameters=[A], variables=[x])
prob2 = cp.Problem(cp.Minimize(cp.sum_squares(A @ x - b2)))
layer2 = CvxpyLayer(prob2, parameters=[A], variables=[x])
A_tch = torch.randn(m, n, requires_grad=True)
solver_args = {
"eps": 1e-10,
"acceleration_lookback": 0,
"max_iters": 10000
}
torch.autograd.gradcheck(lambda A: torch.cat(
[layer1(A, solver_args=solver_args)[0],
layer2(A, solver_args=solver_args)[0]]), (A_tch,))
示例5: _generate_cvxpy_problem
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import sum_squares [as 别名]
def _generate_cvxpy_problem(self):
'''
Generate QP problem
'''
# Construct the problem
# minimize 1/2 z.T * z + np.ones(m).T * (r + s)
# subject to Ax - b - z = r - s
# r >= 0
# s >= 0
# The problem reformulation follows from Eq. (24) of the following paper:
# https://doi.org/10.1109/34.877518
x = cvxpy.Variable(self.n)
z = cvxpy.Variable(self.m)
r = cvxpy.Variable(self.m)
s = cvxpy.Variable(self.m)
objective = cvxpy.Minimize(.5 * cvxpy.sum_squares(z) + cvxpy.sum(r + s))
constraints = [self.Ad@x - self.bd - z == r - s,
r >= 0, s >= 0]
problem = cvxpy.Problem(objective, constraints)
return problem, (x, z, r, s)
示例6: _generate_cvxpy_problem
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import sum_squares [as 别名]
def _generate_cvxpy_problem(self):
'''
Generate QP problem
'''
# Construct the problem
# minimize 1/2 z.T * z + np.ones(m).T * (r + s)
# subject to Ax - b - z = r - s
# r >= 0
# s >= 0
# The problem reformulation follows from Eq. (24) of the following paper:
# https://doi.org/10.1109/34.877518
x = cvxpy.Variable(self.n)
z = cvxpy.Variable(self.m)
r = cvxpy.Variable(self.m)
s = cvxpy.Variable(self.m)
objective = cvxpy.Minimize(.5 * cvxpy.sum_squares(z) + cvxpy.sum(r + s))
constraints = [self.Ad@x - self.bd - z == r - s,
r >= 0, s >= 0]
problem = cvxpy.Problem(objective, constraints)
return problem, (x, z, r, s)
示例7: linear_softmax_reg
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import sum_squares [as 别名]
def linear_softmax_reg(X, Y, params):
m, n = X.shape[0], X.shape[1]
Theta = cp.Variable(n, len(params['d']))
f = cp.sum_entries(cp.log_sum_exp(X*Theta, axis=1) -
cp.sum_entries(cp.mul_elemwise(Y, X*Theta), axis=1)) / m
lam = 1e-5 # regularization
cp.Problem(cp.Minimize(f + lam * cp.sum_squares(Theta)), []).solve()
Theta = np.asarray(Theta.value)
return Theta
# Optimize expected value of inventory allocation
示例8: reg
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import sum_squares [as 别名]
def reg(self, X): return self.nu*cp.sum_squares(X)
示例9: test_single_func
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import sum_squares [as 别名]
def test_single_func(self):
"""Test problems with only a single function to minimize.
"""
X = Variable((4, 2))
B = np.reshape(np.arange(8), (4, 2)) * 1.
prox_fns = [sum_squares(X - B)]
prob = Problem(prox_fns[0])
prob.solve(solver="admm", eps_rel=1e-6, eps_abs=1e-6)
self.assertItemsAlmostEqual(X.value, B, places=2)
示例10: test_multiple_vars
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import sum_squares [as 别名]
def test_multiple_vars(self):
"""Test problems with multiple variables."""
x = Variable(3)
y = Variable(6)
rhs = np.array([1, 2, 3])
prob = Problem([sum_squares(x - rhs),
sum_squares(subsample(y, [2]) - x)])
prob.solve(solver="admm", eps_rel=1e-6, eps_abs=1e-6)
self.assertItemsAlmostEqual(x.value, [1, 2, 3], places=3)
self.assertItemsAlmostEqual(y.value, [1, 0, 2, 0, 3, 0], places=3)
示例11: test_sum_squares
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import sum_squares [as 别名]
def test_sum_squares(self):
"""Test sum squares prox fn.
"""
# No modifiers.
tmp = Variable(10)
fn = sum_squares(tmp)
rho = 1
v = np.arange(10) * 1.0
x = fn.prox(rho, v.copy())
self.assertItemsAlmostEqual(x, v * rho / (2 + rho))
rho = 2
x = fn.prox(rho, v.copy())
self.assertItemsAlmostEqual(x, v * rho / (2 + rho))
# With modifiers.
mod_fn = sum_squares(tmp, alpha=2, beta=-1,
c=np.ones(10) * 1.0, b=np.ones(10) * 1.0, gamma=1)
rho = 2
v = np.arange(10) * 1.0
x = mod_fn.prox(rho, v.copy())
# vhat = mod_fn.beta*(v - mod_fn.c/rho)*rho/(rho+2*mod_fn.gamma) - mod_fn.b
# rho_hat = rho/(mod_fn.alpha*np.sqrt(np.abs(mod_fn.beta)))
# xhat = fn.prox(rho_hat, vhat)
x_var = cvx.Variable(10)
cost = 2 * cvx.sum_squares(-x_var - np.ones(10)) + \
np.ones(10).T * x_var + cvx.sum_squares(x_var) + \
(rho / 2) * cvx.sum_squares(x_var - v)
prob = cvx.Problem(cvx.Minimize(cost))
prob.solve()
self.assertItemsAlmostEqual(x, x_var.value, places=3)
示例12: test_merge
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import sum_squares [as 别名]
def test_merge(self):
"""Test merging functions.
"""
# sum_entries
x = Variable(10)
fn1 = sum_entries(x, gamma=1.0)
fn2 = norm1(x)
assert can_merge(fn1, fn2)
merged = merge_fns(fn1, fn2)
v = np.arange(10) * 1.0 - 5.0
prox_val1 = merged.prox(1.0, v.copy())
tmp = norm1(x, c=np.ones(10), gamma=1.0)
prox_val2 = tmp.prox(1.0, v.copy())
self.assertItemsAlmostEqual(prox_val1, prox_val2)
# sum_squares
x = Variable(10)
val = np.arange(10)
fn1 = sum_squares(x, gamma=1.0, beta=2.0, alpha=3.0, b=val)
fn2 = norm1(x)
assert can_merge(fn1, fn2)
merged = merge_fns(fn1, fn2)
v = np.arange(10) * 1.0 - 5.0
prox_val1 = merged.prox(1.0, v.copy())
tmp = norm1(x, c=-12 * val, gamma=1.0 + 12, d=val.dot(val))
prox_val2 = tmp.prox(1.0, v.copy())
self.assertItemsAlmostEqual(prox_val1, prox_val2)
示例13: test_merge_all
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import sum_squares [as 别名]
def test_merge_all(self):
"""Test function to merge all prox operators possible.
"""
# merge all
x = Variable(10)
lin_op = grad(x)
fns = [sum_squares(lin_op), sum_entries(lin_op), nonneg(lin_op)]
merged = merge_all(fns)
assert len(merged) == 1
v = np.reshape(np.arange(10) * 1.0 - 5.0, (10, 1))
prox_val1 = merged[0].prox(1.0, v.copy())
tmp = nonneg(lin_op, c=np.ones((10, 1)), gamma=1.0)
prox_val2 = tmp.prox(1.0, v.copy())
self.assertItemsAlmostEqual(prox_val1, prox_val2)
示例14: test_const_val
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import sum_squares [as 别名]
def test_const_val(self):
"""Test obtaining the constant offset.
"""
x = Variable(10)
b = np.arange(10)
expr = x - b
self.assertItemsAlmostEqual(-b, expr.get_offset())
fn = sum_squares(expr)
new_fn = absorb_offset(fn)
self.assertItemsAlmostEqual(b, new_fn.b)
示例15: simple_qp
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import sum_squares [as 别名]
def simple_qp():
# print(f'--- {sys._getframe().f_code.co_name} ---')
print('simple qp')
npr.seed(0)
nx, ncon = 2, 3
G = cp.Parameter((ncon, nx))
h = cp.Parameter(ncon)
x = cp.Variable(nx)
obj = cp.Minimize(0.5 * cp.sum_squares(x - 1))
cons = [G * x <= h]
prob = cp.Problem(obj, cons)
data, chain, inv_data = prob.get_problem_data(solver=cp.SCS)
param_prob = data[cp.settings.PARAM_PROB]
print(param_prob.A.A)
x0 = npr.randn(nx)
s0 = npr.randn(ncon)
G.value = npr.randn(ncon, nx)
h.value = G.value.dot(x0) + s0
prob.solve(solver=cp.SCS)
delC = npr.randn(param_prob.c.shape[0])[:-1]
delA = npr.randn(param_prob.A.shape[0])
num_con = delA.size // (param_prob.x.size + 1)
delb = delA[-num_con:]
delA = delA[:-num_con]
delA = sp.csc_matrix(np.reshape(delA, (num_con, param_prob.x.size)))
del_param_dict = param_prob.apply_param_jac(delC, delA, delb)
print(del_param_dict)
var_map = param_prob.split_solution(npr.randn(param_prob.x.size))
print(var_map)
print(param_prob.split_adjoint(var_map))
print(x.value)