Fix search algorithm and finish project.
This commit is contained in:
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user