diff --git a/README.md b/README.md index 5cf5091..075011b 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/requirements.txt b/requirements.txt new file mode 100755 index 0000000..666ef24 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +ipython[all] +numpy +random +matplotlib diff --git a/road.png b/road.png deleted file mode 100644 index b90c241..0000000 Binary files a/road.png and /dev/null differ diff --git a/traffic/car.py b/traffic/car.py new file mode 100755 index 0000000..4485666 --- /dev/null +++ b/traffic/car.py @@ -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 diff --git a/traffic/road.py b/traffic/road.py new file mode 100755 index 0000000..06bb896 --- /dev/null +++ b/traffic/road.py @@ -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))) diff --git a/traffic/simulation.py b/traffic/simulation.py new file mode 100644 index 0000000..768b91f --- /dev/null +++ b/traffic/simulation.py @@ -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) diff --git a/traffic_simulation.py b/traffic_simulation.py new file mode 100755 index 0000000..bd62b43 --- /dev/null +++ b/traffic_simulation.py @@ -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()