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

问题描述 投票:0回答:1

我正在构建一个简单的A.I用于“蜘蛛游戏”(与蛇游戏几乎相同的概念,但移动逻辑有点不同)。我正在尝试实现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')
python algorithm breadth-first-search
1个回答
0
投票

您在移动功能的底部有一个逻辑错误。最后一个完整的陈述是

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

你的评论是不正确的:else子句是由8以外的任何移动执行的。如果移动是8,那么你返回None,因为你跳过了返回spidy的语句。

正如第一条评论所提到的,使用if ... elif ... else作为逻辑结构,你会做得更好。更好的是,按照许多移动项目的在线示例:制作移动的列表或字典,如下所示:

move_dir = [
    (-1, +2),  # move 0
    (-2, +1),  # move 1
    (+1,  0),  # move 2
    ...        # fill in the rest
]

if move in range(len(move_dir)):
    spidy[0] += move_dir[move[0]]
    spidy[1] += move_dir[move[1]]
    return spidy
else:
    raise ValueError ...
© www.soinside.com 2019 - 2024. All rights reserved.