Road

Control the flow of the simulation

class road.Road[source]

Handler for the running of the code.

add_car(starting_position, starting_velocity, car_class=None, **car_kwargs)[source]

Add a car to the car list.

Parameters:
  • starting_velocity
  • starting_position
  • car_class – Car like object to use, default to the simple Car class
  • **car_kwargs – extra keywords to give to cars
add_multiple_cars(starting_positions, starting_velocity, car_class=None, **car_kwargs)[source]

Add several cars to the list.

Parameters:
  • starting_positions – may be a list or float (for a single car)
  • starting_velocity – constant starting velocity for all cars
  • car_class – Car like object to use, default to the simple Car class
  • **car_kwargs – extra keywords to give to cars
get_distance_to_next_car(car, prev_position)[source]

Get the distance to the car in front.

Parameters:
  • car – Car object of the car of interest.
  • prev_position – x value of the car in front.
Returns:

The distance between the car between the rears of the two cars.

get_history_position_array()[source]

Create a history-position array for all the cars on the road

Returns:The value of x positions of the cars, each row represents a different car, each column is a time point in the simulation.
run_simulation(total_timesteps)[source]

Step through all the time steps in simulation.

At the start of the simulation we sort the cars by position to ensure that the car at the start of the list is at the front of the road.

Returns:History of the positions of the cars.
Return type:(n_cars x n_time_steps+1) array
update_car_positions()[source]

Move all the cars at the given time step.