def revive_simulation(rev_parameters, sim_functions): print('\n[' + rev_parameters["simulation_name"].upper() + ' REVIVAL SIMULATION]') Populations = {} if "sim_folder" in rev_parameters: print('Accessing simulation files directory...') print('Excavating World entity: ' + rev_parameters['eco_file'] + '...') World = excavate_world(rev_parameters['sim_folder'] + \ rev_parameters['eco_file']) print('Updating parameters with World dimensions...') rev_parameters["world_z"] = len(World.ecosystem[0][0][0]) rev_parameters["world_y"] = len(World.ecosystem[0][0]) rev_parameters["world_x"] = len(World.ecosystem[0]) for i in range(len(rev_parameters["pop_files"])): print('\nReviving population file: ' + \ rev_parameters["pop_files"][i] + '...') pop_file = rev_parameters["sim_folder"] + \ rev_parameters["pop_files"][i] Populations[rev_parameters["population_names"][i]] = \ revive_population(pop_file) print('\nUpdating revival generation start in simulation parameters...') rev_parameters["rev_start"] = [Populations[pop_name].generation for pop_name in Populations] elif "database_source" in rev_parameters: print('Constructing database directory...') dbpath = os.sep.join([os.getcwd(), 'Simulations', rev_parameters["database_source"]]) print('Connecting to database file: ' + \ rev_parameters["database_source"] + '...') (con, cur) = connect_database(dbpath, None) if rev_parameters["simulation_time"] == 'default': print('Acquiring simulation starting time...') rev_parameters["simulation_time"] = db_list_simulations(cur)[0][0] print('Reconstructing old simulation parameters...') temp_parameters = db_reconstruct_simulation_parameters(cur, rev_parameters["simulation_time"]) print('Assimilating old simulation parameters with new simulation parameters...') for key in temp_parameters: if key not in rev_parameters: rev_parameters[key] = temp_parameters[key] print('Reconstructing World entity...') World = db_reconstruct_world(cur, rev_parameters["simulation_time"], rev_parameters["rev_start"][0]) print('\nUpdating population names parameter...') for pop_name in rev_parameters["population_names"]: print('Reconstructing population: ' + pop_name + '...') Populations[pop_name] = db_reconstruct_population(cur, rev_parameters["simulation_time"], pop_name, rev_parameters["rev_start"][rev_parameters["population_names"].index(pop_name)]) print('Terminating database connection...') con.close() print('Updating last generation revival and population size simulation parameters...') rev_parameters["rev_finish"] = [(Populations[pop_name].generation + \ rev_parameters["extend_gen"]) for pop_name in Populations] rev_parameters["rev_pop_size"] = [len(Populations[pop_name].agents) for pop_name in Populations] print('\nStarting simulation core...') simulation_core(sim_functions, rev_parameters, Populations, World)
def __init__(self, db_source, population_name, starting_time = 'default'): print('\n[INITIALIZING ANALYSIS]') self.db_source = db_source self.population_name = population_name print('Assembling database file directory...') dbpath = os.getcwd().split(os.sep) dbpath[-1] = 'examples' dbpath = os.sep.join(dbpath) dbpath = os.sep.join([dbpath, 'Simulations', db_source]) print('Connecting to database file: ' + db_source + '...') (self.con, self.cur) = database_calls.connect_database(dbpath, None) print('Acquiring simulation starting time...') if starting_time == 'default': self.starting_time = database_calls.db_list_simulations(self.cur)[0][0] else: self.starting_time = starting_time
def __init__(self, db_source, population_name, starting_time='default'): print('\n[INITIALIZING ANALYSIS]') self.db_source = db_source self.population_name = population_name print('Assembling database file directory...') dbpath = os.getcwd().split(os.sep) dbpath[-1] = 'examples' dbpath = os.sep.join(dbpath) dbpath = os.sep.join([dbpath, 'Simulations', db_source]) print('Connecting to database file: ' + db_source + '...') (self.con, self.cur) = \ database_calls.connect_database(dbpath, None) print('Acquiring simulation starting time...') if starting_time == 'default': self.starting_time = \ database_calls.db_list_simulations(self.cur)[0][0] else: self.starting_time = starting_time
def simulation_core(sim_functions, sim_parameters, eb, populations, world): ''' Sequential ecological cell DOSE simulator. Performs the following operations: - Creating simulation file directory for results text file, population freeze, and world burial storage - Generate a simulation start time to identify the current simulation - Define active Ragaraja instructions - Connecting to logging database (if needed) - Writing simulation parameters into results text file - Initialize World and Population - Deploy population(s) onto the world - Run the simulation and recording the results - Writing final results into results text file - Close logging database (if used) - Copy simulation script into simulation file directory Events available for subscription and data: - SIMULATION_START: - time_start: datetime = simulation start time - directory: String = simulation file path directory - max_generation: int = maximum generations - generation: int = starting generation - GENERATION_WORLD_UPDATE: - world: dose_world.World = world object - generation: int = current generation - GENERATION_POPULATIONS_UPDATE: - populations: dict = dictionary of population objects - generation: int = current generation - SIMULATION_END: - time_end: datetime = simulation end time @param sim_functions: implemented simulation functions (see dose.dose_functions) @param sim_parameters: simulation parameters dictionary (see Examples) @param eb: An instance of dose.event_broker @param populations: dictionary of population objects @param world: dose_world.World object ''' eb.log('Started simulation preparation') time_start = datetime.utcnow() time_start_str = '-'.join([str(time_start).split(' ')[0], str(time())]) eb.log('Creating simulation file directories') directory = '_'.join([sim_parameters["simulation_name"], time_start_str]) directory = os.sep.join([os.getcwd(), 'Simulations', directory]) + os.sep if not os.path.exists(directory): os.makedirs(directory) eb.log('Adding simulation directory to simulation parameters') sim_parameters["directory"] = directory eb.log('Adding starting time to simulation parameters') sim_parameters["starting_time"] = time_start_str sim_functions = sim_functions() eb.log('Activating ragaraja version: ' + str(sim_parameters["ragaraja_version"])) if sim_parameters["ragaraja_version"] == 0: ragaraja.activate_version(sim_parameters["ragaraja_version"], sim_parameters["ragaraja_instructions"]) else: ragaraja.activate_version(sim_parameters["ragaraja_version"]) con = None cur = None if "database_file" in sim_parameters and "database_logging_frequency" in sim_parameters: eb.log('Connecting to database file: ' + sim_parameters["database_file"]) (con, cur) = connect_database(None, sim_parameters) eb.log('Logging simulation parameters to database file') (con, cur) = db_log_simulation_parameters(con, cur, sim_parameters) max_generations = None generation_count = None for pop_name in populations: eb.log('Preparing population: ' + pop_name + ' for simulation') if 'sim_folder' in sim_parameters or 'database_source' in sim_parameters: eb.log('Calculating final generation count') max_generations = sim_parameters["rev_start"][sim_parameters["population_names"].index(pop_name)] + \ sim_parameters["extend_gen"] eb.log('Updating generation count from previous simulation') generation_count = sim_parameters["rev_start"][0] else: eb.log('Deploying population in World entity') if sim_parameters["deployment_code"] == 0: eb.log('Executing user defined deployment scheme') deploy_0(sim_parameters, populations, pop_name, world) elif sim_parameters["deployment_code"] == 1: eb.log('Executing deployment code 1: Single eco-cell deployment') deploy_1(sim_parameters, populations, pop_name, world) elif sim_parameters["deployment_code"] == 2: eb.log('Executing deployment code 2: Random eco-cell deployment') deploy_2(sim_parameters, populations, pop_name, world) elif sim_parameters["deployment_code"] == 3: eb.log('Executing deployment code 3: Even eco-cell deployment') deploy_3(sim_parameters, populations, pop_name, world) elif sim_parameters["deployment_code"] == 4: eb.log('Executing deployment code 4: Centralized eco-cell deployment') deploy_4(sim_parameters, populations, pop_name, world) max_generations = sim_parameters["maximum_generations"] generation_count = 0 eb.log('Writing simulation parameters into txt file report') write_parameters(sim_parameters, pop_name) eb.log('Updating generation count') populations[pop_name].generation = generation_count eb.log('Simulation preparation complete') eb.publish(events.SIMULATION_START, { 'time_start': time_start, 'directory': directory, 'max_generation': max_generations, 'generation': generation_count }) while generation_count < max_generations: generation_count += 1 sim_functions.ecoregulate(world) eco_cell_iterator(world, sim_parameters, sim_functions.update_ecology) eco_cell_iterator(world, sim_parameters, sim_functions.update_local) eco_cell_iterator(world, sim_parameters, sim_functions.report) eb.publish(events.GENERATION_WORLD_UPDATE, { 'world': world, 'generation': generation_count }) if generation_count % int(sim_parameters["eco_buried_frequency"]) == 0: bury_world(sim_parameters, world, generation_count) for pop_name in populations: if sim_parameters["interpret_chromosome"]: interpret_chromosome(sim_parameters, populations, pop_name, world) report_generation(sim_parameters, populations, pop_name, sim_functions, generation_count) sim_functions.organism_movement(populations, pop_name, world) sim_functions.organism_location(populations, pop_name, world) eb.publish(events.GENERATION_POPULATIONS_UPDATE, { 'populations': populations, 'generation': generation_count }) if "database_file" in sim_parameters and "database_logging_frequency" in sim_parameters and \ generation_count % int(sim_parameters["database_logging_frequency"]) == 0: (con, cur) = db_report(con, cur, sim_functions, sim_parameters["starting_time"], populations, world, generation_count) eb.log('Generation ' + str(generation_count) + ' complete') eb.log('Closing simulation results') for pop_name in populations: close_results(sim_parameters, pop_name) if "database_file" in sim_parameters and "database_logging_frequency" in sim_parameters: eb.log('Committing logged data into database file and terminating database connection') con.commit() con.close() eb.log('Copying simulation file script to simulation results directory') # DV on my system, the inspect.stack()[2][1] value returns the full # path to the file ('/home/douwe/scr/.../scriptname.py'). # The end result is an error for the original code, below, as the # copyfile command is pointed to a wrong location: the directory # is added twice. This might be Py3, or Windows functionality. # With using the os.path module, this should give an OS-independent # way of extracting the basename and linking to the directory. sim_script_basename = os.path.basename(inspect.stack()[2][1]) copyfile(inspect.stack()[2][1], # DV_original # sim_parameters['directory'] + inspect.stack()[2][1]) os.path.join(sim_parameters['directory'], sim_script_basename)) eb.log('Simulation ended') eb.publish(events.SIMULATION_END, { 'time_end': datetime.utcnow() })
def revive_simulation(rev_parameters, sim_functions, eb = event_broker()): eb.log("[" + rev_parameters["simulation_name"].upper() + " REVIVAL SIMULATION]") populations = {} world = None eb.log("Accessing simulation files directory") if "sim_folder" in rev_parameters: eb.log('Excavating World entity: ' + rev_parameters['eco_file']) world = excavate_world(rev_parameters['sim_folder'] + rev_parameters['eco_file']) eb.log('Updating parameters with World dimensions') rev_parameters["world_z"] = len(world.ecosystem[0][0][0]) rev_parameters["world_y"] = len(world.ecosystem[0][0]) rev_parameters["world_x"] = len(world.ecosystem[0]) for i in range(len(rev_parameters["pop_files"])): eb.log('Reviving population file: ' + rev_parameters["pop_files"][i]) pop_file = rev_parameters["sim_folder"] + rev_parameters["pop_files"][i] populations[rev_parameters["population_names"][i]] = revive_population(pop_file) eb.log('Updating revival generation start in simulation parameters') rev_parameters["rev_start"] = [populations[pop_name].generation for pop_name in populations] elif "database_source" in rev_parameters: eb.log('Constructing database directory') dbpath = os.sep.join([os.getcwd(), 'Simulations', rev_parameters["database_source"]]) eb.log('Connecting to database file: ' + rev_parameters["database_source"]) (con, cur) = connect_database(dbpath, None) if rev_parameters["simulation_time"] == 'default': eb.log('Acquiring simulation starting time') rev_parameters["simulation_time"] = db_list_simulations(cur)[0][0] eb.log('Reconstructing old simulation parameters') temp_parameters = db_reconstruct_simulation_parameters(cur, rev_parameters["simulation_time"]) eb.log('Assimilating old simulation parameters with new simulation parameters') for key in temp_parameters: if key not in rev_parameters: rev_parameters[key] = temp_parameters[key] eb.log('Reconstructing World entity') world = db_reconstruct_world(cur, rev_parameters["simulation_time"], rev_parameters["rev_start"][0]) eb.log('Updating population names parameter') for pop_name in rev_parameters["population_names"]: eb.log('Reconstructing population: ' + pop_name) populations[pop_name] = db_reconstruct_population(cur, rev_parameters["simulation_time"], pop_name, rev_parameters["rev_start"][rev_parameters ["population_names"].index(pop_name)]) eb.log('Terminating database connection') con.close() eb.log('Updating last generation revival and population size simulation parameters') rev_parameters["rev_finish"] = [(populations[pop_name].generation + rev_parameters["extend_gen"]) for pop_name in populations] rev_parameters["rev_pop_size"] = [len(populations[pop_name].agents) for pop_name in populations] eb.log('Starting simulation core') simulation_core(sim_functions, rev_parameters, populations, world)
# needed to run this example without prior # installation of DOSE into Python site-packages import run_examples_without_installation # Example codes starts from here import dose, genetic, random, os import simulation_calls as helper import database_calls dbpath = os.sep.join([os.getcwd(), 'Simulations', "case_study_01"]) (con, cur) = database_calls.connect_database(dbpath, None) World = database_calls.db_reconstruct_world(cur, '2013-10-19-1382200534.1', 1000)
def simulation_core(sim_functions, sim_parameters, Populations, World): ''' Sequential ecological cell DOSE simulator. Performs the following operations: - Creating simulation file directory for results text file, population freeze, and world burial storage - Generate a simulation start time to identify the current simulation - Define active Ragaraja instructions - Connecting to logging database (if needed) - Writing simulation parameters into results text file - Initialize World and Population - Deploy population(s) onto the world - Run the simulation and recording the results - Writing final results into results text file - Close logging database (if used) - Copy simulation script into simulation file directory @param sim_functions: implemented simulation functions (see dose.dose_functions) @param sim_parameters: simulation parameters dictionary (see Examples) @param Populations: dictionary of population objects @param World: dose_world.World object ''' time_start = '-'.join([str(datetime.utcnow()).split(' ')[0], str(time())]) print('Creating simulation file directories...') directory ='_'.join([sim_parameters["simulation_name"],time_start]) directory = os.sep.join([os.getcwd(), 'Simulations', directory]) directory = directory + os.sep if not os.path.exists(directory): os.makedirs(directory) print('Adding simulation directory to simulation parameters...') sim_parameters["directory"] = directory print('Adding starting time to simulation parameters...') sim_parameters["starting_time"] = time_start sim_functions = sim_functions() if sim_parameters["ragaraja_version"] == 0: print('Activating ragaraja version: 0...') ragaraja.activate_version(sim_parameters["ragaraja_version"], sim_parameters["ragaraja_instructions"]) else: print('Activating ragaraja version: ' + \ str(sim_parameters["ragaraja_version"]) + '...') ragaraja.activate_version(sim_parameters["ragaraja_version"]) if "database_file" in sim_parameters and \ "database_logging_frequency" in sim_parameters: print('Connecting to database file: ' + \ sim_parameters["database_file"] + '...') (con, cur) = connect_database(None, sim_parameters) print('Logging simulation parameters to database file...') (con, cur) = db_log_simulation_parameters(con, cur, sim_parameters) for pop_name in Populations: print('\nPreparing population: ' + pop_name + ' for simulation...') if 'sim_folder' in sim_parameters or \ 'database_source' in sim_parameters: print('Calculating final generation count...') max = sim_parameters["rev_start"] \ [sim_parameters["population_names"].index(pop_name)] + \ sim_parameters["extend_gen"] print('Updating generation count from previous simulation...') generation_count = sim_parameters["rev_start"][0] else: print('Deploying population in World entity...') if sim_parameters["deployment_code"] == 0: print('Executing user defined deployment scheme...') deploy_0(sim_parameters, Populations, pop_name, World) elif sim_parameters["deployment_code"] == 1: print('Executing deployment code 1: Single eco-cell deployment...') deploy_1(sim_parameters, Populations, pop_name, World) elif sim_parameters["deployment_code"] == 2: print('Executing deployment code 2: Random eco-cell deployment...') deploy_2(sim_parameters, Populations, pop_name, World) elif sim_parameters["deployment_code"] == 3: print('Executing deployment code 3: Even eco-cell deployment...') deploy_3(sim_parameters, Populations, pop_name, World) elif sim_parameters["deployment_code"] == 4: print('Executing deployment code 4: Centralized eco-cell deployment...') deploy_4(sim_parameters, Populations, pop_name, World) print('Adding maximum generations to simulation parameters...') max = sim_parameters["maximum_generations"] generation_count = 0 print('Writing simulation parameters into txt file report...') write_parameters(sim_parameters, pop_name) print('Updating generation count...') Populations[pop_name].generation = generation_count print('\nSimulation preparation complete...') while generation_count < max: generation_count = generation_count + 1 sim_functions.ecoregulate(World) eco_cell_iterator(World, sim_parameters, sim_functions.update_ecology) eco_cell_iterator(World, sim_parameters, sim_functions.update_local) eco_cell_iterator(World, sim_parameters, sim_functions.report) bury_world(sim_parameters, World, generation_count) for pop_name in Populations: if sim_parameters["interpret_chromosome"]: interpret_chromosome(sim_parameters, Populations, pop_name, World) report_generation(sim_parameters, Populations, pop_name, sim_functions, generation_count) sim_functions.organism_movement(Populations, pop_name, World) sim_functions.organism_location(Populations, pop_name, World) if "database_file" in sim_parameters and \ "database_logging_frequency" in sim_parameters and \ generation_count % \ int(sim_parameters["database_logging_frequency"]) == 0: (con, cur) = db_report(con, cur, sim_functions, sim_parameters["starting_time"], Populations, World, generation_count) print('Generation ' + str(generation_count) + ' complete...') print('\nClosing simulation results...') for pop_name in Populations: close_results(sim_parameters, pop_name) if "database_file" in sim_parameters and \ "database_logging_frequency" in sim_parameters: print('Committing logged data into database file...') con.commit() print('Terminating database connection...') con.close() print('Copying simulation file script to simulation results directory...') #DV on my system, the inspect.stack()[2][1] value returns the full # path to the file ('/home/douwe/scr/.../scriptname.py'). # The end result is an error for the original code, below, as the # copyfile command is pointed to a wrong location: the directory # is added twice. This might be Py3, or Windows functionality. # With using the os.path module, this should give an OS-independent # way of extracting the basename and linking to the directory. sim_script_basename = os.path.basename(inspect.stack()[2][1]) copyfile(inspect.stack()[2][1], #DV_original# sim_parameters['directory'] + inspect.stack()[2][1]) os.path.join(sim_parameters['directory'], sim_script_basename)) print('\nSimulation ended...')