本文整理汇总了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()
#.........这里部分代码省略.........