def test_calc_relat(self):
        relat_sim = HamadryasSim()
        relat_pop = HamaPopulation()
        group = HamadryasGroup(1)
        relat_pop.groupsdict[1] = group
        self.setup(relat_sim, relat_pop, group)

        output = relatedness.main(relat_pop, relat_sim.parent_dict)

        self.assertTrue(output)
    def test_full_grandniece(self):
        relat_sim = HamadryasSim()
        relat_pop = HamaPopulation()
        group = HamadryasGroup(1)
        relat_pop.groupsdict[1] = group
        self.setup(relat_sim, relat_pop, group)

        R = relatedness.calc_relatedness(
            [relat_pop.dict[9], relat_pop.dict[15]], relat_sim.parent_dict)
        self.assertEqual(R, 0.125)
    def test_half_sib(self):
        relat_sim = HamadryasSim()
        relat_pop = HamaPopulation()
        group = HamadryasGroup(1)
        relat_pop.groupsdict[1] = group
        self.setup(relat_sim, relat_pop, group)

        R = relatedness.calc_relatedness(
            [relat_pop.dict[4], relat_pop.dict[5]], relat_sim.parent_dict)
        self.assertEqual(R, 0.25)
    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_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 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 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_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)