一、问题描述
给定两个工厂、四个中转站、六个客户:
Factory | Supply(最大生产量) |
F1 | 150,000 |
F2 | 200,000 |
Depot | Throughput(吞吐量:中转站不生产、不消耗资源,只是传递资源) |
D1 | 70,000 |
D2 | 50,000 |
D3 | 100,000 |
D4 | 40,000 |
Customers | Demands(需要的资源) |
C1 | 50,000 |
C2 | 10,000 |
C3 | 40,000 |
C4 | 35,000 |
C5 | 60,000 |
C6 | 20,000 |
二、建模
1.集合
工厂集合:
中转站集合:
客户点集合:
2.参数
工厂的最大生产能力 :
中转站的最大吞吐量:
客户的资源需求:
从节点s到节点t的单位资源的运输成本:
3.决策变量
从节点s到节点t运输的资源数量:
4.目标函数
最小化总的运输成本:
5.约束条件
工厂的产量约束:
中转站的吞吐量约束:
中转站的流量平衡约束:
客户点的需求约束:
三、代码实现
import gurobipy as gp
from gurobipy import *
# 参数
supply = dict({
"F1": 150000,
"F2": 200000
})
through = dict({
"D1": 70000,
"D2": 50000,
"D3": 100000,
"D4": 40000
})
demand = dict({
"C1": 50000,
"C2": 10000,
"C3": 40000,
"C4": 35000,
"C5": 60000,
"C6": 20000
})
arcs, cost = gp.multidict({
("F1","D1"): 0.5,
("F1","D2"):0.5,
("F1","D3"): 1.0,
("F1","D4"):0.2,
("F1","C1"):1.0,
("F1","C3"):1.5,
("F1","C4"):2.0,
("F1","C6"):1.0,
("F2","D2"):0.3,
("F2","D3"):0.5,
("F2","D4"):0.2,
("F2","C1"):2.0,
("D1","C2"):1.5,
("D1","C3"):0.5,
("D1","C5"):1.5,
("D1","C6"):1.0,
("D2","C1"):1.0,
("D2","C2"):0.5,
("D2","C3"):0.5,
("D2","C4"):1.0,
("D2","C5"):0.5,
("D3","C2"):1.5,
("D3","C3"):2.0,
("D3","C5"):0.5,
("D3","C6"):1.5,
("D4","C3"):0.2,
("D4","C4"):1.5,
("D4","C5"):0.5,
("D4","C6"):1.5
})
print(arcs)
# model
model = gp.Model()
# decision var
flow = model.addVars(arcs, vtype = GRB.CONTINUOUS, name = "flow") # flow为tupledict
print(flow)
# constraints
# factory production
factory = supply.keys()
model.addConstrs((flow.sum(f, "*") <= supply[f] for f in factory), name = "factory constraints")
# depot constraints
depot = through.keys()
model.addConstrs((flow.sum("*", d) <= through[d] for d in depot), name = "depot constraint 1")
model.addConstrs((flow.sum("*", d) == flow.sum(d, "*") for d in depot), name = "depot constraint 2")
# customer constraints
customers = demand.keys()
model.addConstrs((flow.sum("*", c) == demand[c] for c in customers), name = "customers constraints")
# objective
obj = flow.prod(cost)
model.setObjective(obj, GRB.MINIMIZE)
# optimize
model.write("test.lp")
model.update()
model.optimize()
提示:上述代码实现中用到的multidict()、sum()、prod()等在另一篇帖子里有详细介绍
标签:1.5,F1,Network,Supply,0.5,Design,D4,D2,D3 From: https://blog.csdn.net/qq_44533176/article/details/136854988