Commit 0568d879 authored by 张晓彤's avatar 张晓彤

增加等待时间算法

parent 864a0c90
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
# G = nx.Graph(name= 'dispatch_topo')
# # print(G.graph)
# point = np.array([1,2,3,'a','b','c','m','n'])
# for i in point:
# G.add_node(i)
# # print(G.graph)
# G.add_edge(1,'m',weight = 100)
# G.add_edge(2,'m',weight = 80)
# G.add_edge('a','m', weight = 120)
# G.add_edge('n','m',weight = 200)
# G.add_edge('b','m',weight = 200)
# G.add_edge('c','m',weight = 210)
# print(G.edges(data = True))
# nx.draw(G,node_color = 'red')
# plt.show()
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
G = nx.Graph()
# G.add_edge('a', 'b', weight=0.6)
# G.add_edge('a', 'c', weight=0.2)
# G.add_edge('c', 'd', weight=0.1)
# G.add_edge('c', 'e', weight=0.7)
# G.add_edge('c', 'f', weight=0.9)
# G.add_edge('a', 'd', weight=0.3)
G.add_edge(1,'m',length = 100)
G.add_edge(2,'m',length = 80)
G.add_edge('a','m', length = 120)
G.add_edge('n','m',length = 10)
G.add_edge('b','n',length = 100)
G.add_edge('h','n',length = 10)
G.add_edge('b','h',length = 80)
G.add_edge('c','n',length = 210)
# elarge = [(u, v) for (u, v, d) in G.edges(data=True) if d['weight'] > 0.5]
# esmall = [(u, v) for (u, v, d) in G.edges(data=True) if d['weight'] <= 0.5]
#
# pos = nx.spring_layout(G) # positions for all nodes
#
# # nodes
# nx.draw_networkx_nodes(G, pos, node_size=700)
#
# # edges
# nx.draw_networkx_edges(G, pos, edgelist=elarge,
# width=6)
# nx.draw_networkx_edges(G, pos, edgelist=esmall,
# width=6, alpha=0.5, edge_color='b', style='dashed')
#
# # labels
# nx.draw_networkx_labels(G, pos, font_size=20, font_family='sans-serif')
# plt.axis('off')
# plt.savefig("weight.jpg")
pos=nx.shell_layout(G)
nx.draw(G,pos,with_labels=True, node_color='red', edge_color='blue', font_size = 18,width = 5,node_size=600, alpha=0.5 )
# pylab.title('Self_Define Net',fontsize=15)
plt.show()
# print('dijkstra方法寻找最短路径:')
# path = nx.dijkstra_path(G, source='a', target='b', weight='length')
# print('节点a到b的路径:', path)
G_node = np.array(G.edges)
GG = G.number_of_edges()
print (G_node[0][1], type(G_node[0][1]))
print(GG,type(GG))
# G1 = nx.Graph()
# print(np.array(G1.nodes))
# G2 = nx.Graph() # 创建:空的 无向图
# G2.add_weighted_edges_from([(1,2,2),(1,3,8),(1,4,1),
# (2,3,6),(2,5,1),
# (3,4,7),(3,5,5),(3,6,1),(3,7,2),
# (4,7,9),
# (5,6,3),(5,8,2),(5,9,9),
# (6,7,4),(6,9,6),
# (7,9,3),(7,10,1),
# (8,9,7),(8,11,9),
# (9,10,1),(9,11,2),
# (10,11,4)]) # 向图中添加多条赋权边: (node1,node2,weight)
#
This diff is collapsed.
from path_plan.topo_graph import *
import networkx as nx
import matplotlib.pyplot as plt
# from traffic_flow.traffic_flow_planner import *
# from path_plan.path_planner_2 import *
# from dispatcher import Dispatcher, PreSchedule
topo = Topo()
topo.generate_topo_graph()
# path_planner = PathPlanner(topo)
# a, b = path_planner.path_cost_generate("ef89f721-5134-3ef1-91e2-95b4e5004675", "e5d0ac4e-c134-3ef1-901b-867980c8453e", False)
# print(a,b)
load_G = topo.get_load_G()
unload_G = topo.get_unload_G()
print(unload_G.edges(data=True))
print(load_G.edges(data=True))
# alpha = 1
# beta = 1
# source_area = "e44052d4-1134-6602-ea64-f14d05ecfb3f"
# target_area = "659ca4d5-2134-6602-ed05-1deddfca63f9"
# unload_source_node = str(session_postgre.query(DiggingWorkArea).filter_by(Id=source_area).first().ExitNodeId)
# unload_end_node = str(session_postgre.query(DumpArea).filter_by(Id=target_area).first().EntranceNodeId)
# # _, park_path = nx.single_source_dijkstra(unload_G, source=source_node, target=source_node, weight="real_distance")
# # path = park_path
# # for i in range(len(path)-1):
# # pair_now = [path[i], path[i+1]]
# # # edge_lane = nx.get_edge_attributes(load_G,'lane')
# # data = dict(unload_G[path[i]][path[i+1]]['lane'])
# # for u,v in data.items():
# # data[u] = [v[0],beta*3]
# # val = list(data.values())
# # unload_G[path[i]][path[i+1]]['locked_distance'] = sum([beta * sum(i[0] for i in val) + alpha * sum(i[-1] for i in val)])
#
# unload_cost = nx.dijkstra_path_length(unload_G, source=unload_source_node, target=unload_end_node,
# weight="locked_distance")
# print(unload_cost)
# # print([path[i], path[i+1]], unload_G[path[i]][path[i+1]]['locked_distance'])
# # for i in range(len(park_path) - 1):
# # data = dict(unload_G[park_path[i]][park_path[i + 1]]['lane'])
# # for u, v in data.items():
# # # u_lane_cost = lane_cost_generate(u)
# # data[u] = [v[0], beta *3]
# # val = list(data.values())
# # # unload_G[park_path[i]][park_path[i + 1]]['locked_distance'] = sum(beta * sum(i[0] for i in val) + alpha * sum(i[-1] for i in val))
# # unload_G[park_path[i]][park_path[i + 1]]['locked_distance'] = sum( beta * sum(i[0] for i in val))
# # # unload_G[park_path[i]][park_path[i + 1]]['locked_distance'] = sum(beta * sum(i[0] for i in val) + alpha * sum(i[-1] for i in val))
# # unload_G[park_path[i]][park_path[i + 1]]['locked_distance'] = sum( beta * sum(i[0] for i in val))
#
# # print(load_G.edges(data=True))
# print(unload_G.edges(data=True))
# # distance = nx.dijkstra_path_length(load_G, source="81be6a9e-8134-3ef2-141e-4bb045b8d097", target= "1c85bc2c-9134-6281-1265-fd19d0dcbd52", weight="locked_distance")
# # print(distance)
# # print(distance,path)
# # aaa = topo.get_load_source_node("7b9c8e89-7134-63ac-9c44-f183674b090c")
# # print(aaa)
# # path,_, _= topo.get_load_target_node_real("7b9c8e89-7134-63ac-9c44-f183674b090c",None)
# # print(path)
# # updata_edge = {}
# # for i in range(len(path)-1):
# # pair_now = [path[i], path[i+1]]
# # # edge_lane = nx.get_edge_attributes(load_G,'lane')
# # data = dict(load_G[path[i]][path[i+1]]['lane'])
# # for u,v in data.items():
# # data[u] = [v[0],v[0]]
# # load_G[path[i]][path[i+1]]['locked_distance'] = sum(i[-1] for i in list(data.values()))
# # print([path[i], path[i+1]], load_G[path[i]][path[i+1]]['locked_distance'])
#
#
# unload_G = topo.get_un data=True))
pos = nx.shell_layout(load_G)
nx.draw(load_G, pos, with_labels=True, node_color='red', edge_color='blue', font_size=18, width=5, node_size=600,
alpha=0.5)
plt.show()
pos = nx.shell_layout(unload_G)
nx.draw(unload_G, pos, with_labels=True, node_color='red', edge_color='blue', font_size=18, width=5, node_size=600,
alpha=0.5)
plt.show()
# unload_G = topo.get_unload_G()
# print(unload_G.edges(data=True))
#
# edge_arrtibutes = dict(nx.get_edge_attributes(unload_G,'lane'))
# print(edge_arrtibutes)
# print(type(edge_arrtibutes))
# print(edge_arrtibutes.values())
# print(type(edge_arrtibutes.values()))
# pos = nx.shell_layout(load_G)
# nx.draw(load_G, pos, with_labels=True, node_color='red', edge_color='blue', font_size=18, width=5, node_size=600,
# alpha=0.5)
# plt.show()
# a = "-rw-r--r-- 1 cs9315 22439 Feb 18 2021"
# print(len(a))
# end= time.process_time()
# print('Running time: %s Seconds'%(end-start))
import copy
import numpy as np
# # Question 1 optimisation problem
A = np.array([[1., 2., 1., -1.],
[-1., 1., 0., 2.],
[0., -1., -2., 1.]])
b = np.array([[3.], [2.], [-2.]])
x = np.array([[1.], [1.], [1.], [1.]])
k = 0
alpha = 0.1
gamma = 0.2
def gradient_step_1(A, b, x, gamma):
updated_x = np.array(A.T.dot((A.dot(x) - b)) + gamma*x)
return np.round(updated_x,4)
print_form = []
def edit_form(print_form,x):
print_form.pop(0)
x_k = copy.copy(x)
print_form.append([k, x_k])
while True:
if k > 0 and k <= 5:
x_k= copy.copy(x)
print_form.append((k,x_k))
if k == 6:
for item in print_form:
print(f"k = {item[0]}, x({item[0]}) = {item[1]}")
if k > 6:
edit_form(print_form,x)
k += 1
x -= alpha*gradient_step_1(A, b, x, gamma)
if np.linalg.norm(gradient_step_1(A, b, x, gamma), ord=2)<0.001:
edit_form(print_form, x)
k += 1
x -= alpha * gradient_step_1(A, b, x, gamma)
edit_form(print_form, x)
for item in print_form:
print(f"k = {item[0]}, x({item[0]}) = {item[1]}")
break
# x = np.array([[1.123], [1.123], [1.123], [1.123]])
# x =np.array([[-4.59627504],
# [8.95686683],
# [-9.76586351],
# [ 8.87335576]])
# print(np.around(x,2))
# for i in range(10):
# x = A.T.dot((A.dot(x) - b)) + gamma*x
# print(np.round(x,4))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment