Пример #1
0
    def test_dispatch_invalid(self, mock_uniformgenerator):
        """
        ignore the invalid dispatch (cannot stop at the dispatch)
        and decelerate when needed (test velocity_planner)
        """
        max_floors = 8
        # mansion_config
        world = MansionConfig(dt=0.50,
                              number_of_floors=max_floors,
                              floor_height=4.0)

        # test_elevator
        test_elevator = Elevator(start_position=0.0,
                                 mansion_config=world,
                                 name="test_eleavtor")
        test_elevator._direction = 1
        test_elevator._current_velocity = 2.0
        test_elevator._current_position = 8.0  # currently at 3 floor
        test_elevator._target_floors = [5, 8]  # target 5 floor
        test_elevator._loaded_person = [
            list() for i in range(test_elevator._number_of_floors)
        ]
        test_elevator._loaded_person[4].append(
            PersonType(6, 40, 1, 5, world.raw_time))
        test_elevator._loaded_person[7].append(
            PersonType(7, 40, 1, 8, world.raw_time))
        test_elevator._load_weight = 80

        # test_mansion
        tmp_uniform_generator = UniformPersonGenerator()

        ret_person = []
        person_generators.uniform_generator.UniformPersonGenerator.generate_person = mock.Mock(
            return_value=(ret_person))

        test_mansion = MansionManager(elevator_number=1,
                                      person_generator=tmp_uniform_generator,
                                      mansion_config=world,
                                      name="test_mansion")
        test_mansion._elevators = [test_elevator]
        dispatch = []
        dispatch.append(ElevatorAction(3, 1))

        test_mansion.run_mansion(dispatch)
        state = test_mansion.state
        self.assertAlmostEqual(state.ElevatorStates[0].Velocity, 2.0)

        test_mansion.run_mansion(dispatch)
        state = test_mansion.state
        self.assertAlmostEqual(state.ElevatorStates[0].Velocity,
                               2.0)  # ignore the invalid dispatch

        for i in range(5):
            test_mansion.run_mansion(dispatch)
        test_mansion.run_mansion(dispatch)
        test_mansion.run_mansion(dispatch)
        test_mansion.run_mansion(dispatch)
        state = test_mansion.state
        self.assertAlmostEqual(state.ElevatorStates[0].Velocity, 0.0)
Пример #2
0
    def test_no_dispatch(self, mock_uniformgenerator):
        """
        arrive at the target, no dispatch, hold still
        """
        max_floors = 8
        # mansion_config
        world = MansionConfig(dt=0.50,
                              number_of_floors=max_floors,
                              floor_height=4.0)

        # test_elevator
        test_elevator = Elevator(start_position=0.0,
                                 mansion_config=world,
                                 name="test_eleavtor")
        test_elevator._direction = 1
        test_elevator._current_velocity = 0
        test_elevator._current_position = 8.0  # currently at 3 floor
        test_elevator._target_floors = [3]  # target 3 floor
        test_elevator._loaded_person = [
            list() for i in range(test_elevator._number_of_floors)
        ]
        test_elevator._loaded_person[2].append(
            PersonType(0, 40, 1, 3, world.raw_time))
        test_elevator._load_weight = 40

        # test_mansion
        tmp_uniform_generator = UniformPersonGenerator()

        ret_person = []
        person_generators.uniform_generator.UniformPersonGenerator.generate_person = mock.Mock(
            return_value=(ret_person))

        test_mansion = MansionManager(elevator_number=1,
                                      person_generator=tmp_uniform_generator,
                                      mansion_config=world,
                                      name="test_mansion")
        test_mansion._elevators = [test_elevator]
        dispatch = []
        dispatch.append(ElevatorAction(-1, 0))

        test_mansion.run_mansion(dispatch)
        test_mansion.run_mansion(dispatch)  # open the door

        for i in range(4):
            test_mansion.run_mansion(dispatch)  # unload person 0

        test_mansion.run_mansion(dispatch)
        test_mansion.run_mansion(dispatch)  # close the door

        test_mansion.run_mansion(dispatch)
        test_mansion.run_mansion(dispatch)
        state = test_mansion.state
        self.assertAlmostEqual(state.ElevatorStates[0].DoorState, 0.0)
        self.assertAlmostEqual(state.ElevatorStates[0].Velocity, 0.0)
        self.assertAlmostEqual(state.ElevatorStates[0].Floor, 3.0)
        self.assertAlmostEqual(state.ElevatorStates[0].Direction, 0)
