Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Traffic simulation

#Run traffic_simulation.py in python3

## Description

Analyze the behavior of drivers on a new road to determine the optimal speed
Expand Down
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ipython[all]
numpy
random
matplotlib
Binary file removed road.png
Binary file not shown.
44 changes: 44 additions & 0 deletions traffic/car.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import random


class Car():
"""
Creates a 5m long car and adjusts its speed and location based on
other cars around it. This car will randomly slow down by 10% (2 m/s).
"""

def __init__(self):
self.speed = 0
self.size = 5
self.location = 0

def accelerate(self):
if random.random() < .1:
if self.speed > 1:
self.speed -= 2
else:
if self.speed < 32:
self.speed += 2
else:
self.speed

def proximity(self, other_car):
self.speed = other_car.speed

def adjust_location(self):
self.location += self.speed
if self.location >= 999:
self.location -= 999

def close_proximity_check(self, other_car):
if self.location >= 974:
return True
elif (other_car.location - self.location) <= 25:
return True
else:
return False

def collision_check(self, other_car):
if self.location == (other_car.location - 4):
self.speed = 0
return True
10 changes: 10 additions & 0 deletions traffic/road.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import numpy as np


class Road():
"""
Creates a 1,000m array that serves as an infinite road loop
"""

def __init__(self):
self.track = np.array(np.zeros((1, 1000)))
53 changes: 53 additions & 0 deletions traffic/simulation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

class Simulation():
"""
- Creates a simulation of 30 equidistant cars on a 1000m road
- Checks to see if cars collide and if so, makes cars stop
- Runs simulation for a 1 minute loop
"""

def __init__(self):
self.crash_count = 0

def car_iterate(self, roadway, car):
roadway.track[0, car.location] += 1
return roadway

def car_subtract(self, roadway, car):
roadway.track[0, car.location] -= 0
return roadway

def drive_cars(self, roadway, cars):
for car in cars:
self.car_subtract(roadway, car)
car.adjust_location()
self.car_iterate(roadway, car)
return roadway

def driving_rules(self, cars):
counter = 1
for car in cars:
if car.close_proximity_check(cars[counter]):
car.proximity(cars[counter])
if counter == (len(cars) - 1):
counter = 0
else:
counter += 1
return True

def car_collision(self, cars):
counter = 1
for car in cars:
if car.collision_check(cars[counter]):
self.crash_count += 1
if counter == (len(cars) - 1):
counter = 0
else:
counter += 1

def accelerate_cars(self, cars):
for car in cars:
car.accelerate()

def metric_conversion(num):
return num * (3600/1000)
75 changes: 75 additions & 0 deletions traffic_simulation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""TO DO: Still needs a lot of work with matplotlib, stats, reporting, and ipython notebook"""

import numpy as np
import matplotlib.pyplot as plt
from traffic.simulation import Simulation
from traffic.road import Road
from traffic.car import Car


def multiple_simulations(num_simulations=100):

output_car_speeds = np.array([]).reshape(0, 30)
output_tracks = np.array(np.zeros((1, 1000)))

for _ in range(num_simulations):
track_results, car_speeds = one_simulation()
output_car_speeds = np.vstack([output_car_speeds, [car_speeds]])
output_tracks = np.append(output_tracks, track_results, axis=0)

output_tracks = np.delete(output_tracks, 0, 0)
return output_tracks, output_car_speeds


def one_simulation(time=60):

car_list = car_factory()
sim = Simulation()

for _ in range(time):
output = loop(car_list, sim)

car_speeds = [car.speed for car in car_list]
return output, car_speeds


def loop(cars, simulation):

the_road = Road()
simulation.driving_rules(cars)
simulation.car_collision(cars)
simulation.drive_cars(the_road, cars)
simulation.accelerate_cars(cars)
return the_road.track


def car_factory(car_fleet=30):

car_list = []
counter = 33
for car in range(car_fleet):
car = Car()
car.location += counter
car_list.append(car)
counter += 33
return car_list


def reporting():

track_results, speed = multiple_simulations()
speed_mean = Simulation.metric_conversion(np.mean(speed))
speed_std = Simulation.metric_conversion(np.std(speed))
rec_speed = speed_mean + speed_std
plotting(track_results)
return rec_speed


def plotting(track_results):

x = track_results
plt.imshow(x, cmap="binary_r", interpolation="gaussian")
plt.show()


reporting()