コード例 #1
0
    def test_reproduction_gives_offspring_number(self):
        self.indiv = Ind(m=3)

        self.indiv.reproduce(fecundity=2)
        assert self.indiv.offspring != None, "No offspring number generated"
        assert type(
            self.indiv.offspring
        ) is int, "Offspring number of wrong format: {0} instead of integer".format(
            type(self.indiv.offspring))
        assert self.indiv.offspring >= 0, "Offspring number cannot be negative"
コード例 #2
0
    def test_gathering_depends_on_resources_no_neighbours(self):
        self.ind = Ind(m=3)
        self.ind.gather(resources=5,
                        share=1 - self.ind.vigilance,
                        efficiency=0.9)
        firstGathering = self.ind.storage
        self.ind.gather(resources=10,
                        share=1 - self.ind.vigilance,
                        efficiency=0.9)

        assert self.ind.storage > firstGathering
        assert (self.ind.storage - firstGathering) > firstGathering
コード例 #3
0
    def test_gathering_depends_on_neighbours_vigilance(self):
        self.ind = Ind(m=3)
        self.ind.gather(resources=6,
                        share=(1 - self.ind.vigilance + 0.9) / 2,
                        efficiency=0.9)
        firstGathering = self.ind.storage
        self.ind.gather(resources=6,
                        share=(1 - self.ind.vigilance + 0.7 * 2) / 3,
                        efficiency=0.9)

        assert self.ind.storage > firstGathering
        assert (self.ind.storage - firstGathering) < firstGathering
コード例 #4
0
    def test_individuals_make_steps(self):
        gridSide = 6
        self.ind = Ind(m=gridSide)

        storeStepsH = [99] * 10
        storeStepsV = [99] * 10

        for i in range(10):
            before = self.ind.coordinates
            self.ind.explore()
            after = self.ind.coordinates
            storeStepsH[i] = before[0] - after[0]
            storeStepsV[i] = before[1] - after[1]

        assert any([x != 0 for x in storeStepsH]) or any(
            [x != 0 for x in storeStepsV]), "no individual changed position!"
コード例 #5
0
    def create(self, n=None):
        """ Create the population.
		Return nothing
		Create n Individual prey instances and store in self.individuals
		Create ecosystem Grid instance and store in self.grid.
		"""
        if n == None:
            n = self.nIndiv

        self.deathCount = 0  # everyone is alive at the beginning of the simulation
        self.ecoTime = 0  # ecological time set to zero at beginning of simulation
        self.ecologyShortHistory = np.empty([0, 4])
        self.explorationShortHistory = np.empty([0, 4])

        self.grid = Grid(dim=self.gridSize, init=self.initRes)
        self.individuals = []
        for i in range(n):
            self.individuals.append(Ind(m=self.gridSize, v=self.v))
コード例 #6
0
    def test_steps_are_between_minus_one_and_one(self):
        gridSide = 6
        self.ind = Ind(m=gridSide)

        storeStepsH = []
        storeStepsV = []

        for i in range(10):
            before = self.ind.coordinates
            self.ind.explore()
            after = self.ind.coordinates
            storeStepsH.append(before[0] - after[0])
            storeStepsV.append(before[1] - after[1])

        assert all([x in [-1, 0, 1] for x in storeStepsH
                    ]), "wrong horizontal step size in {0}".format(storeStepsH)
        assert all([x in [-1, 0, 1]
                    for x in storeStepsV]), "wrong vertical step size"
コード例 #7
0
    def test_gathering_depends_on_vigilance_no_neighbours(self):
        self.ind = Ind(m=3)
        assert self.ind.storage == 0
        self.ind.vigilance = 0.1
        self.ind.gather(resources=6,
                        share=1 - self.ind.vigilance,
                        efficiency=0.9)
        firstGathering = self.ind.storage
        expect = (1 - 0.1) * 0.9 * 6
        assert firstGathering == expect, "gathered {0} instead of {1}".format(
            firstGathering, expect)
        self.ind.vigilance = 0.8
        self.ind.gather(resources=6,
                        share=1 - self.ind.vigilance,
                        efficiency=0.9)

        assert self.ind.storage > firstGathering
        assert (self.ind.storage - firstGathering) < firstGathering
