Пример #1
0
def advance(simulation_step, species, habitat):
    next_step = SimulationStep()

    next_month = simulation_step.month + 1
    logger.debug('Advancing to step %d', next_month)
    next_step.month = next_month

    # Copy over the existing animals
    alive_animals = simulation_step.animals

    age_check = AgeCheck(simulation_step, species)
    food_check = FoodCheck(simulation_step, habitat, species)
    drink_check = DrinkCheck(simulation_step, habitat, species)

    fluctuation = get_fluctuation()

    season = simulation_step.get_current_season()
    temperature = habitat.average_temperatures[season] + fluctuation
    logger.debug(
        'Current temperature: %d (%d%+d)',
        temperature,
        habitat.average_temperatures[season],
        fluctuation,
    )

    heat_check = HeatCheck(temperature, species)
    cold_check = ColdCheck(temperature, species)

    checks = [age_check, food_check, drink_check, cold_check, heat_check]

    for check in checks:
        for animal in alive_animals:
            check.update(animal)

        (alive_animals, dead_animals) = separate_alive_from_dead(
            alive_animals,
            lambda animal: check.is_still_alive(animal),
        )

        if dead_animals:
            logger.debug('Deaths due to %s: %d', check.name, len(dead_animals))
            next_step.deaths[check.death_type] = dead_animals

    next_step.animals = alive_animals

    # TODO: Probably should add a test condition, to not spawn if there are
    # no males
    females = tuple(
        animal
        for animal in next_step.animals
        if animal.gender == GENDER_FEMALE
    )

    born_animals = tuple(breed_animals(females, species, simulation_step))
    logger.debug('Animals born: %d', len(born_animals))
    next_step.animals += born_animals

    logger.debug('Population currently at: %d', len(next_step.animals))

    return next_step
Пример #2
0
    def test(self):
        animal = Animal()

        species = Species()
        species.minimum_breeding_age = 1

        simulation_step = SimulationStep()
        simulation_step.month = MONTHS_IN_YEAR

        self.assertTrue(can_breed(animal, species, simulation_step))
Пример #3
0
    def test_too_young(self):
        animal = Animal()

        species = Species()
        species.minimum_breeding_age = 1

        simulation_step = SimulationStep()
        simulation_step.month = 0

        self.assertFalse(can_breed(animal, species, simulation_step))
Пример #4
0
 def test_get_current_season(self):
     months_in_seasons = {
         SEASON_SPRING: [0, 1, 2],
         SEASON_SUMMER: [3, 4, 5],
         SEASON_FALL: [6, 7, 8],
         SEASON_WINTER: [9, 10, 11],
     }
     for season, months in months_in_seasons.items():
         for month in months:
             with self.subTest(month=month):
                 simulation_step = SimulationStep()
                 simulation_step.month = month
                 self.assertEqual(
                     season,
                     simulation_step.get_current_season(),
                 )
Пример #5
0
    def test_old_age(self):
        animal = Animal()

        # Males cannot reproduce, so we can use them to test if they will die
        # correctly.
        simulation_step = SimulationStep()
        simulation_step.month = 13
        simulation_step.animals = [animal]

        species = Species()
        species.life_span = 1

        habitat = Habitat()

        next_step = advance(simulation_step, species, habitat)
        self.assertEqual(0, len(next_step.animals))
        self.assertIn(DEATH_OLD_AGE, next_step.deaths)
Пример #6
0
    def test_thirst(self):
        animal = Animal()

        # Males cannot reproduce, so we can use them to test if they will die
        # correctly.
        simulation_step = SimulationStep()
        simulation_step.month = 3
        simulation_step.animals = [animal]

        species = Species()
        species.life_span = 100
        species.monthly_water_consumption = 1

        habitat = Habitat()
        habitat.monthly_water = 0

        next_step = advance(simulation_step, species, habitat)
        self.assertEqual(0, len(next_step.animals))
        self.assertIn(DEATH_THIRST, next_step.deaths)
Пример #7
0
    def test_feed(self):
        animals = [Animal(), Animal()]

        # Males cannot reproduce, so we can use them to test if they will die
        # correctly.
        simulation_step = SimulationStep()
        simulation_step.month = 4
        simulation_step.animals = animals

        species = Species()
        species.life_span = 1
        species.monthly_food_consumption = 1

        habitat = Habitat()
        habitat.monthly_food = 1

        next_step = advance(simulation_step, species, habitat)
        # One animal should have died due to starvation from not enough food
        self.assertEqual(1, len(next_step.animals))
        self.assertIn(DEATH_STARVATION, next_step.deaths)
Пример #8
0
def get_initial_simulation_step():
    male = Animal()
    male.gender = GENDER_MALE

    female = Animal()
    female.gender = GENDER_FEMALE

    simulation_step = SimulationStep()
    simulation_step.animals.append(male)
    simulation_step.animals.append(female)
    return simulation_step
Пример #9
0
    def test_finish_breeding(self):
        simulation_step = SimulationStep()

        animal = Animal()
        animal.gender = GENDER_FEMALE
        animal.gestation_months = 1

        species = Species()
        species.gestation_months = 1

        new_animals = tuple(breed_animals([animal], species, simulation_step))
        self.assertEqual(0, animal.gestation_months)
        self.assertEqual(1, len(new_animals))
Пример #10
0
    def test_too_cold(self):
        animal = Animal()
        animal.consecutive_cold_months = 1

        # Males cannot reproduce, so we can use them to test if they will die
        # correctly.
        simulation_step = SimulationStep()
        simulation_step.animals = [animal]

        species = Species()
        species.life_span = 100
        species.minimum_temperature = 100
        species.maximum_temperature = 200

        habitat = Habitat()
        habitat.monthly_food = 1
        habitat.monthly_water = 1
        habitat.average_temperatures[SEASON_SPRING] = -1000

        next_step = advance(simulation_step, species, habitat)
        self.assertEqual(0, len(next_step.animals))
        self.assertIn(DEATH_TOO_COLD, next_step.deaths)