我想用Python解决以下Python混合整数二次编程问题。不过,我对优化并不熟悉 Python的工具箱。
有人可以提供如下示例的向量X1,X2,X3,X4吗?
X1 = np.array([3,10,20,10])
X2 = np.array([5,1,3,4])
X3 = np.array([2,3,1,4])
X4 = np.array([10,0,1,2])
我试图用CVXPY解决它,但是我遇到了布尔值的问题
变量x = cp.Variable(1, boolean=True)
:
import numpy
import numpy as np
import cvxpy as cp
X1 = np.array([3,10,20,10])
X2 = np.array([5,1,3,4])
X3 = np.array([2,3,1,4])
X4 = np.array([10,0,1,2])
M = 100
x = cp.Variable(1, boolean=True)
Y1 = cp.Parameter(4)
Y2 = cp.Parameter(4)
a = cp.Parameter(1)
b = cp.Parameter(1)
c = cp.Parameter(1)
d = cp.Parameter(1)
delta = cp.Variable(1)
constraints = [Y1 <= X1 - a,
Y1 <= X2 - b,
Y1 >= X1 - a - M*delta,
Y1 >= X2 - b - M*(1-delta),
Y2 <= X3 - c,
Y2 <= X4 - d,
Y2 >= X3 - c - M*delta,
Y2 >= X4 - d - M*(1-delta),
0 <= a, a <= 10,
0 <= b, b <= 5,
0 <= c, c <= 5,
0 <= d, d <= 10,
delta == x]
obj = cp.Minimize(cp.sum_squares(Y1-Y2))
prob = cp.Problem(obj, constraints)
print(prob.solve())
答案 0 :(得分:1)
在cvxpy
中,参数是您要为其设置值的东西。在您的问题中,除了X1
至X4
以外的所有符号基本上都是变量。将cp.Parameter
替换为cp.Variable
的全局替换也是如此。
然后,我发现结果是
$ python3 cvxtest.py
69.99998471073722