So I am trying to make the pacman project from Berkeley and the UniformCostSearch function works except for one test and I am not very sure what the problem is. *** student solution: ['Right', 'Down', 'Down'] *** student expanded_states: ['A', 'B', 'D', 'G']
*** correct solution: ['Right', 'Down', 'Down'] *** correct expanded_states: ['A', 'B', 'D', 'C', 'G']
This is the code:
def uniformCostSearch(problem: SearchProblem):
"""Search the node of least total cost first."""
"*** YOUR CODE HERE ***"
node = {'state': problem.getStartState(), 'cost': 0}
if problem.isGoalState(node['state']):
return []
# Folosesc PriorityQueue
# In frontier pun nodurile intalnite dar care nu o fost explorate
frontier = util.PriorityQueue()
frontier.push(node, 0)
# In explored pun nodurile care o fost deja explorate
explored = set()
while True:
if frontier.isEmpty():
raise Exception('Search failed!')
# Luam nodu din frontier ca sa il exploram
node = frontier.pop()
# Daca nodu o fost deja explorat dam skip la ieratia curenta
if node['state'] in explored:
continue
# Reconstruim calea pana la Goal folosindu ne de parent si ne tot intoarcem in spate
explored.add(node['state'])
if problem.isGoalState(node['state']):
actions = []
while 'parent' in node:
actions.append(node['action'])
node = node['parent']
actions.reverse()
return actions
# Obtinem succesorii urmatoarei stari
successors = problem.getSuccessors(node['state'])
for successor in successors:
child = {'state': successor[0], 'action': successor[1], 'cost': successor[2], 'parent': node}
if child['state'] not in explored:
frontier.push(child, child['cost'] + node['cost'])