searchAgents.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:Pacman-AI 作者: ryanshrott 项目源码 文件源码
def getSuccessors(self, state):
    """
    Returns successor states, the actions they require, and a cost of 1.

     As noted in search.py:
         For a given state, this should return a list of triples, 
     (successor, action, stepCost), where 'successor' is a 
     successor to the current state, 'action' is the action
     required to get there, and 'stepCost' is the incremental 
     cost of expanding to that successor
    """
    (x,y,c1,c2,c3,c4) = state
    corners_hist_old = [c1,c2,c3,c4] # corners we have already seen
    successors = []

    for action in [Directions.NORTH, Directions.SOUTH, Directions.EAST, Directions.WEST]:
      dx, dy = Actions.directionToVector(action)
      nextx, nexty = int(x + dx), int(y + dy)
      if not self.walls[nextx][nexty]:
        nextState = (nextx, nexty)
        if nextState in self.corners: # we found a corner
            #print nextState
            for i in range(4): # determine which corner we hit 
                if nextState == self.corners[i]:
                    corners_hist_new = corners_hist_old[:]
                    corners_hist_new[i] = 1 # mutate the relevant corner 
                    nextValue = nextState + tuple(corners_hist_new)
        else: # we did not hit a corner 
            nextValue = nextState + tuple(corners_hist_old)

        cost = self.costFn(nextValue)
        successors.append( ( nextValue, action, cost) )

    self._expanded += 1 
    return successors
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号