I am trying to randomly select nodes using NDLib and Network X. Instead I am coming up with the same number repeated throughout the list. Is there anyway to adjust this code to randomly choose a number
import networkx as nx
import numpy as np
from networkx.drawing.nx_pydot import graphviz_layout
import ndlib.models.ModelConfig as mc
import ndlib.models.epidemics as ep
import operator
import random
G= nx.erdos_renyi_graph(1000, 0.1)##This might be a bad example
def simulate_threshold(G, importance_measure=None, iterate=50, n=1, threshold=0.25):
if importance_measure:
# select seed nodes
sorted_node = sorted(importance_measure(G).items(), key=operator.itemgetter(1))[::-1]
highest_nodes = [n for n, _ in sorted_node[:n]]
infected_nodes=highest_nodes
config = mc.Configuration()
config.add_model_initial_configuration("Infected", infected_nodes)
else:
fraction=float(n)/len(G.nodes)
fraction = random.uniform(0,fraction)
config = mc.Configuration()
config.add_model_parameter('fraction_infected', float(n)/len(G.nodes))
# Model selection
model = ep.ThresholdModel(G, seed = 42)
for i in G.nodes:
config.add_node_configuration("threshold",i,threshold)
model.set_initial_status(config)
# Simulation execution
iterations = model.iteration_bunch(iterate)
return [it['node_count'][1] for it in iterations]
I=50
N=5
T=0.3
random.seed(42)
node_count_random = simulate_threshold(G, importance_measure=None, iterate=I, n=N, threshold=T)
Aucun commentaire:
Enregistrer un commentaire