Skip to content

Commit

Permalink
add vehicle group analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
toruseo committed Aug 19, 2024
1 parent 96b2d01 commit fb1d6a6
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 0 deletions.
57 changes: 57 additions & 0 deletions tests/test_other_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,3 +510,60 @@ def test_area():
assert df["total_trips"][(df["origin_area"] == "areaNW") & (df["destination_area"] == "areaSE")].values[0] == 1750
assert df["average_free_travel_time"][(df["origin_area"] == "areaNW") & (df["destination_area"] == "areaSE")].values[0] == 400.0
assert df["average_shortest_distance"][(df["origin_area"] == "areaNW") & (df["destination_area"] == "areaSE")].values[0] == 8000.0

@pytest.mark.flaky(reruns=10)
def test_vehicle_group():
W = World(
name="",
deltan=10,
tmax=4000,
print_mode=1, save_mode=1, show_mode=0,
random_seed=None,
)

n_nodes = 4
imax = n_nodes
jmax = n_nodes
nodes = {}
for i in range(imax):
for j in range(jmax):
nodes[i,j] = W.addNode(f"n{(i,j)}", i, j, flow_capacity=1.6)

links = {}
for i in range(imax):
for j in range(jmax):
if i != imax-1:
links[i,j,i+1,j] = W.addLink(f"l{(i,j,i+1,j)}", nodes[i,j], nodes[i+1,j], length=1000)
if i != 0:
links[i,j,i-1,j] = W.addLink(f"l{(i,j,i-1,j)}", nodes[i,j], nodes[i-1,j], length=1000)
if j != jmax-1:
links[i,j,i,j+1] = W.addLink(f"l{(i,j,i,j+1)}", nodes[i,j], nodes[i,j+1], length=1000)
if j != 0:
links[i,j,i,j-1] = W.addLink(f"l{(i,j,i,j-1)}", nodes[i,j], nodes[i,j-1], length=1000)


areas = {
"areaN": [nodes[0,0], nodes[0, n_nodes-1]],
"areaS": [nodes[n_nodes-1,0], nodes[n_nodes-1, n_nodes-1]],
"areaNW": [nodes[0,0]],
"areaSE": [nodes[n_nodes-1, n_nodes-1]]
}

W.adddemand_areas2areas(areas["areaN"], areas["areaS"], 0, 3000, volume=7000)

W.exec_simulation()
W.analyzer.print_simple_stats()

dt = 500
group_dict = {}
for t in range(0, W.TMAX, dt):
group_dict[f"depart_t={t}"] = [veh for veh in W.VEHICLES.values() if t <= veh.departure_time_in_second < t+dt]

df = W.analyzer.vehicle_groups_to_pandas(list(group_dict.values()), list(group_dict.keys()))
print(df)

assert df["average_travel_time"][df["group"]=="depart_t=0"].values[0] < df["average_travel_time"][df["group"]=="depart_t=1500"].values[0]
assert df["average_delay_ratio"][df["group"]=="depart_t=0"].values[0] < df["average_delay_ratio"][df["group"]=="depart_t=1500"].values[0]
assert df["average_traveled_distance"][df["group"]=="depart_t=0"].values[0] < df["average_traveled_distance"][df["group"]=="depart_t=1500"].values[0]
assert df["average_detour_ratio"][df["group"]=="depart_t=0"].values[0] < df["average_detour_ratio"][df["group"]=="depart_t=1500"].values[0]
assert df["average_speed"][df["group"]=="depart_t=0"].values[0] > df["average_speed"][df["group"]=="depart_t=1500"].values[0]
99 changes: 99 additions & 0 deletions uxsim/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,105 @@ def areas2areas_to_pandas(s, areas, area_names=None):

s.df_areas2areas = pd.DataFrame(out[1:], columns=out[0])
return s.df_areas2areas

def vehicle_groups_to_pandas(s, groups, group_names=None):
"""
Converts the vehicle group analysis results to a pandas DataFrame.
Parameters
----------
groups : list
The list of vehicle groups. Each group is defined as a list of vehicle object.
group_names : list, optional
The list of names of vehicle groups.
Returns
-------
pd.DataFrame
"""
df_od = s.W.analyzer.od_to_pandas()

if group_names == None:
group_names = [f"group {i} including {groups[0].name}" for i in range(len(groups))]

total_trip_rec = []
completed_trip_rec = []
average_travel_time_rec = []
average_delay_rec = []
std_delay_rec = []
average_traveled_distance_rec = []
average_detour_rec = []
std_detour_rec = []
averae_speed_rec = []
std_speed_rec = []
for i, group in enumerate(groups):
total_trips = 0
completed_trips = 0
travel_times = []
delays = []
traveled_distances = []
detours = []
speeds = []


for veh in group:

total_trips += 1
if veh.state == "end":
completed_trips += 1
travel_times.append(veh.travel_time)
traveled_distances.append(veh.distance_traveled)

free_travel_time = df_od["free_travel_time"][(df_od["orig"]==veh.orig.name) & (df_od["dest"]==veh.dest.name)].values[0]
shortest_distance = df_od["shortest_distance"][(df_od["orig"]==veh.orig.name) & (df_od["dest"]==veh.dest.name)].values[0]

delays.append(veh.travel_time/free_travel_time)
detours.append(veh.distance_traveled/shortest_distance)

speeds.append(veh.distance_traveled/veh.travel_time)

#print(f"{group_names[i]=}, {np.average(travel_times)=}, {np.average(traveled_distances)=}, {np.average(delays)=}, {np.average(detours)=}, {np.std(delays)=}, {np.std(detours)=}, {np.average(speeds)}, {np.std(speeds)}")

total_trip_rec.append(total_trips)
completed_trip_rec.append(completed_trips)
if completed_trips > 0:
average_travel_time_rec.append(np.average(travel_times))
average_delay_rec.append(np.average(delays))
std_delay_rec.append(np.std(delays))
average_traveled_distance_rec.append(np.average(traveled_distances))
average_detour_rec.append(np.average(detours))
std_detour_rec.append(np.std(detours))
averae_speed_rec.append(np.average(speeds))
std_speed_rec.append(np.std(speeds))
else:
average_travel_time_rec.append(np.nan)
average_delay_rec.append(np.nan)
std_delay_rec.append(np.nan)
average_traveled_distance_rec.append(np.nan)
average_detour_rec.append(np.nan)
std_detour_rec.append(np.nan)
averae_speed_rec.append(np.nan)
std_speed_rec.append(np.nan)

df = pd.DataFrame({
"group": group_names,
"total_trips": total_trip_rec,
"completed_trips": completed_trip_rec,
"average_travel_time": average_travel_time_rec,
"average_delay_ratio": average_delay_rec,
"std_delay_ratio": std_delay_rec,
"average_traveled_distance": average_traveled_distance_rec,
"average_detour_ratio": average_detour_rec,
"std_detour_ratio": std_detour_rec,
"average_speed": averae_speed_rec,
"std_speed": std_speed_rec,
})

s.df_vehicle_groups = df

return s.df_vehicle_groups




def mfd_to_pandas(s, links=None):
Expand Down
1 change: 1 addition & 0 deletions uxsim/uxsim.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,7 @@ def __init__(s, W, orig, dest, departure_time, name=None, route_pref=None, route
s.departure_time = departure_time
else:
s.departure_time = int(departure_time/s.W.DELTAT)
s.departure_time_in_second = departure_time*s.W.DELTAT #TODO: temporal workaround
s.arrival_time = -1
s.link_arrival_time = -1
s.travel_time = -1
Expand Down

0 comments on commit fb1d6a6

Please sign in to comment.