Skip to content

Instantly share code, notes, and snippets.

@steven-tey
Created October 8, 2019 16:26
Show Gist options
  • Save steven-tey/e1b24bb5322ee7ecd2a1587343bd02af to your computer and use it in GitHub Desktop.
Save steven-tey/e1b24bb5322ee7ecd2a1587343bd02af to your computer and use it in GitHub Desktop.
CS166 Session 5.1 Pre-Class Work
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Modify the Code from Session 4.2 to Model a Two-Lane Freeway Scenario"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import random"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"..3...2..0.5....................1.1.03.....03...5.....3...3...........04......3...3......2...04.....\n",
".2.1....01.....5..3.......4.0.....3...3.........23.....................34.0....2.01...1.....4.......\n",
"['_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_']\n",
".....2..01......4................1.01...2..0...3.....2...3...4........0....4.....3...3.....1.0....4.\n",
"...21...0.1.........53......0.4......3...3........2.3.....................0.4....0.1...1........4...\n",
"['_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_']\n",
"..3....00.1.........5.............00.2....01......3....3....4....4....1........4....3...3...01......\n",
"4....1..0..1............35..0.....4.....3...3.......2..3..................0.....40..1...1...........\n",
"['_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_']\n",
".....1.00..2.............5........00...1..1.2........4....4.....4....1.1...........3...2...00.2.....\n",
"....4.1.0...1..............30.5.......4....3...3......2...3...............0......0..41...1..........\n",
"['_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_']\n",
"......001....2................2...00....1..1..2..........4....4.....0.1.2.............1..0.01...3...\n",
".......10....1..............0.3....5......4...3...3.....2....3............0......0....1.4.1.........\n",
"['_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_']\n",
"......01.2.....3................0.01.....1..1...2............3....0.0..2..3............0.1.0.2.....3\n",
"........0.....1.............0....3......5.....4..3...3....2.....3.........0......0.....1...14.......\n",
"['_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_']\n",
"..2...1.2..2......4.............0.0.2.....2..2....3.............1.0.1....2...4.........0..01...2....\n",
"........0......1............0.......3........5....4.3...3...2......3......0......0......1...1...4...\n",
"['_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_']\n",
"....2..1..2..2........5.........1.0...3.....1..2.....4...........00..2.....2.....5.....0..1.1....2..\n",
"4.......0.......1...........0..........3..........5...43...3..2.......3...0......0.......1...1......\n",
"['_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_']\n",
"......0.1...1..2...........4.....00......2...1...3.......4.......00....3.....2........01...0.1.....3\n",
"....4...0........1..........0.............3............5..3...3.2........30......0........1...1.....\n",
"['_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_']\n",
"..3...0..2...1...3.............1.00........2..1.....3........3...01.......3....2......1.2..1..2.....\n",
"........0.........1.........0................3..............53...32.......0.3....0.........1...1....\n",
"['_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_']\n",
".....01....2..2.....3...........001..........0.1.......4........00.1.........3...2.....1..0.1...3...\n",
"........0..........1........0...................3...............35..2.....0....3.0..........1...1...\n",
"['_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_']\n",
".....1.2.....1..2......3........01.2.........1..1..........3....01..2...........2..2....0.0..1.....3\n",
"........0...........1.......0......................3...............3..2...0......03..........1...1..\n",
"['_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_']\n",
"..2...2..2....1...2.......4.....1.1..3........1..1............1.1.2...3...........2..1..0.0...1.....\n",
"........0............1......0.........................3...............3.2.0......0...3........1...1.\n",
"['_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_']\n",
"....3...1..2...2....2.........2..0.2....4......1..2............0.1..3....4..........0.0.0.0....2....\n",
"........0.............1.....0............................3...............30......0......3......1...1\n",
"['_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_']\n",
".......0.1...3...2....2.........00...3......2...1...3..........1..2....4.....4......0.1.0.0......2..\n",
"1.......0..............1....0...............................3.............0.3....0.........3....1...\n",
"['_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_']\n",
".......0..2.....2..3....2.......01......4.....2..1.....3........1...2......5.....2..1..00.1........3\n",
".1......0...............1...0..................................3..........0....3.0............3..1..\n",
"['_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_']\n",
"..4....0....2.....3...3...3.....1.1.........2...1.1.......3......2....3.........1..0.1.01..2........\n",
"..1.....0................1..0.....................................3.......0......03..............31.\n",
"['_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_']\n",
"......01......2......3...3...2...1.2..........1..1.2.........4.....3.....4.......1.1..00.1...2......\n",
"3..1....0.................1.0........................................3....0......0...3.............1\n",
"['_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_']\n",
"......0.2.......2.......2...2..1..1..3.........2..1..2...........3....4......4....1.0.01..1....2....\n",
"1..31...0..................10...........................................3.0......0......3...........\n",
"['_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_']\n",
"......0...3.......2.......3...1.1..2....4........0.2...3............3.....5......0.00.0.1..2.....3..\n",
".1...13.0...................0.............................................03.....0.........3........\n",
"['_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_']\n",
"3.....1......4......3........1.1.1...2......3....1...2....4............4.......0.0.00.0..2...3......\n",
"..1...1.03..................0.............................................0...3..0............3.....\n",
"['_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_']\n",
"...2...2.........5.....4......0.1.2....3.......2..2....3......5............2...1.1.00.1....3....3...\n",
"...1...10...3...............0.............................................0......0...............3..\n",
"['_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_']\n",
".....3...2............4....1..0..1..2.....3......2..2.....4........5.........1..1.001..1......3....4\n",
"3...1...0......3............0.............................................0......0..................\n",
"['_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_']\n",
"...4....2..2..............0.1.1...1...3......4.....1..3.......5.........5.....1..000.2..1........3..\n",
"...3.1..0.........3.........0.............................................0......0..................\n",
"['_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_']\n",
"3......1..1..3............0..1.2...1.....3.......2..2....3.........5.........1.0.001...0.2..........\n",
"......1.0............3......0.............................................0......0..................\n",
"['_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_']\n",
"...3....1..1....4.........0...2..2..1.......4......1..3.....3...........5.....00.01.2..0...2........\n",
".......10...............3...0.............................................0......0..................\n",
"['_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_']\n",
"......1..2..1.......4.....1.....1..0.1..........2...1....3.....4.............001.1.2..01.....2......\n",
"........0..................30.............................................0......0..................\n",
"['_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_']\n",
".......1...0.1..........1..1.....0.1..2...........2..2......3......5.........00.0.1..01.1......3....\n",
"........0...................0.3...........................................0......0..................\n",
"['_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_']\n",
"........1..0..2..........2..1....1..2...2...........2..3.......4........3....00.0..1.1.1.1........3.\n",
"........0...................0....3........................................0......0..................\n",
"['_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_']\n",
".3.......1.1....3..........0.2....2...3...2...........2...3........4.......0.01.0...1.1.1.2.........\n",
"........0...................0.......3.....................................0......0..................\n",
"['_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_']\n"
]
}
],
"source": [
"class TrafficSimulation():\n",
" \n",
" def __init__(self, length = 100, density = 0.2, max_velocity = 5, slow_prob = 0.5, switch_prob = 0.5):\n",
" # Here, we define the model parameters (road length, traffic density, maximum velocity, \n",
" # probability of slowing down) \n",
" self.length = length # The length of the road (number of cells)\n",
" self.density = density # The density of traffic\n",
" self.max_velocity = max_velocity # The maximum speed of the cars\n",
" self.slow_prob = slow_prob # The probability of slowing down for no apparent reason\n",
" self.switch_prob = switch_prob # The probability of switching lanes\n",
" self.state = -np.ones(self.length, dtype=int) # \"-1\" represents an empty cell\n",
" filled_cells = np.random.choice(range(self.length), size=int(round(density * self.length)), replace=False)\n",
" self.state[filled_cells] = np.random.randint(0, self.max_velocity + 1, size=len(filled_cells))\n",
" # Building the other lane in a two-lane freeway model\n",
" self.sister = -np.ones(self.length, dtype=int)\n",
" filled_cells = np.random.choice(range(self.length), size=int(round(density * self.length)), replace=False)\n",
" self.sister[filled_cells] = np.random.randint(0, self.max_velocity + 1, size=len(filled_cells))\n",
" \n",
" \n",
" def step(self, display = True): \n",
" for i in range(self.length):\n",
" if self.state[i] != -1: # We only change the speed if there's a car in the cell\n",
" dist = 0 # Algorithm that scans the grids in front of the car until it finds the next car\n",
" while self.state[(i + (dist + 1)) % self.length] == -1:\n",
" dist += 1\n",
" # Here we will code for the 3 rules for speed change in the Nagel-Schreckenberg model\n",
" if dist > self.state[i] and self.state[i] < self.max_velocity:\n",
" self.state[i] += 1\n",
" if dist < self.state[i]:\n",
" self.state[i] = dist \n",
" if self.state[i] > 0 and np.random.random() < self.slow_prob:\n",
" self.state[i] -= 1 \n",
" \n",
" if display == True: # Display the model\n",
" self.display()\n",
" \n",
" # Here we are coding for the last rule in the Nagel-Schrekenberg model - the movement rule\n",
" next_state = -np.ones(self.length, dtype=int) \n",
" for i in range(self.length):\n",
" if self.state[i] != -1:\n",
" next_state[(i + self.state[i]) % self.length] = self.state[i]\n",
" self.state = next_state \n",
" \n",
" next_sister = -np.ones(self.length, dtype=int) \n",
" for i in range(self.length):\n",
" if self.sister[i] != -1:\n",
" next_sister[(i + self.sister[i]) % self.length] = self.sister[i]\n",
" self.sister = next_sister \n",
" \n",
"\n",
" def changeLane(self):\n",
" for i in range(self.length):\n",
" if self.state[i] != -1: # We only change the speed if there's a car in the cell\n",
" dist = 0\n",
" dist_sister_front = 0\n",
" dist_sister_back = 0\n",
" while self.state[(i + (dist + 1)) % self.length] == -1:\n",
" dist += 1\n",
" while self.sister[(i + (dist_sister_front + 1)) % self.length] == -1:\n",
" dist_sister_front += 1\n",
" while self.sister[(i - (dist_sister_back + 1)) % self.length] == -1:\n",
" dist_sister_back +=1\n",
" # Here we will code for the 5 rules for lane change in the Nagel-Schreckenberg model\n",
" if dist >= self.state[i]+1 and dist_sister_front >= self.state[i]+1 and dist_sister_back >= self.max_velocity and np.random.random() < self.switch_prob and np.random.random() > self.slow_prob: \n",
" self.sister[i] = self.state[i]\n",
" self.state[i] = -1\n",
" else: \n",
" self.state[i] = self.state[i]\n",
" \n",
"\n",
"\n",
" def display(self):\n",
" print(''.join('.' if x == -1 else str(x) for x in self.state))\n",
" print(''.join('.' if x == -1 else str(x) for x in self.sister))\n",
" print(['_' for _ in range(20)]) #separate between two different time steps\n",
" \n",
"sim = TrafficSimulation()\n",
"for i in range(30):\n",
" sim.step() "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Questions from Pre-Class Work"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"## Question 1\n",
"\n",
"Initial State:\n",
"...................\n",
".......2...2.......\n",
"\n",
"Next State:\n",
"\n",
"..........3...3....\n",
"...................\n",
"\n",
"Next Next State:\n",
"\n",
".............3.....\n",
"..................4"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"## Question 2\n",
"\n",
"Initial State:\n",
"...................\n",
"........2..2.......\n",
"\n",
"Next State:\n",
"\n",
"..............3....\n",
"..........2........\n",
"\n",
"Next Next State:\n",
"\n",
".............3.....\n",
"..................4"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"## Question 3\n",
"\n",
"Initial State:\n",
".....1.............\n",
"........2..2.......\n",
"\n",
"Next State:\n",
"\n",
"..............3....\n",
".......2..2........\n",
"\n",
"Next Next State:\n",
"\n",
".............3.....\n",
".........2........4"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment