import cvxpy as cp
import numpy as np
import pandas as pd
data = pd.read_excel('附件1.xlsx')
X = data.iloc[1:, 1:2]
Y = data.iloc[1:, 2:3]
W = data.iloc[2:, 3:4] # 收集点垃圾量(i=1..n)
xi = X.values.flatten() # 转换为一维数组 (n,)
yi = Y.values.flatten() # 转换为一维数组 (n,)
wi = W.values.flatten() # 转换为一维数组 (n,)
# 定义参数
n = 30 # 收集点数量(i=1..n,0为处理厂)
K = 30 # 车辆总数
Q = 5 # 最大载重
# 计算距离矩阵dij(i,j=0..n)
dij = np.zeros((n+1, n+1))
for i in range(n+1):
for j in range(n+1):
dij[i, j] = np.sqrt((xi[i] - xi[j])**2 + (yi[i] - yi[j])**2)
x = cp.Variable((n+1, n+1, K), boolean=True) # x_ijv: 车辆v从i到j
y = cp.Variable((n, K), boolean=True) # y_iv: 车辆v服务收集点i(i=1..n,索引0..n-1)
z = cp.Variable(K, boolean=True) # z_v: 使用车辆v
u = cp.Variable((n+1, K), nonneg=True) # u_iv: 车辆v访问i后的累计载重(i=0..n,u[0,v]=0)
objective = cp.Minimize(cp.sum(cp.sum(cp.sum(dij @ x, axis=2), axis=1), axis=0))
# 约束条件
constraints = []
# 1. 每个收集点被 exactly 一辆车服务(i=1..n,对应y的索引0..n-1)
for i in range(n):
constraints.append(cp.sum(y[i, :]) == 1)
# 2. 流量守恒(i=1..n,处理厂i=0的约束在6中)
for i in range(1, n+1): # 收集点i(1..n,y索引i-1)
for v in range(K):
constraints.append(cp.sum(x[i, :, v]) == y[i-1, v]) # 离开i的次数等于服务i的次数
constraints.append(cp.sum(x[:, i, v]) == y[i-1, v]) # 进入i的次数等于服务i的次数
# 3. 载重约束
for v in range(K):
constraints.append(cp.sum(wi * y[:, v]) <= Q * z[v])
# 4. MTZ约束(子回路消除)
for v in range(K):
constraints.append(u[0, v] == 0) # 处理厂出发时载重为0
for i in range(1, n+1): # 收集点i(1..n,wi[i-1])
constraints.append(u[i, v] >= wi[i-1]) # 载重下限
constraints.append(u[i, v] <= Q) # 载重上限
for j in range(1, n+1):
if i != j:
constraints.append(u[i, v] - u[j, v] + Q * x[i, j, v] <= Q - wi[j-1])
# 5. 车辆使用约束:服务收集点则使用车辆
for i in range(n):
for v in range(K):
constraints.append(y[i, v] <= z[v])
# 6. 出发返回约束(处理厂i=0,出发到收集点1..n,返回从收集点1..n到0)
for v in range(K):
constraints.append(cp.sum(x[0, 1:, v]) == z[v]) # 出发次数等于车辆使用
constraints.append(cp.sum(x[1:, 0, v]) == z[v]) # 返回次数等于车辆使用
# 建立问题并求解
problem = cp.Problem(objective, constraints)
problem.solve()
# 输出结果(示例,需根据实际需求处理)
print("最优目标值:", problem.value)
print("使用的车辆:", z.value)D:\python\python.exe D:\35433\Documents\数模练习\电工杯\问题1.py
D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
This code path has been hit 1 times so far.
D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
This code path has been hit 2 times so far.
D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
This code path has been hit 3 times so far.
D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
This code path has been hit 4 times so far.
D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
This code path has been hit 5 times so far.
D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
This code path has been hit 6 times so far.
D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
This code path has been hit 7 times so far.
D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
This code path has been hit 8 times so far.
D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
This code path has been hit 9 times so far.
D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
This code path has been hit 10 times so far.
D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
This code path has been hit 11 times so far.
D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
This code path has been hit 12 times so far.
D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
This code path has been hit 13 times so far.
D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
This code path has been hit 14 times so far.
D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
This code path has been hit 15 times so far.
D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
This code path has been hit 16 times so far.
D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
This code path has been hit 17 times so far.
D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
This code path has been hit 18 times so far.
D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
This code path has been hit 19 times so far.
D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
This code path has been hit 20 times so far.
D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
This code path has been hit 21 times so far.
D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
This code path has been hit 22 times so far.
D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
This code path has been hit 23 times so far.
D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
This code path has been hit 24 times so far.
D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
This code path has been hit 25 times so far.
D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
This code path has been hit 26 times so far.
D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
This code path has been hit 27 times so far.
D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
This code path has been hit 28 times so far.
D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
This code path has been hit 29 times so far.
D:\python\Lib\site-packages\cvxpy\expressions\expression.py:674: UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
This code path has been hit 30 times so far.
D:\python\Lib\site-packages\cvxpy\reductions\solvers\solving_chain_utils.py:41: UserWarning:
The problem has an expression with dimension greater than 2. Defaulting to the SCIPY backend for canonicalization.