Esempio n. 1
0
def test_animal_spawn():
    island_cell = IslandCell(None, None)
    island_cell.initialize_herbs(10, 4, 20)
    island_cell.initialize_carns(10, 5, 20)
    assert island_cell.get_pop_herb() == 10
    assert island_cell.get_pop_carn() == 10
    assert island_cell.cell_herb_pop[0].age == 4
    assert island_cell.cell_herb_pop[0].weight == 20
    assert island_cell.cell_carn_pop[0].age == 5
    assert island_cell.cell_carn_pop[0].weight == 20
Esempio n. 2
0
def test_aging():
    """
    The test will see if the aging function works as intended
    The Herbovire object should go from age 0 to 1
    """
    island_cell = IslandCell()
    island_cell.initialize_herbs()
    assert island_cell.cell_herb_pop[0].age == 0
    island_cell.execute_aging()
    assert island_cell.cell_herb_pop[0].age == 1
Esempio n. 3
0
def test_weight_loss():
    """
        The test will test for the yearly weight loss function
        If it does, the Herbivore objects weight should reduce with 5% of its current weight
    """
    island_cell = IslandCell()
    island_cell.initialize_herbs()
    test_weight = island_cell.cell_herb_pop[0].weight
    island_cell.execute_weightloss()
    assert island_cell.cell_herb_pop[0].weight == \
           test_weight - (test_weight * island_cell.cell_herb_pop[0]._ani_params['eta'])
    def __init__(self,
                 island_map=None,
                 ini_pop=[],
                 seed=None,
                 ymax_animals=20,
                 cmax_animals={
                     'Herbivore': 10,
                     'Carnivore': 20
                 },
                 hist_specs=None,
                 img_dir=None,
                 img_name=_DEFAULT_GRAPHICS_NAME,
                 img_fmt='png',
                 img_base=None):
        if seed is None:
            random.seed(100)
        else:
            random.seed(seed)
        self.island = []
        self.letter_island = island_map
        self.total_herb_pop = 0
        self.total_carn_pop = 0
        self.num_animals = 0
        self.num_animals_per_species = {'Carnivore': 0, 'Herbivore': 0}
        self.cycles = 50
        self._fig = None
        self._herb_heat = None
        self._herb_axis = None
        self._carn_heat = None
        self._carn_axis = None
        self._mean_ax = None
        self._mean_line = None
        self.island_map = None
        self._img_ctr = 0
        self._img_fmt = img_fmt
        self.herbivore_line = None
        self.carnivore_line = None
        self.animal_dictionary = {}
        self.island_dataframe = None
        self.herbivore_dataframe = None
        self.carnivore_dataframe = None
        self.year = 0
        self.final_year = None
        self.axt = None

        if img_dir is not None:
            self._img_base = os.path.join(img_dir, img_name)
        else:
            self._img_base = img_base
        self.map_split = island_map.split('\n')
        self.map_length = len(self.map_split[0])
        for index in range(len(self.map_split)):
            if len(self.map_split[index - 1]) != self.map_length:
                raise ValueError("All map rows must be have equal lenght.")

        if len(self.map_split) > 3:
            if 'L' in (self.map_split[0] or self.map_split[-1]):
                raise ValueError('The island must be surrounded by water')
            elif 'H' in (self.map_split[0] or self.map_split[-1]):
                raise ValueError('The island must be surrounded by water')
            elif 'D' in (self.map_split[0] or self.map_split[-1]):
                raise ValueError('The island must be surrounded by water')

            for row in self.map_split:
                if row[0] != 'W':
                    raise ValueError('The island must be surrounded by water')
                elif row[-1] != 'W':
                    raise ValueError('The island must be surrounded by water')

        for letter in island_map:
            if letter == 'W':
                self.island.append(IslandCell(Water()))
            elif letter == 'L':
                self.island.append(IslandCell(Lowlands()))
            elif letter == 'H':
                self.island.append(IslandCell(Highlands()))
            elif letter == 'D':
                self.island.append(IslandCell(Desert()))
            elif letter == '\n':
                pass
            else:
                raise ValueError(
                    "Input map must consist of Water ('W'), Lowland('L'),\
                                 Desert('D'), Highland('H')")
        # Give each cell its coordinate
        r = 1
        c = 1
        i = 0
        for cell in self.island:
            cell.coordinate = (r, c)
            c += 1
            i += 1
            if i == (self.map_length * r):
                r += 1
                c = 1
        # find each cells neighboring cells
        for cell in self.island:
            index = self.island.index(cell)
            if index + 1 <= len(self.island) - 1:
                cell.neighbors[0] = self.island[index + 1]
            if index - 1 >= 0:
                cell.neighbors[1] = self.island[index - 1]
            if index - self.map_length > 0:
                cell.neighbors[2] = self.island[index - self.map_length]
            if index + self.map_length < len(self.island):
                cell.neighbors[3] = self.island[index + self.map_length]

        # find index to place animals in

        if type(ini_pop) != list:
            raise TypeError('Input population must be a list')
        elif len(ini_pop) == 0:
            pass
        elif type(ini_pop[0]) != dict:
            raise TypeError('Input population list must contain a dictionary')
        else:
            for key in ini_pop[0]:
                if key not in ['loc', 'pop']:
                    raise KeyError(
                        'Input population dictionary must only contain loc and pop'
                    )
                elif key == 'loc':
                    if type(ini_pop[0][key]) != tuple:
                        TypeError('Input location must be a tuple')
                    elif len(ini_pop[0][key]) != 2:
                        raise IndexError(
                            'Input loc must consist of two numbers')
                    else:
                        for value in ini_pop[0][key]:
                            if type(value) != int:
                                raise TypeError(
                                    "Input loc tuple must consist of two integers"
                                )
                            elif value <= 0:
                                raise ValueError(
                                    "Input integer must be a positive number and not zero"
                                )
                        for cell in self.island:
                            if cell.coordinate == ini_pop[0]['loc']:
                                index = self.island.index(cell)
                elif key == 'pop':
                    if type(ini_pop[0][key]) != list:
                        raise TypeError('Input population must be a list')
                    else:
                        for dic in ini_pop[0][key]:
                            if type(dic) != dict:
                                raise TypeError(
                                    'Input population must come in the form of dictionaries '
                                    'in the list')
                            elif len(dic) != 3:
                                raise ValueError(
                                    "Dictionary must contain three keys: species,age,weight"
                                )
                            elif "species" and "age" and "weight" not in dic:
                                raise ValueError(
                                    "Dictionary must contain three keys: species,age,weight"
                                )
                            elif type(dic['age']) != int and type(
                                    'weight') not in [int, float]:
                                raise ValueError(
                                    "Age must be an integer and weight must be an integer or float"
                                )
                            elif dic['age'] < 0 or dic['weight'] <= 0:
                                raise ValueError(
                                    "Age must be a positive number and \
                                weight must not equal zero and be positive")
                            elif dic['species'] not in [
                                    'Herbivore', 'Carnivore'
                            ]:
                                raise NameError(
                                    'Species must be a herbivore or carnivore')

                            elif dic['species'] == 'Herbivore':
                                if self.island[
                                        index].location.environment == 'W':
                                    raise TypeError(
                                        "Animals can't swim, do not place them in water"
                                    )
                                self.island[index].cell_herb_pop.append \
                                    (Herbivore(dic['age'], dic['weight']))
                            else:
                                self.island[index].cell_carn_pop.append \
                                    (Carnivore(dic['age'], dic['weight']))
