本文整理汇总了Python中pygraphviz.AGraph.itersucc方法的典型用法代码示例。如果您正苦于以下问题:Python AGraph.itersucc方法的具体用法?Python AGraph.itersucc怎么用?Python AGraph.itersucc使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pygraphviz.AGraph
的用法示例。
在下文中一共展示了AGraph.itersucc方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: DotGraphSearchProblem
# 需要导入模块: from pygraphviz import AGraph [as 别名]
# 或者: from pygraphviz.AGraph import itersucc [as 别名]
class DotGraphSearchProblem(Problem):
"""
Playground for stuff in the library... eats a .dot graph and allows you
to try it with the search methods.
"""
def __init__(self, filename):
self.G = AGraph(filename)
xs = [(nodo, nodo.attr.get("initial", None))
for nodo in self.G.iternodes()]
xs = [x for x in xs if x[1]]
if len(xs) == 0:
raise BadInputGraph("Missing 'initial' node")
elif len(xs) > 1:
raise BadInputGraph("Cannot have two initial nodes")
if not any(nodo.attr.get("goal", None) for nodo in self.G.iternodes()):
raise BadInputGraph("Missing a goal state '[goal=\"1\"]'")
super(DotGraphSearchProblem, self).__init__(xs[0][0])
self.initial_state.attr["shape"] = "doublecircle"
for node in self.G.iternodes():
if self.is_goal(node):
node.attr["shape"] = "hexagon"
node.attr["color"] = "blue"
self.seen = set()
self.visit(self.initial_state)
for edge in self.G.iteredges():
edge.attr["style"] = "dotted"
x = edge.attr.get("weight", None)
if x:
x = int(x)
else:
x = 1
edge.attr["weight"] = x
edge.attr["label"] = x
def actions(self, state):
assert state in self.G
if self.G.is_directed():
return self.G.itersucc(state)
else:
assert self.G.is_undirected()
return self.G.iterneighbors(state)
def result(self, state, action):
assert state in self.G and action in self.G
self.visit(state)
return action
def cost(self, state1, action, state2):
assert state1 in self.G and action in self.G and action == state2
x = self.G.get_edge(state1, state2).attr["weight"]
if float(x) == int(x):
return int(x)
else:
return float(x)
def visit(self, state):
if state in self.seen:
return
self.seen.add(state)
attr = self.G.get_node(state).attr
attr["color"] = "firebrick"
def is_goal(self, state):
return bool(state.attr.get("goal", False))
def value(self, state):
assert state in self.G
value = self.G.get_node(state).attr.get("value", None)
if not value:
return 0
return float(value)