def run_world_with_demand_and_power_plants(self): number_of_steps = 40 data_folder = "minimum_sized_country_3" fixed_params = { "initialization_year": 2018, "number_of_steps": number_of_steps, "demand_change": [1.0] * 50, "carbon_price_scenario": [10] * 50, "data_folder": data_folder, "time_run": False } variable_params = { "total_demand": [1000, 5000, 10000, 20000, 30000, 40000, 50000, 75000, 100000] } batch_run = BatchRunnerMP(World, fixed_parameters=fixed_params, variable_parameters=variable_params, iterations=1, max_steps=number_of_steps, nr_processes=3) batch_run.run_all()
def launch_batch_processing(self): batch = BatchRunnerMP( self.mock_model, nr_processes=None, variable_parameters=self.variable_params, fixed_parameters=self.fixed_params, iterations=self.iterations, max_steps=self.max_steps, model_reporters=self.model_reporters, agent_reporters=self.agent_reporters, ) batch.run_all() return batch
def launch_batch_processing_debug(self): """ Tests with one processor for debugging purposes """ batch = BatchRunnerMP( self.mock_model, nr_processes=1, variable_parameters=self.variable_params, fixed_parameters=self.fixed_params, iterations=self.iterations, max_steps=self.max_steps, model_reporters=self.model_reporters, agent_reporters=self.agent_reporters, ) batch.run_all() return batch
def perform_analysis(): problem = { 'num_vars': 1, 'names': [ 'tolerance' ], # available parameters: 'tolerance','social_extroversion','mobility','decay' 'bounds': [[0.01, 0.99]] } # Set the repetitions, the amount of steps, and the amount of distinct values per variable replicates = 10 max_steps = 10 distinct_samples = 10 # Set the outputs model_reporters = { "Friends score": lambda m: m.avg_friends_score(), "Friends distance": lambda m: m.avg_friends_social_distance(), "Friends spatial distance": lambda m: m.avg_friends_spatial_distance() } data = {} begin = time.time() for i, var in enumerate(problem['names']): # Get the bounds for this variable and get <distinct_samples> samples within this space (uniform) samples = np.linspace(*problem['bounds'][i], num=distinct_samples) batch = BatchRunnerMP(Friends, max_steps=max_steps, iterations=replicates, variable_parameters={var: samples}, model_reporters=model_reporters, display_progress=True, nr_processes=multiprocessing.cpu_count() - 1) batch.run_all() end = time.time() print("Performed", replicates * distinct_samples, "model runs in", np.round(end - begin), "seconds.") data[var] = batch.get_model_vars_dataframe() return [problem, data]
def run_ofat(self): """ Collects data of model for ofat analysis. Writes raw data as nested pandas dataframe to pickle and csv. """ # We define our variables and bounds params = { 'obstacle_density': [0, 15, 30], 'food_density': [5, 15, 25], 'nr_hives': [1, 3, 5] } # Set the repetitions, the amount of steps, and the amount of distinct values per variable replicates = 1 max_steps = 30 # Define output parameters model_reporters = { 'step_data': lambda m: m.datacollector.get_model_vars_dataframe(), 'obstacle_density': lambda m: m.obstacle_density, 'food_density': lambda m: m.food_density, 'nr_hives': lambda m: m.nr_hives } data = {} for var in params: batch = BatchRunnerMP(BeeForagingModel, max_steps=max_steps, nr_processes=os.cpu_count(), iterations=replicates, variable_parameters={var: params[var]}, model_reporters=model_reporters, display_progress=True) batch.run_all() data = batch.get_model_vars_dataframe() data.to_csv(f'pickles/{self.time_stamp}_{var}.csv') data.to_pickle(f'pickles/{self.time_stamp}_{var}.p')
max_steps=100, display_progress=True, model_reporters={ "history": track_model_steps, }, ) # simulation batch run total_iter, num_conf, num_iter = get_info(batch_run) print(f'Starting simulation with the following setup:') print(f'- Total number of iterations: {total_iter}') print(f'- Number of configurations: {num_conf}') print(f'- Iterations per configuration: {num_iter}') print(f'- Number of processing cores: {CPU_COUNT}') start = time.time() batch_run.run_all() end = time.time() duration = end - start print( f'Simulation completed after {duration:.2f} seconds (speed: {total_iter/duration:.2f} iterations/second)' ) # tracking data df = get_tracking_data(batch_run) print(f'Created dataframe from batch run data') timestr = time.strftime("%Y%m%d-%H%M%S") print( f'Created raw dataframe with following shape: {df.shape[0]} rows, {df.shape[1]} columns' ) # data preprocessing
import matplotlib.pyplot as plt from mesa.batchrunner import BatchRunnerMP from tictactoe import get_game_grid, TicTacToe runner = BatchRunnerMP( TicTacToe, nr_processes=2, iterations=256, model_reporters={ 'endgame': lambda m: m.endgame, 'endgrid': get_game_grid }, ) runner.run_all() df = runner.get_model_vars_dataframe() print(df.endgame.value_counts()) fig = plt.figure(figsize=(16, 16)) for i in range(256): subplot = fig.add_subplot(16, 16, i + 1) subplot.imshow(df.endgrid[i]) subplot.axis('off') subplot.set_title(df.endgame[i].name, size=8) fig.tight_layout() plt.subplots_adjust(wspace=-.9)
"recovered_seed_pc": [0.01, 0.1, 0.23], "high_risk_pc": [0.25], "grid_area": ["Small", "Large"], "house_init": ["Neighborhood"], "release_strat": [ "Everyone release", "Random individual houses", "Low risk individuals", "Low risk houses" ], "mobility_speed": ["low"], "weeks_to_second_release": [2, 4] } br = BatchRunnerMP( Virus, nr_processes=4, variable_parameters=br_params, iterations=3, # number of times to run each parameter combination max_steps=600, # number of steps for each model run model_reporters={"Data Collector": lambda m: m.datacollector}) if __name__ == '__main__': br.run_all() br_df = br.get_model_vars_dataframe() br_step_data = pd.DataFrame() for i in range(len(br_df["Data Collector"])): if isinstance(br_df["Data Collector"][i], DataCollector): i_run_data = br_df["Data Collector"][i].get_model_vars_dataframe() br_step_data = br_step_data.append(i_run_data, ignore_index=True) br_step_data.to_csv( "/Users/shwu2259/GroupRotation/VirusModel_lowmob_lowfrac.csv")
def run_model( input_file, output_file, n_processors, class_id=None, all_classes=True, webserver=False, model_params=None, test_mode=False, speedup=1, ): input_filepath = os.path.join(os.getcwd(), input_file) all_data = InputData(input_filepath) class_ids = all_data.get_class_ids() if webserver: if all_classes: click.echo("Cannot run over all classes in webserver mode (yet!)") sys.exit(2) else: if not all_classes: if class_id not in class_ids: click.echo( f"Invalid class ID {class_id}. Valid classes are: {class_ids}" ) sys.exit(1) if not webserver: class_ids = [class_id] output_data_writer = OutputDataWriter(output_file) # Get data first to determine grid size model_initial_state = ModelState(0, 0, 0, 0, 0) logging.info("Running on classes: %s", ", ".join([str(i) for i in class_ids])) if not model_params: model_params = DEFAULT_MODEL_PARAMS if test_mode: model_params.maths_ticks_mean = 10 model_params.maths_ticks_sd = 0.1 model_params.ticks_per_home_day = 10 # To ensure each thread in the BatchProcessor gets a different random # number generator, we use a seed sequence to generate a new seed for # each instance of SimModel (one per class), as they run on parallel # processors in batch mode, and we need to ensure they don't all produce # the same numbers # We use a non-reproducible seed sequence to ensure changes to parameters # are not masked by the random numbers generated ss = np.random.SeedSequence() # If we want the rngs to be reproducible in batch mode, we can use the # following to get a SeedSequence (see # https://albertcthomas.github.io/good-practices-random-number-generators/) # random_number_generator = np.random.default_rng(2021) # ss = random_number_generator.bit_generator._seed_seq # Create an rng for each class rngs = [np.random.default_rng(s) for s in ss.spawn(len(class_ids))] if webserver: canvas_grid = create_canvas_grid(14, 14) css_element = CssElement() pupil_element = PupilMonitorElement() class_element = ClassMonitorElement() max_speedup = round(model_params.maths_ticks_mean / 100) * 100 summary_filepath = os.path.join( os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "classes_input", "sample_class_summaries.csv", ) summary_data = None if os.path.isfile(summary_filepath): summary_data = pd.read_csv(summary_filepath) server = ModularServer( SimModel, [ sim_element, class_element, pupil_element, canvas_grid, sim_chart, css_element, ], "SimulatED", { "all_data": all_data, "model_initial_state": model_initial_state, "output_data_writer": output_data_writer, "model_params": model_params, "summary_data": summary_data, "canvas_grid": canvas_grid, "instructions": UserSettableParameter( "static_text", value= "<p>Classrooms in school are places when students learn and this model examines how this " "learning in mathematics changes over a year. As time passes students can be attentive (green), " "passive (yellow) or disruptive (red).</p>" "<p>There are some variables related to the classroom which you can change in the model using the " "option on the left. Then click Reset before setting it going. (Setting <em>Frames Per Second</em> " "to 0 runs the model at maximum speed.)</p>" "<p>Whilst the model runs you can watch individual students or the average score for the class." "Try changing the value of the parameters to improve class learning.</p>", ), "class_id": UserSettableParameter("choice", "Class ID", value=class_ids[0], choices=class_ids), "teacher_quality_mean": UserSettableParameter( "slider", "Teaching quality mean", model_params.teacher_quality_mean, 0, 5.0, 0.1, ), "teacher_control_mean": UserSettableParameter( "slider", "Teaching control mean", model_params.teacher_control_mean, 0.00, 5.0, 0.1, ), "random_select": UserSettableParameter( "slider", "Mean for random number used at each step", model_params.random_select, 0.00, 10.0, 0.1, ), "group_size": UserSettableParameter( "slider", "Size of each group of pupils", model_params.group_size, 1, 40, 1, ), "group_by_ability": UserSettableParameter( "checkbox", "Group pupils by ability (rather than at random)", model_params.group_by_ability, ), "speedup": UserSettableParameter( "slider", "How much to speed up (and approximate) the simulation", 1, 1, max_speedup, ), }, ) port = int(os.getenv("PORT", 4200)) server.launch(port=port, open_browser=False) else: print(f"BatchRunnerMP will use {n_processors} processors") batch_run = BatchRunnerMP( SimModel, variable_parameters={ "class_id_and_rng": list(zip(class_ids, rngs)), }, fixed_parameters={ "all_data": all_data, "model_initial_state": model_initial_state, "output_data_writer": output_data_writer, "model_params": model_params, "speedup": speedup, }, nr_processes=n_processors, iterations=1, max_steps=1000000, ) batch_run.run_all()
samples = np.linspace(*problem['bounds'][i], num=distinct_samples) # Keep in mind that wolf_gain_from_food should be integers. You will have to change # your code to acommidate for this or sample in such a way that you only get integers. #if var == 'wolf_gain_from_food': # samples = np.linspace(*problem['bounds'][i], num=distinct_samples, dtype=int) batch = BatchRunnerMP(Friends, max_steps=max_steps, iterations=replicates, variable_parameters={var: samples}, model_reporters=model_reporters, display_progress=True, nr_processes=multiprocessing.cpu_count() - 1) batch.run_all() end = time.time() print("Model run-time:", end - begin) data[var] = batch.get_model_vars_dataframe() def perform_analysis(): problem = { 'num_vars': 1, 'names': [ 'tolerance' ], # available parameters: 'tolerance','social_extroversion','mobility','decay' 'bounds': [[0.01, 0.99]] }