更改字符串参数值的方法(Python)

时间:2017-09-29 19:31:57

标签: python machine-learning artificial-intelligence a-star

上周我开始从EdX开始从事人工智能在线课程,我一直在尝试推进这个项目,但是我还没能解决我遇到的特定错误。该项目包括建立一个A *代理,找到Pacman迷宫上的所有4个角落。

目前课程论坛不是很活跃,我在Stackoverflow上找不到类似的东西,所以我决定问问自己。我正在研究的代码是课程构建的更大项目的一部分,解决迷宫问题的类是CornersProblem,它有一个构造函数,一个getStartState,isCorner,isGoalState,getCostOfActions,cornerHeuristic和getSuccessors方法。

抛出错误的代码如下:

    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
    """
    successors = []
    self._expanded += 1  # DO NOT CHANGE
    for direction in [Directions.NORTH, Directions.SOUTH, Directions.EAST, Directions.WEST]:
        x, y = state[0]
        **print direction
        dx, dy = Actions.directionToVector(direction)**
        nextx, nexty = int(x + dx), int(y + dy)
        if not self.walls[nextx][nexty]:
            nextState = (nextx, nexty)
            cost = self.getCostOfActions(direction)
            if self.isCorner(self, nextState):
                state[1] += 1
            successors.append(((nextState, state[1]), direction, cost))
    return successors

此方法调用directionToVector:

    def directionToVector(direction, speed = 1.0):
    print direction
    dx, dy =  Actions._directions[direction]
    return (dx * speed, dy * speed)
directionToVector = staticmethod(directionToVector)

我收到一条KeyError消息:'N'表示'N'不在字典中,它应该是'North'。如果我在将其传递给directionToVector方法之前打印方向,则输出'North',但在方法内部之后输出为'N'。这是错误消息:

Command line error

我对Python不是很有经验,所以可能我在这里缺少一些东西。这是我在这个论坛上的第一个问题,所以提前感谢您的回答。

0 个答案:

没有答案