簡體   English   中英

為什么我們在尋找 A* 的啟發式時返回四個角的最大距離?

[英]Why do we return the max distance out of the four corners when finding the heuristic for A*?

我試圖解決 cs188 ai project-1 搜索。 在cornersHeuristic() function 中,我們需要返回一個啟發式,以便我們在遍歷角落時使用它。 我們想使用最短路徑,所以我們使用 A-star 搜索。 代碼摘要以更好地理解它:我們有 (x,y) 格式的角位置,我們將字符的當前位置再次設為 (x, y) 格式。 返回的啟發式是該位置到所有角落之間的距離的最大距離。 但這里的邏輯是什么? 我們不應該將最小距離返回到最近的角落嗎? 為什么我們從這些中返回最大值,這如何幫助找到最短路徑? 代碼如下: 代碼說明:corner1、corner2、corner3、corner4、location變量都是元組,存儲x和y值。 如 (1, 1), (4, 5)

def cornersHeuristic(state, problem):

    A heuristic for the CornersProblem that you defined.

      state:   The current search state
               (a data structure you chose in your search problem)

      problem: The CornersProblem instance for this layout.

    This function should always return a number that is a lower bound on the
    shortest path from the state to a goal of the problem; i.e.  it should be
    admissible (as well as consistent).

    corners = problem.corners # These are the corner coordinates
    walls = problem.walls # These are the walls of the maze, as a Grid (game.py)

    location, corner1, corner2, corner3, corner4 = state
    corners = [corner1, corner2, corner3, corner4]
    heuristic = 0
    for x in corners[1]:
        heuristic = max(heuristic,(abs(location[0]-x[0]) + abs(location[1] - x[1])))
    return max_r

啟發式是實際距離的下限。 您希望此下限盡可能大,同時仍保證為下限

所以你是正確的min會起作用。 但是max更好,因為它是一個更大的下限。

我們可以保證它仍然是一個下限,因為問題陳述是我們想要訪問所有角落。 這樣做所需的路徑長度將始終至少是訪問最遠角落的成本。 如果問題是訪問任何一個角落,那么我們需要使用min

暫無
暫無

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

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