_, routes = load_airport_and_route(deep_load=True) netx = from_edgelist(routes) N = number_of_nodes(netx) net = network(N, graph=netx) budget = balls_per_node * N print('Data imported and network generated') # Get optimal distribution optimal = optimal_distribution(uniform, deep_load=True) # Initialize opponent distribution if uniform: red = array([balls_per_node] * N) else: degrees = array(sorted(degree(netx), key=lambda d: d[0]))[:, 1] max_d_node = argmax(degrees) red = zeros(N) red[max_d_node] = budget # Run optimal strategy net.set_initial_distribution(black=optimal, red=red) exposures = run_polya(net, steps=num_steps) # Define constants file_name = ('uniform_red' if uniform else 'single_red') + '_trial' data_name = '../../data/optimal_distribution/' + file_name + '.csv' # Save and plot data save_trials(exposures, data_name, single_line=True)
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)
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]) data = array([red_node_counts, infections]) save_trials(infections, data_path, titles=red_node_counts, single_line=True) plot_scatter_data(data, x_label='Number of sites with red balls', y_label='$I_{250}$', file_name=fig_path, size=fig_size, connect=True)
fig_path = '../../results/analysis/uniform.png' # Run trial if fresh_data: # Import data and generate network _, routes = load_airport_and_route(deep_load=True) netx = from_edgelist(routes) N = number_of_nodes(netx) net = network(N, graph=netx) budget = balls_per_node * N print('Data imported and network generated') # Calculate node degree degrees = array(sorted(degree(netx), key=lambda d: d[0]))[:, 1] max_d_node = argmax(degrees) # Initialize opponent distribution red = zeros(N) red[max_d_node] = budget # Run basic metrics net.set_initial_distribution(red=red) exposures = run_polya(net, steps=steps) save_trials(exposures, data_name, single_line=True) else: exposures = load_csv_col(data_name, parse=float) # Plot data plot_infection(exposures, file_name=fig_path)
# Import data and generate network _, edges = load_airport_and_route(deep_load=True) netx = from_edgelist(edges) N = number_of_nodes(netx) budget = N * balls_per_node net = network(N, graph=netx) print('Data import and network generated') # Find and sort cliques cliques = sorted(find_cliques(netx), key=lambda c: len(c), reverse=True) trial_infections = [] num_cliques = linspace(1, 120, 40).astype(int) for num in num_cliques: popularity_contest(net, num, budget) trial = run_polya(net, trials=2) trial_infections.append(trial[len(trial) - 1]) else: trial_infections, num_cliques = load_csv_col(data_path, with_headers=True, parse=float, trans=True) data = array([num_cliques, trial_infections]) save_trials(trial_infections, data_path, titles=num_cliques, single_line=True) plot_scatter_data(data, x_label='Number of Cliques', y_label='Time n infection', size=fig_size, connect=True, file_name=fig_path)
netx = from_edgelist(edges) N = number_of_nodes(netx) net = network(N, graph=netx) bud = N * balls_per_node ratios = linspace(.1, .9, 15) budgets = [(bud / ratio - bud) for ratio in ratios] print(ratios) trial_infection = [] for budget in budgets: tmp = round(budget / N) B = [tmp] * N print(budget, tmp) net.set_initial_distribution(black=B) infection = run_polya(net, steps=num_steps, trials=5) trial_infection.append(infection[len(infection) - 1]) save_trials(trial_infection, data_path, titles=ratios, single_line=True) else: [trial_infection], ratios = load_csv_col(data_path, with_headers=True, parse=float) ratios = [1 / (1 + float(ratio)) for ratio in ratios] data = array([ratios, trial_infection]).astype(float) plot_scatter_data(data, file_name=fig_path, x_label='$\\frac{R}{R+B}$', y_label='$I_{250}$', size=fig_size,
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) # plot_infection(trial_infection, leg=delay, multiple=True, file_name=img_name, blocking=False)
# Initialize opponent distribution if uniform: red = array([balls_per_node] * N) else: degrees = array(sorted(degree(netx), key=lambda d: d[0]))[:, 1] max_d_node = argmax(degrees) red = zeros(N) red[max_d_node] = budget # Set initial distribution net.set_initial_distribution(red=red) # Run metric maximum_entropy(net, metric_id=1) exposures = run_polya(net) # Calculate number of nodes that were allocated balls B = [node.black for node in net.nodes] non_zero = 0 for Bi in B: if Bi > 0: non_zero += 1 print('Number of allocated nodes ' + str(non_zero)) # Define constants file_name = 'uniform_red' if uniform else 'single_red' data_name = '../../data/max_entropy/opt_' + file_name + '.csv' # Save and plot data save_trials(exposures, data_name, single_line=True)
max_ent_path = '../../data/analysis/max_ent_variance.csv' fig_path = '../../results/analysis/variance.png' if fresh_data: nodes, edges = load_airport_and_route(deep_load=True) netx = from_edgelist(edges) N = number_of_nodes(netx) net = network(N, graph=netx) x_vals = array(range(1, num_steps + 2)) R = [balls_per_node] * N # Maximum Entropy # maximum_entropy(net, metric_id=1) max_ent_infections = run_polya(net, steps=num_steps, combine=False, trials=num_trials) else: max_ent_infections = load_csv_col(max_ent_path, parse=float, trans=True) max_ent_var = var(max_ent_infections, axis=0) if fresh_data: save_trials(max_ent_infections, max_ent_path) max_ent_data = zeros((2, len(max_ent_var))) for i in range(len(max_ent_var)): max_ent_data[0, i] = i + 1 max_ent_data[1, i] = max_ent_var[i] plot_scatter_data(max_ent_data,