-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_multiple_simulations.py
65 lines (52 loc) · 2.64 KB
/
run_multiple_simulations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import itertools
import os
import multiprocessing
import pandas as pd
from Environment import Environment
from tqdm import tqdm
import numpy as np
# Constants for the simulation
GRID_SIZE = 500
INITIAL_POPULATION = 10
INFECTED_FRACTION = 0.2
MAX_TIME = 8760 # hours to simulate, 8760 is a year
SAVE_PATH = "/home/bgeurten/wolbachia_spread_model/raw_data/compare_spread_features/" # Update with your desired path
PARALLEL_THREADS = 20 #Number of parallel simulations
def run_simulation(args):
"""
Runs a single Wolbachia simulation and saves the results to a CSV file.
Parameters:
args (tuple): A tuple containing wolbachia_effects and trial_number.
"""
wolbachia_effects, trial_number = args
env = Environment(GRID_SIZE, INITIAL_POPULATION, wolbachia_effects, INFECTED_FRACTION)
for hour in range(MAX_TIME): # Simulation runs for one year or until all beetles are infected
env.run_simulation_step()
#if env.infected_fraction >= 1.0:
# break
# Process and save data to daily median values
save_simulation_results(env, wolbachia_effects, trial_number)
def save_simulation_results(env, wolbachia_effects, trial_number):
"""
Processes and saves the simulation results to a CSV file.
Parameters:
env (Environment): The simulation environment with data to be saved.
wolbachia_effects (dict): Dictionary of the Wolbachia effects used in the simulation.
trial_number (int): The trial number of the simulation.
"""
daily_population_size = [np.median(env.population_size[i:i+24]) for i in range(0, len(env.population_size), 24)]
daily_infection_rate = [np.median(env.infection_history[i:i+24]) for i in range(0, len(env.infection_history), 24)]
filename = os.path.join(SAVE_PATH, f"{'_'.join([k+'_'+str(v) for k,v in wolbachia_effects.items()])}_{trial_number:03d}.csv")
pd.DataFrame({'Population Size': daily_population_size, 'Infection Rate': daily_infection_rate}).to_csv(filename, index=False)
def main():
"""
Main function to run multiple simulations across various Wolbachia effect combinations.
"""
all_combinations = list(itertools.product([True, False], repeat=4))
trials = list(range(500))
jobs = [(dict(zip(['cytoplasmic_incompatibility', 'male_killing', 'increased_exploration_rate', 'increased_eggs'], combo)), trial) for combo in all_combinations for trial in trials]
# Run simulations in parallel using multiprocessing
with multiprocessing.Pool(PARALLEL_THREADS) as pool: # Adjust the number of processes as needed
list(tqdm(pool.imap(run_simulation, jobs), total=len(jobs)))
if __name__ == "__main__":
main()