Question: Hi :) i need help with programing in pycharm. when can i send the file? import math import random class Animal: def __init__(self, age=None, weight=None):
Hi :) i need help with programing in pycharm. when can i send the file?
import math import random class Animal: def __init__(self, age=None, weight=None): """Create animal with given age and weight""" self.a = 0 if age is None else age self.w = random.gauss(self.mu, self.sigma_birth) if weight is None else weight self.fitness() def set_params(cls, new_params): """ Set class parameters. Parameters ---------- new_params : dict Legal keys: ['w_birth', 'sigma_birth', 'beta', 'eta', 'a_half', 'zeta', 'phi_age', 'w_half', 'phi_weight', 'mu', 'gamma', 'xi', 'omega', 'F', 'DeltaPhiMax'] Raises ------ ValueError, KeyError """ for key in new_params: if key not in cls.default_params.keys(): raise KeyError('Invalid parameter name: ' + key) if not all(value >= 0 for value in new_params.values()): raise ValueError('All values must be positive') if 'DeltaPhiMax' in new_params: if not 0 < new_params['DeltaPhiMax']: raise ValueError('DeltaPhiMax must be strictly positive (>0).') cls.DeltaPhiMax = new_params['DeltaPhiMax'] if 'eta' in new_params: if not 0 <= new_params['eta'] <= 1: raise ValueError('eta must be in [0, 1].') cls.eta = new_params['eta'] def fitness(self): """Calculate the fitness of the animal.""" if self.w <= 0: fitness = 0 e_plus = math.exp(self.phi_age * (self.a - self.a_half)) e_minus = math.exp(-self.phi_weight * (self.w - self.w_half)) q_plus = 1 / (1 + e_plus) q_minus = 1 / (1 + e_minus) fitness = q_plus * q_minus if not 0 <= fitness <= 1: raise ValueError('fitness must be in [0, 1].') self.phi = fitness def migration(self): """ Decide whether an animal migrates. Returns ------- bool True if animal migrates. """ p_migrate = self.mu * self.phi return random.random() < p_migrate def ages(self): """Animal ages by one cycle.""" self.a += 1 def dies(self): """ Decide whether an animal dies. Returns ------- bool True if animal dies. """ if self.w == 0: p_death = 0 else: p_death = self.omega * (1 - self.fitness) return random.random() < p_death def gives_birth(self, num_animals): """ Decide whether an animal gives birth. Parameters ---------- num_animals : int (number of animals of same species in the cell) Returns ------- bool True if animal gives birth. """ p_birth = 0 if num_animals >= 2: if self.w < (self.zeta * (self.w_birth * self.sigma_birth)): p_birth = 0 else: p_birth = min(1, (self.gamma * self.phi * (num_animals - 1))) return random.random() < p_birth def eat(self): """Weight gain of animal on eating fodder F""" self.w += self.beta * self.F def weight_loss(self): """Yearly weight loss of an animal""" self.w -= self.eta * self.w class Herbivore(Animal): w_birth = 8.0 sigma_birth = 1.5 beta = 0.9 eta = 0.05 phi_age = 0.6 w_half = 10.0 phi_weight = 0.1 mu = 0.25 gamma = 0.2 xi = 1.2 omega = 0.4 F = 10.0, a_half = 40.0 zeta = 3.5 default_params = {'w_birth': w_birth, 'sigma_birth': sigma_birth, 'beta': beta, 'eta': eta, 'a_half': a_half, 'zeta': zeta, 'phi_age': phi_age, 'w_half': w_half, 'phi_weight': phi_weight, 'mu': mu, 'gamma': gamma, 'xi': xi, 'omega': omega, 'F': F} def __init__(self): super().__init__() self.species = 'Herbivore' @classmethod def get_params(cls): """ Get class parameters. Returns ------- dict Dictionary with class parameters. """ return {'w_birth': cls.w_birth, 'sigma_birth': cls.sigma_birth, 'beta': cls.beta, 'eta': cls.eta, 'a_half': cls.Animal.a_half, 'zeta': cls.Animal.zeta, 'phi_age': cls.phi_age, 'w_half': cls.w_half, 'phi_weight': cls.phi_weight, 'mu': cls.mu, 'gamma': cls.gamma, 'xi': cls.xi, 'omega': cls.omega, 'F': cls.F} class Carnivore(Animal): w_birth = 6.0 sigma_birth = 1.0 beta = 0.75 eta = 0.125 phi_age = 0.3 w_half = 4.0 phi_weight = 0.4 mu = 0.4 gamma = 0.8 xi = 1.1 omega = 0.8 F = 50.0 DeltaPhiMax = 10.0, a_half = 40.0 zeta = 3.5 default_params = {'w_birth': w_birth, 'sigma_birth': sigma_birth, 'beta': beta, 'eta': eta, 'a_half': a_half, 'zeta': zeta, 'phi_age': phi_age, 'w_half': w_half, 'phi_weight': phi_weight, 'mu': mu, 'gamma': gamma, 'xi': xi, 'omega': omega, 'F': F, 'DeltaPhiMax': DeltaPhiMax} def __init__(self): super().__init__() self.species = 'Carnivore' @classmethod def get_params(cls): """ Get class parameters. Returns ------- dict Dictionary with class parameters. """ return {'w_birth': cls.w_birth, 'sigma_birth': cls.sigma_birth, 'beta': cls.beta, 'eta': cls.eta, 'a_half': cls.a_half, 'zeta': cls.zeta, 'phi_age': cls.phi_age, 'w_half': cls.w_half, 'phi_weight': cls.phi_weight, 'mu': cls.mu, 'gamma': cls.gamma, 'xi': cls.xi, 'omega': cls.omega, 'F': cls.F, 'DeltaPhiMax': cls.DeltaPhiMax } def kill(self, herbivore): if self.phi <= herbivore.phi: p_kill = 0 elif 0 < (self.phi - herbivore.phi) < self.DeltaPhiMax: p_kill = (self.phi - herbivore.phi) / self.DeltaPhiMax else: p_kill = 1 kill = random.random() < p_kill if kill: self.w += self.beta * herbivore.w return kill
I understand the moste of the code, but when it comes to sup_class so it's hard for me to understand why it's writen like that
def __init__(self):
super().__init__()
self.species = 'Herbivore'
and
def __init__(self):
super().__init__()
self.species = 'Carnivore'
what is the diffrence between fixture test and statistical tests?
thanks :)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
