选址问题是很多工厂、物流公司的核心研究问题。其目标是整个网络配送整体成本最低。本例使用Or-tools来解决选址问题。
问题说明:如何在以下条件下进行仓库选址并安排日常配送,使总成本最低。
1)有七个点可供选址仓库,这七个点的日均租金、及仓库最大容量如下:
2)需配送至100个网点,仓库到100个网点的运输成本如下:
3)100个网点的需求量:该数据已传至列表如下:
#最下面一行是需求量,最右边一列是仓库最大容量 D=[ [7,7,4,3,8,2,6,4,10,2,10,7,3,4,8,10,6,5,6,3,10,9,4,2,6,1,7,2,9,3,8,3,5,8,9,1,7,9,7,5,1,9,1,5,5,10,8,7,5,9,9,1,4,5,1,7,7,10,5,8,2,8,4,7,1,5,10,7,5,6,5,6,6,1,2,2,9,8,2,10,4,1,9,3,3,4,3,3,10,9,7,10,1,9,2,6,2,5,3,5,100], [5,8,1,5,1,10,8,1,3,3,6,3,1,10,1,6,3,5,5,9,4,9,1,7,7,8,8,5,9,10,9,6,5,4,3,4,3,2,4,10,6,7,5,4,6,10,5,7,3,7,8,9,9,8,10,5,10,9,5,1,7,10,2,3,9,2,5,8,10,3,9,5,8,10,2,9,3,6,4,5,9,1,6,9,1,5,1,8,10,3,10,8,3,6,9,2,3,8,6,6,50], [9,4,9,5,4,6,4,4,9,3,10,2,6,10,1,2,3,2,8,10,4,6,10,10,9,6,6,2,9,3,7,7,6,10,4,4,8,7,5,5,8,3,1,3,7,3,6,3,3,2,8,1,6,6,5,9,4,9,4,9,1,10,10,2,8,10,4,2,1,4,3,7,7,8,9,2,3,6,8,1,10,9,6,5,10,1,1,10,2,7,3,1,5,3,10,9,8,4,6,9,100], [8,7,10,4,3,5,8,2,3,2,2,7,1,4,8,4,8,3,2,1,3,2,6,5,5,9,8,3,1,2,9,5,9,2,9,1,10,2,2,3,1,5,8,3,3,4,7,2,2,6,6,6,3,6,1,7,7,8,2,4,5,6,6,3,6,6,3,5,6,4,3,2,3,3,3,3,10,10,3,10,6,9,2,7,10,8,3,5,2,2,2,8,10,1,5,10,2,3,4,4,100], [4,4,9,7,8,7,1,8,10,5,10,1,1,6,8,5,5,9,7,9,1,8,2,1,1,9,6,6,8,2,5,3,6,10,6,1,4,1,9,5,9,3,2,10,6,5,1,10,8,8,9,6,9,8,7,1,3,8,4,6,7,1,3,1,3,5,3,9,7,6,2,8,7,8,2,10,5,10,7,4,8,7,7,6,9,4,1,5,1,7,1,8,1,6,7,4,1,1,3,6,150], [4,3,8,10,3,5,1,7,5,7,10,5,8,2,8,2,5,2,6,5,3,10,10,6,9,5,10,8,2,2,4,10,1,3,7,1,10,3,8,7,1,4,1,3,8,2,10,5,7,2,5,4,1,2,6,4,9,3,4,10,7,9,7,4,1,9,5,9,10,7,9,4,8,5,8,3,10,6,2,3,1,2,10,1,6,7,5,6,4,2,7,4,4,6,6,1,9,6,5,8,100], [6,9,1,7,5,7,5,5,4,8,1,8,6,7,6,1,4,5,7,8,2,5,10,5,8,7,1,9,5,8,4,1,8,9,10,7,4,3,10,7,1,1,2,2,6,5,2,4,5,10,3,2,1,1,4,9,10,5,6,7,10,9,4,10,8,7,10,8,1,9,4,7,7,6,4,9,4,4,3,10,8,3,1,2,7,7,8,10,1,2,7,6,7,1,4,7,10,2,9,8,100], [5,3,2,5,5,2,4,3,4,4,2,3,2,4,3,2,2,4,3,4,2,2,2,2,3,2,4,5,5,3,5,4,5,2,4,5,2,2,4,3,2,4,3,4,3,2,4,3,5,4,5,3,3,4,3,4,3,3,3,3,4,4,2,3,3,4,3,5,2,4,4,3,4,3,3,5,3,2,5,3,3,5,3,3,3,4,3,5,3,3,2,3,2,3,3,3,2,2,4,5,0]] #仓库成本数据 F=[90,40,50,60,55,123,70]
定义决策变量
这里使用ortools中的pywraplp模块包,该包中使用自带的CBC混合整数规划求解器。也可尝试用其他求解器。
t = '选址问题' s = pywraplp.Solver(t,pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)#可更换求解器 m,n = len(D)-1,len(D[0])-1 #定义变量 x = [[s.IntVar(0,1,'') for j in range(n)] for i in range(m)] y = [s.IntVar(0,1,'') for i in range(m)]
决策变量有100*7+7=707个。均为0,1变量。(该决策变量的设定,与前面一篇pulp选址逻辑相同。)
写入约束条件
这里约束包含容量约束、仓库与网点之间的对应约束、仓库数量等。
#需求约束 for i in range(m): s.Add(D[i][-1]*y[i] >= sum(x[i][j]*D[-1][j] for j in range(n))) #每个网点对应一个仓库 for nn in range(n): s.Add(sum([x[mm][nn] for mm in range(m)])==1) #仓库容量>供应总量 for j in range(n): s.Add(D[-1][j] == sum(x[i][j]*D[-1][j] for i in range(m))) #数量限制,该约束可暂不用,方便后面对仓库数量做限制。 s.Add(sum([y[i] for i in range(m)])>=1) s.Add(sum([y[i] for i in range(m)])<=7) Fcost = s.Sum(y[i]*F[i] for i in range(m)) Dcost = s.Sum(x[i][j]*D[i][j] for i in range(m) for j in range(n)) s.Minimize(Dcost + Fcost) rc = s.Solve()
打印结果
调用输出的结果。最优结果:选BCDE四个仓库,其对应的最低成本是447,不同仓库对应网点也已展示。
如果设定约束,比如只要求设定3个仓库,只需要在之前的环节更改约束条件即可。3个仓库最优成本为448。
5个仓库最优成本为482。
从本例来看,ortools建模解决小规模混合整数规划问题可行,速度也可以。