コード例 #8
0
    def test_average_survival_for_intermediate_vigilance(self):
        self.ind = Ind(m=3)
        self.ind.vigilance = random.random()

        deathCount = 0
        sampleSize = 1000

        for i in range(sampleSize):
            self.ind.survive(p=1)
            if not self.ind.alive:
                deathCount += 1

        stat1, pval1 = scistats.ttest_1samp([1] * deathCount + [0] *
                                            (sampleSize - deathCount),
                                            1 - self.ind.vigilance)
        assert pval1 > 0.05, "T-test mean failed. Observed: {0}, Expected: {1}".format(
            deathCount / sampleSize, 1 - self.ind.vigilance)
        self.test = scistats.binom_test(deathCount,
                                        sampleSize,
                                        1 - self.ind.vigilance,
                                        alternative="two-sided")
        assert self.test > 0.05, "Success rate = {0} when predation rate = {1}".format(
            self.mutantCount / self.nIndividuals, 1 - self.ind.vigilance)
コード例 #9
0
    def update(self):
        """ Update population
		Return nothing
		Create new individual instances for the new generation, who inherit their parent's vigilance level and coordinates on the grid.
		Mutate individual vigilance phenotype.
		Calculate mean vigilance in the population and store it in self.vigilance.
		"""
        tmpIndividuals = []
        self.totalVigilance = 0

        for offspring in range(self.nIndiv):
            ind = Ind(m=self.gridSize)
            parent = self.individuals[self.nextGeneration[offspring]]
            setattr(ind, "vigilance", parent.vigilance)
            setattr(ind, "coordinates", parent.coordinates)

            ind.mutate(mutRate=self.mutRate, mutStep=self.mutStep)
            tmpIndividuals.append(ind)

            self.totalVigilance += ind.vigilance

        self.individuals = tmpIndividuals
        self.vigilance = self.totalVigilance / self.nIndiv
コード例 #10
0
 def test_individual_instance_has_vigilance_attr(self):
     self.ind = Ind(m=3)
     assert hasattr(self.ind,
                    "vigilance"), "individual has no vigilance level"
コード例 #11
0
    def test_individual_gets_initial_coord(self):
        self.ind = Ind(m=3)

        assert self.ind.coordinates != [-1, -1]
        assert self.ind.coordinates[0] >= 0
        assert self.ind.coordinates[1] >= 0
コード例 #12
0
 def test_individual_instance_has_coordinates_attr(self):
     self.ind = Ind(m=3)
     assert hasattr(self.ind,
                    "coordinates"), "individual has no coordinates"
コード例 #13
0
    def test_individual_vigilance_of_right_format(self):
        self.ind = Ind(m=3)

        assert type(self.ind.vigilance) is float
        assert self.ind.vigilance >= 0.0
        assert self.ind.vigilance <= 1.0
コード例 #14
0
 def test_individuals_can_gather_resources(self):
     assert hasattr(Ind(m=3),
                    "gather"), "individual has no consumption method"
     assert callable(getattr(Ind(m=3), "gather"))
コード例 #15
0
 def test_individuals_can_die(self):
     self.ind = Ind(m=3)
     self.ind.survive(p=0.5)
     assert hasattr(self.ind, "alive")
     assert self.ind.alive is not None
     assert type(self.ind.alive) is bool
コード例 #16
0
 def test_individual_has_access_to_storage(self):
     assert hasattr(Ind(m=3),
                    "storage"), "individual cannot store resources"
     self.ind = Ind(m=3)
     assert self.ind.storage == 0, "individual initial resource storage should be empty"
コード例 #17
0
 def test_individual_can_explore_grid(self):
     assert hasattr(Ind(m=3), "explore"), "ind cannot explore"
     assert callable(getattr(Ind(m=3), "explore")), "explore not a method"
コード例 #18
0
 def test_individual_can_reproduce(self):
     assert hasattr(Ind(m=3), "reproduce"), "ind cannot reproduce"
コード例 #19
0
 def test_individuals_have_survival_function(self):
     assert hasattr(Ind(m=3), "survive"), "individuals cannot survive"
     assert callable(getattr(Ind(m=3), "survive"))