# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Baseline Results Notebook:
#
# The code below demonstrates our baseline implementation and some associated functionality we have developed.

# Automatically reload custom code modules when there are changes:
# %load_ext autoreload
# %autoreload 2


# Adjust relative path so that the notebook can find the code modules:
import sys
sys.path.append('../code/')


# +
import numpy as np
import pandas as pd

import matplotlib as mpl
import matplotlib.pyplot as plt

# %matplotlib notebook

# -

# Import code modules:
from structures import RingRoad
from animations import Animation


# Hide warnings about safe distance violation (still in development):
import warnings
warnings.filterwarnings("ignore", category=UserWarning)


# +
# Define a ring road environment:
env = RingRoad(
    num_vehicles = 22,  # The vechicles at index 0 is an A.V.
    ring_length = 230.0,  # The road is a cicle.
    starting_noise = 4.0,  # Uniformly add noise to starting positions.
    temporal_res = 0.3,  # Set the size of simulation steps (seconds).
    av_activate = 30,  # Set when the PID controller is activated.
    seed = 286,  # Set a random seed.
)

# Plot initial conditions:
fig,ax = env.plot_ring(step=0, draw_cars_to_scale=True, draw_safety_buffer=True)

# -

# Run the simulation for set number of time steps:
total_time = 90  # In seconds.
total_steps = int(np.ceil(total_time/env.dt))
env.run(steps=total_steps)


# Show step of simulation:
step = env.step
fig, axs = env.plot_dashboard(
    step=step, total_steps=total_steps,
    draw_cars_to_scale=True, draw_safety_buffer=True, label_step=True, label_cars=True,
)


# Calculate performance stats:
steps_before = range(0,env.av_activate)
steps_after = range(env.av_activate, env.step)
speeds_before = env.get_vehicle_vel_table(steps_before)
speeds_after = env.get_vehicle_vel_table(steps_after)
print("Mean standard deviation before: {}".format(speeds_before.std(axis=0).mean()))
print("Mean standard deviation after: {}".format(speeds_after.std(axis=0).mean()))
df_before = pd.DataFrame(speeds_before.std(axis=0).to_frame(name='before AV control'))
df_after = pd.DataFrame(speeds_after.std(axis=0).to_frame(name='after AV control'))
pd.concat([df_before,df_after],axis=1)


# +
# Build animation:
anim = Animation(env, speedup=5.0, interval=5)
anim.animate_dashboard(draw_cars_to_scale=True, draw_safety_buffer=False, show_sigma=True)

# Show animation:
# anim.show()

# # Save animation as GIF (may take a few minutes -- pauses preview above):
# anim.save_gif(filepath="../outputs/baseline.gif", overwrite=True)

# -

# <a href='https://github.com/chickert/autonomous_vehicles/raw/main/outputs/baseline.gif'><img src='https://github.com/chickert/autonomous_vehicles/raw/main/outputs/baseline.gif' /></a>
#



StarcoderdataJupyterScriptsDedupFiltered