本文整理汇总了Python中tree.Node.on_list方法的典型用法代码示例。如果您正苦于以下问题:Python Node.on_list方法的具体用法?Python Node.on_list怎么用?Python Node.on_list使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tree.Node
的用法示例。
在下文中一共展示了Node.on_list方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: find_solution_BFS
# 需要导入模块: from tree import Node [as 别名]
# 或者: from tree.Node import on_list [as 别名]
def find_solution_BFS(connections, initial_state, solution):
solved = False
visited_nodes = []
frontier_nodes = []
startNode = Node(initial_state)
frontier_nodes.append(startNode)
while(not solved) and len(frontier_nodes) != 0:
node = frontier_nodes[0]
# extract node and add it to visited_nodes
visited_nodes.append(frontier_nodes.pop(0));
if node.get_data() == solution:
# solution found
solved = True
return node
else:
# expand child nodes (cities with connection)
node_data = node.get_data();
children_list = []
for one_child in connections[node_data]:
child = Node(one_child)
children_list.append(child)
if not child.on_list(visited_nodes) \
and not child.on_list(frontier_nodes):
frontier_nodes.append(child)
node.set_children(children_list)
示例2: find_solution_rec_DFS
# 需要导入模块: from tree import Node [as 别名]
# 或者: from tree.Node import on_list [as 别名]
def find_solution_rec_DFS(node, solution, visited, limit):
if limit > 0:
visited.append(node)
if node.get_data() == solution:
return node
else:
# expand children nodes (cities with connection)
node_data = node.get_data();
children_list = []
for a_child in connections[node_data]:
child = Node(a_child)
if not child.on_list(visited):
children_list.append(child)
node.set_children(children_list)
for child_node in node.get_children():
if not child_node.get_data() in visited:
# recursive call
sol = find_solution_rec_DFS(child_node, solution, \
visited, limit-1)
if sol != None:
return sol
return None
示例3: find_sol_UCS
# 需要导入模块: from tree import Node [as 别名]
# 或者: from tree.Node import on_list [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)
示例4: find_solution_DFS
# 需要导入模块: from tree import Node [as 别名]
# 或者: from tree.Node import on_list [as 别名]
def find_solution_DFS(initial_state, solution):
solved = False
visited_nodes = []
frontier_nodes = []
startNode = Node(initial_state)
frontier_nodes.append(startNode)
while (not solved) and len(frontier_nodes) != 0:
for i in frontier_nodes:
print i,
print ""
node = frontier_nodes.pop()
# extract node and add it to visited_nodes
visited_nodes.append(node)
if node.get_data() == solution:
# solution found
solved = True
return node
else:
# expand children nodes
node_data = node.get_data()
# right operator
child = [node_data[0], node_data[1], node_data[3], node_data[2]]
right_child = Node(child)
if not right_child.on_list(visited_nodes) \
and not right_child.on_list(frontier_nodes):
frontier_nodes.append(right_child)
# central operator
child = [node_data[0], node_data[2], node_data[1], node_data[3]]
central_child = Node(child)
if not central_child.on_list(visited_nodes) \
and not central_child.on_list(frontier_nodes):
frontier_nodes.append(central_child)
# left operator
child = [node_data[1], node_data[0], node_data[2], node_data[3]]
left_child = Node(child)
if not left_child.on_list(visited_nodes) \
and not left_child.on_list(frontier_nodes):
frontier_nodes.append(left_child)
node.set_children([left_child, central_child, right_child])