广度优先搜索与坐标列表python

时间:2019-02-26 15:58:14

标签: python algorithm breadth-first-search

我正在为“蜘蛛游戏”(与蛇游戏大致相同的概念,但移动逻辑有些不同)构建一个简单的人工智能。我正在尝试实现BFS算法,以便蜘蛛能够找到将其引导至蚂蚁的路径。该算法似乎可以进行多次迭代,但是当我在调试器之外运行它时,它会在None内获得一个node_list值,这会使其他方法失败(因为您无法为任何其他方法获得下一步进展) )。

这是BFS算法:

def BFS(spider_state, ant_state):
    goal = False
    count = 0
    initial_node = Node(spider_state, None, 0)
    node_list = [initial_node]
    initial_ant_state = ant_state

    while goal == False or len(node_list) == 0:
        e = node_list.pop(0)
        future_ant_state = initial_ant_state
        for i in range(0, e.depth):
            future_ant_state = get_next_ant_move(border_choice, initial_ant_state)
        for move in POSSIBLE_MOVES:
            count += 1
            next_node = Node(None, None, None)
            next_node.state = get_next_spider_move(deepcopy(e.state), move)
            next_node.parent = e
            next_node.depth = e.depth + 1
            if next_node.state == future_ant_state:
                goal = True
                break
            else:
                node_list.append(next_node)
    return node_list

节点:

class Node():
    def __init__(self, state, parent, depth):
        self.state = state
        self.parent = parent
        self.depth = depth

蜘蛛和蚂蚁表示为xy位置的简单列表:

spider = [15, 35]
ant = [20, 10]

get next move方法如下:

def get_next_spider_move(spidy, move):
    if spidy:
        # check the bounds and assign the new value to spidy
        spidy = spider_bounds(spidy)
        # farthest right
        if move == 0:
            spidy[1] += 2
            spidy[0] -= 1
        # furhter up and right
        if move == 1:
            spidy[1] += 1
            spidy[0] -= 2
        # backwords
        if move == 2:
            spidy[0] += 1
        # farthest left
        if move == 3:
            spidy[1] -= 2
            spidy[0] -= 1
        # furhter up and to the left
        if move == 4:
            spidy[1] += 1
            spidy[0] -= 2
        # one left
        if move == 5:
            spidy[1] -= 1
        # one right
        if move == 6:
            spidy[1] -= 1
        # side right
        if move == 7:
            spidy[1] += 1
            spidy[0] += 1
        # side left
        if move == 8:
            spidy[1] -= 1
            spidy[0] -= 1
        else:
            # if no valid direction was given
            return spidy
    else:
        raise ValueError('spidy must contain an x and y position. %s',  spidy, ' was found')

运行时产生的错误:

    File "spider_game_bfs.py", line 141, in <module>
    path = BFS(spider, ant)
  File "spider_game_bfs.py", line 130, in BFS
    next_node.state = get_next_spider_move(deepcopy(e.state), move)
  File "spider_game_bfs.py", line 100, in get_next_spider_move
    raise ValueError('spidy must contain an x and y position. %s',  spidy, ' was found')
ValueError: ('spidy must contain an x and y position. %s', None, ' was found')

1 个答案:

答案 0 :(得分:0)

在移动功能的底部出现逻辑错误。最后一个完整的语句是

    if move == 8:
        spidy[1] -= 1
        spidy[0] -= 1
    else:
        # if no valid direction was given
        return spidy

您的注释不正确:else子句由除8以外的任何移动 执行。如果移动 8,则返回{{1 }},因为您跳过了返回None的语句。

正如前面提到的第一条评论,使用spidy作为逻辑结构会做得更好。甚至比这更好的是,请遵循许多用于移动项目的在线示例:列出移动的清单或命令,如下所示:

if ... elif ... else