传递环境变量

时间:2017-06-12 07:29:45

标签: python networkx

如何使用Relabel_node函数中的映射在switch类中传递环境变量(env)?

其实我试过了:

H = nx.relabel_nodes(G, mapping=switch(env) ,copy=True

但它不起作用,给我错误。 为模拟开始传递env变量至关重要。

**class switch(object):
    def __init__(self, id):
        self.id = id
        self.env = env
        self.store = simpy.Store(env)
        self.rate = 100
        self.out = None
        self.packets_rec = 0
        self.packets_drop = 0
        self.qlimit = None
        self.byte_size = 0  # Current size of the queue in bytes
        self.debug = False
        self.interfaces = []
        self.numinterfaces = 16
        self.busy = 0
        for i in range(self.numinterfaces):
            self.interfaces.append(SwitchPort(env, self.rate))

    def __repr__(self):
        return self.id

def edges_():
    """ Return the lanl internet view graph from lanl.edges
    """

    try:
        fh = open('edges.list', 'r')
    except IOError:
        print("File not found")
        raise

    G = nx.Graph()

    for line in fh.readlines():
        (node1, node2) = line.split()
        G.add_edge(str(node1), str(node2))

    # get largest component and assign ping times to G0time dictionary

    return G

if __name__ == '__main__':
    import networkx as nx

    env = simpy.Environment()
    G = edges_()
    H = nx.relabel_nodes(G, mapping=switch ,copy=True)
    print("The graph has %d nodes with %d edges" \
          % (nx.number_of_nodes(H), nx.number_of_edges(H)))**

1 个答案:

答案 0 :(得分:0)

根据您的Switch类代码,传递给switch对象的参数应为np.mean(xs)而不是numpy,因此修改Switch类以接受两个变量id和env,如下所示:

image.png

然后调用Switch对象创建将是:

id