-
Notifications
You must be signed in to change notification settings - Fork 0
/
elevator.py
76 lines (58 loc) · 2.09 KB
/
elevator.py
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
68
69
70
71
72
73
74
75
76
import random
class Elevator:
# Only supports single destination
directions = ["up", "down", "idle"]
# Dunder methods
def __init__(self, floors):
self.floors = floors
cur_floor = random.randint(floors[0], floors[-1])
self.origin = cur_floor
self.location = cur_floor
self.destination = cur_floor
self.direction = self.directions[-1]
def __repr__(self):
# A fun __repr__ of elevator loc: 1 2 3 4 # 6 7 8 9 10
return "\t".join(
["#" if floor == self.location else str(floor) for floor in self.floors]
)
"""
Public methods used by Controller
"""
def pickup(self, floor):
# Called by controller to set new pickup loc
self._dispatch(floor)
def dropoff(self, floor):
# Called by controller when user inputs desired dropoff loc
self._dispatch(floor)
def is_idle(self):
# Used by controller to find all idle elevators
return self.direction == "idle"
def distance(self, floor):
# Calculates distance between self and floor
return abs(floor - self.location) + self._penalty(floor)
"""
Private methods used within class only
"""
def _dispatch(self, floor):
# Sets internal state on elevator
if floor < self.location:
desired_direction = "down"
elif floor > self.location:
desired_direction = "up"
else:
desired_direction = "idle"
self.direction = desired_direction
self.destination = floor
# TODO dispatch elevator on different thread perhaps
def _arrived(self, floor):
# Called after each evolution to check if location == destination, if so set internal state
pass
def _penalty(self, floor):
# Calculate penalty for travelling in wrong direction as pickup loc
delta = floor - self.location
return (
2
if ((delta < 0) and self.direction == "up")
or ((delta > 0) and self.direction == "down")
else 0
)