启发式超时

时间:2015-02-24 02:02:31

标签: python artificial-intelligence

我目前正在edx.org上参加AI课程,我想请求一些帮助。该项目是一个起搏器游戏,其中pacman找到了自己获取所有点的路径。我在启发式功能方面遇到了一些麻烦。

更多信息: 自动编程器明确说明问题在于启发式;它说我正确编码广度优先搜索。 该计划的最终结果是找到pacman吃掉所​​有点的最佳途径。 该函数返回启发式,它告诉搜索算法在某个方向上移动的成本。

这是我的代码:

def foodHeuristic(state, problem):

"""
Your heuristic for the FoodSearchProblem goes here.

This heuristic must be consistent to ensure correctness.  First, try to come
up with an admissible heuristic; almost all admissible heuristics will be
consistent as well.

If using A* ever finds a solution that is worse uniform cost search finds,
your heuristic is *not* consistent, and probably not admissible!  On the
other hand, inadmissible or inconsistent heuristics may find optimal
solutions, so be careful.

The state is a tuple ( pacmanPosition, foodGrid ) where foodGrid is a Grid
(see game.py) of either True or False. You can call foodGrid.asList() to get
a list of food coordinates instead.

If you want access to info like walls, capsules, etc., you can query the
problem.  For example, problem.walls gives you a Grid of where the walls
are.

If you want to *store* information to be reused in other calls to the
heuristic, there is a dictionary called problem.heuristicInfo that you can
use. For example, if you only want to count the walls once and store that
value, try: problem.heuristicInfo['wallCount'] = problem.walls.count()
Subsequent calls to this heuristic can access
problem.heuristicInfo['wallCount']
"""
    position, foodGrid = state
    fh = 0
    bfsResults = search.breadthFirstSearch(problem) #bfs: breadth first search

    fh = len(bdsResults)

    "*** YOUR CODE HERE ***"
    print "Heuristic: " + str(fh)
    return fh

我对python没有太多经验,但对我来说似乎很好。当我运行自动编程器时,在检查启发式时,它会超时。请帮忙!

1 个答案:

答案 0 :(得分:1)

A *搜索有效,因为它包含了有关搜索过程中可能是一条好路径的信息,并且能够首先搜索更好的路径,从而更快地到达目标状态。广度优先搜索目前是您基于启发式搜索的所有内容,它提供了有关哪些选项比其他选项更有希望的最少信息。

正如您可能已经了解到的那样,A *启发式算法需要被接受。即他们永远不会高估与节点的距离。为了提出这样的启发式方法,考虑对可以放松的真正问题的约束通常是有帮助的。因此,举例来说,您可能决定在计算启发式时忽略隔离墙(我不知道这实际上是不是一个好主意)。

看起来你有很多数据要尝试使用。看看你是否能提出更具信息性的启发式方法。如果你找到一个足够好的,你应该能够在启发式超时之前找到目标状态。