def about_to_crash(self, box=None, agent=None):
        my_action = self.current_plan[0]
        if len(agent.current_plan) == 0:
            return False
        else:
            other_action = agent.current_plan[0]
        if other_action.action.action_type == ActionType.NoOp:
            return False
        if box is None and agent is None:
            return True
        if my_action.action.action_type == ActionType.Move or my_action.action.action_type == ActionType.Pull:
            my_dir = my_action.action.agent_dir

        elif my_action.action.action_type == ActionType.Push:
            my_dir = my_action.action.box_dir

        if box is not None:  #is moving
            #agent box direction
            other_agent_dir = other_action.action.box_dir
            if my_dir == reverse_direction(other_agent_dir):
                return True
            else:
                return False

        else:
            other_agent_dir = other_action.action.agent_dir
            if my_dir == reverse_direction(other_agent_dir):
                return True
            else:
                return False
Beispiel #2
0
    def swicth_from_pull_to_push(self,
                                 action_before: UnfoldedAction,
                                 action_after: UnfoldedAction,
                                 ignore_all_other_agents=True):
        box_direction = reverse_direction(action_before.action.agent_dir)
        agent_dir = action_after.action.agent_dir
        row, col = action_after.agent_from
        #choose direction
        for direction in [Dir.N, Dir.S, Dir.E, Dir.W]:
            if direction == box_direction or direction == agent_dir:
                continue
            new_row = row + direction.d_row
            new_col = col + direction.d_col
            if not (self.beliefs.is_free(new_row, new_col) or
                    (ignore_all_other_agents and
                     (new_row, new_col) in self.beliefs.agents)):
                continue
            turn_dir = direction
            break

        #pull
        row, col = action_before.agent_to
        act1 = Action(ActionType.Move, turn_dir, None)
        part1 = UnfoldedAction(act1, self.id_, True, (row, col))
        resulting_action1 = self.convert_move_to_pull(
            part1, action_before.action.agent_dir)

        #push
        new_location = (row + turn_dir.d_row, col + turn_dir.d_col)
        act2 = Action(ActionType.Move, reverse_direction(turn_dir), None)
        part2 = UnfoldedAction(act2, self.id_, True, new_location)
        resulting_action2 = self.convert_move_to_push(
            part2, action_after.action.agent_dir)

        return [resulting_action1, resulting_action2]
    def simple_pull_actions_possible(self,
                                     ignore_directions,
                                     starting_pos=None):
        state = self.beliefs
        if starting_pos is None:
            starting_pos = (self.row, self.col)

        success = False
        #look N, S, E, W of agent and see if we can find two connecting free spots (ignore direction in list)
        for direction in [Dir.N, Dir.S, Dir.E, Dir.W]:
            if direction in ignore_directions:
                continue
            if state.is_free(starting_pos[0] + direction.d_row,
                             starting_pos[1] + direction.d_col):
                #look for second free spot (ignore where we came from):
                row, col = starting_pos[0] + direction.d_row, starting_pos[
                    1] + direction.d_col
                dir1 = direction
                for dir2 in [Dir.N, Dir.S, Dir.E, Dir.W]:
                    if dir2 == reverse_direction(dir1):
                        continue
                    if state.is_free(row + dir2.d_row, col + dir2.d_col):
                        success = True
                        return success, [dir1, dir2]
        return False, None
 def invert_move(self, action):
     if action.action.action_type == ActionType.Push:
         #creat pull action
         move = UnfoldedAction(
             Action(ActionType.Move,
                    reverse_direction(action.action.agent_dir), None),
             self.id_, True, action.agent_to)
         return self.convert_move_to_pull(
             move, action.action.box_dir
         )  #opposite from the box, directions from before
     if action.action.action_type == ActionType.Pull:
         move = UnfoldedAction(
             Action(ActionType.Move,
                    reverse_direction(action.action.agent_dir), None),
             self.id_, True, action.agent_to)
         return self.convert_move_to_push(
             move, reverse_direction(action.action.box_dir)
         )  #opposite from the box, directions from before
