本文整理汇总了Python中tree.Node.get_cost方法的典型用法代码示例。如果您正苦于以下问题:Python Node.get_cost方法的具体用法?Python Node.get_cost怎么用?Python Node.get_cost使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tree.Node
的用法示例。
在下文中一共展示了Node.get_cost方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: find_sol_UCS
# 需要导入模块: from tree import Node [as 别名]
# 或者: from tree.Node import get_cost [as 别名]
def find_sol_UCS(conns, init_st, sol):
solved = False
visited_nodes = []
frontier_nodes = []
start_node = Node(init_st)
start_node.set_cost(0)
frontier_nodes.append(start_node)
while (not solved) and len(frontier_nodes) != 0:
# sort the frontier nodes
frontier_nodes = sorted(frontier_nodes, cmp=compare)
current_node = frontier_nodes[0]
# extract first node and add it to visited
visited_nodes.append(frontier_nodes.pop(0))
if current_node.get_data() == sol:
# solution found
solved = True
return current_node
else:
# expand child nodes (connected cities)
cn_data = current_node.get_data()
children_list = []
for ch in conns[cn_data]:
child = Node(ch)
# find g(n)
cost = conns[cn_data][ch]
child.set_cost(current_node.get_cost()+cost)
children_list.append(child)
if not child.on_list(visited_nodes):
# if is on list we replace it the new
# cost value, if less.
if child.on_list(frontier_nodes):
for n in frontier_nodes:
if n.equals(child) and n.get_cost() > child.get_cost():
frontier_nodes.remove(n)
frontier_nodes.append(child)
else:
frontier_nodes.append(child)
current_node.set_children(children_list)