main

Main script to run simulations in parallel or single mode.

 1"""Main script to run simulations in parallel or single mode."""
 2
 3
 4import shutil  
 5import config
 6import time
 7import importlib
 8import sys
 9import constants
10
11if config.SCENARIO_NAME == "Base":
12    shutil.copy('config.py', 'constants.py')
13    shutil.copy('inputs/terminal_data_base.csv',  'inputs/terminal_data.csv')
14    import constants
15    importlib.reload(constants)
16
17elif config.SCENARIO_NAME == "BottleneckDetection" or config.SCENARIO_NAME == "BreakpointAnalysis":
18    if 'constants' in sys.modules:
19        del sys.modules['constants']
20    import constants
21
22else:
23    raise ValueError("Invalid SCENARIO_NAME. It should be either 'Base' or 'BottleneckDetection'.")
24
25import time
26import multiprocessing as mp
27from tqdm import tqdm
28from simulation_analysis.collate_results import collate_results
29from simulation_handler.run_simulation import run_simulation
30from simulation_handler.helpers import clean_results_directory
31from simulation_analysis.whatif_scenarios import disruption_report_smoothed, fog_peak_slopes
32
33
34def parallel_run():
35    """Main function to execute the simulation and collate results."""
36    start_time = time.time()
37    num_runs = constants.NUM_RUNS
38    num_cores = constants.NUM_CORES
39    start_seed = constants.START_SEED
40
41    if constants.DELETE_RESULTS_FOLDER:
42        clean_results_directory()
43    seeds = range(start_seed, start_seed + num_runs)
44    with mp.Pool(processes=num_cores) as pool:
45        for _ in tqdm(pool.imap_unordered(run_simulation, seeds), total=num_runs, desc="Simulations Progress"):
46            pass
47    total_time = round((time.time() - start_time) / 60, 2)
48    collate_results(num_runs, total_time)
49    if constants.MODEL_HURRICANE:
50        print("\nHurricane disruption report (smoothed):")
51        disruption_report_smoothed(plot_legend=True)
52    if constants.MODEL_FOG:
53        print("\nFog peak slopes:")
54        fog_peak_slopes()
55
56
57def single_run():
58    """Run a single seed simulation."""
59    start_time = time.time()
60    seed = constants.START_SEED
61    if constants.DELETE_RESULTS_FOLDER:
62        clean_results_directory()
63    run_simulation(seed)
64    # collate_results(1)
65    if constants.MODEL_HURRICANE:
66        print("\nHurricane disruption report (smoothed):")
67        disruption_report_smoothed()
68    if constants.MODEL_FOG:
69        print("\nFog peak slopes:")
70        fog_peak_slopes()
71    total_time = round((time.time() - start_time) / 60, 2)
72    print(f"\nTotal time taken: {total_time} minutes")
73   
74if __name__ == "__main__":
75    # single_run() # Comment this line and uncomment the next line to run multiple simulations
76    parallel_run()  # Comment this line and uncomment the previous line to run a single simulation
77
78    # if the SCENARIO_NAME is not "Base" or "BreakpointAnalysis", then the results will be saved in a new collated folder
79    if constants.SCENARIO_NAME == "BottleneckDetection":
80        num_runs = constants.NUM_RUNS
81        log_num = constants.LOG_NUM
82        ARRIVAL_INCREASE_FACTOR_CTR = constants.ARRIVAL_INCREASE_FACTOR_CTR
83        logfileid = f"{int(constants.NUM_MONTHS)}_months_{num_runs}_runs_run_{log_num}"
84        collated_folder = f"collatedResults/{logfileid}"
85        new_folder_name = f"bottleneckAnalysis/S_{constants.SCENARIO_NAME}_{ARRIVAL_INCREASE_FACTOR_CTR}x_{logfileid}"
86        shutil.copytree(collated_folder, new_folder_name)
87        shutil.rmtree(collated_folder)
88        print(f"Collated results saved in {new_folder_name} folder.")
89        
def parallel_run():
35def parallel_run():
36    """Main function to execute the simulation and collate results."""
37    start_time = time.time()
38    num_runs = constants.NUM_RUNS
39    num_cores = constants.NUM_CORES
40    start_seed = constants.START_SEED
41
42    if constants.DELETE_RESULTS_FOLDER:
43        clean_results_directory()
44    seeds = range(start_seed, start_seed + num_runs)
45    with mp.Pool(processes=num_cores) as pool:
46        for _ in tqdm(pool.imap_unordered(run_simulation, seeds), total=num_runs, desc="Simulations Progress"):
47            pass
48    total_time = round((time.time() - start_time) / 60, 2)
49    collate_results(num_runs, total_time)
50    if constants.MODEL_HURRICANE:
51        print("\nHurricane disruption report (smoothed):")
52        disruption_report_smoothed(plot_legend=True)
53    if constants.MODEL_FOG:
54        print("\nFog peak slopes:")
55        fog_peak_slopes()

Main function to execute the simulation and collate results.

def single_run():
58def single_run():
59    """Run a single seed simulation."""
60    start_time = time.time()
61    seed = constants.START_SEED
62    if constants.DELETE_RESULTS_FOLDER:
63        clean_results_directory()
64    run_simulation(seed)
65    # collate_results(1)
66    if constants.MODEL_HURRICANE:
67        print("\nHurricane disruption report (smoothed):")
68        disruption_report_smoothed()
69    if constants.MODEL_FOG:
70        print("\nFog peak slopes:")
71        fog_peak_slopes()
72    total_time = round((time.time() - start_time) / 60, 2)
73    print(f"\nTotal time taken: {total_time} minutes")

Run a single seed simulation.