Skip to content

Commit 368325b

Browse files
Optimized GUI, and it now has a higher refresh rate
1 parent c4ec633 commit 368325b

1 file changed

Lines changed: 30 additions & 30 deletions

File tree

controller.py

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,36 @@ def __init__(self, elevators, floors):
1414
for x in range(floors):
1515
self.called[x+1] = {"up": False, "down": False}
1616

17-
def assign_elevator(self):
18-
"""Monitors elevator calls and assigns an elevator that is either not busy or is able to service the call"""
17+
def monitor_calls(self):
18+
"""Monitors elevator calls"""
1919
while True:
20-
for elevator in self.elevators:
21-
if elevator.busy:
22-
if elevator.going_up:
23-
for floor in range(elevator.current_floor+1, self.floors+1):
24-
if self.called[floor]["up"]:
25-
elevator.go_to[floor] = True
26-
self.called[floor]["up"] = False
27-
else:
28-
for floor in range(elevator.current_floor-1, 0, -1):
29-
if self.called[floor]["down"]:
30-
elevator.go_to[floor] = True
31-
self.called[floor]["down"] = False
32-
else:
33-
now_busy = False
34-
for floor in range(1, self.floors+1):
35-
if self.called[floor]["up"]:
36-
elevator.go_to[floor] = True
37-
self.called[floor]["up"] = False
38-
now_busy = True
39-
break
40-
if now_busy:
41-
break
42-
for floor in range(self.floors, 0, -1):
43-
if self.called[floor]["down"]:
44-
elevator.go_to[floor] = True
45-
self.called[floor]["down"] = False
46-
break
20+
for floor in range(1, self.floors+1):
21+
for direction in ["up", "down"]:
22+
if self.called[floor][direction]:
23+
self.assign_elevator(floor, direction)
24+
25+
def assign_elevator(self, floor, direction):
26+
"""Assigns an elevator that is either not busy or is able to service the call"""
27+
assigned = None
28+
elevators = sorted(self.elevators, key=lambda e: abs(e.current_floor - floor))
29+
for elevator in elevators:
30+
if elevator.busy:
31+
# called to go up and elevator going up from lower floor
32+
if (direction == "up") and elevator.going_up and (elevator.current_floor < floor):
33+
elevator.go_to[floor] = True
34+
self.called[floor][direction] = False
35+
break
36+
# called to go down and elevator going down from higher floor
37+
elif (direction == "down") and not elevator.going_up and (elevator.current_floor > floor):
38+
elevator.go_to[floor] = True
39+
self.called[floor][direction] = False
40+
break
41+
else:
42+
# elevator has nothing to do, so give it something to do
43+
elevator.busy = True
44+
elevator.go_to[floor] = True
45+
self.called[floor][direction] = False
46+
break
4747

4848
def call_elevator(self, floor, direction):
4949
"""Calls an elevator from floor `floor` to the given direction ("up"/"down")"""
@@ -54,4 +54,4 @@ def run(self):
5454
for elevator in self.elevators:
5555
Thread(target=elevator.run).start()
5656
# Start the elevator assigner
57-
Thread(target=self.assign_elevator).start()
57+
Thread(target=self.monitor_calls).start()

0 commit comments

Comments
 (0)