【问题标题】:Berkeley Pacman CornersProblem伯克利吃豆人角问题
【发布时间】:2018-06-16 22:45:54
【问题描述】:

我一直致力于伯克利的 Pacman 项目,用于他们的人工智能。课程。我遇到了一个问题,要弄清楚如何找到一条路径,以便 pacman 触及 pacman 板的所有四个角。它使用一般的广度优先搜索算法。它只在有一个目标状态时返回一条路径,而不是四个。

这个特定问题的项目说明:

在角落迷宫中,有四个点,每个角落一个。我们新的搜索问题是找到穿过迷宫的最短路径,触及所有四个角(迷宫是否真的有食物)。请注意,对于一些像 tinyCorners 这样的迷宫,最短路径并不总是首先到达最近的食物!提示:通过 tinyCorners 的最短路径需要 28 步。

在 searchAgents.py 中实现 CornersProblem 搜索问题。您将需要选择一个状态表示来编码检测是否已到达所有四个角所需的所有信息。

BFS:

fringe = util.Queue()
fringe.push( (problem.getStartState(), [], 0) )    
visited = set()

while not fringe.isEmpty():
    curState, curAction, curCost = fringe.pop()

    if curState in visited:
        continue
    visited.add(curState)

    if problem.isGoalState(curState):
        return curAction

    for state, action, cost in problem.getSuccessors(curState):
        fringe.push( (state, curAction + [action], cost ) )

return []

util.raiseNotDefined()

搜索代理开始状态:

def getStartState(self):
    """
    Returns the start state (in your state space, not the full Pacman state
    space)
    """
    "*** YOUR CODE HERE ***"
    return self.startingPosition
    util.raiseNotDefined()

SearchAgent 目标状态

"""
Returns whether this search state is a goal state of the problem.
"""
"*** YOUR CODE HERE ***"

// TODO
util.raiseNotDefined()

我坚持返回目标状态,以便测试通过。目前,该函数返回:

*** FAIL: test_cases/q5/corner_tiny_corner.test

*** Corners missed: [(1, 1), (1, 6), (6, 1), (6, 6)]

*** Tests failed.

或者我只返回 4 个角中的 1 个。

【问题讨论】:

  • ...这是什么编程语言?
  • python v 2.7.12

标签: python pacman


【解决方案1】:

什么定义了目标状态?当所有 4 个角都被触摸时。

鉴于您已经拥有所有 4 个角坐标,您可以使用一个简单的 Python 列表来存储角,因为您将状态传递给后继状态,当您到达该状态时从角列表中删除。 (检查本身将进入getSuccessors() 函数,因为您传递了一个包含您当前位置的状态)。

那么你的目标条件应该很容易检查这个列表。

【讨论】:

  • if state in self.corners: self.corners = list(self.corners) self.corners.remove(state) print self.corners print len(self.corners) return len(self.corners) == 0
  • 我尝试使用上面的代码在 pacman 到达角坐标时从 self.corners 中删除角,然后根据是否满足 len(self.corners) == 0 条件返回 true/false if 语句。我仍然得到同样的错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-15
  • 2011-12-28
相关资源
最近更新 更多