Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi :) import random from animal import Herbivore, Carnivore class Cell: def __init__(self, coord): Parameters ---------- num_h : int number of herbivores in a

Hi :)

import random from animal import Herbivore, Carnivore class Cell: def __init__(self, coord): """ Parameters ---------- num_h : int number of herbivores in a cell num_c : int number of carnivores in a cell """ self.coord = coord self.h_pop = [] self.c_pop = [] self.f = self.f_max self._accessible = True @classmethod def set_params(cls, new_params): """ Set class parameters :param new_params: dict: legal keys: 'f_max' :return: :raises KeyError """ for key in new_params: if key not in 'f_max': raise KeyError('Invalid parameter name: ' + key) if 'f_max' in new_params: cls.f_max = new_params['f_max'] def get_num_h(self): """Return number of herbivores in a cell.""" return len(self.h_pop) def get_num_c(self): """Return number of carnivores in a cell.""" return len(self.c_pop) def aging(self): """Age all animal in cell by one cycle.""" for herbivore in self.h_pop: herbivore.ages() for carnivore in self.c_pop: carnivore.ages() def death(self): """Remove dying animals.""" def survivors(pop): return [animal for animal in pop if not animal.dies()] self.h_pop = survivors(self.h_pop) self.c_pop = survivors(self.c_pop) def procreate(self): """For each animal that gives birth, add one new.""" num_h = self.get_num_h() num_c = self.get_num_c() def newborns(pop, n): newborn_animals = [parent.newborn() for parent in pop if parent.gives_birth(n)] return [child for child in newborn_animals if child] self.h_pop.extend(newborns(self.h_pop, num_h)) self.c_pop.extend(newborns(self.c_pop, num_c)) def migration(self, neighbour_cell): """ Process of migration :return: """ if neighbour_cell is None: return def remaining_after_migration(pop): animals_left = [] animals_migrated = [] for animal in pop: if animal.migrate(): animals_migrated.append(animal) else: animals_left.append(animal) return animals_left, animals_migrated self.h_pop, migrated_h = remaining_after_migration(self.h_pop) neighbour_cell.h_pop.extend(migrated_h) self.c_pop, migrated_c = remaining_after_migration(self.c_pop) neighbour_cell.c_pop.extend(migrated_c) def feeding(self): """ Animals feed on the fodder available in the cell :return: """ for herbivore in self.h_pop: herbivore.eat(self.f) self.f -= herbivore.F if self.f >= herbivore.F else self.f for carnivore in self.c_pop: self.h_pop = carnivore.eat(self.h_pop) def yearly_weight_loss(self): """ Yearly weight loss of an animal :return: """ for herbivore in self.h_pop: herbivore.wt_loss() for carnivore in self.c_pop: carnivore.wt_loss() def populate(self, pop_list): """ Populate cell with given population list :param pop_list: Population list :raises: ValueError """ if self.accessible: for animal in pop_list: age = animal['age'] weight = animal['weight'] if animal['species'] == 'Herbivore': self.h_pop.append(Herbivore(age, weight)) elif animal['species'] == 'Carnivore': self.c_pop.append(Carnivore(age, weight)) else: raise ValueError('Location not habitable.') def reset_flags(self): """ Reset the flags of animals to default :return: """ def _reset(pop): for animal in pop: animal.migrated = 'False' animal.given_birth_this_year = 'False' _reset(self.h_pop) _reset(self.c_pop) @property def accessible(self): return self._accessible @accessible.setter def accessible(self, value): self._accessible = value class Lowlands(Cell): f_max = 800.0 def __init__(self, coord): Cell.__init__(self, coord) class Highlands(Cell): f_max = 300.0 def __init__(self, coord): Cell.__init__(self, coord) class Desert(Cell): f_max = 0.0 def __init__(self, coord): Cell.__init__(self, coord) class Water(Cell): f_max = 0.0 def __init__(self, coord): Cell.__init__(self, coord) self._accessible = False

I couldn't make test code for this one also. can i get some help :)?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Relational Database And SQL

Authors: Lucy Scott

3rd Edition

1087899699, 978-1087899695

More Books

Students also viewed these Databases questions

Question

7. Where Do We Begin?

Answered: 1 week ago