Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Daniel Newell Traffic Simulations #14

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
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
307 changes: 307 additions & 0 deletions Traffic_Simulations.ipynb

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions car.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from road import Road
import random


class Car:

def __init__(self, desired_speed, speed, road, start_location):

self.speed = speed
self.road = road
self.location = start_location
self.desired_speed = desired_speed

def slow(self):
"""slows the car by 2 m/s"""
if self.speed > 1:
self.speed -= 2

def acc(self):
"""Accellerates the car by 2 m/s"""
if self.speed < self.desired_speed:
self.speed += 2

def check_location(self):
"""Makes sure car wont drive off the map"""
if self.location + self.speed <= self.road.length:
return True

def move_car(self):
"""Moves car up as many spaces as its speed"""
self.location += self.speed

def loop_car(self):
""" for cars at end of track puts it to a beginning location based on
speed"""

def adjust_ratio():
return (self.location + self.speed) - self.road.length
self.location = adjust_ratio()

def go(self):
"""Moves the cars after checking driving logic"""
if self.check_location():
self.move_car()
else:
self.loop_car()
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
numpy
matplotlib
seaborn
15 changes: 15 additions & 0 deletions road.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import numpy as np


class Road:

def __init__(self):

self.length = 999


class HardRoad(Road):

def __init__(self):

self.length = 6999
143 changes: 143 additions & 0 deletions simulation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
from car import Car
from road import Road
from road import HardRoad
import random
import statistics as st


class Simulation:

def __init__(self, road):

self.total_cars = 0
self.seconds = 0
self.traffic = []
self.road = road
self.snapshots = []
self.length = self.road.length

def make_cars(self, n, desired_speed, speed):
"""appends self.traffic with n number of class Car"""
counter = 0
for a_car in range(n):
a_car = Car(desired_speed, speed, self.road, counter)
counter += 30
self.traffic.append(a_car)

def is_first_car(self, car):
"""Checks if is the farthest ahead at beginning"""
if self.traffic.index(car) == len(self.traffic) - 1:
return True

def next_car(self, car):
"""returns the car in front of current car"""
if self.is_first_car(car):
return self.traffic[0]
else:
return self.traffic[self.traffic.index(car)+1]

def is_end_of_loop(self, car):
"""Checks if the trailing car is towards the end of the loop"""
if car.location > self.next_car(car).location:
return True

def nxt_loc(self, car):
"""gets the next location of a car"""
return car.location + car.speed

def loop_adjust(self, car):
return 1000 - car.location + car.speed

def end_of_loop_logic(self, car):
"""Checks collision dist for cars that are nearing 'end' of track"""
if self.nxt_loc(car) - self.length > self.nxt_loc(self.next_car(car)):
car.speed = self.next_car(car).speed
car.location = self.next_car(car).location - 1
elif self.next_car(car).location - self.loop_adjust(car) < 25:
car.slow()
else:
car.acc()

def normal_logic(self, car):
"""checks collision distance for cars"""

if self.nxt_loc(car) > self.nxt_loc(self.next_car(car)):
car.speed = self.next_car(car).speed
car.location = self.next_car(car).location - 1
elif self.next_car(car).location - car.location < 25:
if car.speed > 2:
car.speed -= 2
else:
car.acc()

def check_for_cars(self):
""""Checks if cars in front are too close"""
for car in self.traffic:
if self.is_end_of_loop(car):
self.end_of_loop_logic(car)
else:
self.normal_logic(car)

def acc_cars(self):
"""Increases cars speed if they are not at desired speed"""
for car in self.traffic:
if car.speed < self.optimal_speed:
car.acc()

def start(self):
"""runs all distance checks, acc, slowing and movement of all cars"""
counter = 0
while counter <= 60:
car_locations = []
counter += 1
self.check_for_cars()
self.random_slow(.10)
for cars in self.traffic:
car_locations.append((counter, cars.location))
cars.go()
self.snapshots.append(car_locations)

def random_slow(self, percent_chance):
"""adds a 10 percent chance to randomly slow for all cars"""
for car in self.traffic:
if random.random() <= percent_chance:
if car.speed > 1:
car.slow()


class HardMode(Simulation):

def rand(self, threshold):
"""Sets the oddsf or a slowed car"""
if random.random() < threshold:
return True

def random_slow(self):
"""Slows cars based on location"""

for car in self.traffic:
if 2000 > car.location > 999:
if self.rand(.4):
car.slow()
elif 4000 > car.location > 2999:
if self.rand(.99):
car.slow()
elif 6000 > car.location > 4999:
if self.rand(.2):
car.slow()
else:
if self.rand(.1):
car.slow()

def start(self):
"""runs all distance checks, acc, slowing and movement of all cars"""
counter = 0
while counter <= 600:
car_locations = []
counter += 1
self.check_for_cars()
self.random_slow()
for cars in self.traffic:
car_locations.append((counter, cars.location))
cars.go()
self.snapshots.append(car_locations)