模型代码如下
```python
from pulp import *
# 配送点数量和车辆数量
num_points = 10
num_vehicles = 5
# 距离矩阵
distances = [
[0, 0.56, 1.30, 0.50, 0.94, 0.03, 0.77, 0.55, 0.95, 0.46, 0.38],
[0.56, 0, 1.50, 0.84, 1.40, 1.10, 0.16, 0.90, 1.40, 0.94, 0.86],
[1.30, 1.50, 0, 0.41, 0.57, 0.71, 1.40, 0.45, 0.81, 1.10, 1.00],
[0.50, 0.84, 0.41, 0, 1.20, 0.31, 0.98, 0.05, 1.20, 0.73, 0.65],
[0.94, 1.40, 0.57, 1.20, 0, 1.20, 1.20, 0.96, 0.004, 1.40, 1.50],
[0.03, 1.10, 0.71, 0.31, 1.20, 0, 0.67, 0.51, 0.92, 0.42, 0.34],
[0.77, 0.16, 1.40, 0.98, 1.20, 0.67, 0, 0.80, 1.30, 1.10, 1.00],
[0.55, 0.90, 0.45, 0.05, 0.96, 0.51, 0.80, 0, 1.20, 0.71, 0.62],
[0.95, 1.40, 0.81, 1.20, 0.004, 0.92, 1.30, 1.20, 0, 1.30, 1.30],
[0.46, 0.94, 1.10, 0.73, 1.40, 0.42, 1.10, 0.71, 1.30, 0, 0.71],
[0.38, 0.86, 1.00, 0.65, 1.50, 0.34, 1.00, 0.62, 1.30, 0.71, 0]
]
# 需求量
demands = {
1: 118, 2: 125, 3: 126, 4: 115, 5: 120,
6: 130, 7: 110, 8: 116, 9: 129, 10: 124
}
# 每辆车的最大运载量
Q = 260
#单位距离运输成本
C=10
# 创建问题
problem = LpProblem("Vehicle Routing Problem", LpMinimize)
# 创建变量
x = LpVariable.dicts("route", [(i, j, k) for i in range(num_points+1)
for j in range(num_points+1)
for k in range(num_vehicles)],
lowBound=0, upBound=1, cat=LpInteger)
d = LpVariable.dicts("demand", [i for i in range(num_points+1)],
lowBound=0, upBound=None, cat=LpInteger)
#目标函数
problem += lpSum([C * distances[i][j] * x[(i, j, k)] for i in range(num_points+1)
for j in range(num_points+1)
for k in range(num_vehicles)])
# 约束条件 - 每个配送点只被一个车辆访问
for i in range(1, num_points+1):
problem += lpSum([x[(i, j, k)] for j in range(num_points+1) for k in range(num_vehicles)]) == 1
# 约束条件 - 每辆车的容量约束
for k in range(num_vehicles):
problem += lpSum([demands[i] * x[(i, j, k)] for i in range(1,num_points+1) for j in range(num_points+1)]) <= Q
# 约束条件 - 避免子循环路径
for i in range(1, num_points+1):
for k in range(num_vehicles):
problem += lpSum([x[(i, j, k)] for j in range(num_points+1)]) <= 1
# 约束条件 - 出发点和回到出发点
for k in range(num_vehicles):
problem += lpSum([x[(0, j, k)] for j in range(1, num_points+1)]) == 2
# 求解问题
problem.solve()
# 输出结果
print("最小运输成本:", value(problem.objective))
print("车辆配送方案:")
for k in range(num_vehicles):
print("车辆", k+1, ":", end=" ")
route = [0]
for i in range(1, num_points+1):
for j in range(num_points+1):
if value(x[(i, j, k)]) == 1:
route.append(i)
break
route.append(0)
print(route)
输出结果
最小运输成本: 20.5
车辆配送方案:
车辆 1 : [0, 4, 7, 0]
车辆 2 : [0, 8, 10, 0]
车辆 3 : [0, 3, 5, 0]
车辆 4 : [0, 1, 9, 0]
车辆 5 : [0, 2, 6, 0]
目标函数的结果是错误的,因为根据车辆配送的方案可以计算出运输成本应该为103.5
车辆 1 : = 10 * (0.94 + 0.96 + 0.55) = 24.5
车辆 2 : = 10 * (0.95 + 1.30 + 0.38) = 26.3
车辆 3 : = 10 * (0.50 + 0.31 + 0.03) = 8.4
车辆 4 : = 10 * (0.56 + 0.94 + 0.46) = 9.6
车辆 5 : = 10 * (1.30 + 1.40 + 0.77) = 34.7
总运输成本 = 24.5 + 26.3 + 8.4 + 9.6 + 34.7 = 103.5,请问目标函数应该如何修改?