Пример #3
0
    def test_dispatch_twice(self, mock_uniformgenerator):
        """
        no target, dispatch (3, 1) first, then (8, -1)
        decelerate then accelerate
        not accelerate immediately
        """
        max_floors = 8
        # mansion_config
        world = MansionConfig(dt=0.50,
                              number_of_floors=max_floors,
                              floor_height=4.0)

        # test_elevator
        test_elevator = Elevator(start_position=0.0,
                                 mansion_config=world,
                                 name="test_elevator")
        test_elevator._direction = 1
        test_elevator._current_velocity = 2.0
        test_elevator._current_position = 9.0
        test_elevator._target_floors = list()
        test_elevator._loaded_person = [
            list() for i in range(test_elevator._number_of_floors)
        ]
        test_elevator._load_weight = 0

        # mansion
        tmp_uniform_generator = UniformPersonGenerator()
        ret_person = []
        person_generators.uniform_generator.UniformPersonGenerator.generate_person = mock.Mock(
            return_value=(ret_person))
        test_mansion = MansionManager(elevator_number=1,
                                      person_generator=tmp_uniform_generator,
                                      mansion_config=world,
                                      name="test_mansion")
        test_mansion._elevators = [test_elevator]
        # first, dispatch to 8 floor
        dispatch = []
        dispatch.append(ElevatorAction(4, 1))
        test_mansion.run_mansion(dispatch)
        test_mansion.run_mansion(dispatch)
        test_mansion.run_mansion(dispatch)
        dispatch = []
        dispatch.append(ElevatorAction(8, -1))
        test_mansion.run_mansion(dispatch)  # accelerate at once
        test_mansion.run_mansion(dispatch)
        state = test_mansion.state
        self.assertAlmostEqual(state.ElevatorStates[0].Velocity, 2.0)
Пример #4
0
    def test_cancel_dispatch(self, mock_uniformgenerator):
        """
        no target, dispatch first, accelerate, then cancel dispatch, decelerate
        """
        max_floors = 8
        # mansion_config
        world = MansionConfig(dt=0.50,
                              number_of_floors=max_floors,
                              floor_height=4.0)

        # test_elevator
        test_elevator = Elevator(start_position=0.0,
                                 mansion_config=world,
                                 name="test_elevator")
        test_elevator._direction = 0
        test_elevator._current_velocity = 0.0
        test_elevator._current_position = 8.0
        test_elevator._target_floors = list()
        test_elevator._loaded_person = [
            list() for i in range(test_elevator._number_of_floors)
        ]
        test_elevator._load_weight = 0

        # mansion
        tmp_uniform_generator = UniformPersonGenerator()
        ret_person = []
        person_generators.uniform_generator.UniformPersonGenerator.generate_person = mock.Mock(
            return_value=(ret_person))
        test_mansion = MansionManager(elevator_number=1,
                                      person_generator=tmp_uniform_generator,
                                      mansion_config=world,
                                      name="test_mansion")
        test_mansion._elevators = [test_elevator]
        dispatch = []
        dispatch.append(ElevatorAction(6, 1))
        test_mansion.run_mansion(dispatch)
        test_mansion.run_mansion(dispatch)  # t = 1.0
        dispatch = []
        dispatch.append(ElevatorAction(0, -1))
        for i in range(10):
            test_mansion.run_mansion(dispatch)

        state = test_mansion.state
        self.assertAlmostEqual(state.ElevatorStates[0].DoorState, 0.0)
        self.assertAlmostEqual(state.ElevatorStates[0].Velocity, 0.0)
