-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredings-rabbits-2018.py
150 lines (117 loc) · 6.06 KB
/
redings-rabbits-2018.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import random
import math
import timeit
start = timeit.default_timer()
num_alb=10 # The number of albino (recessive) alleles in the population. 10 is a default value to start.
num_brown=10 # The number of brown (dominant) alleles in the population. 10 is a default value to start.
# The simulation starts with a 50-50 breakdown of dominant and recessive alleles in the population.
pct_dom = 50
pct_rec = 50
rec_tot = 50
brown_death = 25
alb_death = 75
round_count = 1
ans = "Y"
alb_ct=1
responded = False
# Variable to remember in which round the user decided to continue the simulation. Apparently needed to check if they want to continue again?
ans_round = 0
# Ask users to provide a population size, then figure out the total number of alleles (2 for every member of the population)
pop = int(input("Enter a starting population size: "))
num_alleles = pop * 2
# How frequently should results be output to show samples of the population over generations
interval = int(input("Print out results every _ generations: "))
# At what rate do brown rabbits and albino rabbits die in this environment? The default values are 25% for brown and 75% for albino.
brown_death = int(input("Please enter a brown rabbit mortality rate (%): "))
alb_death = int(input("Please enter an albino rabbit mortality rate (%): "))
# Main body of the program. This will continue to run as long as the user wishes to continue and the potential for an albino rabbit remains in the population (at least 2 recessive alleles).
while(rec_tot>=1 and ans.upper()=="Y"):
# This is a reset so that the program will ask the user again if he/she wishes to continue every pop/10 rounds
#if(ans_round>0 and (round_count == 25)):
# responded = False
alleles = [] # Creates/empties our array of alleles each round
rabbits = [] # Creates/empties our array of rabbits each round
#Distribute allele frequency over population
num_brown = round((num_alleles*pct_dom)/100)
num_alb = round((num_alleles*pct_rec)/100)
# Picking the larger number of alleles in order to reduce run time. Not sure if this actually saves any time.
largest_num = max(num_brown, num_alb)
# Generating list of all alleles in the population. Theoretically alternates until the min(num_alb, num_brown) is reached, then it will only add the allele that occurs at greater frequency.
for i in range(0,largest_num):
if(i<num_brown):
alleles.append('B')
if(i<num_alb):
alleles.append('b')
# Distribute alleles randomly into pairs to create our population of rabbits for each round
for i in range(0, pop):
curr_rab = [alleles.pop(random.randint(0,len(alleles)-1)), alleles.pop(random.randint(0,len(alleles)-1))]
rabbits.append(curr_rab)
# Reset for each round to clear out any data on the last round's population
albinos = []
browns = []
alb_kill_ct = 0
brown_kill_ct = 0
alb_ct = 0
brown_ct = 0
# Check each rabbit in our population to see how many are albino and how many are brown. Sort rabbits out according to this.
for rabbit in rabbits:
#print("Alleles: ", rabbit[0], rabbit[1])
if(rabbit[0]=='b' and rabbit[1]=='b'):
alb_ct+=1
albinos.append(rabbit)
else:
brown_ct+=1
browns.append(rabbit)
# If it is the nth round, as defined by the user, print out a snapshot of this population
#if(round_count%interval==0):
# print("\nRound ", round_count, ":\n")
# print("Brown alleles- ", num_brown, " \nAlbino alleles- ", num_alb)
# print("Brown ct: ",len(browns), "Alb ct:", len(albinos))
rec_tot = 0
dom_tot = 0
if(len(albinos)>1):
alb_kill_ct = math.ceil(len(albinos)*(alb_death/100)) # Remove albinos from the population, according to their mortality rate
for i in range(0, alb_kill_ct):
#print("r", i, ";", alb_kill_ct)
rabbits.remove(albinos.pop()) # Remove all killed albinos from our population of rabbits
# elif was set up to automatically kill the only albino in the population, if only 1 existed. NOT redundant.
elif(len(albinos)==1):
alb_kill_ct=1
rabbits.remove(albinos.pop())
if(len(browns)>0):
brown_kill_ct = math.ceil(len(browns)*(brown_death/100))
for i in range(0, (brown_kill_ct)):
rand_brown = random.choice(browns) # For each brown rabbit that dies, randomly choose that rabbit from our list of all browns
rabbits.remove(rand_brown) # Remove chosen rabbit from overall population
browns.remove(rand_brown) # Remove chosen rabbit from sub-population of browns, so that it isn't chosen again
# Go back through to get allele counts, now that dead rabbits have been removed from the population
for rabbit in rabbits:
if(rabbit[0]=='b'):
rec_tot+=1
else:
dom_tot+=1
if(rabbit[1]=='b'):
rec_tot+=1
else:
dom_tot+=1
pct_dom = ((dom_tot)/(dom_tot+rec_tot))*100
pct_rec = ((rec_tot)/(dom_tot+rec_tot))*100
if(round_count%interval==0 or rec_tot==0 or (alb_ct==0 and responded != True)):
print("\nGeneration ", round_count, ":\n")
print("Brown Rabbits: ", brown_ct, "\nAlbino Rabbits: ", alb_ct, "\n", alb_kill_ct, " albinos die.\n", brown_kill_ct, " brown rabbits die.\nRemaining rabbits: ", len(rabbits))
print("Dom alleles: ", dom_tot, "\nRec Alleles: ", rec_tot)
print("Dom percent: ", pct_dom, "\nRec Percent: ", pct_rec)
round_count+=1
if(alb_ct==0 and rec_tot>=2 and responded != True):
ans = input("You are %d rounds in. There are no albino rabbits in the population, but there are enough alleles for another albino to theoretically be born. Continute to 25 generations? (Y/N) " %(round_count-1))
ans_round= round_count-1
responded = True
elif(round_count-1 == 25):
ans = input("You now have data for 25 generations of rabbits. Continute until recessive allele disappears completely? (Y/N) ")
ans_round= round_count-1
responded = True
if(rec_tot<1):
print("It took ", round_count-1, " rounds for all possibility for albino rabbits to disappear.")
ans = "N"
stop = timeit.default_timer()
print("\n\n Program Runtime: \t", (stop-start), "s")