Question

In: Computer Science

Material covered: Loops Functions Data Structures Randomness Numpy arrays (python language) Problem statement A single amoeba...

Material covered:

  • Loops
  • Functions
  • Data Structures
  • Randomness
  • Numpy arrays

(python language)

Problem statement

A single amoeba sits in a pitri dish. Every 5 minutes one of two things will happen to the amoeba:

1.    There is a chance that the amoeba dies - producing no offspring.

2.    If it does not die the amoeba splits into two amoebas.

make a function that simulates a single trial to calculate the lifespan of a colony of amoebas, given their chance of survival. Then, run that trial for some number of repeated trials (iterations).

To make this work, write the following functions. Test your functions fully before moving to the next one. You do not have to follow this order, but it is encouraged.

BUILD A PROGRAM USING THIS 4 STEPS.

1 - Calculate next generation

make a function that is passed the number of living amoebas, and the odds of survival - then calculates how many amoebas are alive in the next generation. Each amoeba in the population has a random chance of splitting, or dying. See the above random chance of dividing, or dying. Calculate the number of amoeba in the new generation.

2 - Single Trial

built a function that executes a single trial of the amoeba experiment. A trial is to simulate up to 20 generations of an amoeba colony. If there are no remaining amoebas, the function should exit without continuing calculating more generations.

This function should return a tuple that contains three pieces of data:

1.    The number of iterations that were simulated.

2.    A boolean indicates if all the amoebas are dead before 20 generations.

3.    The final population.

3 - Repeat the trial

built a function that will repeat the trial function 1000 times, storing the output of the trial in numpy arrays. Print the report for these repeated trials. Your output should be to two decimal places.

Report on the percentage of colonies that did not survive (end with 0 amoebas), and the average number of generations for failed colonies. Finally, report on the average population size on successful populations.

4 - Repeat trials with different survival rates

Do many trials, using different survival rates. Start with a 50% survival rate, then report in intervals of 5 all the way up to 95% survival rate.

This piece can be in a function, but does not need to be.

Sample output - yours will vary in terms of numbers:

OUTPUT:

For survival odds 0.50:

  The amoebas did not survive 93.70% of the time.

  On failures, there were 2.83 generations on average.

  If the amoebas did survive, the average population was 14.10

For survival odds 0.55:

  The amoebas did not survive 80.10% of the time.

  On failures, there were 2.71 generations on average.

  If the amoebas did survive, the average population was 31.61

And so on, up to 95%

Solutions

Expert Solution

(*Note: Please up-vote. If any doubt, please let me know in the comments)

CODE:

import numpy as np

def next_gen(num_living,odd_survival):
next_gen_prob = np.random.random(num_living) #assign a random number for each amoeba, if number is less than or equal to survival rate, the amoeba will survive
prob_survival = odd_survival/(1+odd_survival)
prob_death = 1-prob_survival
next_gen_count = 2*next_gen_prob[next_gen_prob>prob_death].size #those survived will become twice by slplitting
return next_gen_count #return count of those whose prob value > odds of not surviving

def trial(odd_survival):
iterations = 0
start = 100
curr_count = start
all_dead_before_20 = False
while curr_count>0 and iterations<20:
curr_count = next_gen(curr_count,odd_survival)
iterations += 1
if(curr_count<=0):
all_dead_before_20 = True
return (iterations,all_dead_before_20,curr_count)

def repeat_trial(odd_survival,num_trials):
result = np.zeros((num_trials,3))
for i in range(num_trials):
result[i] = trial(odd_survival)
#print(result[i])
print("The amoebas did not survive {:.2f}% of the time.".format(100*result[:,1].sum()/num_trials))
print("On failures, there were {:.2f} generations on average.".format(result[result[:,1]>0][:,0].mean()))
print("If the amoebas did survive, the average population was {:.2f}".format(result[result[:,1]==0][:,2].mean()))

rate = 0.5

while rate<1:
print("For survival odds %.2f : "%rate)
repeat_trial(rate,1000)
rate += 0.05

Output Screenshot:


Related Solutions

Please write code for C language Problem: Write a couple of functions to process arrays. Note...
Please write code for C language Problem: Write a couple of functions to process arrays. Note that from the description of the function you have to identify what would be the return type and what would be part of the parameter. display(): The function takes an int array and it’s size and prints the data in the array. sumArray(): It takes an int array and size, and returns the sum of the elements of the array. findMax(): It takes an...
This problem contains loops, sorting arrays, random generation, and variable matching. Include an analysis (data requirements)...
This problem contains loops, sorting arrays, random generation, and variable matching. Include an analysis (data requirements) and algorithm in your reply. Write a “lottery engine” program to simulate drawing lottery numbers. You will draw 6 numbers, randomly selected from 1 to 22. a) Allow the user to enter their own selection of numbers first, b) Then run your “lottery engine” to select the “winning numbers”. Numbers are drawn randomly. Use the clock as the seed value for your random function...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT