簡體   English   中英

廣度優先搜索與坐標列表python

[英]Breadth First Search with list of coordinates python

我正在為“蜘蛛游戲”(與蛇游戲幾乎相同的概念,但移動邏輯有些不同)構建一個簡單的AI。 我正在嘗試實現BFS算法,以便蜘蛛能夠找到將其引導至螞蟻的路徑。 該算法似乎可以進行多次迭代,但是當我在調試器之外運行它時,它會在node_list內獲得None值,這會使其他方法失敗(因為您無法繼續下一步)。

這是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')

您的移動功能底部有一個邏輯錯誤。 最后一個完整的語句是

    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 ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM