コード例 #1
0
def a_star(goal, frontier, env):
    visited_states = []
    actual_node = frontier.pop(0)

    while env.matrix[actual_node.state.row][
            actual_node.state.col].type != goal.type:
        children = successor_function(actual_node, env.matrix)

        for c in children:
            if c.state not in visited_states:
                frontier.append(c)
                c.set_hValue(get_manhattan_distance(c, env.food_position))
                if env.matrix[c.state.row][
                        c.state.col].type == CellTypes.TYPE_EMPTY:
                    env.matrix[c.state.row][c.state.col].set_expanded()

            if env.matrix[actual_node.state.row][
                    actual_node.state.col].type == CellTypes.TYPE_EMPTY:
                env.matrix[actual_node.state.row][
                    actual_node.state.col].set_visited()

            visited_states.append(actual_node.state)
        frontier.sort(key=lambda node: (node.hValue + node.gValue))
        actual_node = frontier.pop(0)
    return actual_node
コード例 #2
0
def dfs(goal, frontier, space):
    visited_states = []
    actual_node = frontier.pop(0)

    while space[actual_node.state.row][
            actual_node.state.col].type != goal.type:
        children = successor_function(actual_node, space)
        for c in children:
            if c.state not in visited_states:
                frontier.insert(0, c)
                if space[c.state.row][
                        c.state.col].type == CellTypes.TYPE_EMPTY:
                    space[c.state.row][c.state.col].set_expanded()

            if space[actual_node.state.row][
                    actual_node.state.col].type == CellTypes.TYPE_EMPTY:
                space[actual_node.state.row][
                    actual_node.state.col].set_visited()
            visited_states.append(actual_node.state)

        actual_node = frontier.pop(0)
    return actual_node