Exemple #1
0
    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)