Exemple #1
0
def ucs(current_state, goal_pos, chooseVisited=False):
    startpos = current_state.agent.pos
    factor = -1 if chooseVisited else 1
    # print(startpos, goal_pos)

    explored = []
    frontier = PriorityQueue()
    frontier.push((startpos, []), 0)
    while not frontier.isEmpty():
        pos, moves = frontier.pop()
        if pos == goal_pos:
            return moves
        explored.append(pos)

        for adj, action in GameState.get_adjs(current_state, pos):
            if adj not in explored:
                newMoves = moves + [action]
                frontier.update(
                    (adj, newMoves),
                    factor *
                    getCostOfActions(current_state, startpos, newMoves))
Exemple #2
0
    def get_action(self, state):
        pos = state.agent.pos
        adjs = GameState.get_adjs(state, pos)
        self.description = '\n--------------------\nPos: ' + name(pos) + '\n'
        self.description += "KB:\t" + str(self.KB.KB) + '\n'

        "Moving following by the temporary goals"
        if (self.actions != []):
            self.description += 'Goal: Move to the next safe place: '
            if (self.iLeaving):
                self.description += name(state.exit) + ' (Exit)'
            else:
                self.description += name(self.safe_places[0])
            return self.actions.pop(0)

        "Gold picking - Prioritize picking gold"
        if ('G' in state.explored[pos]):
            self.description += 'Goal: Pick gold'
            return Actions.PICK_GOLD

        "Add KB"
        if 'S' in state.explored[pos]:
            self.handle_stench(pos, adjs)
            "If there is enough condition to conclude the wumpus position"
            target_dir, target_pos = self.wumpus_check(pos, adjs)
            if (target_dir != None and target_pos not in self.checked_places):
                self.checked_places.append(target_pos)
                # self.safe_places.append(target_pos)
                self.description += 'Goal: Attempting to shoot at ' + \
                    name(target_pos)
                return Actions.shoot(target_dir)
        else:
            self.handle_not_stench(pos, adjs)
        if 'B' in state.explored[pos]:
            self.handle_breeze(pos, adjs)
        else:
            self.handle_not_breeze(pos, adjs)

        "Update safe places"
        temp = []
        for adj, _ in adjs:
            if (self.is_safe(adj)
                ) and adj not in self.visited and adj not in self.safe_places:
                temp.append(adj)
                self.safe_places.insert(0, adj)
        if (temp != []):
            self.description += 'Percept: ' + \
                str(temp) + ' is safe and not yet visited\n'

        "Update visited lists"
        self.visited.append(pos)
        if (pos in self.safe_places):
            self.safe_places.remove(pos)

        "Find the next place to move"
        if (self.safe_places != []):
            self.actions = ucs(state, self.nearest_safe_place())
            self.description += 'Goal: Move to the next unvisited safe place: ' + \
                name(self.safe_places[0])
            return self.actions.pop(0)

        "If there is no safe place => Exit"
        if pos == state.exit and self.iLeaving:
            self.description += 'Goal: Climb out of the cave'
            return Actions.EXIT
        else:
            self.iLeaving = True
            self.description += 'Goal: Find the exit to leave'
            self.actions = ucs(state, state.exit)
            return self.actions.pop(0)