Application of Breath-first Search in AI (route search)

Source: Internet
Author: User

According to BFS, it's a search method to go through all the nodes layer by layer, until the GAL has been found.


To make it simple, there is a visiting sequence of BFS:

Attention:

When we have the goal test with node 0 We should create-a-Z node but the nothing to does with goal test, as before and when we have G OAL Test with Node 1, we should create 4,5,6 immediately and so on.


Here comes an exercise:

A Red Bird wants to find the yellow bird in the shortest route and find this route in the shortest time.

We want to use the BFS, while due to the physical situation, we had to offer some methods to avoid heading back and visit ing the some position twice.

Below is my code in Python3:

Import frontiersdef solve (problem)  :    state = problem.initial_ state # the start location    map = {} # a  Dic to store the whole map    map[state] = set ()  #  a set to store a node    map[state].add (0)  # add  the parents information to a set    map[state].add (' root ')   # initialize the root node    queue_node_created =  frontiers. Queue ()   # store the position of nodes created     Queue_node_created.push (state)   # initialization    path_stack =  Frontiers. Stack ()  # store the path in inverted order    list_queue = []  # store the path in right order     while true:        node_value = queue_node_ Created.pop ()   # pop the node in the queue for goal_test  or expending new nodes        if  Problem.goal_test (Node_value):  # goal test             while True:                 list_1 = list (Map[node_value])                  if type (List_1[0])  == str:                      parent_node_value = list_1[1]                     last_action = list_1[0]# find action taken and parent  node                else :                     parent_node_value = list_1[0]                     last_action = list_1[1]   # find action taken and parent node                 if last_action ==  ' Root ':   # if find the root                     break                 path_stack.push (last_action)   # store the action taken                 node_value =  parent_node_value  # refresh the node_value                 print (last_action)              while not path_stack.is_empty ():                 list_queue.append (Path_stack.pop ())              return list_queue  #  return the path        else:          &nbSp;  next_steps = problem.get_successors (Node_value)   # get successors             for node in next_steps:                 node_position  = node[0]                 if node_position in map.keys ():  # in case that the  same node is visited more than twice                     continue                 else:                     map[node_position]  = set () &Nbsp; # push the node info  (Parent ' s position and action)   into map                     map[node_position].add (Node_value)                      map[node_position].add (Node[1])                  queue_node_ Created.push (node_position)   # push the new node into the queue

In the appendix, there is both pictures of the result and the shade of color means the frequency of visiting in our Algori tHM.


At the beginning, I used the list with dictionaries in it. The list represents the whole tree and a dictionary act as a layer. In this, I can easily store the entire tree. But, after testing, the effect of it is quite low, like the 24th or 25th floor could is the deepest layer in 1 min ' s Runn Ing.


I felt hopeless due to the perform of what I had programmed. I spent 2days to finish it. Thanks to my female friend who was much smarter than me, I found a better solution to deal with node storage and new node ' Test in sequence just as can see in my code.

Pay more attention to the scope of the variables, semantic problems and solutions to the problem. We can save lots of time.









This article is from the "11885445" blog, please be sure to keep this source http://11895445.blog.51cto.com/11885445/1912287

Application of Breath-first Search in AI (route search)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.