コード例 #1
0
	def evolve(self):
		""" Takes the best genes and creates a new genome for the next generation
		"""
		# Create 20 mutations of each of the 2 best chromosomes
		i = self.population-1
		nextGenChromosomesA = []
		nextGenChromosomesB = []
		while i >= 0:
			AChrom = Chromosome()
			BChrom = Chromosome()
			AChrom.genes = matrixEvolution.mutateMatrix(self.bestChromosomes[0].genes,self.mutationRate,self.mutationRange)
			BChrom.genes = matrixEvolution.mutateMatrix(self.bestChromosomes[1].genes,self.mutationRate,self.mutationRange)
			nextGenChromosomesA.append(AChrom)
			nextGenChromosomesB.append(BChrom)
			i -= 1

		#combine the two groups to form the next generation genome
		nextGenChromos = []
		for i in range(len(nextGenChromosomesA)):
			chrom = Chromosome()
			chrom.genes = matrixEvolution.mixMatrix(nextGenChromosomesA[i].genes,nextGenChromosomesB[i].genes)
			nextGenChromos.append(chrom)

		self.chromosomes = nextGenChromos
		self.generation += 1
コード例 #2
0
   def evolve(self):
           """ Takes the best genes and creates a new genome for the next generation
           """
           #print self.chromosomes[0].strength, self.chromosomes[1].strength
           bestChrom1=self.chromosomes[0]
           bestChrom2=self.chromosomes[1]
           for chrom in self.chromosomes[2:]:
               if chrom.strength > bestChrom1.strength:
                   #print 'strength1',chrom.strength
                   bestChrom1=chrom
               elif chrom.strength > bestChrom2.strength:
                   #print 'strength 2',chrom.strength
                   bestChrom2=chrom
           self.bestChromosomes=[bestChrom1,bestChrom2]
           # Create 20 mutations of each of the 2 best chromosomes
           i = self.population-3
           nextGenChromosomesA = []
           nextGenChromosomesB = []
           while i >= 0:
               AChrom = Chromosome(500)
               BChrom = Chromosome(500)
               AChrom.genes = matrixEvolution.mutateMatrix(self.bestChromosomes[0].genes,self.mutationRate,self.mutationRange)
               BChrom.genes = matrixEvolution.mutateMatrix(self.bestChromosomes[1].genes,self.mutationRate,self.mutationRange)
               nextGenChromosomesA.append(AChrom)
               nextGenChromosomesB.append(BChrom)
               i -= 1
               
           self.generation += 1
           
           #combine the two groups to form the next generation genome
           nextGenChromos = [bestChrom1,bestChrom2]
           for i in range(len(nextGenChromosomesA)):
               chrom = Chromosome(i+2)
               chrom.identification[0] = self.generation
               chrom.genes = matrixEvolution.mixMatrix(nextGenChromosomesA[i].genes,nextGenChromosomesB[i].genes)
               nextGenChromos.append(chrom)
           #print nextGenChromos[0].strength, nextGenChromos[1].strength
 
   
           self.chromosomes = nextGenChromos