猿问

Python中的混合整数二次规划

我想在 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())



叮当猫咪
浏览 405回答 1
1回答
随时随地看视频慕课网APP

相关分类

Python
我要回答