Skip to content

Commit

Permalink
Update visualization for multi-lane traffic
Browse files Browse the repository at this point in the history
  • Loading branch information
toruseo committed May 9, 2024
1 parent acb956a commit 2f022ac
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 38 deletions.
2 changes: 1 addition & 1 deletion uxsim/ResultGUIViewer/ResultGUIViewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def paint(self, painter, option, widget):
for i in range(num_segments):
density = self.density_list[self.t, i]
speed = self.Link.v_mat[self.t, i]
lw = max([density*self.Link.delta])*(maxlw-minlw)+minlw
lw = max([density*self.Link.delta*self.Link.lanes])*(maxlw-minlw)+minlw

c = colormaps["viridis"](speed/self.Link.u)
#color = QColor(int(density/self.Link.jam_density * 255), int(density/self.Link.jam_density * 255), 0, 255)
Expand Down
51 changes: 14 additions & 37 deletions uxsim/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ def compute_accurate_traj(s):
if l_old != l:
l.tss.append([])
l.xss.append([])
l.ls.append(veh.log_lane[i])
l.cs.append(veh.color)
l.names.append(veh.name)

Expand Down Expand Up @@ -321,8 +322,6 @@ def time_space_diagram_traj(s, links=None, figsize=(12,4), plot_signal=True, xli
if s.W.vehicle_logging_timestep_interval != 1:
warnings.warn("vehicle_logging_timestep_interval is not 1. The plot is not exactly accurate.", LoggingWarning)

#リンク車両軌跡の時空間図
s.W.print(" drawing trajectories...")
s.compute_accurate_traj()

#対象がlistであればOKで,単一な場合にはlistに変換する.未指定であれば全部にする.
Expand All @@ -335,35 +334,9 @@ def time_space_diagram_traj(s, links=None, figsize=(12,4), plot_signal=True, xli
except TypeError:
links = [links]

for lll in tqdm(links, disable=(s.W.print_mode==0)):
l = s.W.get_link(lll)

plt.figure(figsize=figsize)
plt.title(l)
for i in range(len(l.xss)):
plt.plot(l.tss[i], l.xss[i], c=l.cs[i], lw=0.5)
if plot_signal:
signal_log = [i*s.W.DELTAT for i in lange(l.end_node.signal_log) if (l.end_node.signal_log[i] not in l.signal_group and len(l.end_node.signal)>1)]
plt.plot(signal_log, [l.length for i in lange(signal_log)], "r.")
plt.xlabel("time (s)")
plt.ylabel("space (m)")
if xlim == None:
plt.xlim([0, s.W.TMAX])
else:
plt.xlim(xlim)
if ylim == None:
plt.ylim([0, l.length])
else:
plt.ylim(ylim)
plt.grid()
plt.tight_layout()
if s.W.save_mode:
plt.savefig(f"out{s.W.name}/tsd_traj_{l.name}.png")
if s.W.show_mode:
plt.show()
else:
plt.close("all")

for lll in links:
s.time_space_diagram_traj_links(linkslist=[lll], figsize=figsize, plot_signal=plot_signal, xlim=xlim, ylim=ylim)

@catch_exceptions_and_warn()
def time_space_diagram_density(s, links=None, figsize=(12,4), plot_signal=True, xlim=None, ylim=None):
"""
Expand Down Expand Up @@ -447,7 +420,7 @@ def time_space_diagram_traj_links(s, linkslist, figsize=(12,4), plot_signal=True
warnings.warn("vehicle_logging_timestep_interval is not 1. The plot is not exactly accurate.", LoggingWarning)

#複数リンクの連続した車両軌跡の時空間図
s.W.print(" drawing trajectories in consecutive links...")
s.W.print(" drawing trajectories...")
s.compute_accurate_traj()

#リンクリストのリストであればそのまま,そうでなければリスト化
Expand All @@ -470,7 +443,8 @@ def time_space_diagram_traj_links(s, linkslist, figsize=(12,4), plot_signal=True
for ll in links:
l = s.W.get_link(ll)
for i in range(len(l.xss)):
plt.plot(l.tss[i], np.array(l.xss[i])+linkdict[l], c=l.cs[i], lw=0.5)
lane_shift = l.ls[i]/l.lanes*s.W.DELTAT/2 #vehicle with the same lane is plotted slightly shifted
plt.plot(np.array(l.tss[i])+lane_shift, np.array(l.xss[i])+linkdict[l], "-", c=l.cs[i], lw=0.5)
if plot_signal:
signal_log = [i*s.W.DELTAT for i in lange(l.end_node.signal_log) if (l.end_node.signal_log[i] not in l.signal_group and len(l.end_node.signal)>1)]
plt.plot(signal_log, [l.length+linkdict[l] for i in lange(signal_log)], "r.")
Expand All @@ -494,7 +468,10 @@ def time_space_diagram_traj_links(s, linkslist, figsize=(12,4), plot_signal=True
plt.grid()
plt.tight_layout()
if s.W.save_mode:
plt.savefig(f"out{s.W.name}/tsd_traj_links_{'-'.join([s.W.get_link(l).name for l in links])}.png")
if len(links) == 1:
plt.savefig(f"out{s.W.name}/tsd_traj_{s.W.get_link(links[0]).name}.png")
else:
plt.savefig(f"out{s.W.name}/tsd_traj_links_{'-'.join([s.W.get_link(l).name for l in links])}.png")
if s.W.show_mode:
plt.show()
else:
Expand Down Expand Up @@ -577,7 +554,7 @@ def network(s, t=None, detailed=1, minwidth=0.5, maxwidth=12, left_handed=1, tmp
minwidth : float, optional
The minimum width of the link visualization. Default is 0.5.
maxwidth : float, optional
The maximum width of the link visualization. Default is 12.
The maximum width of the link per lane visualization. Default is 12.
left_handed : int, optional
If set to 1, the left-handed traffic system (e.g., Japan, UK) is used. If set to 0, the right-handed one is used. Default is 1.
tmp_anim : int, optional
Expand Down Expand Up @@ -622,7 +599,7 @@ def network(s, t=None, detailed=1, minwidth=0.5, maxwidth=12, left_handed=1, tmp
except:
warnings.warn(f"invalid time {t} is specified for network visualization", UserWarning)
return -1
lw[i] = k*l.delta*(maxwidth-minwidth)+minwidth
lw[i] = k*l.delta*(maxwidth*l.lanes-minwidth)+minwidth
c[i] = plt.colormaps["viridis"](v/l.u)
xmid = [((xsize-i)*x1+(i+1)*x2)/(xsize+1)+vx for i in range(xsize)]
ymid = [((xsize-i)*y1+(i+1)*y2)/(xsize+1)+vy for i in range(xsize)]
Expand All @@ -635,7 +612,7 @@ def network(s, t=None, detailed=1, minwidth=0.5, maxwidth=12, left_handed=1, tmp
#簡略モード
k = (l.cum_arrival[int(t/s.W.DELTAT)]-l.cum_departure[int(t/s.W.DELTAT)])/l.length
v = l.length/l.traveltime_instant[int(t/s.W.DELTAT)]
width = k*l.delta*(maxwidth-minwidth)+minwidth
width = k*l.delta*(maxwidth*l.lanes-minwidth)+minwidth
c = plt.colormaps["viridis"](v/l.u)
xmid1, ymid1 = (2*x1+x2)/3+vx, (2*y1+y2)/3+vy
xmid2, ymid2 = (x1+2*x2)/3+vx, (y1+2*y2)/3+vy
Expand Down
4 changes: 4 additions & 0 deletions uxsim/uxsim.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@ def __init__(s, W, name, start_node, end_node, length, free_flow_speed=20, jam_d
s.tss = []
s.xss = []
s.cs = []
s.ls = []
s.names = []

if eular_dx == None:
Expand Down Expand Up @@ -853,6 +854,7 @@ def __init__(s, W, orig, dest, departure_time, name=None, route_pref=None, route
s.log_x = [] #位置
s.log_s = [] #車頭距離
s.log_v = [] #現在速度
s.log_lane = [] #車線
s.color = (random.random(), random.random(), random.random())

s.log_t_link = [[int(s.departure_time*s.W.DELTAT), "home"]] #新たなリンクに入った時にその時刻とリンクのみを保存.経路分析用
Expand Down Expand Up @@ -1167,6 +1169,7 @@ def record_log(s, enforce_log=0):
s.log_x.append(-1)
s.log_s.append(-1)
s.log_v.append(-1)
s.log_lane.append(-1)

if s.state == "wait":
s.W.analyzer.average_speed_count += 1
Expand All @@ -1180,6 +1183,7 @@ def record_log(s, enforce_log=0):
s.log_link.append(s.link)
s.log_x.append(s.x)
s.log_v.append(s.v)
s.log_lane.append(s.lane)
if s.leader != None and s.link == s.leader.link:
s.log_s.append(s.leader.x-s.x)
else:
Expand Down

0 comments on commit 2f022ac

Please sign in to comment.