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

According to bfs, it is 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 goal test with node 0 we should create 1,2,3 node but nothing to do with goal test, as before, when we have goal 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 have to offer some methods to avoid heading back and visiting some position twice.

below is my code in python3:

import frontiers

def 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:
            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()  # 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 are two pictures of the result and the shade of color means the frequency of visiting in our algorithm.

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 way, I can easily store the entire tree. But, after testing, the effect of it was quite low, like the 24th or 25th floor could be the deepest layer in 1 min's running.

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

If I pay more attention to the scope of te variables, semantic problems and solutions to the problem, I am able to save lots of time.

Effect
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末经柴,一起剝皮案震驚了整個(gè)濱河市蛤铜,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌裤园,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,290評(píng)論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件力喷,死亡現(xiàn)場(chǎng)離奇詭異足删,居然都是意外死亡眨层,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,107評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門面哥,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)哎壳,“玉大人,你說(shuō)我怎么就攤上這事尚卫」殚牛” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 156,872評(píng)論 0 347
  • 文/不壞的土叔 我叫張陵吱涉,是天一觀的道長(zhǎng)刹泄。 經(jīng)常有香客問(wèn)我,道長(zhǎng)怎爵,這世上最難降的妖魔是什么特石? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,415評(píng)論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮疙咸,結(jié)果婚禮上县匠,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好乞旦,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,453評(píng)論 6 385
  • 文/花漫 我一把揭開(kāi)白布贼穆。 她就那樣靜靜地躺著,像睡著了一般兰粉。 火紅的嫁衣襯著肌膚如雪故痊。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 49,784評(píng)論 1 290
  • 那天玖姑,我揣著相機(jī)與錄音愕秫,去河邊找鬼。 笑死焰络,一個(gè)胖子當(dāng)著我的面吹牛戴甩,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播闪彼,決...
    沈念sama閱讀 38,927評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼甜孤,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了畏腕?” 一聲冷哼從身側(cè)響起缴川,我...
    開(kāi)封第一講書(shū)人閱讀 37,691評(píng)論 0 266
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎描馅,沒(méi)想到半個(gè)月后把夸,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,137評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡铭污,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,472評(píng)論 2 326
  • 正文 我和宋清朗相戀三年恋日,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片况凉。...
    茶點(diǎn)故事閱讀 38,622評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡谚鄙,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出刁绒,到底是詐尸還是另有隱情闷营,我是刑警寧澤,帶...
    沈念sama閱讀 34,289評(píng)論 4 329
  • 正文 年R本政府宣布知市,位于F島的核電站傻盟,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏嫂丙。R本人自食惡果不足惜娘赴,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,887評(píng)論 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望跟啤。 院中可真熱鬧诽表,春花似錦唉锌、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,741評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至泛啸,卻和暖如春绿语,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背候址。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,977評(píng)論 1 265
  • 我被黑心中介騙來(lái)泰國(guó)打工吕粹, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人岗仑。 一個(gè)月前我還...
    沈念sama閱讀 46,316評(píng)論 2 360
  • 正文 我出身青樓匹耕,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親赔蒲。 傳聞我的和親對(duì)象是個(gè)殘疾皇子泌神,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,490評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容