0

私は100x100グリッドネットワークを扱っています。その中で情報がどの程度効率的に交換されているかを確認するために、その全体的な効率性を判断したいと考えています。

私は効率を計算するために特注の関数を使用しており、それを自分のネットワークに適用しています。

ただし、Memory Error関数が呼び出される行 (最後の行) を指す a に遭遇します。これは、Python が使用している RAM の量に依存しますか? どうすればこれを修正できますか?

コードは次のとおりです。

from __future__ import print_function, division
import numpy
from numpy import *
import networkx as nx
import matplotlib.pyplot as plt
import csv
from collections import *
import os
import glob
from collections import OrderedDict 

def global_efficiency(G, weight=None):
    N = len(G)
    if N < 2:
        return 0   
    inv_lengths = []
    for node in G:
        if weight is None:
            lengths = nx.single_source_shortest_path_length(G, node)
        else:
            lengths=nx.single_source_dijkstra_path_length(G,node,weight=weight)

        inv = [1/x for x in lengths.values() if x is not 0]
        inv_lengths.extend(inv)

    return sum(inv_lengths)/(N*(N-1))

N=100
G=nx.grid_2d_graph(N,N)
pos = dict( (n, n) for n in G.nodes() )
labels = dict( ((i, j), i + (N-1-j) * N ) for i, j in G.nodes() )
nx.relabel_nodes(G,labels,False)
inds=labels.keys()
vals=labels.values()
inds.sort()
vals.sort()
pos2=dict(zip(vals,inds))
nx.draw_networkx(G, pos=pos2, with_labels=False, node_size = 10)
eff=global_efficiency(G)
4

1 に答える 1

2

メモリエラーの理由がわかったと思います。各ノードのすべての最短パスのすべての長さを保持すると、非常に巨大なリストになる可能性がありますinv_lengths

同等の変更をお勧めします:

def global_efficiency(G, weight=None):
    N = len(G)
    if N < 2:
        return 0   
    inv_lengths = []
    for node in G:
        if weight is None:
            lengths = nx.single_source_shortest_path_length(G, node)
        else:
            lengths=nx.single_source_dijkstra_path_length(G,node,weight=weight)

        inv = [1/x for x in lengths.values() if x is not 0]

        # Changes here
        inv_sum = sum(inv)
        inv_lengths.append(inv_sum)  # add results, one per node

    return sum(inv_lengths)/(N*(N-1))

同じ結果が得られます(確認しました)。

于 2016-03-07T12:37:30.507 に答える