Пример #5
0
    def test_set_direction_0(self, mock_uniformgenerator):
        """
        When the elevator is stopped and empty, always set direction as 0 first, 
        then set as dispatch_target_direction
        """
        max_floors = 8
        # mansion_config
        world = MansionConfig(dt=0.50,
                              number_of_floors=max_floors,
                              floor_height=4.0)

        # test_elevator
        test_elevator = Elevator(start_position=0.0,
                                 mansion_config=world,
                                 name="test_elevator")
        test_elevator._direction = 1
        test_elevator._current_velocity = 0.0
        test_elevator._current_position = 8.0  # 3rd floor
        test_elevator._target_floors = list()
        test_elevator._loaded_person = [
            list() for i in range(test_elevator._number_of_floors)
        ]
        test_elevator._load_weight = 0

        # mansion
        tmp_uniform_generator = UniformPersonGenerator()
        ret_person = []
        ret_person.append(PersonType(0, 50, 3, 1, world.raw_time))
        person_generators.uniform_generator.UniformPersonGenerator.generate_person = mock.Mock(
            return_value=(ret_person))
        test_mansion = MansionManager(elevator_number=1,
                                      person_generator=tmp_uniform_generator,
                                      mansion_config=world,
                                      name="test_mansion")
        test_mansion._elevators = [test_elevator]
        dispatch = []
        dispatch.append(ElevatorAction(3, -1))
        test_mansion.run_mansion(dispatch)
        test_mansion.run_mansion(dispatch)  # t = 1.0
        # print(test_mansion.state, "\nworld time is", world.raw_time)
        state = test_mansion.state
        self.assertAlmostEqual(state.ElevatorStates[0].Direction, -1)
Пример #6
0
    def test_stop_at_dispatch(self, mock_uniformgenerator):
        """
        stop at the dispatch floor, open and close the door, then keep going to the target floor
        """
        max_floors = 8
        # mansion_config
        world = MansionConfig(dt=0.50,
                              number_of_floors=max_floors,
                              floor_height=4.0)

        # test_elevator
        test_elevator = Elevator(start_position=0.0,
                                 mansion_config=world,
                                 name="test_elevator")
        test_elevator._direction = 1
        test_elevator._current_velocity = 2.0
        test_elevator._current_position = 4.0  # currently at 2 floor
        test_elevator._target_floors = [5]
        test_elevator._loaded_person = [
            list() for i in range(test_elevator._number_of_floors)
        ]
        test_elevator._loaded_person[5].append(
            PersonType(0, 50, 1, 6, world.raw_time))
        test_elevator._load_weight = 50

        # test_mansion
        tmp_uniform_generator = UniformPersonGenerator()

        ret_person = []
        person_generators.uniform_generator.UniformPersonGenerator.generate_person = mock.Mock(
            return_value=(ret_person))

        test_mansion = MansionManager(elevator_number=1,
                                      person_generator=tmp_uniform_generator,
                                      mansion_config=world,
                                      name="test_mansion")
        test_mansion._elevators = [test_elevator]

        # test
        dispatch = []
        dispatch.append(ElevatorAction(3, 1))

        for i in range(7):
            test_mansion.run_mansion(dispatch)  # stop at the dispatched floor
        # print(test_mansion.state, "\nworld time is", world.raw_time)
        # dispatch = []
        # dispatch.append(ElevatorAction(-1, 0))
        for i in range(2):
            # the door is fully open, t = 4.5
            test_mansion.run_mansion(dispatch)
        state = test_mansion.state
        self.assertAlmostEqual(state.ElevatorStates[0].DoorState, 1.0)

        dispatch = []
        dispatch.append(ElevatorAction(0, 0))

        for i in range(6):
            # finish time open lag and close the door
            test_mansion.run_mansion(dispatch)
        state = test_mansion.state
        self.assertAlmostEqual(state.ElevatorStates[0].DoorState, 0.0)

        for i in range(4):
            test_mansion.run_mansion(dispatch)  # then keep going up
        state = test_mansion.state
        self.assertAlmostEqual(state.ElevatorStates[0].Velocity, 2.0)