def run_simulation(self, save_to_dot=True, save_to_json=True): #import Seed and lifetable data this_generation_population = Population() next_generation_population = None seed_group = seed.load_group(this_generation_population) table_data = loader.load_data() lifetable = table_data.life_table dispersal_table =\ table_data.dispersal_table random_module = RandomModule() #create analytics lists age_record_list = [] population_record_list = [] male_population_record_list = [] female_population_record_list = [] real_birth_rate_list = [] real_death_rate_list = [] edges_per_agent_list = [] adult_males_list = [] adult_females_list = [] adult_females_per_males_list = [] total_agent_relationships_list = [] group_composition_list = [] death_counter = Counter() #used to make sure the correct number #of deaths occur birth_counter = Counter() #used to make sure the correct number #of births take place #assign all_groups by creating several copies of the #seed generation for i in range(0, self.NUMBER_OF_SEED_GROUPS + 1): this_generation_population.add_group(copy.deepcopy(seed_group)) """ I was having a strange error where the 0th group was loaded incorrectly. This is a temporary fix """ del this_generation_population.groups[0] for i in range (0, self.NUMBER_OF_GENERATIONS): self.per_generation_printout(i) #analytics this_age_record = [] this_population_record = 0 this_male_population_record = 0 this_female_population_record = 0 this_edges_per_agent = 0 this_generation_adult_males = 0 this_generation_adult_females = 0 this_generation_group_composition_list = [] #reset counters death_counter.reset() birth_counter.reset() #make the next gen population a copy of this gen's pop this_generation_population.generation = i next_generation_population =\ copy.deepcopy(this_generation_population) #run the simulation for each sub_group. for j in range(0, len(this_generation_population.groups)): this_generation = this_generation_population.groups[j] new_generation = next_generation_population.groups[j] females_to_male =\ this_generation.get_females_to_male() for agent_index in this_generation.whole_set: #print str(agent_index) + ", " + str(len(this_generation.agent_array)) this_agent =\ this_generation.agent_dict[agent_index] new_agent =\ new_generation.agent_dict[agent_index] #increment age new_generation.promote_agent(new_agent) #check birth_rate if this_agent.index in this_generation.female_set: chance_of_birth =\ lifetable.chance_of_birth(females_to_male, this_agent.age) #check for birth self.check_for_birth(this_generation, new_generation, this_agent, new_agent, females_to_male, agent_index, lifetable, random_module, birth_counter, male_population_record_list) #check for death self.check_for_death(lifetable, females_to_male, this_agent, new_agent, new_generation, random_module, death_counter) #check for dispersal self.check_for_dispersal(dispersal_table, females_to_male, this_agent, new_agent, this_generation, new_generation, this_generation_population, next_generation_population, random_module) #check for friendships friendships.check_for_friendships(this_agent, new_agent, this_generation, new_generation, random_module) #unique changes self.conduct_changes_unique_to_experiment_at_agent( this_generation_population, next_generation_population, this_generation, new_generation, this_agent, new_agent, females_to_male, lifetable, random_module, table_data ) #analytics this_edges_per_agent += this_agent.edges() this_age_record.append(this_agent.age) this_population_record += 1 if (this_agent.index in this_generation.male_set): this_male_population_record += 1 elif (this_agent.index in this_generation.female_set): this_female_population_record += 1 this_generation_adult_males +=\ len(this_generation.male_set) this_generation_adult_females +=\ len(this_generation.female_set) this_generation_group_composition_list.append( len(this_generation.whole_set) ) self.conduct_changes_unique_to_experiment_at_gen( this_generation_population, next_generation_population, i, self.NUMBER_OF_GENERATIONS, table_data) #set the old gen to the new one this_generation_population = next_generation_population group_composition_list.append(this_generation_group_composition_list) number_of_groups = len(this_generation_population.groups) adult_males_per_group =\ float(this_generation_adult_males)/number_of_groups adult_females_per_group =\ float(this_generation_adult_females)/number_of_groups adult_males_list.append(adult_males_per_group) adult_females_list.append(adult_females_per_group) #handle div by 0 errors in calculating #females per male if (adult_males_per_group == 0): adult_females_per_males_list.append( adult_females_per_group/1 ) elif (adult_females_per_group == 0): adult_females_per_males_list.append(0) else: adult_females_per_males_list.append( float(adult_females_per_group)/float(adult_males_per_group) ) if (save_to_dot): self.save_data_to_dot(this_generation_population.get_dot_string(), i) if (save_to_json): self.save_data_to_json(this_generation_population.get_json_string(), i) average_edges_per_agent =\ float(this_edges_per_agent)/this_population_record edges_per_agent_list.append(average_edges_per_agent) real_death_rate_list.append( float(death_counter.getCount())/this_population_record) real_birth_rate_list.append( float(birth_counter.getCount())/this_population_record) age_record_list.append(this_age_record) male_population_record_list.append(this_male_population_record) female_population_record_list.append( this_female_population_record) population_record_list.append(this_population_record) total_agent_relationships_list = ( this_generation_population.\ get_population_relationship_stats()) self.save_data(population_record_list, male_population_record_list, female_population_record_list, age_record_list, real_birth_rate_list, real_death_rate_list, edges_per_agent_list, adult_females_per_males_list, group_composition_list, total_agent_relationships_list) print (birth_counter.getCount()) print (death_counter.getCount())