Fix search algorithm and finish project.

This commit is contained in:
2021-10-23 18:38:08 -04:00
parent 7e64e723eb
commit fd8dd8ae35
2 changed files with 48 additions and 17 deletions

View File

@@ -80,19 +80,23 @@ def genericSearch(problem, getNewCostAndPriority):
fringe.push((startState, [], 0), 0)
visited = {}
while not fringe.isEmpty():
while True:
if fringe.isEmpty():
raise Exception("No path found.")
state, actions, cost = fringe.pop()
if problem.isGoalState(state):
return actions
if state in visited and cost >= visited[state]:
continue
visited[state] = cost
for successor, action, stepCost in problem.getSuccessors(state):
newCost, priority = getNewCostAndPriority(cost, stepCost, successor)
if successor in visited and visited[successor] <= newCost:
continue
newActions = list(actions) + [action]
fringe.push((successor, newActions, newCost), priority)
print("No path found.")
raise Exception()
def depthFirstSearch(problem):