def test_gen_end_events(self): e = Elevator(0, True) e.passenger_goals = [] pending_presses = [ ButtonPush(2, "DOWN", 0), ] schedule = BoringElevatorStrategy().get_plan([e], pending_presses, 0) self.assertEqual(schedule.elevator_to_floors[e], [2], "Stopping at floors in the wrong order!") events = schedule.event_gen_and_apply() e1 = heappop(events) self.assertEqual(e1.floor, 0) # Start event on floor 1 self.assertEqual(e1.button_press_handled.direction, "UP") # Start event on floor 1 e2 = heappop(events) self.assertEqual(e2.floor, 2) # End event on floor 2 e3 = heappop(events) self.assertEqual(e3.floor, 2) # Start event on floor 3 self.assertEqual(e3.button_press_handled.direction, "DOWN") # Start event on floor 3 self.assertEqual(len(events), 0, "Should have exhausted all events!")
def test_goes_to_zero(self): e = Elevator(5, False) e.passenger_goals = [] pending_presses = [] schedule = BoringElevatorStrategy().get_plan([e], pending_presses, 0) self.assertEqual(schedule.elevator_to_floors[e], [0], "Stopping at floors in the wrong order!")
def test_gen_start(self): e = Elevator(0, True) e.passenger_goals = [] pending_presses = [ ButtonPush(0, "UP", 0), ] schedule = BoringElevatorStrategy().get_plan([e], pending_presses, 0) self.assertEqual(schedule.elevator_to_floors[e], [0], "Stopping at floors in the wrong order!") events = schedule.event_gen_and_apply() e1 = heappop(events) self.assertEqual(e1.floor, 0) self.assertEqual(len(events), 0)
def test_insert(self): # Start on floor 0, with passengers on 3 and 6. # Ensure that 2, 4, and 8 get added correctly # and that 4 down gets added at the end e = Elevator() e.passenger_goals = [TravelGoal(0, 0, 3), TravelGoal(0, 0, 6)] pending_presses = [ ButtonPush(2, "UP", 3), ButtonPush(8, "UP", 2), ButtonPush(4, "UP", 1), ButtonPush(4, "DOWN", 0), ] schedule = BoringElevatorStrategy().get_plan([e], pending_presses, 0) self.assertEqual(schedule.elevator_to_floors[e], [2, 3, 4, 6, 8, 4], "Stopping at floors in the wrong order!")
def test_complicated(self): # Start on floor 0, with passengers on 3 and 6. # Ensure that 2, 4, and 8 get added correctly # and that 4 down gets added at the end e1 = Elevator() e1.passenger_goals = [ TravelGoal(0, 0, 3), ] e2 = Elevator(3, True) e2.passenger_goals = [] pending_presses = [ ButtonPush(2, "UP", 1), ButtonPush(8, "UP", 2), ButtonPush(4, "UP", 1), ButtonPush(4, "DOWN", 9), ButtonPush(6, "UP", 2), ButtonPush(2, "UP", 1), ButtonPush(1, "DOWN", 3), ButtonPush(6, "DOWN", 5), ButtonPush(8, "UP", 1), ButtonPush(2, "DOWN", 6), ] schedule = BoringElevatorStrategy().get_plan([e1, e2], pending_presses, 0) # Not sure if this is right.. Just to make sure that if there are changes I notice them. self.assertEqual(schedule.elevator_to_floors[e1], [3, 6, 8, 6, 2, 1], "Stopping at floors in the wrong order!") self.assertEqual(schedule.elevator_to_floors[e2], [2, 8, 2, 4], "Stopping at floors in the wrong order!")
def test_insert_2_elevators(self): # Start on floor 0, with passengers on 3 and 6. # Ensure that 2, 4, and 8 get added correctly # and that 4 down gets added at the end e1 = Elevator() e1.passenger_goals = [ TravelGoal(0, 0, 3), ] e2 = Elevator(3) # Going down now (but stopped) e2.passenger_goals = [ TravelGoal(0, 6, 0), TravelGoal(0, 4, 1), ] pending_presses = [ ButtonPush(2, "UP", 0), ButtonPush(8, "UP", 2), ButtonPush(4, "UP", 1), ButtonPush(4, "DOWN", 0), ] schedule = BoringElevatorStrategy().get_plan([e1, e2], pending_presses, 0) self.assertEqual(schedule.elevator_to_floors[e1], [2, 3, 4], "Stopping at floors in the wrong order!") self.assertEqual(schedule.elevator_to_floors[e2], [1, 0, 8], "Stopping at floors in the wrong order!")
def __init__(self, travel_behavior: TravelBehavior, strategy: ElevatorStrategy, num_elevators=3): self.strategy = strategy self.travel_behavior = travel_behavior self.num_elevators = num_elevators # Init Mutable state: # IF IT'S NOT ONE OF THESE THINGS, IT SHOULD NOT BE MUTABLE! self.elevators = [ Elevator(passenger_goals=set()) for _ in range(self.num_elevators) ] self.current_time = 0 self.pending_elevator_events = [] # heap of pending events self.pending_button_presses = {} # ButtonPress to TravelGoal self.current_schedule = None # an ElevatorSchedule object generated from the strategy self.completed_goals = [] self.elevator_history = {e: [] for e in self.elevators}
self.total_dist) def __repr__(self): return "Wait (avg: {} max: {}). Total: (avg: {} max: {}). Dist={}".format( self.avg_wait, self.max_wait, self.avg_total, self.max_total, self.total_dist) if __name__ == '__main__': goal1 = TravelGoal(5, 0, 10) goal1.board_time = 7 goal1.finish_time = 15 goal2 = TravelGoal(10, 10, 0) goal2.board_time = 15 goal2.finish_time = 30 elevator1 = Elevator() elevator1_stops = [(0, 1, 1), (5, 10, 5), (10, 10, 5)] elevator2 = Elevator() elevator2_stops = [(10, 1, 1), (5, 10, 5), (1, 10, 5)] stats = RunStats([goal1, goal2], { elevator1: elevator1_stops, elevator2: elevator2_stops }) print(stats)
ElevatorPhysicsCalculator.time_to(10, 6) print(ElevatorPhysicsCalculator(-4 * 3.9, 0).delta_t) # e1 = Elevator(is_stopped=False) # e1.last_start_event = ElevatorStart(e1, 0, 0, ElevatorStop(e1, ElevatorPhysicsCalculator.time_to(0, 2), 2)) # e2 = Elevator(location=10, is_stopped=False) # e2.last_start_event = ElevatorStart(e2, .5, 10, ElevatorStop(e1, .5 + ElevatorPhysicsCalculator.time_to(10, 4), 4)) # e3 = Elevator() # stops = { # e1: [5, 2, 8, 1], # e2: [6, 1, 0, 5, 8, 2], # e3: [90], # } # schedule = ElevatorSchedule(stops, {e1: "DOWN", e2: "UP", e3: "DOWN"}, 1.5) e1 = Elevator() events = schedule.event_gen_and_apply() while len(events) > 0: print(heappop(events)) # schedule.event_gen_and_apply(0) # print("Start: T = {}, p = {:.4}, v = {:.4}".format(0, float(e1.location), float(e1.velocity))) # print("Start: T = {}, p = {:.4}, v = {:.4}".format(0, float(e2.location), float(e2.velocity))) # print("StarT: T = {}, p = {:.4}, v = {:.4}".format(0, float(e3.location), float(e3.velocity))) # schedule.update_elevator_state(.4999999) # print("Start: T = {}, p = {:.4}, v = {:.4}".format(.5, float(e1.location), float(e1.velocity))) # print("Start: T = {}, p = {:.4}, v = {:.4}".format(.5, float(e2.location), float(e2.velocity))) # print("StarT: T = {}, p = {:.4}, v = {:.4}".format(.5, float(e3.location), float(e3.velocity))) # print("Start: T = {}, p = {:.4}, v = {:.4}".format(1, float(e1.location), float(e1.velocity))) # print("Start: T = {}, p = {:.4}, v = {:.4}".format(1, float(e2.location), float(e2.velocity)))
from elevate.ButtonPush import ButtonPush from elevate.Elevator import Elevator from elevate.Events import ElevatorStart, ElevatorStop from elevate.PhysicsCalculator import ElevatorPhysicsCalculator from elevate.Schedule import ElevatorSchedule from elevate.TravelGoal import TravelGoal from elevate.strategies.BoringElevatorStrategy import BoringElevatorStrategy ''' A PriorBeliefElevatorStrategy is used to follow the Boring elevator strategy with some side constraints - a list of suggested stops passed in at creation time. ''' if __name__ == "__main__": # We have 3 elevators: 1 going up, 1 soon to be going down, and one stopped. e1 = Elevator(is_stopped=True, location=10) e1.last_start_event = ElevatorStart(e1, -8, 12, ElevatorStop(e1, ElevatorPhysicsCalculator.time_to(12, 10)-8, 10)) e1.passenger_goals = [ TravelGoal(time=-8, start_floor=12, end_floor=5), TravelGoal(time=-2, start_floor=10, end_floor=5) ] e2 = Elevator(is_stopped=False, location=1,) e2.last_start_event = ElevatorStart(e2, 0, 1, ElevatorStop(e2, ElevatorPhysicsCalculator.time_to(1, 6), 6)) e2.passenger_goals = [ TravelGoal(time=0, start_floor=1, end_floor=10), TravelGoal(time=0, start_floor=1, end_floor=6) ] e3 = Elevator(is_stopped=True, location=1) e3.passenger_goals = []