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

Traffic Assignment from Hell! #4

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
300 changes: 300 additions & 0 deletions traffic_easy.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,300 @@
{
"metadata": {
"name": "",
"signature": "sha256:11883685a9b05ea8471e57f002d8020117d45525369d6b6fd5cabe7fc048f679"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"import math\n",
"import random\n",
"import matplotlib.pyplot as plt\n",
"import statistics as st\n",
"import numpy as np\n",
"from copy import deepcopy"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%matplotlib inline"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"class Road():\n",
" \"\"\" Responsible for storing\n",
" - Road Length\n",
" - slowing chance\n",
" - Info on where each car is.\n",
" \"\"\"\n",
" def __init__(self, length, slowing_chance = 0):\n",
" self.length = length\n",
" self.slowing_chance = slowing_chance\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"class Car():\n",
" \"\"\" Responsible for storing information\n",
" - Speed\n",
" - Desired Speed\n",
" - Acceleration Rate\n",
" - Desired distance to car in front\n",
" - Car length\n",
" - Slowing Chance\n",
" Methods:\n",
" - Move\n",
" - Collision\n",
" - Distance\n",
"\"\"\"\n",
" def __init__(self, \n",
" x,\n",
" acceleration=2,\n",
" length=5, \n",
" slowing_chance=.1,\n",
" desired_distance = 25,\n",
" desired_speed = (120*1000)/(60*60)):\n",
" self.x = x\n",
" self.acceleration = acceleration\n",
" self.length = length\n",
" self.slowing_chance = slowing_chance\n",
" self.desired_distance = desired_distance\n",
" self.desired_speed = desired_speed\n",
" self.speed = 0\n",
" \n",
" \n",
" def accelerate(self):\n",
" return min(self.speed + self.acceleration,self.desired_speed)\n",
" \n",
"\n",
" def randomly_deaccelerate(self):\n",
" if random.random() <= self.slowing_chance:\n",
" self.speed = max(0, self.speed - self.acceleration) \n",
"\n",
" \n",
" def move_ahead(self, x, speed): \n",
" self.x = x + speed\n",
"\n",
" \n",
" def stop_car(self):\n",
" self.speed = 0\n",
" \n",
" \n",
" def show_car(self):\n",
" return \"I am at position {} with speed {}\".format(self.x,self.speed)\n",
" \n",
" def distance(self, car):\n",
" return car.x - self.x"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"class Simulation:\n",
" # Simulation object receives 3 arguments: the road, the number of cars to put on the road\n",
" # and how many seconds to run the simulation for\n",
" \n",
" def __init__(self, road, number_of_cars, sim_length):\n",
" \n",
" self.road = road\n",
" self.cars = number_of_cars\n",
" self.cars_on_road = []\n",
" \n",
" def add_initial_cars(self):\n",
" distance = self.road.length//self.cars\n",
" row = []\n",
" for x in range(0, self.cars):\n",
" row.append(Car(x*distance))\n",
" self.update_cars_on_road(row)\n",
" \n",
" def update_cars_on_road(self,newrow):\n",
" self.cars_on_road.append(newrow)\n",
" \n",
" def get_row(self,ind):\n",
" return self.cars_on_road[ind]\n",
" \n",
" def return_my_x(self):\n",
" return [self.cars_on_road[row_ind][car_ind].x \n",
" for row_ind in range(len(self.cars_on_road))\n",
" for car_ind in range(len(self.cars_on_road[row_ind]))]\n",
" \n",
" def return_my_speed(self):\n",
" return [self.cars_on_road[row_ind][car_ind].speed \n",
" for row_ind in range(len(self.cars_on_road))\n",
" for car_ind in range(len(self.cars_on_road[row_ind]))]\n",
" \n",
" def print_my_cars(self, car_list):\n",
" for row_ind in range(len(car_list)):\n",
" for ind in range(len(car_list[row_ind])):\n",
" print(\"row-ind\",row_ind,\" car\",ind,\" \", car_list[row_ind][ind].show_car())\n",
" \n",
" \n",
" def next_car_in_sim(self, position):\n",
" if position == self.cars - 1:\n",
" return 0\n",
" else: \n",
" return position + 1\n",
" \n",
" \n",
" def calc_wrap_around(self,car_list):\n",
" # Moves last car to the beginning of the list ro simulate the\n",
" # circular movement\n",
" temp_list = deepcopy(car_list)\n",
" last_car = temp_list[self.cars -1] \n",
" for ind in range(self.cars-2,-1, -1):\n",
" temp_list[ind+1] = temp_list[ind]\n",
" temp_list[0] = last_car\n",
" return temp_list\n",
" \n",
" \n",
" def get_moved_list(self,car_list):\n",
" new_car_pos= deepcopy(car_list)\n",
" #wrap_around will let me know when I need to simulate the circular road\n",
" wrap_around = False\n",
" #I will start moving each car starting with the last car\n",
" for car_ind in range(self.cars-1, -1, -1):\n",
" #Start accelerating. Accelerate will increase spped if less than max desired speed\n",
" new_car_pos[car_ind].speed = new_car_pos[car_ind].accelerate()\n",
" new_x = new_car_pos[car_ind].x + new_car_pos[car_ind].speed \n",
" #The last car is managed different than the rest\n",
" if car_ind == self.cars - 1:\n",
" #print(\"moving last car\")\n",
" if new_x < self.road.length:\n",
" #print (\"i could move it\")\n",
" new_car_pos[car_ind].x = new_x\n",
" else: #if my new_x takes me out of the road\n",
" #print(\"out of road, old x is\",new_x)\n",
" new_x = new_x - self.road.length\n",
" #print(\"out of road, nex x will be\",new_x)\n",
" #it cannot move into the first position on the list\n",
" if new_x < new_car_pos[0].x - new_car_pos[car_ind].desired_distance:\n",
" #print (\"newx\",new_x,\"car 0 is in\",new_car_pos[0].x,\"and desired dist\",new_car_pos[car_ind].desired_distance)\n",
" #print(\"last car too close to first\")\n",
" if new_car_pos[0].speed < new_car_pos[car_ind].speed: \n",
" new_car_pos[car_ind].speed = new_car_pos[0].speed\n",
" else: #car can move to first spot on road\n",
" wrap_around = True\n",
" #print(\"i set wrap\")\n",
" new_car_pos[car_ind].x = new_x\n",
" else:#car is not last on row\n",
" if new_x < new_car_pos[self.next_car_in_sim(car_ind)].x - new_car_pos[car_ind].desired_distance:\n",
" if new_car_pos[self.next_car_in_sim(car_ind)].speed < new_car_pos[car_ind].speed:\n",
" new_car_pos[car_ind].speed = new_car_pos[self.next_car_in_sim(car_ind)].speed\n",
" # print(\"too close\")\n",
" else:\n",
" new_car_pos[car_ind].x = new_x\n",
" # print(\"i could move\")\n",
" # Deaccelerates randomly\n",
" new_car_pos[car_ind].randomly_deaccelerate() \n",
" if wrap_around == True:\n",
" new_car_pos = deepcopy(self.calc_wrap_around(new_car_pos))\n",
" \n",
" return new_car_pos\n",
" \n",
" "
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def trial():\n",
" road = Road(1000)\n",
" cars = 30\n",
" sim_length = 60\n",
" sim = Simulation(road, cars, sim_length)\n",
" sim.add_initial_cars()\n",
" for time in range(sim_length-1):\n",
" row_to_move = deepcopy(sim.cars_on_road[time])\n",
" row = deepcopy(sim.get_moved_list(row_to_move))\n",
" sim.update_cars_on_road(row)\n",
" my_x = sim.return_my_x()\n",
" my_speed = sim.return_my_speed()\n",
" myspeed=np.array(my_speed) \n",
" return np.mean(myspeed)\n",
" \n",
"print (trial())"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"25.4559259259\n"
]
}
],
"prompt_number": 13
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"means = []\n",
"for r in range(1000):\n",
" means.append(trial())\n",
" \n",
"print (st.mean(means))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"25.1446348148\n"
]
}
],
"prompt_number": 16
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Loading