Order Now  +1 678 648 4277 

Modeling the evolution of the rabbit population in Python assignment help

In this simulation, the evolution of the rabbit population must be modeled, starting with two rabbits: one male and one female. Both rabbits will have a young at the end of the month, which will take an additional month to mature before they can breed. At the end of each month, for every two mature adults, a new calf will be born and this process will be repeated during the study time or until there are no more cages available. It is assumed that the scientists carrying the experiment has 500 available cages, and each one can hold one pair of rabbits.
Our Python assignment helpers have created a program that simulates these conditions and prints the number of adults, babies, and total population for each month. This simulation will be executed until there are not enough cages available to hold more rabbits.

Rabbit Population Simulation

To solve this problem, a class called PairOfRabbits has been created which will simulate an adult pair of rabbits. This class will have a field called lifetime which will contain the number of months of life that the pair of rabbits has.

In the main program, you define the number of available cages and the initial population. For this case, an initial population of 2 rabbits is defined, for which 499 cages are available at the time of starting the simulation.

Using a while loop, the simulation will run until the number of available cages drops to zero. When this happens, the total number of months it took for this event to occur will be printed, along with the total number of baby and adult rabbits.

The pairs of rabbits are stored in a list and using the len function the number of pairs (and occupied cages) is obtained for each instant of time.

import csv # We will have an initial pair of rabbits # Each pair of rabbits produces a new pair of new rabbits # Each cage can hold one pair of rabbits # We create a helper class class PairOfRabbits(): def __init__(self, lifetime = 0): self.lifetime = lifetime def step(self): self.lifetime += 1 # We will count the lifetime of the pair of rabbits in units of one, where one unit refers to one month # So, based on their lifetime, the pair of rabbits are categorized as follow: # 0: Baby # 1: Adult Pair of Non-breeders # 2: Adult Pair of Breeders cages_ = 500 # total number of cages # Create the initial pair of rabbits. pairs = list() pairs.append(PairOfRabbits(1)) # Initial number of available cages availableCages = cages_ - len(pairs) print("Month\tBreeders\tNon-breeders\tBabies\tSum") # Open csv file and write header f = open('rabbits.csv', 'w') header = ['Month', 'Adult Pairs of Breeders', 'Adult Pairs of Non-breeders', 'Pairs of Baby Rabbits Born (same as the # of breeder pairs)', 'Sum of Breeders, Non-breeders, and Babies'] writer = csv.DictWriter(f, fieldnames = header) writer.writeheader() month = 1 run_simulation = True while(run_simulation): # Check number of rabbits that can breed breeders = [p for p in pairs if p.lifetime > 1] babies = [p for p in pairs if p.lifetime == 0] nonbreeders = [p for p in pairs if p.lifetime == 1] # Increment the lifetime of each pair of rabbits for index, pair in enumerate(pairs): p = pair p.step() pairs[index] = p # Print results at beginning of current month print("{}\t{}\t\t{}\t\t{}\t{}".format(month, len(breeders), len(nonbreeders), len(babies), len(breeders)+len(nonbreeders)+len(babies))) row = { 'Month': month, 'Adult Pairs of Breeders': len(breeders), 'Adult Pairs of Non-breeders': len(nonbreeders), 'Pairs of Baby Rabbits Born (same as the # of breeder pairs)': len(babies), 'Sum of Breeders, Non-breeders, and Babies': len(breeders) + len(nonbreeders) + len(babies)} writer.writerow(row) # For each breeder, a new pair is generated for i in range(len(breeders)+len(nonbreeders)): pairs.append(PairOfRabbits(0)) availableCages -= 1 if(availableCages < 1): print("At the end of month {} the number of pairs of rabbits reached {}".format(month, len(pairs))) run_simulation = False break month += 1 f.close()