Result analysis

Classes for analyzing and visualizing simulation results.

Analyzer

class unsim.Analyzer(W)

Analyzer for UNsim simulation results.

Parameters:

W (World) – Parent world.

trip_all

Total demand volume (veh).

Type:

float

trip_completed

Total completed trips (veh).

Type:

float

total_travel_time

Total travel time of all vehicles (s).

Type:

float

average_travel_time

Average travel time per completed trip (s).

Type:

float

average_delay

Average delay per completed trip (s).

Type:

float

basic_analysis()

Compute basic aggregate statistics.

Calculates trip_all, trip_completed, total_travel_time, average_travel_time, and average_delay.

Get link-level statistics as a DataFrame.

Returns:

Columns: link, start_node, end_node, traffic_volume, vehicles_remain, free_travel_time, average_travel_time.

Return type:

pandas.DataFrame

network(t=None, figsize=(6, 6), left_handed=True, minwidth=0.5, maxwidth=12, node_size=4, fontsize=0, legend=True)

Draw network state at time t.

Links are colored by speed (viridis) and sized by density.

Parameters:
  • t (float or None, optional) – Time (s). None for TMAX/2.

  • figsize (tuple, optional) – Figure size.

  • left_handed (bool, optional) – True for left-handed traffic (Japan/UK).

  • minwidth (float, optional) – Minimum link width.

  • maxwidth (float, optional) – Maximum link width.

  • node_size (float, optional) – Node marker size.

  • legend (bool, optional) – Show colorbar legend.

Returns:

The created figure.

Return type:

matplotlib.figure.Figure

network_anim(figsize=(6, 6), left_handed=True, minwidth=0.5, maxwidth=12, node_size=4, timestep_skip=10, duration=100, dpi=80, file_name=None)

Create animated GIF of network state over time.

Parameters:
  • figsize (tuple, optional) – Figure size.

  • left_handed (bool, optional) – True for left-handed traffic.

  • minwidth (float, optional) – Minimum link width.

  • maxwidth (float, optional) – Maximum link width.

  • node_size (float, optional) – Node marker size.

  • timestep_skip (int, optional) – Render every N-th timestep.

  • duration (int, optional) – Frame duration in ms.

  • dpi (int, optional) – Image resolution.

  • file_name (str or None, optional) – Output GIF path. None for “out_{name}.gif”.

Returns:

Path to the saved GIF file.

Return type:

str

network_anim_linkbased(figsize=(6, 6), left_handed=True, minwidth=0.5, maxwidth=36, node_size=4, timestep_skip=10, duration=100, dpi=80, file_name=None)

Create animated GIF with link-level aggregated traffic state.

Each link is drawn as a single line segment. Width represents the number of vehicles on the link, and color represents link-average speed (viridis colormap). Suitable for getting an overview of large networks.

Parameters:
  • figsize (tuple, optional) – Figure size.

  • left_handed (bool, optional) – True for left-handed traffic.

  • minwidth (float, optional) – Minimum link width.

  • maxwidth (float, optional) – Maximum link width.

  • node_size (float, optional) – Node marker size.

  • timestep_skip (int, optional) – Render every N-th timestep.

  • duration (int, optional) – Frame duration in ms.

  • dpi (int, optional) – Image resolution.

  • file_name (str or None, optional) – Output GIF path. None for “out_{name}_linkbased.gif”.

Returns:

Path to the saved GIF file.

Return type:

str

network_average(figsize=(6, 6), left_handed=True, minwidth=0.5, maxwidth=12, node_size=4, legend=True, show_labels=True)

Draw network with time-averaged traffic state.

Links are colored by delay ratio (jet) and sized by traffic volume.

Parameters:
  • figsize (tuple, optional) – Figure size.

  • left_handed (bool, optional) – True for left-handed traffic.

  • minwidth (float, optional) – Minimum link width.

  • maxwidth (float, optional) – Maximum link width.

  • node_size (float, optional) – Node marker size.

  • legend (bool, optional) – Show colorbar legend.

  • show_labels (bool, optional) – Show node name labels. Default True.

Returns:

The created figure.

Return type:

matplotlib.figure.Figure

print_simple_stats()

Print basic simulation statistics to stdout.

time_space_diagram(links=None, mode='density', figsize=(12, 4), xlim=None, ylim=None, cmap=None, n_contours=20, nt=100, nx=50, vmin=None, vmax=None)

Draw a time-space diagram for one or more links.

Parameters:
  • links (Link or str or list[Link|str] or None, optional) – Link(s) to plot. Accepts Link objects or names. None for all links.

  • mode (str, optional) – “density” or “k” for density, “flow” or “q” for flow, “speed” or “v” for speed, “N” for cumulative count contours. “k_norm” / “q_norm” / “v_norm” for normalized values (k/kappa, q/q*, v/u per link).

  • figsize (tuple, optional) – Figure size.

  • xlim (tuple or None, optional) – Time axis limits (s).

  • ylim (tuple or None, optional) – Space axis limits (m).

  • cmap (str or None, optional) – Colormap name.

  • n_contours (int, optional) – Number of contour lines for mode=”N”.

  • nt (int, optional) – Number of time grid points.

  • nx (int, optional) – Number of space grid points per link.

  • vmin (float or None, optional) – Minimum value for colormap. None for auto.

  • vmax (float or None, optional) – Maximum value for colormap. None for auto.

Returns:

The created figure.

Return type:

matplotlib.figure.Figure

travel_time(orig, dest, t_depart, path=None)

Compute travel time from node orig to node dest departing at t_depart.

By default, finds the time-dependent shortest path using Dijkstra with actual (congestion-dependent) link travel times from cumulative counts. LTM satisfies FIFO, so Dijkstra yields the optimal solution.

Parameters:
  • orig (str or Node) – Origin node.

  • dest (str or Node) – Destination node.

  • t_depart (float) – Departure time (s).

  • path (list[str|Link] or None, optional) – Explicit path as ordered list of links. None for time-dependent shortest path (default).

Returns:

Travel time (s). Returns inf if the vehicle does not arrive within the simulation period.

Return type:

float