def test_infanticide(self): infant_died = 0 sires = [] for i in range(0, 1000): inf_sim = HamadryasSim() inf_pop = HamaPopulation() inf_pop = HamadryasSeed.makeseed(0, inf_pop, inf_sim) HamadryasSeed.addagenttoseed(0, inf_pop.groupsdict[0], inf_pop, 'f', None, None, 10, inf_sim) mom = inf_pop.dict[66] mom.sire_of_fetus = inf_pop.dict[1] inf_sim.birthagent(mom, inf_pop, 50) infant = mom.offspring[0] sires.append(inf_pop.dict[infant].parents[1]) inf_sim.male_eligibility(inf_pop) HamadryasDispersal.opportun_takeover(mom, inf_pop, inf_sim) if infant not in inf_pop.all: infant_died += 1 print str(infant_died) + " infants died." self.assertAlmostEqual(500, infant_died, delta=50)
def test_challenge_outcome(self): dead2 = 0 dead4 = 0 for i in range(0, 1000): challenge_pop = self.setup_rhp() challenge_sim = HamadryasSim() challenge_pop.groupsdict[1].leadermales = [4] HamadryasSeed.addagenttoseed(1, challenge_pop.groupsdict[1], challenge_pop, 'f', None, None, 10, challenge_sim) HamadryasDispersal.challenge(challenge_pop.dict[2], challenge_pop.dict[4], challenge_pop, challenge_sim) self.assertIn(5, challenge_pop.dict[4].females) HamadryasDispersal.challenge(challenge_pop.dict[1], challenge_pop.dict[4], challenge_pop, challenge_sim) if 2 not in challenge_pop.all: dead2 += 1 if 4 not in challenge_pop.all: dead4 += 1 self.assertIn(5, challenge_pop.dict[1].females) self.assertAlmostEqual(dead2, 500, delta=50) self.assertAlmostEqual(dead4, 500, delta=50)
def test_inherit_works(self): inherits = 0 no_females = 0 new_counter = 0 for i in range(0, 1000): inher_sim = HamadryasSim() inher_pop = HamaPopulation() inher_pop = HamadryasSeed.makeseed(0, inher_pop, inher_sim) leader_to_kill = inher_pop.all[0] leader_to_kill = inher_pop.dict[leader_to_kill] inheritor = inher_pop.all[1] inheritor = inher_pop.dict[inheritor] if leader_to_kill.females: leaders_females = list(leader_to_kill.females) else: no_females += 1 inher_sim.kill_agent(leader_to_kill, inher_pop, inher_pop.groupsdict[0], 50) # self.assertTrue(inher_pop.avail_females) if inheritor.maleState == MaleState.lea: self.assertIn(inheritor.females[0], leaders_females) inherits += 1 new_counter += inher_pop.count_inheritances print "Inherits: " + str(inherits) print "No females to inherit: " + str(no_females) print "New counter: " + str(new_counter) self.assertAlmostEqual(900, inherits, delta=900)
def test_hama_seed(self): fakesim = HamadryasSim() fakepopulation2 = Population() fakepopulation2 = HamadryasSeed.makeseed(1, fakepopulation2, fakesim) self.assertEqual(len(fakepopulation2.groupsdict[1].leadermales), 8) self.assertEqual(len(fakepopulation2.all), 65)
def test_fol_choices(self): fol_sim = HamadryasSim() became_leader = 0 followed = 0 died = 0 for i in range(0, 1000): fol_pop = HamaPopulation() fol_pop = HamadryasSeed.makeseed(1, fol_pop, fol_sim) fol_group = fol_pop.groupsdict[1] # add a solitary HamadryasSeed.addagenttoseed(1, fol_group, fol_pop, 'm', None, None, 20.5, fol_sim) our_guy = fol_pop.all[-1] our_guy = fol_pop.dict[our_guy] our_guy.clanID = 2 leader = random.choice([ x for x in fol_group.leadermales if fol_pop.dict[x].clanID == 2 ]) leader = fol_pop.dict[leader] HamadryasDispersal.follow(our_guy, leader, fol_pop) fol_sim.get_young_natal_females(fol_pop) fol_sim.male_eligibility(fol_pop) # give him choices fol_sim.male_choices(our_guy, fol_pop) if our_guy.index not in fol_pop.all: died += 1 elif our_guy.maleState == MaleState.lea: self.assertTrue(our_guy.females) became_leader += 1 elif our_guy.maleState == MaleState.fol: self.assertFalse(our_guy.females) followed += 1 print became_leader, followed, died self.assertAlmostEqual(followed, 400, delta=100) self.assertAlmostEqual(became_leader, 600, delta=100) self.assertEqual(0, died)
def setup(self, OMU1clan, OMU1band, OMU2clan, OMU2band): recog_sim = HamadryasSim() recog_pop = HamaPopulation() if OMU1band == OMU2band: group1 = HamadryasGroup(OMU1band) recog_pop.groupsdict[OMU1band] = group1 group2 = group1 else: group1 = HamadryasGroup(OMU1band) recog_pop.groupsdict[OMU1band] = group1 group2 = HamadryasGroup(OMU2band) recog_pop.groupsdict[OMU2band] = group2 HamadryasSeed.addagenttoseed(OMU1band, group1, recog_pop, 'm', None, None, 10.0, recog_sim) recog_pop.dict[1].clanID = OMU1clan group1.leadermales.add(recog_pop.dict[1].index) HamadryasSeed.addagenttoseed(OMU1band, group1, recog_pop, 'f', 7, 8, 10.0, recog_sim) HamadryasDispersal.add_female_to_omu(recog_pop.dict[1], recog_pop.dict[2], recog_pop, recog_sim) HamadryasSeed.addagenttoseed(OMU2band, group2, recog_pop, 'm', None, None, 10.0, recog_sim) recog_pop.dict[3].clanID = OMU2clan group2.leadermales.add(recog_pop.dict[1].index) HamadryasSeed.addagenttoseed(OMU2band, group2, recog_pop, 'f', None, None, 10.0, recog_sim) HamadryasDispersal.add_female_to_omu(recog_pop.dict[3], recog_pop.dict[4], recog_pop, recog_sim) HamadryasSeed.addagenttoseed(1, recog_pop.groupsdict[1], recog_pop, 'f', 7, 8, 10.0, recog_sim) recog_pop.dict[5].clanID = 1 return recog_pop
def test_initial_unit(self): dispersed = 0 dispersed_across_bands = 0 new_counter = 0 for i in range(0, 100): init_sim = HamadryasSim() init_pop = HamaPopulation() for i in range(0, 10): init_pop = HamadryasSeed.makeseed(i, init_pop, init_sim) HamadryasSeed.addagenttoseed(0, init_pop.groupsdict[0], init_pop, 'f', None, None, 2, init_sim) our_girl = init_pop.all[-1] our_girl = init_pop.dict[our_girl] start_OMU = our_girl.OMUID start_clan = our_girl.clanID start_band = our_girl.bandID init_sim.get_young_natal_females(init_pop) self.assertTrue(init_pop.young_natal_females) males = [ male for male in init_pop.dict.values() if male.sex == 'm' ] for male in males: init_sim.male_choices(male, init_pop) if our_girl.OMUID != start_OMU: dispersed += 1 if our_girl.bandID != start_band: dispersed_across_bands += 1 new_counter += init_pop.count_initial_units print dispersed, dispersed_across_bands, new_counter self.assertAlmostEqual(90, dispersed, delta=90) self.assertAlmostEqual(30, dispersed_across_bands, delta=30)
def test_sol_choices(self): sol_sim = HamadryasSim() became_leader = 0 followed = 0 died = 0 for i in range(0, 5000): sol_pop = HamaPopulation() sol_pop = HamadryasSeed.makeseed(1, sol_pop, sol_sim) # add a solitary HamadryasSeed.addagenttoseed(1, sol_pop.groupsdict[1], sol_pop, 'm', None, None, 20.5, sol_sim) our_guy = sol_pop.all[-1] our_guy = sol_pop.dict[our_guy] our_guy.maleState = MaleState.sol our_guy.clanID = 2 sol_sim.get_young_natal_females(sol_pop) sol_sim.male_eligibility(sol_pop) self.assertTrue(sol_pop.groupsdict[1].leadermales) # give him choices sol_sim.male_choices(our_guy, sol_pop) if our_guy.index not in sol_pop.all: died += 1 elif our_guy.maleState == MaleState.lea: became_leader += 1 elif our_guy.maleState == MaleState.fol: followed += 1 print became_leader, followed, died self.assertAlmostEqual(followed, 2250, delta=450) self.assertAlmostEqual(became_leader, 1250, delta=250) self.assertAlmostEqual(75, died, delta=50)
def run_simulation(self): population = HamaPopulation() for groupindex in range(0, 10): population = HamadryasSeed.makeseed(groupindex, population, self) for halfyear in range(0, self.duration): population.halfyear = halfyear for group in population.groupsdict.values(): group.leadermales = set() self.mortality_check(population, halfyear) self.male_eligibility(population) self.get_young_natal_females(population) if population.avail_females: for female in population.avail_females: female = population.dict[female] HamadryasDispersal.opportun_takeover( female, population, self) population.avail_females = [] males = [ male for male in population.dict.values() if male.sex == 'm' ] for male in males: self.male_choices(male, population) if population.avail_females: for female in population.avail_females: female = population.dict[female] HamadryasDispersal.opportun_takeover( female, population, self) population.avail_females = [] self.birth_check(population, halfyear) self.promotions(population) # print "Population: " + str(len(population.dict.keys())) # print "Hamadryas half-year " + str(halfyear) + " done!" if len(population.all) == 0: break ratios = self.get_sex_age_ratios(population) self.siring_success = collections.Counter(self.siring_success.values()) return { "sires": self.siring_success, "pop size": len(population.all), "adult sex ratio": ratios["adult sex ratio"], "adult to nonadult ratio": ratios["adult to nonadult ratio"] }
def test_codispersal(self): codi_sim = HamadryasSim() codi_pop = HamaPopulation() codi_sim.codispersal = True for i in range(0, 10): codi_pop = HamadryasSeed.makeseed(i, codi_pop, codi_sim) HamadryasSeed.addagenttoseed(0, codi_pop.groupsdict[0], codi_pop, 'f', 100, None, 10, codi_sim) HamadryasSeed.addagenttoseed(0, codi_pop.groupsdict[0], codi_pop, 'f', 100, None, 10, codi_sim) sister1 = codi_pop.dict[codi_pop.all[-1]] sister2 = codi_pop.dict[codi_pop.all[-2]] HamadryasDispersal.add_female_to_omu(codi_pop.dict[sister1.OMUID], sister2, codi_pop, codi_sim) codi_sim.kill_agent(codi_pop.dict[sister1.OMUID], codi_pop, codi_pop.groupsdict[0], 0) codi_sim.male_eligibility(codi_pop) HamadryasDispersal.opportun_takeover(sister1, codi_pop, codi_sim) self.assertTrue(1 == 1)
def setup_rhp(self): hama_rhp_pop = HamaPopulation() hama_rhp_group = HamadryasGroup(1) hama_rhp_pop.groupsdict[1] = hama_rhp_group hamadryas_sim = HamadryasSim() HamadryasSeed.addagenttoseed(1, hama_rhp_group, hama_rhp_pop, 'm', None, None, 10, hamadryas_sim) HamadryasSeed.addagenttoseed(1, hama_rhp_group, hama_rhp_pop, 'm', None, None, 10, hamadryas_sim) HamadryasSeed.addagenttoseed(1, hama_rhp_group, hama_rhp_pop, 'm', None, None, 13.5, hamadryas_sim) HamadryasSeed.addagenttoseed(1, hama_rhp_group, hama_rhp_pop, 'm', None, None, 13.5, hamadryas_sim) hama_rhp_pop.dict[1].rhp = '1' hama_rhp_pop.dict[2].rhp = '2' hama_rhp_pop.dict[3].rhp = '3' hama_rhp_pop.dict[4].rhp = '4' return hama_rhp_pop
def test_disp_between_bands(self): change_OMU = 0 change_clan = 0 change_band = 0 for i in range(0, 100): band_disp_sim = HamadryasSim() band_disp_pop = HamaPopulation() for groupindex in range(0, 10): band_disp_pop = HamadryasSeed.makeseed(groupindex, band_disp_pop, band_disp_sim) female_to_disp = band_disp_pop.dict[14] start_OMU = female_to_disp.OMUID start_clan = female_to_disp.clanID start_band = female_to_disp.bandID band_disp_sim.kill_agent(band_disp_pop.dict[female_to_disp.OMUID], band_disp_pop, band_disp_pop.groupsdict[0], 50) band_disp_sim.male_eligibility(band_disp_pop) self.assertTrue(band_disp_pop.eligible_males) HamadryasDispersal.opportun_takeover(female_to_disp, band_disp_pop, band_disp_sim) if female_to_disp.OMUID != start_OMU: change_OMU += 1 if female_to_disp.clanID != start_clan and female_to_disp.bandID == start_band: change_clan += 1 if female_to_disp.bandID != start_band: change_band += 1 print "Moved between OMUs: " + str(change_OMU) print "Stayed in natal clan: " + str(100 - change_band - change_clan) print "Moved between clans within a band: " + str(change_clan) print "Moved between bands: " + str(change_band) self.assertEqual(100, change_OMU) self.assertAlmostEqual(15, change_clan, delta=0) self.assertAlmostEqual(60, change_band, delta=0)
def setup(self, relat_sim, relat_pop, group): HamadryasSeed.addagenttoseed(1, group, relat_pop, 'm', None, None, 10.0, relat_sim) group.leadermales.add(1) HamadryasSeed.addagenttoseed(1, group, relat_pop, 'f', None, None, 10.0, relat_sim) relat_sim.parent_dict[2] = [2342, 23423] HamadryasSeed.addagenttoseed(1, group, relat_pop, 'm', None, None, 10.0, relat_sim) group.leadermales.add(3) HamadryasSeed.addagenttoseed(1, group, relat_pop, 'f', 2, 1, 10.0, relat_sim) relat_sim.parent_dict[4] = [2, 1] HamadryasSeed.addagenttoseed(1, group, relat_pop, 'm', 2, 3, 10.0, relat_sim) relat_sim.parent_dict[5] = [2, 3] HamadryasSeed.addagenttoseed(1, group, relat_pop, 'f', 4, 532, 10.0, relat_sim) relat_sim.parent_dict[6] = [4, 532] HamadryasSeed.addagenttoseed(1, group, relat_pop, 'm', 4, 362, 10.0, relat_sim) relat_sim.parent_dict[7] = [4, 362] HamadryasSeed.addagenttoseed(1, group, relat_pop, 'f', 202, 5, 10.0, relat_sim) relat_sim.parent_dict[8] = [202, 5] HamadryasSeed.addagenttoseed(1, group, relat_pop, 'f', 202, 5, 10.0, relat_sim) relat_sim.parent_dict[9] = [202, 5] HamadryasSeed.addagenttoseed(1, group, relat_pop, 'm', 202, 5, 10.0, relat_sim) relat_sim.parent_dict[10] = [202, 5] HamadryasSeed.addagenttoseed(1, group, relat_pop, 'm', 6, 5643, 10.0, relat_sim) relat_sim.parent_dict[11] = [6, 5643] HamadryasSeed.addagenttoseed(1, group, relat_pop, 'f', 6, 85, 10.0, relat_sim) relat_sim.parent_dict[12] = [6, 85] HamadryasSeed.addagenttoseed(1, group, relat_pop, 'f', 8, 231, 10.0, relat_sim) relat_sim.parent_dict[13] = [8, 231] HamadryasSeed.addagenttoseed(1, group, relat_pop, 'f', 9, 879, 10.0, relat_sim) relat_sim.parent_dict[14] = [9, 879] HamadryasSeed.addagenttoseed(1, group, relat_pop, 'f', 13, 65, 10.0, relat_sim) relat_sim.parent_dict[15] = [13, 65] HamadryasSeed.addagenttoseed(1, group, relat_pop, 'f', 14, 555, 10.0, relat_sim) relat_sim.parent_dict[16] = [14, 555]