Python 执行时间问题

101 阅读2分钟

在开发一个基于 Python 的游戏 AI 时,我们遇到了一个执行时间问题。游戏的目标是让一个代理找到从起点到终点的最短路径。该代理使用了一个搜索算法来计算路径。在第一个函数 registerInitialState 中,我们计算出路径并存储在本地变量 self.actions 中。在第二个函数 getAction 中,我们从 self.actions 中获取下一个动作。

def registerInitialState(self, state):
    """
    This is the first time that the agent sees the layout of the game board. Here, we
    choose a path to the goal.  In this phase, the agent should compute the path to the
    goal and store it in a local variable.  All of the work is done in this method!

    state: a GameState object (pacman.py)
    """
    if self.searchFunction == None: raise Exception, "No search function provided for SearchAgent"
    starttime = time.time()
    problem = self.searchType(state) # Makes a new search problem
    self.actions  = self.searchFunction(problem) # Find a path
    totalCost = problem.getCostOfActions(self.actions)
    print('Path found with total cost of %d in %.1f seconds' % (totalCost, time.time() - starttime))
    if '_expanded' in dir(problem): print('Search nodes expanded: %d' % problem._expanded)

def getAction(self, state):
    """
    Returns the next action in the path chosen earlier (in registerInitialState).  Return
    Directions.STOP if there is no further action to take.

    state: a GameState object (pacman.py)
    """
    if 'actionIndex' not in dir(self): self.actionIndex = 0
    i = self.actionIndex
    self.actionIndex += 1
    if i < len(self.actions):
      return self.actions[i]    
    else:
      return Directions.STOP

然而,当我们运行这个程序时,我们发现 getAction 函数没有执行,并且抛出了一个错误:“TypeError: len() of unsized object”。

2、解决方案

为了解决这个问题,我们对代码进行了调试,并发现了问题所在。在 getAction 函数中,我们使用 len(self.actions) 来检查 self.actions 的长度,但是此时 self.actionsNone,因此导致了错误。

问题的根源在于 registerInitialState 函数中。在该函数中,我们调用了 self.searchFunction(problem) 来计算路径,并将结果存储在 self.actions 中。然而,如果 self.searchFunction 函数没有找到路径,则会返回 None

为了解决这个问题,我们在 registerInitialState 函数中添加了一个检查,如果 self.searchFunction 返回 None,则将 self.actions 设置为 []。这样,在 getAction 函数中,我们就不会再遇到 TypeError 错误了。

def registerInitialState(self, state):
    """
    This is the first time that the agent sees the layout of the game board. Here, we
    choose a path to the goal.  In this phase, the agent should compute the path to the
    goal and store it in a local variable.  All of the work is done in this method!

    state: a GameState object (pacman.py)
    """
    if self.searchFunction == None: raise Exception, "No search function provided for SearchAgent"
    starttime = time.time()
    problem = self.searchType(state) # Makes a new search problem
    self.actions  = self.searchFunction(problem) # Find a path
    if self.actions == None: self.actions = [] # Fix the problem
    totalCost = problem.getCostOfActions(self.actions)
    print('Path found with total cost of %d in %.1f seconds' % (totalCost, time.time() - starttime))
    if '_expanded' in dir(problem): print('Search nodes expanded: %d' % problem._expanded)

def getAction(self, state):
    """
    Returns the next action in the path chosen earlier (in registerInitialState).  Return
    Directions.STOP if there is no further action to take.

    state: a GameState object (pacman.py)
    """
    if 'actionIndex' not in dir(self): self.actionIndex = 0
    i = self.actionIndex
    self.actionIndex += 1
    if i < len(self.actions):
      return self.actions[i]    
    else:
      return Directions.STOP

经过修改后,我们的程序能够正确地运行,并且能够找到从起点到终点的最短路径。