degrees = array(sorted(degree(netx), key=lambda d: d[0]))[:, 1] max_d_node = argmax(degrees) # Get index of max degree optimal = optimal_distribution(deep_load=True) if uniform: red = array([balls_per_node] * N) else: red = zeros(N) red[max_d_node] = budget exposures = [] # Run basic metrics for metric_id, _ in enumerate(metric_names): simple_centrality(net, metric_id, red=red) exposures.append(run_polya(net)) # Run optimal strategy net.set_initial_distribution(black=optimal, red=red) exposures.append(run_polya(net)) # Define constants file_name = 'uniform_red' if uniform else 'single_red' img_name = '../../results/centrality_metrics/' + file_name + '.png' data_name = '../../data/centrality_metrics/' + file_name + '.csv' labels = metric_names + ['Optimal'] # Save and plot data save_trials(exposures, data_name, titles=labels) plot_infection(exposures, leg=labels, multiple=True, file_name=img_name)
trial_infection = [] # delay = array([0, 1, 2, 3, 5, 10, 25, 50]) delay = array(linspace(0, 50, 20)) for time in delay: per_node = time + balls_per_node budget = N * per_node if uniform: red = [per_node] * N else: degrees = dict_to_arr(degree(netx)) red = zeros(N) red[argmax(degrees)] = budget print(sum(red), budget, time) simple_centrality(net, 2, red=red) vals = run_polya(net, steps=time_limit) trial_infection.append(vals) else: trial_infection, delay = load_csv_col(data_name, with_headers=True, trans=True, parse=float) delay = array(delay).astype(float) trial_infection = array(trial_infection) time_n = len(trial_infection[0]) - 1 time_N_infections = trial_infection[:, time_n] # Save and plot data if fresh_data: save_trials(trial_infection, data_name, titles=delay)
# Define constants red_node_counts = list(range(1, 20)) fresh_data = False fig_path = '../../results/analysis/multi_red.png' data_path = '../../data/analysis/multi_red.csv' # Import data and generate network _, edges = load_airport_and_route(deep_load=True) netx = from_edgelist(edges) N = number_of_nodes(netx) net = network(N, graph=netx) budget = N * balls_per_node simple_centrality(net, 2, node_restriction=11) degrees = sorted(degree(netx), key=lambda d: d[1], reverse=True) infections = [] for i in red_node_counts: R = [0] * N total = 0 for j in range(i): total += degrees[j][1] for j in range(i): R[degrees[j][0]] += round(degrees[j][1] / total * budget) print(sum(R)) net.set_initial_distribution(red=R) infection = run_polya(net, steps=250, trials=3) infections.append(infection[len(infection) - 1])
ratios = array(linspace(1, 20, 20)) budget = balls_per_node * N # Define opponent distribution if uniform: red = [balls_per_node] * N else: degrees = dict_to_arr(degree(netx)) red = zeros(N) red[argmax(degrees)] = budget # Run trial for set of budget ratios for ratio in ratios: simple_centrality(net, 2, budget_ratio=ratio, red=red, node_restriction=11) vals = run_polya(net, steps=time_limit) trial_infection.append(vals) trial_infection = array(trial_infection) else: trial_infection, ratios = load_csv_col(data_name, with_headers=True, trans=True, parse=float) ratios = array(ratios).astype(float) # Prepare data time_n = len(trial_infection[0]) - 1