Skip to content

Instantly share code, notes, and snippets.

@drewsday
Created August 14, 2012 16:37
Show Gist options
  • Save drewsday/3350681 to your computer and use it in GitHub Desktop.
Save drewsday/3350681 to your computer and use it in GitHub Desktop.
# The user will enter the population size and the number of generations in our GUI
popsize = int(self.ui.linePopsize.text())
numgen = int(self.ui.lineGenerations.text())
# initialize some arrays
allele = ['P' , 'Q']
i = 0
gen0 = []
listP = []
listQ = []
# This next part randomly generates the initial population diversity.
# With a large population it is close to 50/50. With a small population it can be anything.
while i < popsize:
gen0 = gen0 + [allele[(randint(0,1))]]
i = i+1
# j is going to be the counter that steps through the generations
j = 1
prevgen = gen0
while j < numgen+1:
i = 0
nextgen = []
# This is where the magic happens.
# Each time through the loop over the entire population we select a
# random member of the previous generation to reproduce.
# Effectively this means that some members will not reproduce and
# that other members will produce multiple offspring.
while i < popsize:
nextgen = nextgen + [prevgen[(randint(0,popsize-1))]]
i = i+1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment