当前位置: 首页>>代码示例>>Python>>正文


Python AGraph.iteredges方法代码示例

本文整理汇总了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')
开发者ID:goulu,项目名称:firegraph,代码行数:36,代码来源:graphviz2firegraph.py

示例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)
开发者ID:bx5974,项目名称:simpleai,代码行数:73,代码来源:dotsearch.py


注:本文中的pygraphviz.AGraph.iteredges方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。