• 大小: 0.01M
    文件类型: .py
    金币: 1
    下载: 0 次
    发布日期: 2021-06-16
  • 语言: Python
  • 标签: 其他  

资源简介

用户平衡流量分配.py

资源截图

代码片段和文件信息

import networkx as nx
import xlrd
import numpy as np
import xlwt
# 创建一个workbook对象即创建一个excel文件(可中文输入且不压缩)。
workbook = xlwt.Workbook(encoding=‘UTF-8‘ style_compression=0)
# 创建表sheet,表名为“西康路流量统计”,覆盖单元格。
sheet = workbook.add_sheet(“路段流量分配“ cell_overwrite_ok=True)
# 写出表头
sheet.write(0 0 ‘流量‘)


def read_data(path):  # 读取文件函数
    work_book = xlrd.open_workbook(path)
    sheet = work_book.sheets()[0]
    nrows = sheet.nrows  # 行数
    ncols = sheet.ncols  # 列数(读取各节点连接状态)
    node_data_list = [sheet.row_values(row) for row in range(sheet.nrows)]  # 生成节点
    return node_data_list nrows ncols


def read(path):#读取文件函数
    work_book = xlrd.open_workbook(path)
    sheet = work_book.sheets()[0]
    data_list = [ sheet.row_values(row) for row in range(sheet.nrows) ]
    final_data =np.array(data_list)
    return data_listfinal_data

edges_list = []
def get_edges(node_listnrowsncols):#获取连通两点之间的路径
    for i in range(nrows):
        for j in range(ncols):
            if node_list[i][j]==1:
                edges_list.append((i+1j+1))
    return edges_list

def get_path(edges_list):#获取OD两点之间的路径
    graph = nx.DiGraph()
    nodes_list = range(1 25 1)
    edge_length = [1 + 0.1 * i for i in range(len(edges_list))]
    for i e in enumerate(edges_list):
        graph.add_edge(*e weight=edge_length[i])
    for n in nodes_list:
        graph.add_node(n)
    start = int(input(“输入起始点:“))#控制台输入的开始节点
    end = int(input(“输入终点:“))    #控制台输入的结束的节点
    path_iter = nx.all_simple_paths(graph start end)
    pathes = []
    for p in path_iter:
        pathes.append(p)
    # print(pathes)
    return pathes

def transfer_dict(final_data):
    road_dict = {}
    rows = final_data.shape[0]
    for i in range(rows):
        od = (int(final_data[i][0])int(final_data[i][1]))
        xuhao = round(final_data[i][2])
        road_dict[od]=xuhao
    return road_dict

#############################################主程序################################################
path = r“C:\Users\周广京\Desktop\用户均衡资料\节点信息纯表.xlsx“
anotherpath = r“C:\Users\周广京\Desktop\用户均衡资料\自由流时间.xlsx“
getinfo = r“C:\Users\周广京\Desktop\用户均衡资料\路段信息表.xlsx“
get_capacity = r“C:\Users\周广京\Desktop\用户均衡资料\单车道通行能力.xlsx“
node_list nrows ncols = read_data(path)
edges_list = get_edges(node_list nrows ncols)
# first_impedance = read_data(anotherpath)
# print(first_impedance)
paths = get_path(edges_list)

data_listroad_dict = read(getinfo)

data_dict = transfer_dict(road_dict)
# print(data_dict)

dd=[]
final_dict = []
new_dict = []
path=[]
for i in range(len(paths)):
    for j in range(len(paths[i])-1):
        section = (paths[i][j]paths[i][j+1])
        # print(type(section))
        # sec = str(tuple(section))
        # print(sec)
        dd = data_dict[section]
        #print(dd)

        if len(final_dict) == 1:
            final_dict = dd
        else:
            new_dict = (final_dictdd)

评论

共有 条评论