-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroad.py
More file actions
67 lines (55 loc) · 2.69 KB
/
road.py
File metadata and controls
67 lines (55 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""This module defines the road class."""
import constants
import distributions
import random
import spot
import traffic_light
class Road(object):
"""A class representing a road. A road holds traffic lights and cars."""
def __init__(self, color, distribution):
"""Initialize the road with length constants.ROAD_LENGTH"""
self._spots = [spot.Spot() for _ in xrange(0, constants.ROAD_LENGTH)]
self._spots[constants.CROSSING_LOCATION - 1].add_light(color, 0)
self._spots[constants.CROSSING_LOCATION + constants.NUM_LANES].add_light(color, 1)
self._steps = 0
self._num_queued = 0
self._car_distro = distributions.Probability()
self._distribution = distribution
def update(self):
"""Update the cars on the road to move in a time increment."""
self._spots[constants.ROAD_LENGTH - 1].remove_car(0)
self._spots[0].remove_car(1)
for i in xrange(constants.ROAD_LENGTH - 1):
self._num_queued += (self._spots[constants.ROAD_LENGTH - i - 2].
update_spot(self._spots[constants.ROAD_LENGTH - i - 1], 0))
self._num_queued += self._spots[i + 1].update_spot(
self._spots[i], 1)
self.create_car(self._distribution)
self._steps += 1
def has_car(self, i, lane_index):
"""Returns whether spot i on the road has a car."""
return self._spots[i].has_car(lane_index)
def light_color(self):
"""Returns the color of the traffic light on this road."""
return self._spots[constants.CROSSING_LOCATION - 1].light_color()
def crossing_location(self):
"""Returns the location of the traffic light on this road."""
return constants.CROSSING_LOCATION
def flip_color(self):
"""Change the color of the traffic lights on this road."""
self._spots[constants.CROSSING_LOCATION - 1].flip_color()
self._spots[constants.CROSSING_LOCATION + constants.NUM_LANES].flip_color()
def get_amount_queued(self):
"""Return the total number of steps queued over the whole road"""
return self._num_queued
def reset_queueing(self):
"""Reset the queueing counters for each spot"""
self._num_queued = 0
def create_car(self, distribution):
"""Return true if a car should be created"""
for (lane_index, spot_index) in [(0, 1), (1, constants.ROAD_LENGTH - 1)]:
if distribution == distributions.Probability.STANDARD:
if self._steps % (random.randint(0, 9) + 5) == 0:
self._spots[spot_index].add_car(lane_index)
elif self._car_distro.get_value(distribution):
self._spots[spot_index].add_car(lane_index)