GUROBI官方给出的案例,但本篇实现方式与其有些不一样,并且对一些函数给出了解释:ymodeling-examples/supply_network_design/supply_network_design_1.ipynb at master · Gurobi/modeling-examples (github.com)

一、问题描述

给定两个工厂、四个中转站、六个客户:

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.集合

工厂集合:f \in F =\left \{ F1,F2 \right \}

中转站集合:d\in D = \left \{ D1, D2, D3, D4\right \}

客户点集合:c \in C=\left \{ C1, C2, C3, C4, C5, C6 \right \}

2.参数

工厂f的最大生产能力 :supply_{f}\in R^{+}

中转站d的最大吞吐量:through_{d}\in R^{+}

客户c的资源需求:demand_{c}\in R^{+}

从节点s到节点t的单位资源的运输成本:cost(s,t) \in R^{+}

3.决策变量

从节点s到节点t运输的资源数量:flow_{s,t}

4.目标函数

最小化总的运输成本: min z = \sum cost_{s,t}*flow_{s,t}

5.约束条件

工厂的产量约束:\sum_{t} flow_{f,t} <= supply_{f}

中转站的吞吐量约束:\sum_{f} flow_{f, d}<=through_{d}

中转站的流量平衡约束:\sum_{f} flow_{f,d} == \sum_{c}flow_{d,c}

客户点的需求约束:\sum_{s}flow_{s,c} == demand_{c}

三、代码实现

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()等在另一篇帖子里有详细介绍

写文章-CSDN创作中心

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