Esempio n. 5
0
 def create_island(self, n_herbs, n_carns):
     self.island = IslandCell(Highlands())
     self.island.initialize_herbs(n_herbs, 5, 10)
     self.island.initialize_carns(n_carns, 20, 50)
Esempio n. 6
0
class TestFeedingAndHunting:

    @pytest.fixture(autouse=True)
    def create_island(self, n_herbs, n_carns):
        self.island = IslandCell(Highlands())
        self.island.initialize_herbs(n_herbs, 5, 10)
        self.island.initialize_carns(n_carns, 20, 50)

    def test_test_execute_feeding_random(self):
        """
        The function should shuffle the list before feeding starts
        I am testing to see if the list shuffles as intended.
        This will only happen to Herbivores
        This test might fail as there is a possibility that the same herb
        can get the same index
        """

        test_herb = self.island.cell_herb_pop[0]
        self.island.execute_feeding()
        assert test_herb != self.island.cell_herb_pop[0]

    def test_weight_gain_feeding(self):
        """
        The test will see if the feeding function works as intended.
        If it does, the Herbivore should eat and increase its weight
        Since the function shuffles the init_herb_list I have to
        find the index of the object I am testing.
        """

        test_weight = self.island.cell_herb_pop[0].weight
        first_herb = self.island.cell_herb_pop[0]
        self.island.execute_feeding()
        index = self.island.cell_herb_pop.index(first_herb)
        assert self.island.cell_herb_pop[index].weight > test_weight

    def test_not_enough_food(self):
        """
        To test if the Herbivores will eat every food, and check
        that not everybody gain weight
        """
        self.weight = []
        self.island.location.food = 90
        n_ani_eat = self.island.location.food / self.island.cell_herb_pop[0]._ani_params['F']
        self.island.execute_feeding()

        for herb in self.island.cell_herb_pop:
            self.weight.append(herb.weight)

        assert self.weight.count(10) == self.island.get_pop_herb() - int(n_ani_eat)

    @pytest.mark.xfail()
    def test_weight_gain_hunt(self, mocker):
        """
        The test will see if the carnivores will in fact eat Herbivores and
        gain weight from this.
        """
        # det kan være noe galt med fitness funksjonen da sannsynlgiheten for
        # å drepe en herb er ekstremt lav.
        mocker.patch('random.random', return_value=0)
        self.island.cell_carn_pop = [Carnivore(5, 50)]
        self.island.cell_herb_pop = [Herbivore()]
        self.island.location.food = 0
        test_weight = self.island.cell_carn_pop[0].weight
        self.island.execute_feeding()
        assert test_weight < self.island.cell_carn_pop[0].weight

    def test_hunt_herbivore_death(self, mocker):
        """
        Test to see if herbivores die when eaten be carnivores
        Since Herbivores are not removed from the population list in the feeding function
        we have to run the update_list function to see if they die.
        """
        mocker.patch('random.random', return_value=0)

        old_len = len(self.island.cell_herb_pop)
        self.island.execute_feeding()
        self.island.remove_dead()
        assert len(self.island.cell_herb_pop) < old_len

    def test_fittest_carn_eats_first(self, mocker):
        """
        Tests to see if the fittest of three carnivores is the
        one that gets to eat the Herbivore
        """
        mocker.patch('random.random', return_value=0)

        self.island.cell_carn_pop = [Carnivore(1, 20), Carnivore(1, 20), Carnivore(5, 30)]
        self.island.cell_herb_pop = [Herbivore()]
        self.island.location.food = 0
        self.island.execute_feeding()

        assert self.island.cell_carn_pop[2].weight > 30 \
               and self.island.cell_carn_pop[0].weight == 20
