Strike
06-16-2002, 11:07 PM
In this thread (http://coderforums.net/showthread.php?s=&threadid=543), I have the full code posted, but I wanted to post it here too (I know, I know, cross-posting bad, but this isn't a strict cross-post). I wanted to confirm that I'm not insane and that there has to be some sort of error with my Gene class, because I am really quite sure that my algorithm matches the one mentioned here (http://www.gameai.com/buckland/ga_tutorial.html), in the "Brief overview" section. Here's the main part of the code:
# Construct a basic population set
genePopulation = \
[Gene("", mutationRate, crossoverRate) for i in range(populationSize)]
# Now put data in each of them, set the evalFunc, and determine the
# fitness of each.
for gene in genePopulation:
gene.makeRandomBits(chromosomeLength)
gene.setEvalFunc(diophantineEvalFunc)
gene.setFitness(diophantineSolution)
# Create a list of all the genes' likelihood values
# Note that lhList[0] corresponds to the gene in genePopulation[0], as
# does any lhList[i] and genePopulation[i] for i < populationSize
lhList = buildLikelihoods(genePopulation)
# Now we continue crossing the genes over and mutating them until we have
# a likelihood that is greater than some pre-determined acceptable value
# Start a generation counter
generationCount = 0
while max(lhList) < acceptableLikelihood:
print max(lhList), acceptableLikelihood
raw_input()
# Based on the likelihoods, choose populationSize pairs of parent
# genes to produce a new generation of offspring.
#
generationCount += 1
newGenePopulation = []
for i in range(populationSize):
# Get two parent genes
gene1 = genePopulation[getGeneIndex(lhList)]
gene2 = genePopulation[getGeneIndex(lhList)]
# Cross them over
gene1.crossover(gene2)
# Mutate the resulting gene
gene1.mutate()
# Reset the fitness given the new bits values
gene1.setFitness(diophantineSolution)
# Add to the new generation of genes
newGenePopulation.append(gene1)
# Out with the old, in with the new
genePopulation = newGenePopulation
# Recalculate the likelihoods again so we can check them again
lhList = buildLikelihoods(genePopulation)
# We have at least one good gene! Find out what gene it is, and print out
# the data.
geneIndex = lhList.index(max(lhList))
goodGene = genePopulation[geneIndex]
print goodGene
print "\nTook %d generations." % generationCount
Now, it seems to me (after mulling over it like 20 times), that it's a pretty good match. And I'm quite sure that all the non-Gene methods are implemented correctly as well. I was on the verge of building a unit testing scheme for this, but I thought that might be a bit overboard. If no one can help me isolate the error in this, I might have to.
# Construct a basic population set
genePopulation = \
[Gene("", mutationRate, crossoverRate) for i in range(populationSize)]
# Now put data in each of them, set the evalFunc, and determine the
# fitness of each.
for gene in genePopulation:
gene.makeRandomBits(chromosomeLength)
gene.setEvalFunc(diophantineEvalFunc)
gene.setFitness(diophantineSolution)
# Create a list of all the genes' likelihood values
# Note that lhList[0] corresponds to the gene in genePopulation[0], as
# does any lhList[i] and genePopulation[i] for i < populationSize
lhList = buildLikelihoods(genePopulation)
# Now we continue crossing the genes over and mutating them until we have
# a likelihood that is greater than some pre-determined acceptable value
# Start a generation counter
generationCount = 0
while max(lhList) < acceptableLikelihood:
print max(lhList), acceptableLikelihood
raw_input()
# Based on the likelihoods, choose populationSize pairs of parent
# genes to produce a new generation of offspring.
#
generationCount += 1
newGenePopulation = []
for i in range(populationSize):
# Get two parent genes
gene1 = genePopulation[getGeneIndex(lhList)]
gene2 = genePopulation[getGeneIndex(lhList)]
# Cross them over
gene1.crossover(gene2)
# Mutate the resulting gene
gene1.mutate()
# Reset the fitness given the new bits values
gene1.setFitness(diophantineSolution)
# Add to the new generation of genes
newGenePopulation.append(gene1)
# Out with the old, in with the new
genePopulation = newGenePopulation
# Recalculate the likelihoods again so we can check them again
lhList = buildLikelihoods(genePopulation)
# We have at least one good gene! Find out what gene it is, and print out
# the data.
geneIndex = lhList.index(max(lhList))
goodGene = genePopulation[geneIndex]
print goodGene
print "\nTook %d generations." % generationCount
Now, it seems to me (after mulling over it like 20 times), that it's a pretty good match. And I'm quite sure that all the non-Gene methods are implemented correctly as well. I was on the verge of building a unit testing scheme for this, but I thought that might be a bit overboard. If no one can help me isolate the error in this, I might have to.