From b3e1beea7b62643d0cfaf6e55a39f7032962b699 Mon Sep 17 00:00:00 2001 From: ZhuangYumin Date: Tue, 25 Jun 2024 15:20:40 +0000 Subject: [PATCH] write bfs strategy --- search/search.py | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/search/search.py b/search/search.py index 355d6f1..b1d2727 100644 --- a/search/search.py +++ b/search/search.py @@ -86,9 +86,9 @@ def depthFirstSearch(problem: SearchProblem): print("Is the start a goal?", problem.isGoalState(problem.getStartState())) print("Start's successors:", problem.getSuccessors(problem.getStartState())) """ - print("Start:", problem.getStartState()) - print("Is the start a goal?", problem.isGoalState(problem.getStartState())) - print("Start's successors:", problem.getSuccessors(problem.getStartState())) + # print("Start:", problem.getStartState()) + # print("Is the start a goal?", problem.isGoalState(problem.getStartState())) + # print("Start's successors:", problem.getSuccessors(problem.getStartState())) action_list=[] # action_list=["South", "South", "West", "South", "West", "West", "South", "West"] vis_stk=util.Stack() @@ -110,8 +110,26 @@ def depthFirstSearch(problem: SearchProblem): def breadthFirstSearch(problem: SearchProblem): """Search the shallowest nodes in the search tree first.""" - "*** YOUR CODE HERE ***" - util.raiseNotDefined() + # print("Start:", problem.getStartState()) + # 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): """Search the node of least total cost first."""