cvxpy中变量差异的范数
norm of differences of variables in cvxpy
如何在cvxpy
中采用以下规范
sum(norm(x[i] - x[j], p=2), i=1, j>i, i, j = n)
其中 x
是由 x = cp.Variable((n, 2))
定义的 (n, 2)
变量矩阵。对于这个问题,我取 n=16。以下是我试图完全
的代码
import numpy as np
import cvxpy as cp
def solver(pts, var, lmb):
objective = cp.Minimize(cp.norm(pts - var, 2) + cp.norm(var, 2))
prob = cp.Problem(objective)
prob.solve()
print(prob.value)
return np.round(var.value, 2)
pts = np.array([(-2, 3), (-3, 5), (-1, 4), (-3, 7) , (0, 3),
(-2, -2), (-3, 8), (3, 1), (1, -2), (2, 6),
(-2, 6), (-2, 5), (-4, 3), (-4, 6), (-3, 10),
(2, -3)])
n = pts.shape[0]
var = cp.Variable((n, 2))
solver(pts, var, 1)
正是因为你没有任何约束,你的问题就解决了。
我跳过了使您的脚本更短的功能。
import numpy as np
import cvxpy as cp
pts = np.array([(-2, 3), (-3, 5), (-1, 4), (-3, 7), (0, 3),
(-2, -2), (-3, 8), (3, 1), (1, -2), (2, 6),
(-2, 6), (-2, 5), (-4, 3), (-4, 6), (-3, 10),
(2, -3)])
n = pts.shape[0]
x = cp.Variable((n, 2))
objective = cp.Minimize(cp.sum(cp.norm(pts - x, 2, axis=1)))
prob = cp.Problem(objective)
prob.solve()
print(x.value)
# result
# [[-2. 3.]
# [-3. 5.]
# [-1. 4.]
# [-3. 7.]
# [ 0. 3.]
# [-2. -2.]
# [-3. 8.]
# [ 3. 1.]
# [ 1. -2.]
# [ 2. 6.]
# [-2. 6.]
# [-2. 5.]
# [-4. 3.]
# [-4. 6.]
# [-3. 10.]
# [ 2. -3.]]
P.S。也许这不是您的正确答案。问题是从我在你的等式中看到的。我无法区分参数和您的值。
这是否意味着你有两个函数要最小化?一个接近点,另一个接近所有以前的点到当前点?
如果是这样,您可以将代码替换为:
cost1 = cp.sum(cp.norm(pts - x, 2, axis=1))
cost2 = cp.sum([cp.norm(x[j] - x[i],2) for i in range(1, n) for j in range(0, i)])
objective = cp.Minimize(cost1+cost2)
prob = cp.Problem(objective)
prob.solve()
print(x.value)
#result
# [[-1.7909687 4.4720621]
# [-1.7909687 4.4720621]
# [-1.7909687 4.4720621]
# [-1.7909687 4.4720621]
# [-1.7909687 4.4720621]
# [-1.7909687 4.4720621]
# [-1.7909687 4.4720621]
# [-1.7909687 4.4720621]
# [-1.7909687 4.4720621]
# [-1.7909687 4.4720621]
# [-1.7909687 4.4720621]
# [-1.7909687 4.4720621]
# [-1.7909687 4.4720621]
# [-1.7909687 4.4720621]
# [-1.7909687 4.4720621]
# [-1.7909687 4.4720621]]
但是如您所见,结果并不那么有趣。
如何在cvxpy
sum(norm(x[i] - x[j], p=2), i=1, j>i, i, j = n)
其中 x
是由 x = cp.Variable((n, 2))
定义的 (n, 2)
变量矩阵。对于这个问题,我取 n=16。以下是我试图完全
import numpy as np
import cvxpy as cp
def solver(pts, var, lmb):
objective = cp.Minimize(cp.norm(pts - var, 2) + cp.norm(var, 2))
prob = cp.Problem(objective)
prob.solve()
print(prob.value)
return np.round(var.value, 2)
pts = np.array([(-2, 3), (-3, 5), (-1, 4), (-3, 7) , (0, 3),
(-2, -2), (-3, 8), (3, 1), (1, -2), (2, 6),
(-2, 6), (-2, 5), (-4, 3), (-4, 6), (-3, 10),
(2, -3)])
n = pts.shape[0]
var = cp.Variable((n, 2))
solver(pts, var, 1)
正是因为你没有任何约束,你的问题就解决了。 我跳过了使您的脚本更短的功能。
import numpy as np
import cvxpy as cp
pts = np.array([(-2, 3), (-3, 5), (-1, 4), (-3, 7), (0, 3),
(-2, -2), (-3, 8), (3, 1), (1, -2), (2, 6),
(-2, 6), (-2, 5), (-4, 3), (-4, 6), (-3, 10),
(2, -3)])
n = pts.shape[0]
x = cp.Variable((n, 2))
objective = cp.Minimize(cp.sum(cp.norm(pts - x, 2, axis=1)))
prob = cp.Problem(objective)
prob.solve()
print(x.value)
# result
# [[-2. 3.]
# [-3. 5.]
# [-1. 4.]
# [-3. 7.]
# [ 0. 3.]
# [-2. -2.]
# [-3. 8.]
# [ 3. 1.]
# [ 1. -2.]
# [ 2. 6.]
# [-2. 6.]
# [-2. 5.]
# [-4. 3.]
# [-4. 6.]
# [-3. 10.]
# [ 2. -3.]]
P.S。也许这不是您的正确答案。问题是从我在你的等式中看到的。我无法区分参数和您的值。
这是否意味着你有两个函数要最小化?一个接近点,另一个接近所有以前的点到当前点?
如果是这样,您可以将代码替换为:
cost1 = cp.sum(cp.norm(pts - x, 2, axis=1))
cost2 = cp.sum([cp.norm(x[j] - x[i],2) for i in range(1, n) for j in range(0, i)])
objective = cp.Minimize(cost1+cost2)
prob = cp.Problem(objective)
prob.solve()
print(x.value)
#result
# [[-1.7909687 4.4720621]
# [-1.7909687 4.4720621]
# [-1.7909687 4.4720621]
# [-1.7909687 4.4720621]
# [-1.7909687 4.4720621]
# [-1.7909687 4.4720621]
# [-1.7909687 4.4720621]
# [-1.7909687 4.4720621]
# [-1.7909687 4.4720621]
# [-1.7909687 4.4720621]
# [-1.7909687 4.4720621]
# [-1.7909687 4.4720621]
# [-1.7909687 4.4720621]
# [-1.7909687 4.4720621]
# [-1.7909687 4.4720621]
# [-1.7909687 4.4720621]]
但是如您所见,结果并不那么有趣。