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_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_reps(self): sim = HamadryasSim() sim.recog = True pop = self.setup(1, 1, 1, 1) self.assertEqual(pop.dict[1].sex, 'm') reps1 = HamadryasDispersal.recognize(1, pop.dict[1], pop.dict[5], pop, sim) reps2 = HamadryasDispersal.recognize(1, pop.dict[3], pop.dict[5], pop, sim) self.assertEqual(reps1, 2) self.assertEqual(reps1 / 2, reps2)
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 male_choices(self, male, population): if male.maleState == MaleState.fol: HamadryasDispersal.fol_choices(male, population, self) elif male.maleState == MaleState.sol: HamadryasDispersal.sol_choices(male, population, self) elif male.maleState == MaleState.lea: if not male.females: male.maleState = MaleState.sol male.OMUID = None if male.malefols: for malefol in male.malefols: malefol = population.dict[malefol] malefol.maleState = MaleState.sol malefol.OMUID = None male.malefols = []
def killagent(self, agent, population, group, halfyear): if agent.sex == 'f': if agent.offspring and agent.offspring[-1] in population.dict.keys( ): if population.dict[agent.offspring[-1]].age < 2: self.killagent(population.dict[agent.offspring[-1]], population, group, halfyear) if agent.taxon == "hamadryas" and agent.sex == 'm': if agent.index in population.eligible_males: population.eligible_males.remove(agent.index) if agent.females: # if he is a hamadryas leader male if agent.malefols: # malefols inherit first HamadryasDispersal.inherit_females(agent, population, self) # after inheritance, females are "up for grabs" population.avail_females += agent.females if agent.index in group.leadermales: group.leadermales.remove(agent.index) if agent.maleState == MaleState.fol: if agent.OMUID in population.dict.keys(): population.dict[agent.OMUID].malefols.remove(agent.index) elif agent.taxon == "hamadryas" and agent.sex == 'f': if agent.dispersed and agent.OMUID in population.dict.keys(): population.dict[agent.OMUID].females.remove(agent.index) if agent.index in population.avail_females: population.avail_females.remove(agent.index) if agent.age <= 1: if agent.parents: if agent.parents[0] in population.dict.keys(): population.dict[ agent.parents[0]].femaleState = FemaleState.cycling if agent.sex == 'm' and agent.age > 6.0: if halfyear > 40: self.siring_success[agent.index] = (len(agent.offspring)) del population.dict[agent.index] population.all.remove(agent.index) group.agents.remove(agent.index) assert agent.index not in population.all assert agent.index not in population.dict.keys() return 1
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_opp_take_outcome_simple(self): taken_over_by_1 = 0 taken_over_by_2 = 0 for i in range(1, 1000): sim = HamadryasSim() sim.recog = True pop = self.setup(1, 1, 1, 1) sim.male_eligibility(pop) HamadryasDispersal.opportun_takeover(pop.dict[5], pop, sim) if pop.dict[5].OMUID == 1: taken_over_by_1 += 1 elif pop.dict[5].OMUID == 3: taken_over_by_2 += 1 else: break self.assertAlmostEqual(666, taken_over_by_1, delta=66)
def test_opp_take_outcome_5(self): # OMU with relative is outside band, other OMU is in clan taken_over_by_1 = 0 taken_over_by_2 = 0 for i in range(1, 1000): sim = HamadryasSim() sim.recog = True pop = self.setup(OMU1clan=2, OMU1band=2, OMU2clan=1, OMU2band=1) sim.male_eligibility(pop) HamadryasDispersal.opportun_takeover(pop.dict[5], pop, sim) if pop.dict[5].OMUID == 1: taken_over_by_1 += 1 elif pop.dict[5].OMUID == 3: taken_over_by_2 += 1 else: break self.assertAlmostEqual(110, taken_over_by_1, delta=100)
def test_opp_take_outcome_strength(self): # adjust the strength to make sure that works taken_over_by_1 = 0 taken_over_by_2 = 0 for i in range(1, 1000): sim = HamadryasSim() sim.recog = True sim.attraction_strength = 10 pop = self.setup(1, 1, 1, 1) sim.male_eligibility(pop) HamadryasDispersal.opportun_takeover(pop.dict[5], pop, sim) if pop.dict[5].OMUID == 1: taken_over_by_1 += 1 elif pop.dict[5].OMUID == 3: taken_over_by_2 += 1 else: break self.assertAlmostEqual(910, taken_over_by_1, delta=91)
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_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)