A Python guide to the benefits of sexual reproduction.

Sexual reproduction

optimum = list("This is the best genetic code")

Python

import random
random.seed()
rc = []
popchar = []
popnum = 1000
characters = len(optimum)#creating the population - a list of fairly similar genetic codes
for j in range(popnum):
for i in range(characters):
numb = random.randint(32, 40)
char = chr(numb)
rc.append(char)
popchar.append(rc)
rc = []
#calculate fitness - how well adapted that individual is to the 
#environment (how close it is to the optimum sentence)
fitness = []
for x in range(popnum):
count = 0
for y in range(characters):
if popchar[x][y] == optimum[y]:
count += 1
fitness.append(count)

Python: Asexual reproduction

check = 0
counter = 0
while check < 1:
#get two random members of population
a = random.randint(0, (popnum-1))
b = random.randint(0, (popnum-1))
membera = popchar[a]
memberb = popchar[b]
fitnessmembera = fitness[a]
fitnessmemberb = fitness[b]
parenta = []
#see which is fitter and select as parent to mutate
if fitnessmembera > fitnessmemberb:
parenta = parenta + membera
else:
parenta = parenta + memberb
#mutate that parent
for y in range(characters):
numb2 = random.random()
if numb2 < (1. / len(optimum)):
parenta[y] = chr(random.randint(32, 126))
else:
parenta[y] = parenta[y]
child = parenta[:] #calculate fitness of child
fitchild = 0
fit = 0
for y in range(characters):
if child[y] == optimum[y]:
fit += 1
fitchild = fitchild + fit
#find another 2 random members
c = random.randint(0, (popnum-1))
d = random.randint(0, (popnum-1))
memberc = popchar[c]
memberd = popchar[d]
fitnessmemberc = fitness[c]
fitnessmemberd = fitness[d]
#replace weaker of the two members with the child
if fitnessmemberc > fitnessmemberd:
popchar[d] = child
fitness[d] = fitchild
else:
popchar[c] = child
fitness[c] = fitchild
#checks to see if optimum fitness has been reached
for i in range(popnum):
if fitness[i] == 28:
check += 1
print(popchar[i])
# move to the next generation
counter = counter + 1
print(counter)

Python: Sexual reproducer

check = 0
counter = 0
while check < 1: #get two random members of population
a = random.randint(0, (popnum - 1))
b = random.randint(0, (popnum - 1))
membera = popchar[a]
memberb = popchar[b]
fitnessmembera = fitness[a]
fitnessmemberb = fitness[b]
#see which is fitter and select as parent a
parenta = []
if fitnessmembera > fitnessmemberb:
parenta = parenta + membera
else:
parenta = parenta + memberb
#find another 2 random members
c = random.randint(0, (popnum - 1))
d = random.randint(0, (popnum - 1))
memberc = popchar[c]
memberd = popchar[d]
fitnessmemberc = fitness[c]
fitnessmemberd = fitness[d]
#see which is fitter and select as parent b
parentb = []
if fitnessmemberc > fitnessmemberd:
parentb = parentb + memberc
else:
parentb = parentb + memberd
#make the child by randomly combining the parents
child = []
for y in range(characters):
numb2 = random.random()
if numb2 < 0.5:
child.append(parenta[y])
else:
child.append(parentb[y])
#mutate that child
for y in range(characters):
numb2 = random.random()
if numb2 < (1. / len(optimum)):
child[y] = chr(random.randint(32, 126))
else:
child[y] = child[y]
#calculate fitness of child
fitchild = 0
fit = 0
for y in range(characters):
if child[y] == optimum[y]:
fit += 1
fitchild = fitchild + fit
#find another 2 random members
e = random.randint(0, (popnum - 1))
f = random.randint(0, (popnum - 1))
membere = popchar[e]
memberf = popchar[f]
fitnessmembere = fitness[e]
fitnessmemberf = fitness[f]
#replace weaker of the two members with the child
if fitnessmembere > fitnessmemberf:
popchar[f] = child
fitness[f] = fitchild
else:
popchar[e] = child
fitness[e] = fitchild
#checks to see if optimum fitness has been reached
for i in range(popnum):
if fitness[i] == len(optimum):
check += 1
print(popchar[i])
# move to the next generation
counter = counter + 1
print(counter)

Result

  • The ‘optimal’ genetic makeup changing with time — if it changes slowly or quickly.
  • A penalty of population loss for not being adapted to the environment.
  • What effect does the initial genetic variance of the population have on its ability to adapt quickly?
  • How does the initial population affect the time taken to reach the optimum?
  • Can you plot the change in fitness throughout the generations? What does the shape say about the adaptation?

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Janglais

Janglais

1 Follower

PhD in magnetospheric physics. Originally from the UK but now happily settled in France. I write about whatever takes my fancy, but mostly Python.