Esempio n. 7
0
def test_species():
    island_cell = IslandCell(None, None)
    for herb in island_cell.current_herb_infolist:
        assert herb['species'] == 'Herbivore'
Esempio n. 8
0
 def create_island(self, n_herbs, n_carns):
     self.island = IslandCell()
     self.island.initialize_herbs(n_herbs, 5, 40)
     self.island.initialize_carns(n_carns, 5, 40)
Esempio n. 9
0
class TestBirthDeath:

    @pytest.fixture(autouse=True)
    def create_island(self, n_herbs, n_carns):
        self.island = IslandCell()
        self.island.initialize_herbs(n_herbs, 5, 40)
        self.island.initialize_carns(n_carns, 5, 40)

    def test_birth(self):
        """
        Test to see if the birth function in fact will give birth to new
        Herbivores and Carnivores by running it 10 times.
        """
        test_herb = self.island.get_pop_herb()
        test_carn = self.island.get_pop_carn()
        old_num_herbs = self.island.get_pop_herb()
        old_num_carns = self.island.get_pop_carn()
        for _ in range(10):
            self.island.execute_birth()
            # See that the number of animals does not decrease
            num_herb = self.island.get_pop_herb()
            num_carn = self.island.get_pop_carn()
            assert num_herb >= old_num_herbs
            assert num_carn >= old_num_carns
            old_num_herbs, old_num_carns = num_herb, num_carn

        assert self.island.get_pop_herb() > test_herb
        assert self.island.get_pop_carn() > test_carn

    def test_death(self, mocker):
        """
        Test to see if the death function works
        In the test every animal should die since the probability of dying
        is sett to 100% by mocker.patch
        """

        mocker.patch('random.random', return_value=1)

        self.island.execute_death()

        assert self.island.get_pop_herb() == 0
        assert self.island.get_pop_carn() == 0