本文整理汇总了Python中Graph.Graph.showGraph方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.showGraph方法的具体用法?Python Graph.showGraph怎么用?Python Graph.showGraph使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Graph.Graph
的用法示例。
在下文中一共展示了Graph.showGraph方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: doSearch
# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import showGraph [as 别名]
def doSearch(graph: Graph, root: Node, goal_position: MapPoint,
search_type: SearchMethods) -> bool:
'''Perform search on a graph.
Returns true if the solution was found.'''
# This variable will get true if we find a solution, i.e., a path from the
# start position to the goal
solutionFound = False
# Output debug line
print(' ----> Performing depth-first search in a grid-based map:\n')
# Show initial map as text in the terminal
graph.showGraph()
time.sleep(0.1) # Wait 0.1 s, just to allow seeing the number go by.
# List of nodes which were already generated but not yet explored.
nodesToExplore = deque()
# Add the root node to the nodes that were already generated, but not yet
# explored. This will be the first to expanded.
nodesToExplore.append(root)
# Keep expanding nodes until we found a solution (a path from start
# position to the goal position), or until there are no more nodes to
# explore.
while(len(nodesToExplore) > 0):
# Get the first node on the list of nodes to be explored (the node is
# also removed from the list of nodes to be explored)
node = nodesToExplore.popleft()
# Check if the current node is the solution, that is, if its position
# corresponds to the goal position. If so, the search ends now.
if((node.map_position_.x == goal_position.x) and
(node.map_position_.y == goal_position.y)):
# We found the solution, leave...
solutionFound = True
break
# Expand node by generating all its children, stored in the newNodes
# variable.
newNodes = node.expand()
# Add the new nodes to the list of nodes that were already generated
# but not yet explored.
if(search_type == SearchMethods.DEPTH_FIRST):
###################################################################
# Place code here to update nodesToExplore for Depth-first search
###################################################################
pass # REPLACE ME
###################################################################
elif(search_type == SearchMethods.BREADTH_FIRST):
###################################################################
# Place code here to update nodesToExplore for Breadth-first search
###################################################################
pass # REPLACE ME
###################################################################
elif(search_type == SearchMethods.A_STAR):
# Add the nodes such that the ones with lowest total cost are in
# the beggining.
for new_node in newNodes:
# Look for the node with higher total cost than this one, and
# insert the new node before that node.
# This could be done in a more efficient way!
i = 0
while i < len(nodesToExplore):
if(nodesToExplore[i].total_cost_ > new_node.total_cost_):
break
else:
i += 1
nodesToExplore.insert(i, new_node)
# Show map as text for debugging purposes
graph.showGraph()
time.sleep(0.1) # Wait 0.1 s, just to allow seeing the values go by.
return solutionFound
示例2: MapPoint
# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import showGraph [as 别名]
if __name__ == '__main__':
# Start and goal positions of the robot
start_position = MapPoint(6, 1) # Column (x) 6 and row (y) 1
goal_position = MapPoint(1, 6) # Column (x) 1 and row (y) 6
# Create graph with associated grid map
map_graph = Graph('map.png', goal_position)
# Create root node at the given position
# map_graph - graph this node belongs to
# None - no parent
# 0 - no cost
# heuristic function
# start_position
# None - no action needed to reach this node
root = Node(map_graph, None, 0, heuristic, start_position, None)
map_graph.addNode(root, True)
# Perform the map search and, if successful, show the resulting path in the
# terminal output as text
if(doSearch(map_graph, root, goal_position, SearchMethods.A_STAR)):
map_graph.showGraph()
map_graph.showPath(goal_position)
else:
eprint('There is no solution for the specified problem!')
# We're done, lets leave successfully, after pressing any key
# Keep figures open until the user presses 'q' to quit. This is a blocking
# statement
plt.show()