本文整理汇总了Python中pygraphviz.AGraph.iteredges方法的典型用法代码示例。如果您正苦于以下问题:Python AGraph.iteredges方法的具体用法?Python AGraph.iteredges怎么用?Python AGraph.iteredges使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pygraphviz.AGraph
的用法示例。
在下文中一共展示了AGraph.iteredges方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: graphviz_to_firebase
# 需要导入模块: from pygraphviz import AGraph [as 别名]
# 或者: from pygraphviz.AGraph import iteredges [as 别名]
def graphviz_to_firebase(dirname,filename):
"""
since several chars must be escaped from firebase keys,
(see #http://stackoverflow.com/questions/19132867/adding-firebase-data-dots-and-forward-slashes
we can't directly use them as keys so:
- we use integer i as key,
- "nodes" dict maps node name to i to preserve edges coherency
- node name is kept as default node label
"""
f=os.path.join(dirname, filename)
graph=filename.split('.')[0]
print('processing %s'%f),
agraph=AGraph(f)
nodes={}
for i,node in enumerate(agraph.iternodes()):
nodes[node]=i
attrs={}
attrs.update(agraph.node_attr)
attrs['label']=node #default label is node id
attrs.update(node.attr)
key='%s/node/%d'%(graph,i)
ref.patch(key,attrs)
print('.'),
for i,edge in enumerate(agraph.iteredges()):
attrs={}
attrs.update(agraph.edge_attr)
attrs.update(edge.attr)
attrs['source']=nodes[edge[0]]
attrs['target']=nodes[edge[1]]
#edges are added as attributes of their destination node
key='%s/edge/%d'%(graph,i)
ref.patch(key,attrs)
print('-'),
logging.info('ok')
示例2: DotGraphSearchProblem
# 需要导入模块: from pygraphviz import AGraph [as 别名]
# 或者: from pygraphviz.AGraph import iteredges [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)