# -- coding: gbk --
import mip
from mip import Model, BINARY, xsum, minimize
# from enum import Enum
import networkx as nx
from matplotlib import pyplot as plt
# 路网
class Network:
def __init__(self):
self.NodeList = []
self.EdgeList = []
self.ODList = []
self.PrevEdge = {} # 节点入弧
self.NextEdge = {} # 节点出弧
def add_node(self, node):
self.NodeList.append(node)
self.PrevEdge[node] = []
self.NextEdge[node] = []
# 通过车站名称获取区间
def get_edge(self, sta1, sta2):
for sec in self.EdgeList:
if sec.start_station == sta1 and sec.end_station == sta2:
return sec
# 增加区间
def add_edge(self, s_node, e_node, distance):
if s_node not in self.NodeList:
self.add_node(s_node)
if e_node not in self.NodeList:
self.add_node(e_node)
for edge in self.EdgeList:
if s_node == edge.start_station and e_node == edge.end_station:
return
edge = Edge(s_node, e_node, distance)
self.EdgeList.append(edge)
if e_node not in self.PrevEdge.keys():
self.PrevEdge[e_node] = [edge]
else:
self.PrevEdge[e_node].append(edge)
if s_node not in self.NextEdge.keys():
self.NextEdge[s_node] = [edge]
else:
self.NextEdge[s_node].append(edge)
# 弧段
class Edge:
def __init__(self, start_node, end_node, distance):
self.start_station = start_node
self.end_station = end_node
self.distance = distance
class Section:
def __init__(self, start_node, end_node, capacity):
self.start_station = start_node
self.end_station = end_node
self.capacity = capacity
# 各产品的运输需求
class ODFlow:
def __init__(self):
self.Id = 0 # 编号
self.Origin = '' # 出发地
self.Destination = '' # 目的地
self.Num = 0 # 重量
self.shortest_distance = 0
class Demand:
def __init__(self):
self.Flows = []
def add_flow(self, origin, dest, num):
flow = ODFlow()
flow.Origin = origin
flow.Destination = dest
flow.Num = num
self.Flows.append(flow)
def get_flow_num(self, origin, dest):
result = list(filter(lambda x: x.Origin == origin and x.Destination == dest, self.Flows))
if len(result) > 0:
return result[0].Num
else:
return 0
def get_flow(self, origin, dest):
result = list(filter(lambda x: x.Origin == origin and x.Destination == dest, self.Flows))
if len(result) > 0:
return result[0]
else:
return None
# 路径
class Route:
def __init__(self, *list):
self.List = list
def glb_generate_test_world():
section_list = []
section_list.append(Section("1", "2",300))
section_list.append(Section("1", "4", 300))
section_list.append(Section("1", "2", 210))
section_list.append(Section("1", "4", 300))
section_list.append(Section("2", "3", 300))
section_list.append(Section("2", "6", 300))
section_list.append(Section("3", "9", 380))
section_list.append(Section("4", "5", 300))
section_list.append(Section("4", "7", 300))
section_list.append(Section("5", "6", 300))
section_list.append(Section("5", "8", 380))
section_list.append(Section("6", "9", 380))
section_list.append(Section("6", "10", 300))
section_list.append(Section("6", "11", 300))
section_list.append(Section("9", "12", 300))
section_list.append(Section("10", "11", 300))
section_list.append(Section("10", "13", 300))
section_list.append(Section("11", "12", 300))
section_list.append(Section("11", "14", 300))
section_list.append(Section("13", "14", 300))
section_list.append(Section("7", "8", 380))
section_list.append(Section("8", "10", 400))
net = Network()
net.add_edge("1", "2", 210)
net.add_edge("1", "4", 265)
net.add_edge("2", "3", 232)
net.add_edge("2", "6", 123)
net.add_edge("3", "9", 480)
net.add_edge("4", "5", 109)
net.add_edge("4", "7", 190)
net.add_edge("5", "6", 172)
net.add_edge("5", "8", 149)
net.add_edge("6", "9", 368)
net.add_edge("6", "10", 246)
net.add_edge("6", "11", 280)
net.add_edge("9", "12", 503)
net.add_edge("10", "11", 130)
net.add_edge("10", "13", 115)
net.add_edge("11", "12", 336)
net.add_edge("11", "14", 218)
net.add_edge("13", "14", 158)
net.add_edge("7", "8", 117)
net.add_edge("8", "10", 220)
net.add_edge("2", "1", 210)
net.add_edge("4", "1", 265)
net.add_edge("3", "2", 232)
net.add_edge("6", "2", 123)
net.add_edge("9", "3", 480)
net.add_edge("5", "4", 109)
net.add_edge("7", "4", 190)
net.add_edge("6", "5", 172)
net.add_edge("8", "5", 149)
net.add_edge("9", "6", 368)
net.add_edge("10", "6", 246)
net.add_edge("11", "6", 280)
net.add_edge("12", "9", 503)
net.add_edge("11", "10", 130)
net.add_edge("13", "10", 115)
net.add_edge("12", "11", 336)
net.add_edge("14", "11", 218)
net.add_edge("14", "13", 158)
net.add_edge("8", "7", 117)
net.add_edge("10", "8", 220)
demand = Demand()
demand.add_flow("3", "7", 55)
demand.add_flow("7", "9", 35)
demand.add_flow("7", "14", 50)
demand.add_flow("12", "7", 45)
demand.add_flow("8", "9", 64)
demand.add_flow("8", "12", 52)
demand.add_flow("1", "8", 60)
demand.add_flow("1", "14", 40)
demand.add_flow("12", "1", 50)
demand.add_flow("3", "8", 65)
demand.add_flow("3", "11", 52)
demand.add_flow("10", "3", 40)
demand.add_flow("2", "12", 50)
demand.add_flow("4", "9", 45)
demand.add_flow("11", "4", 46)
demand.add_flow("5", "13", 70)
demand.add_flow("5", "12", 68)
demand.add_flow("14", "5", 62)
demand.add_flow("3", "13", 50)
demand.add_flow("10", "1", 45)
return net, demand, section_list
def glb_finding_path(net, demand): # 定义了一个函数glb_finding_path,作用是寻找从一个起点到终点的路径.
# 创建图
G = nx.DiGraph() # 创建了一个空的有向图G
for edge in net.EdgeList: # for循环,用于遍历problem_input对象的EdgeList属性中的每个边
G.add_edge(edge.start_station, edge.end_station, weight=edge.distance)
for i in net.NodeList:
for j in net.NodeList:
flow = demand.get_flow(i, j)
if flow is not None:
flow.shortest_distance = nx.dijkstra_path_length(G, i, j, "distance")
# 在循环体内,向有向图G中添加一个边。这个边连接edge.start_station和edge.end_station,并将权重设置为edge.Distance
# 绘制路网图
nx.draw(G, with_labels=True) # 使用NetworkX的draw函数绘制有向图G。参数with_labels=True表示将显示节点的标签
plt.title('Continue in 5 seconds')
plt.axis('on')
plt.xticks([])
plt.yticks([])
plt.savefig('路网图.png')
# plt.show() # 显示图表
def glb_solve_mip(net, demand, section_list):
model = Model()
y = {(i, j, e): model.add_var(var_type=BINARY) for i in net.NodeList for j in net.NodeList for e in net.EdgeList}
model.objective = minimize(xsum(y[i, j, e] * e.distance * demand.get_flow_num(i, j) for i in net.NodeList for j in net.NodeList for e in net.EdgeList))
for i in net.NodeList:
for j in net.NodeList:
if demand.get_flow_num(i, j) > 0:
model += xsum(y[i, out_edge.end_station, out_edge] for out_edge in net.NextEdge[i]) == 1
for i in net.NodeList:
for j in net.NodeList:
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
<项目介绍> 基于动态罚函数的铁路车流分配与径路优化模型python源码 - 不懂运行,下载完可以私聊问,可远程教学 该资源内项目源码是个人的毕设,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 --------
资源推荐
资源详情
资源评论
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083646.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![thumb](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 3 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
- ember_Q2024-05-07能看一下这个算法的原来的模型吗奋斗奋斗再奋斗的ajie2024-06-29感谢下载,不懂的可以私聊
- 唇亡寒(***)2024-05-17资源内容详细全面,与描述一致,对我很有用,有一定的使用价值。
- W.I's2024-05-15这个资源内容超赞,对我来说很有价值,很实用,感谢大佬分享~
- 叮当啪2024-05-01怎么能有这么好的资源!只能用感激涕零来形容TAT...
![avatar](https://profile-avatar.csdnimg.cn/c50e8df751c24a199e1df838a4ddef09_2301_78627004.jpg!1)
奋斗奋斗再奋斗的ajie
- 粉丝: 1166
- 资源: 2518
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)