Beispiel #5
0
 def convert_move_to_pull(self, action: UnfoldedAction, direction: Dir):
     reversed_direction = reverse_direction(direction)
     new_action = Action(ActionType.Pull, action.action.agent_dir,
                         reversed_direction)
     resulting_action = UnfoldedAction(new_action, action.agent_id)
     resulting_action.agent_from = action.agent_from
     resulting_action.agent_to = action.agent_to
     resulting_action.box_from = (action.agent_from[0] +
                                  reversed_direction.d_row,
                                  action.agent_from[1] +
                                  reversed_direction.d_col)
     resulting_action.box_to = action.agent_from
     resulting_action.required_free = resulting_action.agent_to
     resulting_action.will_become_free = resulting_action.box_from
     return resulting_action
    def retreat_move(self, directions, retreat_type, wait_duration=3):
        state = self.beliefs
        NoOp = UnfoldedAction(Action(ActionType.NoOp, Dir.N, None), self.id_,
                              True, (self.row, self.col))
        has_box, pos_box, relative_pos = self.agent_has_box(
        )  #True, box, pos, relative_pos
        #is just an agent
        if retreat_type == "move":
            direction = directions[0]
            opposite = reverse_direction(direction)
            retreat_move = UnfoldedAction(
                Action(ActionType.Move, direction, None), self.id_, True,
                (self.row, self.col))
            new_location = (self.row + direction.d_row,
                            self.col + direction.d_col)
            NoOp = UnfoldedAction(Action(ActionType.NoOp, Dir.N, None),
                                  self.id_, True, new_location)
            back_move = UnfoldedAction(Action(ActionType.Move, opposite, None),
                                       self.id_, True, new_location)
            #moves = [retreat_move,NoOp,NoOp,back_move]
            moves = [retreat_move, NoOp]
            return moves, 1
        if retreat_type == "push":
            moves = []
            #direction = self.current_plan[0].action.agent_dir
            direction = relative_pos  #where the box is relative to agent # Ex. box is located north of the agent
            dir1, dir2 = directions[0], directions[1]
            move1 = UnfoldedAction(Action(ActionType.Move, direction, None),
                                   self.id_, True, (self.row, self.col))
            new_location = (self.row + direction.d_row,
                            self.col + direction.d_col)
            move2 = UnfoldedAction(Action(ActionType.Move, dir1, None),
                                   self.id_, True, new_location)

            #convert to push actions
            moves.append(self.convert_move_to_push(move1, dir1))
            moves.append(self.convert_move_to_push(move2, dir2))

            end_location = moves[-1].agent_to

            #wait 2 or 3 depending on other agent have box
            moves = moves + [
                UnfoldedAction(Action(ActionType.NoOp, Dir.N, None), self.id_,
                               True, end_location)
            ] * wait_duration

            #pull back
            # moves.append(self.invert_move(moves[1]))
            # moves.append(self.invert_move(moves[0]))
            return moves, 2
        if retreat_type == "pull":
            moves = []
            #direction = self.current_plan[0].action.agent_dir
            direction = relative_pos  #where the box is relative to agent # Ex. box is located north of the agent
            dir1, dir2 = directions[0], directions[1]
            move1 = UnfoldedAction(Action(ActionType.Move, dir1, None),
                                   self.id_, True, (self.row, self.col))
            new_location = (self.row + dir1.d_row, self.col + dir1.d_col)
            move2 = UnfoldedAction(Action(ActionType.Move, dir2, None),
                                   self.id_, True, new_location)

            log("dir1={}, dir2={}, relative_pos={}, move1={}, move2={}".format(
                dir1, dir2, relative_pos, move1, move2))

            #convert to pull actions
            moves.append(
                self.convert_move_to_pull(move1,
                                          reverse_direction(direction)))  #1
            moves.append(self.convert_move_to_pull(move2, dir1))  #2

            end_location = moves[-1].agent_to

            #wait 2 or 3 depending on other agent have box
            moves = moves + [
                UnfoldedAction(Action(ActionType.NoOp, Dir.N, None), self.id_,
                               True, end_location)
            ] * wait_duration

            #push back
            # moves.append(self.invert_move(moves[1]))
            # moves.append(self.invert_move(moves[0]))
            return moves, 2

        #[RETREAT] Agent 5 is doing a retreat move of type pull to make way for agent 9. (Moves: [Pull(N,W), Pull(N,N), NoOp, NoOp, Push(S,N), Push(S,W)])
        '''
Beispiel #7
0
    def convert_paths_to_plan(self,
                              path1,
                              path2,
                              action=ActionType.Push,
                              ignore_all_other_agents=True):
        if action != ActionType.Push:
            raise NotImplementedError

        result = path1[:-1]

        dir1 = path1[-1].action.agent_dir
        dir2 = path2[0].action.agent_dir
        break_point = 0
        turn = None
        #if going back where we came from: start with pull actions
        if dir1 == reverse_direction(dir2):
            for i in range(1, len(path2)):
                #check if there is room to turn from pull to push
                if self.count_free_spaces(
                        path2[i],
                        ignore_all_other_agents=ignore_all_other_agents) >= 3:
                    #change to push
                    turn = self.swicth_from_pull_to_push(
                        path2[i - 1],
                        path2[i],
                        ignore_all_other_agents=ignore_all_other_agents)
                    result = result + turn
                    break_point = i
                    break
                #otherwise pull one
                next_action = self.convert_move_to_pull(
                    path2[i], path2[i - 1].action.agent_dir)
                result.append(next_action)
            #we couldn't turn and should pull onto goal
            if break_point == len(path2) - 1 and turn is None:
                last_action = path2[-1]
                location = last_action.agent_to
                for direction in [Dir.N, Dir.S, Dir.E, Dir.W]:

                    if direction == reverse_direction(
                            last_action.action.agent_dir):
                        continue
                    row, col = location[0] + direction.d_row, location[
                        1] + direction.d_col
                    if self.beliefs.is_free(row, col):
                        action = Action(ActionType.Move, direction, None)
                        unfolded_action = UnfoldedAction(
                            action, self.id_, True, location)
                        next_action = self.convert_move_to_pull(
                            unfolded_action, last_action.action.agent_dir)
                        result.append(next_action)
                        break
                return result
        else:
            next_action = self.convert_move_to_push(path1[-1],
                                                    path2[0].action.agent_dir)
            result.append(next_action)
        for i in range(break_point, len(path2) - 1):
            #push
            next_action = self.convert_move_to_push(
                path2[i], path2[i + 1].action.agent_dir)
            result.append(next_action)

        # TODO: what if we cannot turn?
        return result