Working on Pacman UCS algorithm, the provided autograder PASS me on all tests case except goaldAtDequeue.test. Here is my code:
explored=[]
frontier=util.PriorityQueue() #initiate Priority Queue
startState=(problem.getStartState(),'Stop',0)
frontier.push(startState,0)
explored.append(problem.getStartState())
parentPath=list()
childNode=list() # in the form of [(x,y),'action1','action2','action3',]
while True:
if frontier.isEmpty():
return False
else:
pNode=frontier.pop()
if pNode[0] == problem.getStartState():
pass
else:
parentPath=[]
parentPath=copy.deepcopy(pNode[1:])
if problem.isGoalState(pNode[0]):
return parentPath
for frontiers in problem.getSuccessors(pNode[0]):
if frontiers[0] in explored:
pass
else:
childNode =[] #point childNode to an empty list
childNode.append(frontiers[0]) #add successor's coordinate (x,y) into the childNode
if parentPath!=[]:
childNode+=parentPath #add the parent node 's path into the childNode
childNode.append(frontiers[1]) #add the successors's move into the childNode
pathCost=problem.getCostOfActions(childNode[1:])
# print "current Child Node", frontiers[0]
# print " Path Cost", pathCost
frontier.push(childNode,pathCost)
explored.append(frontiers[0]) #add state into explored list
The error autograder gave me was that:
Incorrectly testing if a node is a goal before adding it into the queue, instead of testing when you remove the node from the queue.
Aucun commentaire:
Enregistrer un commentaire