本文整理汇总了Python中population.Population.mutateChromosome方法的典型用法代码示例。如果您正苦于以下问题:Python Population.mutateChromosome方法的具体用法?Python Population.mutateChromosome怎么用?Python Population.mutateChromosome使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类population.Population
的用法示例。
在下文中一共展示了Population.mutateChromosome方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run
# 需要导入模块: from population import Population [as 别名]
# 或者: from population.Population import mutateChromosome [as 别名]
def run(self):
population = Population(self.populationSize,self.chromosomeSize)
self.adjustedMutationRate = self.mutationRate
for epochNumber in range (1,self.numberOfEpochs+1):
print("Epoch # " + str(epochNumber))
nextGenerationChromosomes = []
weightedChromosomes,foundSolution = self.createWeightedSetOfChromosomes(population.getPopulation())
if foundSolution:
print("")
print("")
print("")
print("")
print("**********************")
print("Found the right answer")
print("**********************")
print("Chromosome: ")
print( weightedChromosomes[1])
print( "Readable version:")
print(self.chromosomeReadableFunction(weightedChromosomes[1]))
break
#if we have a stagnant population, up the mutation rate
populationIsStagnant= self.isPopulationStagnant(weightedChromosomes,self.acceptableStdDev)
if populationIsStagnant:
print("population was stagnant or stuck in local optima increasing mutation rate to "),
self.adjustedMutationRate += self.mutationRate
#capped at 50% because more is meaningless as we approach just flipping all the bits
if self.adjustedMutationRate > .30:
self.adjustedMutationRate = .30
print(self.adjustedMutationRate)
else :
self.adjustedMutationRate = self.mutationRate
if self.turnsTopWeightHasntChanged > 15:
print("top dog has been top too long...KILL THEM OFF")
datBomb = """
. . .
\|/
`--+--'
/|\
' | '
|
|
,--'#`--.
|#######|
_.-'#######`-._
,-'###############`-.
,'#####################`,
/#########################\
|###########################|
|#############################|
|#############################|
|#############################|
|#############################|
|###########################|
\#########################/
`.#####################,'
`._###############_,'
`--..#####..--'
"""
print(datBomb)
weightedChromosomes = Population.invokeWrathOfGodCatastrophe(weightedChromosomes)
self.turnsTopWeightHasntChanged = 0
weighting,bestChromosome = GeneticAlgorithm.findHighestWeightedChromosome(weightedChromosomes)
if weighting == self.lastTopWeight:
self.turnsTopWeightHasntChanged+=1
else:
self.lastTopWeight = weighting
self.turnsTopWeightHasntChanged = 0
readableVersion = self.chromosomeReadableFunction(bestChromosome)
print("the best of all the chromosomes was this:")
print("Weight:" + str(weighting) + " chromosome: " + bestChromosome + " readableVersion:" + readableVersion + " value: " + str(eval(readableVersion)))
if self.fitnessFunction(bestChromosome) == 0.0:
print("found the right answer")
break
numberOfElites = (int)(self.eliteRatio * self.populationSize)
elites = generalFunctions.takeHighestWeightedItems(weightedChromosomes,numberOfElites)
#if self.adjustedMutationRate > 0.4:
# print elites
nextGenerationChromosomes.extend(x[1] for x in elites)
for elite in elites:
for i in range (0,self.numberOfTimesToMutateElite):
mutatedElite = Population.mutateChromosome(elite[1],self.adjustedMutationRate)
nextGenerationChromosomes.append(mutatedElite)
while len(nextGenerationChromosomes) < self.populationSize:
#print('mayyyyyytinnnnng #' + str(len(nextGenerationChromosomes)))
pairToMate = GeneticAlgorithm.selectTwoToMate(self.selectionType,weightedChromosomes)
firstChild,secondChild = Population.crossoverChromosomes(pairToMate[0],pairToMate[1],self.adjustedMutationRate,self.geneSize)
nextGenerationChromosomes.append(firstChild)
nextGenerationChromosomes.append(secondChild)
population.setPopulation(nextGenerationChromosomes)