首页 > 数据库 > 数据库开发技术 > 多仓库选址-MIP问题建模及求解
from pulp import *
Customer = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]#15个需配送的网点
Facility = ['Fac-1', 'Fac-2', 'Fac-3']#3个备选配送选址点
#从三个备选点运输过去的成本。成本可以是根据距离计算的运费,或者是根据货量计算的加权运费,可自行修改。
transportation_cost = {'Fac-1' : {1 : 4, 2 : 5, 3 : 6, 4 : 8, 5 : 10,6 : 1, 7 : 5, 8 : 1, 9 : 8, 10 : 10,11 : 14, 12 : 1, 13 : 1, 14 : 1, 15 : 1},
'Fac-2' : {1 : 1, 2 : 1, 3 : 3, 4 : 5, 5 : 8,6 : 2, 7 : 1, 8 : 16, 9 : 1, 10 : 1,11 : 1, 12 : 15, 13 : 1, 14 : 8, 15 : 7},
'Fac-3' : {1 : 1, 2 : 1, 3 : 1, 4 : 1, 5 : 4,6 : 1, 7 : 12, 8 : 1, 9 : 8, 10 : 1,11 : 14, 12 : 15, 13 : 16, 14 : 1, 15 : 1}
}
#每个选址点对应的日均成本
fixed_cost = {'Fac-1' : 20, 'Fac-2' : 30, 'Fac-3' : 100}
# 定义决策变量
use_facility = LpVariable.dicts("Use Facility", Facility, 0, 1, LpBinary)#是否使用该选址点(0-1离散变量)
ser_customer = LpVariable.dicts("Service", [(i,j) for i in Customer for j in Facility], 0)#
# 设定目标函数
cflp += (lpSum(fixed_cost[j]*use_facility[j] for j in Facility) +
lpSum(transportation_cost[j][i]*ser_customer[(i,j)] for j in Facility for i in Customer))
# 设定约束
for i in Customer:
cflp += lpSum(ser_customer[(i,j)] for j in Facility) == 1#每个网点有且仅有一个选址点服务。
cflp+=lpSum(use_facility[j] for j in Facility) <=3#三个仓库最多选3个,
cflp+=lpSum(use_facility[j] for j in Facility) >=1#三个仓库最少选1个,
for i in Customer: #若超过1个分配至某个仓库,则需选择该仓库,对应的use_facility[j]取1
for j in Facility:
cflp += ser_customer[(i,j)] <= use_facility[j]
找到一个最优解。
# 打印结果
Tolerance = 0.0001
for j in Facility:
if use_facility[j].varValue > Tolerance:
print(use_facility[j].varValue)
print("选址点= ", j)
# 打印网点分配结果
for v in cflp.variables():
if v.varValue>0:
print(v.name, "=", v.varValue)
#打印目标结果
print("Total Cost = ", value(cflp.objective))
原文作者:Python学习杂记;如有侵权,请联系管理员删除。