def generate_all_data_for_two_reach(): num_networks = 1000 sims_per_network = 25 high_reach = 60 low_reach = 30 contracted_reach = 20 sim_len_cap = 1000 disease = Disease(4, .3) lazy_networks = [ MakeLazySpatialNetwork( make_random_spatial_configuration((500, 500), 500, np.random.default_rng(seed))) for seed in tqdm(range(num_networks)) ] # TODO: add flicker and run behaviors = ((lambda reach, mknet, rng: no_update, 'Static Network'), (lambda reach, mknet, rng: SimplePressureBehavior( mknet(reach), rng, 2, .5), 'SimplePressure(radius=2, flicker_prob=.5)'), (lambda reach, mknet, rng: SpatialContraction( mknet(reach), mknet(contracted_reach), 2, .5, rng), 'SpatialContraction(radius=2, contraction_prob=.5)')) all_data = RawDataCSV(f'spatial nets\nsim_len={sim_len_cap} {disease}', {}) for mkbeh, beh_name in behaviors: temp_data = two_reach_sim(lazy_networks, sims_per_network, disease, high_reach, low_reach, sim_len_cap, mkbeh, beh_name) all_data = RawDataCSV.union(all_data.title, all_data, temp_data) all_data.save().save_boxplots()
def test(): name = 'test' diseases = [Disease(1, .69)] def new_network(): return Network(nx.connected_caveman_graph(10, 10), community_size=10) flicker_config = RandomFlickerConfig(.5, 'test_rand_config') rng = np.random.default_rng(66) run_inf_prob_vs_perc_sus(name, diseases, new_network, flicker_config, 10, rng)
def __init__(self, encoding_to_network: Callable[[np.ndarray], Network], rand) -> None: """ This objective is problematic because there is so much variance in how much a disease spreads. """ self._enc_to_G = encoding_to_network self._rand = rand self._disease = Disease(4, .2) self._sim_len = 100 self._n_sims = 100 self._decay_func = DecayFunction(.5)
def ba_pressure_vs_none_entry_point(): rng = np.random.default_rng(0xbeefee) num_trials = 1000 disease = Disease(4, .4) N = 500 m = 3 make_ba = MakeBarabasiAlbert(N, m, 0xbeefee) pressure_configurations = [ SimplePressureConfig(radius, prob, rng) for radius, prob in it.product((1, 2, 3), (.25, .5, .75)) ] pressure_experiment(make_ba, pressure_configurations, disease, num_trials, rng)
def poisson_entry_point(): """Run experiments on connected community networks with a poisson degree distribution.""" print('Running poisson connected community experiments') N_comm = 10 # agents per community num_comms = 50 # number of communities num_trials = 100 rand = np.random.default_rng(420) lam = 8 # this is lambda for both the inner and outer degree distributions configuration = PoissonConfiguration(rand, lam, lam, num_comms, N_comm) disease = Disease(4, .2) safe_run_trials(configuration.name, run_connected_community_trial, (configuration, disease, rand), num_trials)
def agent_generated_entry_point(seed=1337): print('Running agent generated experiments') num_trials = 100 rand = np.random.default_rng(seed) disease = Disease(4, .2) N = 500 behaviors = [ TimeBasedBehavior(N, lb_connection, ub_connection, steps_to_stability, rand) for lb_connection, ub_connection, steps_to_stability in [( 4, 6, 5), (4, 6, 10), (4, 6, 20)] ] for agent_behavior in behaviors: safe_run_trials(agent_behavior.name, run_agent_generated_trial, (disease, agent_behavior, N, rand), num_trials)
def uniform_entry_point(): """Run experiments on connected community networks with a unifrom degree distribution.""" print('Running uniform connected community experiments') num_trials = 100 disease = Disease(4, .2) rand = np.random.default_rng(0) configurations = [UniformConfiguration(rand, inner_bounds, outer_bounds, N_comm, num_comms) for inner_bounds, outer_bounds, N_comm, num_comms in [((10, 16), (2, 5), 25, 20), ((10, 16), (5, 11), 25, 20), ((5, 9), (2, 5), 10, 50), ((5, 9), (5, 9), 10, 50)]] for configuration in configurations: print(configuration.name) safe_run_trials(configuration.name, run_connected_community_trial, (configuration, disease, rand), num_trials)
def cc_pressure_vs_none_entry_point(): rng = np.random.default_rng(0xbeefee) num_trials = 1000 disease = Disease(4, .4) inner_bounds = 1, 15 outer_bounds = 1, 5 community_size = 20 n_communities = 25 make_ccn = MakeConnectedCommunity(community_size, inner_bounds, n_communities, outer_bounds, rng) pressure_configurations = [ SimplePressureConfig(radius, prob, rng) for radius, prob in it.product((1, 2, 3), (.25, .5, .75)) ] # pressure_experiment(make_ccn, pressure_configurations, disease, num_trials, rng) net = make_ccn() simulate(net.M, make_starting_sir(net.N, 1, rng), disease, pressure_configurations[1].make_behavior(net), 100, nx.spring_layout(net.G))
def connected_community_entry_point(): rng = np.random.default_rng(501) min_inner, max_inner = 1, 15 min_outer, max_outer = 1, 5 community_size = 20 n_communities = 25 next_network = MakeConnectedCommunity(community_size, (min_inner, max_inner), n_communities, (min_outer, max_outer), rng) base_name = f'Connected Community N_comm={community_size} [{min_inner}, {max_inner}]\n'\ f'num_comms={n_communities} [{min_outer}, {max_outer}]' diseases = [ Disease(2, trans_prob) for trans_prob in np.linspace(.1, 1.0, 25) ] for flicker_prob in np.linspace(1, .2, 9): run_inf_prob_vs_perc_sus( f'{base_name} flicker_prob={flicker_prob:.2f}', diseases, next_network, RandomFlickerConfig(flicker_prob, rand=rng), 100, rng)
def social_circles_entry_point(): print('Running social circles experiments.') num_trials = 100 rand = np.random.default_rng(0xdeadbeef) N = 500 N_purple = int(N * .1) N_blue = int(N * .2) N_green = N - N_purple - N_blue agents = { networkgen.Agent('green', 30): N_green, networkgen.Agent('blue', 40): N_blue, networkgen.Agent('purple', 50): N_purple } # grid_dim = (int(N/.005), int(N/.005)) # the denominator is the desired density grid_dim = N, N disease = Disease(4, .2) title = f'Social Circles -- Elitist {grid_dim} {N}' safe_run_trials(title, run_social_circles_trial, (agents, grid_dim, disease, rand), num_trials)
def entry_point(): start_time = time.time() network_names = ('agent-generated-500', 'annealed-agent-generated-500', 'annealed-large-diameter', 'annealed-medium-diameter', 'annealed-short-diameter', 'cgg-500', 'watts-strogatz-500-4-.1', 'elitist-500', 'spatial-network', 'connected-comm-50-10', 'cavemen-50-10') network_paths = ['networks/' + name + '.txt' for name in network_names] # verify that all the networks exist found_errors = False for path in network_paths: if not os.path.isfile(path): print(f'{path} does not exist!') found_errors = True if found_errors: print('Fix errors before continuing') exit(1) flicker_configurations = [ StaticFlickerConfig((True, ), 'Static'), StaticFlickerConfig((True, True, False), 'Two Thirds Flicker'), StaticFlickerConfig((True, False), 'One Half Flicker'), StaticFlickerConfig((True, False, False), 'One Third Flicker') ] arguments = [(path, 1000, 500, Disease(4, .2), flicker_configurations, flicker_configurations[0].name) for path in network_paths] # use a maximum of 10 cores with Pool(min(len(arguments), 10)) as p: expirement_results = p.map(run_experiments, arguments) # type: ignore results_dir = 'experiment results' if not os.path.exists(results_dir): os.mkdir(results_dir) for result in expirement_results: if result is not None: result.save(results_dir) print(f'Finished simulations ({time.time()-start_time}).')
def main(): n_trials = 100 max_steps = 100 rng = np.random.default_rng(0) disease = Disease(4, .2) names = ('elitist-500', 'cavemen-50-10', 'spatial-network', 'cgg-500', 'watts-strogatz-500-4-.1') paths = fio.network_names_to_paths(names) behavior_configs = (RandomFlickerConfig(.5, 'Random .5', rng), StaticFlickerConfig((True, False), 'Static .5')) for net_name, path in zip(names, paths): net = fio.read_network(path) to_flicker = tuple((u, v) for u, v in net.edges if net.communities[u] != net.communities[v]) proportion_flickering = len(to_flicker) / net.E social_good = rate_social_good(net) trial_to_pf = tuple(proportion_flickering for _ in range(n_trials)) trial_to_sg = tuple(social_good for _ in range(n_trials)) print(f'Running simulations for {net_name}.') for config in behavior_configs: behavior = config.make_behavior(net.M, to_flicker) sim_results = [ get_final_stats( simulate(net.M, make_starting_sir(net.N, 1, rng), disease, behavior, max_steps, None, rng=rng)) for _ in tqdm(range(n_trials)) ] results = BasicExperimentResult(f'{net_name} {config.name}', sim_results, trial_to_pf, trial_to_sg) results.save_csv(RESULTS_DIR) results.save_box_plots(RESULTS_DIR) results.save_perc_sus_vs_social_good(RESULTS_DIR)
def pressure_test_entry_point(): rng = np.random.default_rng() net = fio.read_network('networks/cavemen-50-10.txt') simulate(net.M, make_starting_sir(net.N, (0, ), rng), Disease(4, 0.3), SimplePressureBehavior(net, 1), 200, None, rng)