write bfs strategy

This commit is contained in:
2024-06-25 15:20:40 +00:00
parent 7d08ea3480
commit b3e1beea7b

View File

@ -86,9 +86,9 @@ def depthFirstSearch(problem: SearchProblem):
print("Is the start a goal?", problem.isGoalState(problem.getStartState())) print("Is the start a goal?", problem.isGoalState(problem.getStartState()))
print("Start's successors:", problem.getSuccessors(problem.getStartState())) print("Start's successors:", problem.getSuccessors(problem.getStartState()))
""" """
print("Start:", problem.getStartState()) # print("Start:", problem.getStartState())
print("Is the start a goal?", problem.isGoalState(problem.getStartState())) # print("Is the start a goal?", problem.isGoalState(problem.getStartState()))
print("Start's successors:", problem.getSuccessors(problem.getStartState())) # print("Start's successors:", problem.getSuccessors(problem.getStartState()))
action_list=[] action_list=[]
# action_list=["South", "South", "West", "South", "West", "West", "South", "West"] # action_list=["South", "South", "West", "South", "West", "West", "South", "West"]
vis_stk=util.Stack() vis_stk=util.Stack()
@ -110,8 +110,26 @@ def depthFirstSearch(problem: SearchProblem):
def breadthFirstSearch(problem: SearchProblem): def breadthFirstSearch(problem: SearchProblem):
"""Search the shallowest nodes in the search tree first.""" """Search the shallowest nodes in the search tree first."""
"*** YOUR CODE HERE ***" # print("Start:", problem.getStartState())
util.raiseNotDefined() # print("Is the start a goal?", problem.isGoalState(problem.getStartState()))
# print("Start's successors:", problem.getSuccessors(problem.getStartState()))
action_list=[]
vis_que=util.Queue()
has_visited={}
vis_que.push((problem.getStartState(),[]))
has_visited[problem.getStartState()]=True
while True:
if vis_que.isEmpty():
break
cur_state, cur_actions=vis_que.pop()
if problem.isGoalState(cur_state):
action_list=cur_actions
break
for next_state,action,_ in problem.getSuccessors(cur_state):
if next_state not in has_visited:
vis_que.push((next_state,cur_actions+[action]))
has_visited[next_state]=True
return action_list
def uniformCostSearch(problem: SearchProblem): def uniformCostSearch(problem: SearchProblem):
"""Search the node of least total cost first.""" """Search the node of least total cost first."""