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)
_, 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)
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, connect=True)
_, 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)
data_path = '../../data/algorithm_performance/bonmin.csv' net_sizes = logspace(1, 3, 10) optimize_times = [] trials = 10 for raw_size in net_sizes: size = int(raw_size) netx = barabasi_albert_graph(size, 5) net = network(size, graph=netx) neighbourhoods = [[neigh.id for neigh in node] for node in net.nodes] init_dist = [balls_per_node] * size budget = balls_per_node * size tmp = 0 for i in range(trials): start = time() optimize_distribution(neighbourhoods, init_dist, init_dist, budget) end = time() tmp += end - start optimize_times.append(tmp / trials) print('Done ' + str(size)) data = array([net_sizes, optimize_times]) save_trials(optimize_times, data_path, titles=net_sizes, single_line=True) plot_scatter_data(data, file_name=img_name, x_label='Network Size', y_label='Time to Optimize (s)')
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) data = array([delay, time_N_infections]) plot_scatter_data(data, x_label='Time step delay', y_label='$I_{' + str(time_n) + '}$', connect=True, file_name=scatter_name, size=fig_size)
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, file_name=fig_path, x_label='Time Step, n', y_label='Variance', connect=True, y_format=True, dot_size=10, size=fig_size)
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 time_N_infections = trial_infection[:, time_n] # Save and plot data if fresh_data: save_trials(trial_infection, data_name, titles=ratios) data = array([ratios, time_N_infections]) plot_scatter_data(data, x_label='Black Budget / Red Budget', connect=True, y_label='$I_{' + str(time_n) + '}$', file_name=scatter_name, size=(10, 7.5))
for num in num_nodes: total = sum(cents[:num, 1]) B = [0] * N for i in range(num): ind = int(round(cents[i, 0])) B[ind] = cents[i, 1] / total * budget print(sum(B)) net.set_initial_distribution(black=B, red=R) tmp = run_polya(net, steps=time_limit) centrality_infections.append(tmp[len(tmp) - 1]) trial_exposures.append(centrality_infections) trial_exposures = array(trial_exposures) save_trials(trial_exposures, data_path, titles=num_nodes) else: trial_exposures, num_nodes = load_csv_col(data_path, with_headers=True, trans=True, parse=float) num_nodes = array(num_nodes).astype(float) # plot data data = [] for trial in trial_exposures: data.append([num_nodes, trial]) data = array(data) plot_scatter_data([data[0], data[1], data[3], data[2]], x_label='Number of Nodes with Black Balls, $m$',
from utilities import save_trials ex_data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] titles = ['a', 'b', 'c'] save_trials(ex_data, '../data/bla.csv', titles=titles)
for metric_id in metrics: data = calculate_clique_weights(net, metric_id=metric_id) trial_infections = [] for num in num_cliques: weighted_cliques(net, num, budget, data=data) trial = run_polya(net, trials=2) trial_infections.append(trial[len(trial) - 1]) infections.append(trial_infections) else: infections, num_cliques = load_csv_col(data_path, with_headers=True, parse=float, trans=True) num_cliques = list(map(int, num_cliques)) data = array([[num_cliques, infection] for infection in list(infections)]).astype(float) save_trials(infections, data_path, titles=num_cliques) plot_scatter_data(data, multiple=True, leg=metric_names, x_label='Number of Cliques', y_label='Time n infection', size=fig_size, connect=True, file_name=fig_path)