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


Python DiGraph.in_edges方法代码示例

本文整理汇总了Python中networkx.DiGraph.in_edges方法的典型用法代码示例。如果您正苦于以下问题:Python DiGraph.in_edges方法的具体用法?Python DiGraph.in_edges怎么用?Python DiGraph.in_edges使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在networkx.DiGraph的用法示例。


在下文中一共展示了DiGraph.in_edges方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TermGraph

# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import in_edges [as 别名]

#.........这里部分代码省略.........
        """
        Return a topologically-sorted iterator over the terms in ``self`` which
        need to be computed.
        """
        return iter(topological_sort(
            self.graph.subgraph(
                {term for term, refcount in refcounts.items() if refcount > 0},
            ),
        ))

    def ordered(self):
        return iter(topological_sort(self.graph))

    @lazyval
    def loadable_terms(self):
        return tuple(
            term for term in self.graph if isinstance(term, LoadableTerm)
        )

    @lazyval
    def jpeg(self):
        return display_graph(self, 'jpeg')

    @lazyval
    def png(self):
        return display_graph(self, 'png')

    @lazyval
    def svg(self):
        return display_graph(self, 'svg')

    def _repr_png_(self):
        return self.png.data

    def initial_refcounts(self, initial_terms):
        """
        Calculate initial refcounts for execution of this graph.

        Parameters
        ----------
        initial_terms : iterable[Term]
            An iterable of terms that were pre-computed before graph execution.

        Each node starts with a refcount equal to its outdegree, and output
        nodes get one extra reference to ensure that they're still in the graph
        at the end of execution.
        """
        refcounts = self.graph.out_degree()
        for t in self.outputs.values():
            refcounts[t] += 1

        for t in initial_terms:
            self._decref_depencies_recursive(t, refcounts, set())

        return refcounts

    def _decref_depencies_recursive(self, term, refcounts, garbage):
        """
        Decrement terms recursively.

        Notes
        -----
        This should only be used to build the initial workspace, after that we
        should use:
        :meth:`~zipline.pipeline.graph.TermGraph.decref_dependencies`
        """
        # Edges are tuple of (from, to).
        for parent, _ in self.graph.in_edges([term]):
            refcounts[parent] -= 1
            # No one else depends on this term. Remove it from the
            # workspace to conserve memory.
            if refcounts[parent] == 0:
                garbage.add(parent)
                self._decref_depencies_recursive(parent, refcounts, garbage)

    def decref_dependencies(self, term, refcounts):
        """
        Decrement in-edges for ``term`` after computation.

        Parameters
        ----------
        term : zipline.pipeline.Term
            The term whose parents should be decref'ed.
        refcounts : dict[Term -> int]
            Dictionary of refcounts.

        Return
        ------
        garbage : set[Term]
            Terms whose refcounts hit zero after decrefing.
        """
        garbage = set()
        # Edges are tuple of (from, to).
        for parent, _ in self.graph.in_edges([term]):
            refcounts[parent] -= 1
            # No one else depends on this term. Remove it from the
            # workspace to conserve memory.
            if refcounts[parent] == 0:
                garbage.add(parent)
        return garbage
开发者ID:FranSal,项目名称:zipline,代码行数:104,代码来源:graph.py

示例2: synthesize

# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import in_edges [as 别名]
def synthesize(tsys, exprtab, init_flags='ALL_ENV_EXIST_SYS_INIT'):
    assert init_flags.upper() == 'ALL_ENV_EXIST_SYS_INIT', 'Only the initial condition interpretation ALL_ENV_EXIST_SYS_INIT is supported.'

    W, Y_list, X_list = get_winning_set(tsys, return_intermediates=True)
    initial_states = get_initial_states(W, tsys, exprtab, init_flags)
    if initial_states is None:
        return None

    goalnames = ['SYSGOAL'+str(i) for i in range(tsys.num_sgoals)]

    for goalmode in range(tsys.num_sgoals):
        Y_list[goalmode][0] = set([s for s in W if goalnames[goalmode] in tsys.G.node[s]['sat']])

    strategy = DiGraph()
    next_id = len(initial_states)
    workset = list(range(next_id))
    strategy.add_nodes_from([(i, {'state': s, 'mode': 0, 'initial': True})
                             for (i,s) in enumerate(initial_states)])
    while len(workset) > 0:
        nd = workset.pop()

        j = 0
        while j < len(Y_list[strategy.node[nd]['mode']]):
            if strategy.node[nd]['state'] in Y_list[strategy.node[nd]['mode']][j]:
                break
            j += 1
        if j == 0:
            assert goalnames[strategy.node[nd]['mode']] in tsys.G.node[strategy.node[nd]['state']]['sat']
            original_mode = strategy.node[nd]['mode']
            while goalnames[strategy.node[nd]['mode']] in tsys.G.node[strategy.node[nd]['state']]['sat']:
                strategy.node[nd]['mode'] = (strategy.node[nd]['mode'] + 1) % tsys.num_sgoals
                if strategy.node[nd]['mode'] == original_mode:
                    break
            if strategy.node[nd]['mode'] != original_mode:
                repeat_found = False
                for possible_repeat, attr in strategy.nodes_iter(data=True):
                    if (possible_repeat != nd
                        and attr['mode'] == strategy.node[nd]['mode']
                        and attr['state'] == strategy.node[nd]['state']):
                        repeat_found = True
                        for (u,v) in strategy.in_edges_iter(nd):
                            strategy.add_edge(u, possible_repeat)
                        strategy.remove_edges_from(strategy.in_edges(nd))
                        strategy.remove_node(nd)
                        break
                if repeat_found:
                    continue

            j = 0
            while j < len(Y_list[strategy.node[nd]['mode']]):
                if strategy.node[nd]['state'] in Y_list[strategy.node[nd]['mode']][j]:
                    break
                j += 1
            if j == 0:
                assert goalnames[strategy.node[nd]['mode']] in tsys.G.node[strategy.node[nd]['state']]['sat']

        for envpost in tsys.envtrans[strategy.node[nd]['state']]:
            next_state = None
            for succ_nd in tsys.G.successors(strategy.node[nd]['state']):
                if (tuple([succ_nd[i] for i in tsys.ind_uncontrolled]) == envpost
                    and ((j > 0 and succ_nd in Y_list[strategy.node[nd]['mode']][j-1])
                         or (j == 0 and succ_nd in W))):
                    next_state = succ_nd
                    break

            if next_state is None:
                assert j > 0
                if j == 0:
                    import pdb; pdb.set_trace()
                blocking_index = None
                blocking_sets = X_list[strategy.node[nd]['mode']][j-1]
                for k in range(len(blocking_sets)):
                    if strategy.node[nd]['state'] in blocking_sets[k]:
                        blocking_index = k
                        break
                assert blocking_index is not None
                for succ_nd in tsys.G.successors(strategy.node[nd]['state']):
                    if (tuple([succ_nd[i] for i in tsys.ind_uncontrolled]) == envpost
                        and succ_nd in blocking_sets[blocking_index]):
                        next_state = succ_nd
                        break
                assert next_state is not None

            foundmatch = False
            for candidate, cattr in strategy.nodes_iter(data=True):
                if cattr['state'] == next_state and cattr['mode'] == strategy.node[nd]['mode']:
                    strategy.add_edge(nd, candidate)
                    foundmatch = True
                    break
            if not foundmatch:
                if j == 0:
                    new_mode = (strategy.node[nd]['mode'] + 1) % tsys.num_sgoals
                else:
                    new_mode = strategy.node[nd]['mode']

                workset.append(next_id)
                strategy.add_node(next_id, {'state': next_state,
                                            'mode': new_mode,
                                            'initial': False})
                strategy.add_edge(nd, next_id)
#.........这里部分代码省略.........
开发者ID:slivingston,项目名称:gr1py,代码行数:103,代码来源:solve.py


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