My code:
nodes = []
stack = util.Stack()
child = []
currNode = problem.getStartState()
stack.push(currNode)
while not stack.isEmpty():
nodes = stack.pop()
if problem.isGoalState(nodes):
return nodes
else:
child = problem.getSuccessors(nodes)
for nodes,_,_ in child:
stack.push(nodes)
return None
The values returned by the functions are:
problem.getStartState() - (5, 5)
problem.isGoalState(problem.getStartState())- False
problem.getSuccessors(problem.getStartState()) - [((5, 4), 'South', 1), ((4, 5), 'West', 1)]
The code for the successor func is:
def getSuccessors(self, state):
"""
Returns successor states, the actions they require, and a cost of 1.
"""
successors = []
for action in [Directions.NORTH, Directions.SOUTH, Directions.EAST, Directions.WEST]:
x,y = state
dx, dy = Actions.directionToVector(action)
nextx, nexty = int(x + dx), int(y + dy)
if not self.walls[nextx][nexty]:
nextState = (nextx, nexty)
cost = self.costFn(nextState)
successors.append( ( nextState, action, cost) )
# Bookkeeping for display purposes
self._expanded += 1
if state not in self._visited:
self._visited[state] = True
self._visitedlist.append(state)
return successors
The code for getcostof action funtion is:
def getCostOfActions(self, actions):
"""
Returns the cost of a particular sequence of actions. If those actions
include an illegal move, return 999999
"""
if actions == None: return 999999
x,y= self.getStartState()
cost = 0
for action in actions:
# Check figure out the next state and see whether its' legal
dx, dy = Actions.directionToVector(action)
x, y = int(x + dx), int(y + dy)
if self.walls[x][y]: return 999999
cost += self.costFn((x,y))
return cost
I can only change my code, not these functions. I worte the code but when I execute it, it just hanged up. I know, there is some mistake in the logic and I need to find that one. I need to return the list of actions that are listed in achieving the goal.