cvxpy 约束归一化方程 (abs)

cvxpy contrained normalization equations (abs)

我正在研究一个优化问题 (A*v = b),我想对一组备选方案 X = {x1,x2,x3,x4} 进行排序。但是,我有以下规范化约束:|v[i] - v[j]| <= 1,可以是 -1 <= v[i] - v[j] <= 1 的形式。 我的代码如下:

import cvxpy as cp

n = len(X) #set of alternatives
v = cp.Variable(n)
objective = cp.Minimize(cp.sum_squares(A*v - b))
constraints = [0 <= v]
#Normalization condition -1 <= v[i] - v[j] <= 1
for i in range(n):
    for j in range(n):
        constraints = [-1 <= v[i]-v[j], 1 >= v[i]-v[j]]
prob = cp.Problem(objective, constraints)
# The optimal objective value is returned by `prob.solve()`.
result = prob.solve()
# The optimal value for v is stored in `v.value`.
va2 = v.value

输出:

[-0.15  0.45 -0.35  0.05]

结果,与应该的不接近,甚至有负值。我认为,我的规范化约束代码很可能是错误的。

您不是在附加约束,而是每次都覆盖它们。而不是这一行

constraints = [-1 <= v[i]-v[j], 1 >= v[i]-v[j]]

你应该

constraints += [-1 <= v[i]-v[j], 1 >= v[i]-v[j]]

为了清洁,您可能需要更改此设置

for i in range(n):
    for j in range(n):

每对只考虑一次:

for i in range(n):
    for j in range(i+1, n):