資源簡(jiǎn)介
用戶(hù)平衡流量分配.py
代碼片段和文件信息
import?networkx?as?nx
import?xlrd
import?numpy?as?np
import?xlwt
#?創(chuàng)建一個(gè)workbook對(duì)象即創(chuàng)建一個(gè)excel文件(可中文輸入且不壓縮)。
workbook?=?xlwt.Workbook(encoding=‘UTF-8‘?style_compression=0)
#?創(chuàng)建表sheet,表名為“西康路流量統(tǒng)計(jì)”,覆蓋單元格。
sheet?=?workbook.add_sheet(“路段流量分配“?cell_overwrite_ok=True)
#?寫(xiě)出表頭
sheet.write(0?0?‘流量‘)
def?read_data(path):??#?讀取文件函數(shù)
????work_book?=?xlrd.open_workbook(path)
????sheet?=?work_book.sheets()[0]
????nrows?=?sheet.nrows??#?行數(shù)
????ncols?=?sheet.ncols??#?列數(shù)(讀取各節(jié)點(diǎn)連接狀態(tài))
????node_data_list?=?[sheet.row_values(row)?for?row?in?range(sheet.nrows)]??#?生成節(jié)點(diǎn)
????return?node_data_list?nrows?ncols
def?read(path):#讀取文件函數(shù)
????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):#獲取連通兩點(diǎn)之間的路徑
????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兩點(diǎn)之間的路徑
????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(“輸入起始點(diǎn):“))#控制臺(tái)輸入的開(kāi)始節(jié)點(diǎn)
????end?=?int(input(“輸入終點(diǎn):“))????#控制臺(tái)輸入的結(jié)束的節(jié)點(diǎn)
????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\用戶(hù)均衡資料\節(jié)點(diǎn)信息純表.xlsx“
anotherpath?=?r“C:\Users\周廣京\Desktop\用戶(hù)均衡資料\自由流時(shí)間.xlsx“
getinfo?=?r“C:\Users\周廣京\Desktop\用戶(hù)均衡資料\路段信息表.xlsx“
get_capacity?=?r“C:\Users\周廣京\Desktop\用戶(hù)均衡資料\單車(chē)道通行能力.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)
評(píng)論
共有 條評(píng)論