3.1Implementing the Search Engine of a Forward State Space Planner

基于A的planner*

import random
import heapq
import logging
from search import searchspace
from planning_task import Task
from heuristics import BlindHeuristic



def a_star(task, heuristic=BlindHeuristic):
    """
    Searches for a plan in the given task using A* search.

    @param task The task to be solved
    @param heuristic  A heuristic callable which computes the estimated steps
                      from a search node to reach the goal.
    """
    # get the initial info and then store the current states
    current_state = task.initial_state
    # a data structure storing the heuristic values on the same level
    heap = []
    # a list store the node chosen
    Node_list = []

    # build up a root node
    node = searchspace.make_root_node(current_state)
    Node_list.append(node)
    heuristic_value_node = {}  # a dic, map from heuristic_value to (nodes)


    # if the goal state have not been achieved
    while not task.goal_reached(current_state):

        successors = task.get_successor_states(current_state)  # get the successors of current_state

        if not successors:  # if there is no successors, then this search is failed.
            return False

        # go through all the successors and choose the best as the next state
        for successor in successors:
            # build up a child node
            node = searchspace.make_child_node(Node_list[len(Node_list) - 1], successor[0], successor[1])

            # calculate the f(n)
            f_value = heuristic(node) + node.g
            if f_value not in heap:
                heapq.heappush(heap, f_value)  # push one heuristic_value into heap

            if f_value in heuristic_value_node:
                # push the node into the nodes set in the dic
                heuristic_value_node[f_value].add(node)
            else:
                heuristic_value_node[f_value] = set()  # create a set in dic
                heuristic_value_node[f_value].add(node)  # push the node into the nodes set in the dic


        # choose this node as the next node
        best_node = heuristic_value_node[heap[0]].pop()
        # then choose it as the next state
        best_state = best_node.state

        # if the heuristic_value has no corresponding node then delete it
        if heuristic_value_node[heap[0]] == set():
            del heuristic_value_node[heapq.heappop(heap)]


        # choose the final current_best_state as the next state
        current_state = best_state

        # push the final current_best_node chosen into node_list
        Node_list.append(best_node)

    return Node_list.pop().extract_solution()  # return the solution

首先,把state转化为node,以树为数据结构,方便找到goal之后的回溯和记录cost。
其次,判断是否为goal,是则跳到下一步;不是则得出所有的子节点,计算其f(n)。全局对比寻找最小f(n)的点(全局最优解),循环这一步。
最后,返回整个Node_list。

评价:该implement没有实现对goal_less情况的应对方式。没有对重复的state处理。goal_test应该针对frontiers(所有子节点),而不是选中的node。


基于Greedy Search的planner

import heapq
import logging
from search import searchspace
from planning_task import Task
from heuristics import BlindHeuristic

def gbfs(task, heuristic=BlindHeuristic):
    """
    Searches for a plan in the given task using Greedy Best First Search search.

    @param task The task to be solved
    @param heuristic  A heuristic callable which computes the estimated steps
                      from a search node to reach the goal.
    """
    # a list of current best states, get the initial info and then store the current states
    current_state_list = [task.initial_state]
    # a data structure storing the heuristic values on the same level
    heap = []
    # build up a root node
    node = searchspace.make_root_node(task.initial_state)
    # store the best node option at present
    best_node =[node]



    while True:

        # the best_node as the father node for the current level nodes
        father_node_list = best_node
        best_state = []  # store the best state option at present
        best_node = []  # store the best node option at present

        for state_index in range(0, len(current_state_list)):
            # get the successors of current_state
            successors = task.get_successor_states(current_state_list[state_index])

            same_level_Node_list = []  # a list, store the node formed on the some level
            heuristic_value_list = []  # a list, store all the heuristic value

            if not successors:  # if there is no successors, then this search is failed.
                return False

            # go through all the successors and choose the best as the next state
            for successor in successors:
                # build up a child node
                node = searchspace.make_child_node(father_node_list[state_index], successor[0], successor[1])
                same_level_Node_list.append(node)
                # calculate the f(n)
                f_value = heuristic(node)
                # push one heuristic_value into heap
                heapq.heappush(heap, f_value)
                # push current f(n) into the list
                heuristic_value_list.append(f_value)

            # go through all the heuristic values
            for index in range(0, len(heuristic_value_list)):
                # if its' heuristic value is the smallest in all heuristic values

                if heuristic_value_list[index] == heap[0]:
                    best_state.append(successors[index][1])  # then choose it as the next state .

                    best_node.append(same_level_Node_list[index])  # choose this node as the next node

            # refresh the heap list for the next iteration.
            heap = []

        # choose the final current_best_state as the next state
        current_state_list = best_state

        # check whether there is a state satisfying the goal state
        for current_state in current_state_list:

            # this state does not satisfy the goal state
            if not task.goal_reached(current_state):
                continue
            else:
                for index in range(0, len(current_state_list)):
                    # get the final node, which satisfies the goal
                    if task.goal_reached(current_state_list[index]):
                        last_node = best_node[index]
                        # return the solution
                        return last_node.extract_solution()

首先,把state转化为node,以树为数据结构,方便找到goal之后的回溯和记录cost。
其次,对该点进行goal测试,是则进行下一步,不是则得出所有的子节点,计算其f(n)(与A*的计算方式不同)。同层对比寻找最小f(n)的点(局部最优解),循环这一步。
最后,返回整个Node_list。

注意:当局部的点有相同的f(n)时,应同时拓展这些点直到有高低之分为止

评价:该implement没有实现对goal_less情况的应对方式。没有对重复的state处理。goal_test应该针对frontiers(所有子节点),而不是选中的node

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,663评论 0 33
  • 上面是我家臭圆,我就是每天这样和她对视治好颈椎病的...哎哎哎,别走啊,我是开玩笑的。 才工作一年脖子就有点僵硬了...
    _番茄沙司阅读 776评论 5 5
  • 今天才确认以及肯定,知道了你喜欢什么样的。突然很想把你拉黑,退出你的世界,不再有任何交集。等到有一天让你大吃一惊。...
    流苏Bella阅读 136评论 0 0
  • 在家的第四天,从凌晨三点多守到早上七点多,看着她一次次的发作,我亲吻她的手,亲吻她的额头,依旧心如刀割,我内心...
    ___indulgence__阅读 346评论 0 0