-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathelevator_controller.py
27 lines (22 loc) · 1.02 KB
/
elevator_controller.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
from threading import Thread
from elevator import Elevator
from request import Request
class ElevatorController:
def __init__(self, num_elevators: int, capacity: int):
self.elevators = []
for i in range(num_elevators):
elevator = Elevator(i + 1, capacity)
self.elevators.append(elevator)
Thread(target=elevator.run).start()
def request_elevator(self, source_floor: int, destination_floor: int):
optimal_elevator = self.find_optimal_elevator(source_floor, destination_floor)
optimal_elevator.add_request(Request(source_floor, destination_floor))
def find_optimal_elevator(self, source_floor: int, destination_floor: int) -> Elevator:
optimal_elevator = None
min_distance = float('inf')
for elevator in self.elevators:
distance = abs(source_floor - elevator.current_floor)
if distance < min_distance:
min_distance = distance
optimal_elevator = elevator
return optimal_elevator