我在尝试解决cs188人工智能项目-1中的搜索问题。在cornersHeuristic()函数中,我们需要返回一个启发式函数,以便在遍历角落时使用。我们希望使用最短路径,因此我们使用A*搜索算法。为了更好地理解代码,这里是代码的总结:我们有角落的位置以(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