当前位置: 首页>>代码示例>>Python>>正文


Python Stats.stat_node_expansion方法代码示例

本文整理汇总了Python中stats.Stats.stat_node_expansion方法的典型用法代码示例。如果您正苦于以下问题:Python Stats.stat_node_expansion方法的具体用法?Python Stats.stat_node_expansion怎么用?Python Stats.stat_node_expansion使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在stats.Stats的用法示例。


在下文中一共展示了Stats.stat_node_expansion方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from stats import Stats [as 别名]
# 或者: from stats.Stats import stat_node_expansion [as 别名]
class ProblemSolver:
    """
    Solves problems by searching for a solution.
    """

    def __init__(self):
        self.stats = Stats()

    def search_problem_solution(self, problem, algorithm, strategy=SearchStrategy.NONE):
        """
        Applys a search algorithm and strategy to a problem.
        """
        if algorithm == SearchAlgorithm.TREESEARCH:
            strategy = strategy if strategy != SearchStrategy.NONE else SearchStrategy.A_STAR
            return self.treesearch(problem, strategy)

        elif algorithm == SearchAlgorithm.GRAPHSEARCH:
            strategy = strategy if strategy != SearchStrategy.NONE else SearchStrategy.A_STAR
            return self.graphsearch(problem, strategy)

        elif algorithm == SearchAlgorithm.RBFS:
            return self.RBFS(problem)

        elif algorithm == SearchAlgorithm.ITERATIVE_DEEPENING:
            return self.iterative_deepening(problem)

        return NotImplemented

    def expand(self, problem, pnode):
        """
        Expands a node and returns its successors.
        """
        successors = []
        self.stats.stat_node_expansion(pnode)
        for act_res in problem.succ_f(pnode.state):
            cnode = Node(act_res.state, pnode, act_res.action, pnode.depth+1, pnode.pathCost+act_res.action.cost)
            cnode.h = problem.h(cnode)
            cnode.f = problem.f(cnode)
            successors.append(cnode)
            self.stats.stat_node_creation(cnode)
        #shuffle(successors)
        return successors

    def path(self, root, node):
        """
        Returns a path from a root node to a node in a deeper layer.
        """
        path = []
        while node != root:
            if node == None:
                return None
            path.insert(0, node);
            node = node.parent
        return path

    def get_stats(self):
        """
        Returns the statistics generated by applying search algorithms to
        problems.
        """
        return self.stats

    def treesearch(self, problem, strategy):
        """
        Searches for a solution to the given problem using the treesearch
        algorithm and a given search strategy.
        """
        root = Node(problem.start_state, None, Action(0, None))
        self.stats.stat_node_creation(root)
        
        fringe = Fringe(strategy)
        fringe.insert(root)
        while True:
            if fringe.empty():
                return None
            fringe.print_contents()

            currNode = fringe.pop()
            if (problem.is_goal(currNode)):
                return self.path(root, currNode)
            successors = self.expand(problem, currNode)
            if problem.maximum_depth > 0 and len([x for x in successors if x.depth > problem.maximum_depth]) > 0:
                return None
            fringe.extend(successors)

    def graphsearch(self, problem, strategy):
        """
        Searches for a solution to the given problem using the graphsearch
        algorithm and a given search strategy.
        """
        root = Node(problem.start_state, None, Action(0, None))
        self.stats.stat_node_creation(root)
        fringe = Fringe(strategy)
        fringe.insert(root)
        closed = set([])
        while True:
            if fringe == []:
                return None
            fringe.print_contents()

#.........这里部分代码省略.........
开发者ID:janstrohbeck,项目名称:problemsolver,代码行数:103,代码来源:problemsolver.py


注:本文中的stats.Stats.stat_node_expansion方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。