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_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 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_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 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 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 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)