-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaseline.py
More file actions
85 lines (68 loc) · 2.84 KB
/
baseline.py
File metadata and controls
85 lines (68 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import warnings
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from structures import RingRoad
from animations import Animation
if __name__=='__main__':
# Hide warnings about safe distance violation (still in development):
warnings.filterwarnings("ignore", category=UserWarning)
# Define a ring road environment:
env = RingRoad(
num_vehicles = 22,
ring_length = 230.0,
starting_noise = 4.0,
temporal_res = 0.3,
av_activate = 30,
seed = 286,
)
# Run the simulation for set number of time steps:
total_time = 50 # In seconds.
total_steps = int(np.ceil(total_time/env.dt))
env.run(steps=total_steps)
# Plot initial state:
fig,ax = env.plot_ring(step=0, draw_cars_to_scale=True, draw_safety_buffer=False)
filename = "../outputs/environment_initial_state.png"
plt.savefig(filename)
print("Saved : {} .".format(filename))
# Plot state at activation:
fig,ax = env.plot_ring(step=int(np.ceil(env.av_activate/env.dt))-1, draw_cars_to_scale=True, draw_safety_buffer=False)
filename = "../outputs/environment_before_activation.png"
plt.savefig(filename)
print("Saved : {} .".format(filename))
# Plot stable state:
fig,ax = env.plot_ring(step=env.step, draw_cars_to_scale=True, draw_safety_buffer=False)
filename = "../outputs/environment_stable_state.png"
plt.savefig(filename)
print("Saved : {} .".format(filename))
# Plot position history:
fig,ax = env.plot_positions()
filename = "../outputs/positions_history.png"
plt.savefig(filename)
print("Saved : {} .".format(filename))
# Plot velocity history:
fig,ax = env.plot_velocities(show_sigma=True)
filename = "../outputs/velocities_history.png"
plt.savefig(filename)
print("Saved : {} .".format(filename))
# Calculate standard deviations:
speeds_before = env.get_vehicle_vel_table( range(0,env.av_activate) )
speeds_after = env.get_vehicle_vel_table( range(env.av_activate, env.step) )
df = pd.concat([
pd.DataFrame(speeds_before.std(axis=0).to_frame(name='before AV control')),
pd.DataFrame(speeds_after.std(axis=0).to_frame(name='after AV control')),
], axis=1)
df_mean = df.mean(axis=0).to_frame(name='mean').transpose()
df = pd.concat([df_mean,df], axis=0)
filename = "../outputs/velocity_standard_deviations.csv"
df.to_csv(filename, index=False)
print("Saved : {} .".format(filename))
print("")
print("Mean standard deviation before: {}".format(df.loc['mean','before AV control']))
print("Mean standard deviation after: {}".format(df.loc['mean','after AV control']))
print("""
To visualize the baseline as an animation,
please run the code/animation.py script
or see the notebooks/Animations.ipynb notebook.
""")
print("Done.")