def realCrossover( self, parent1, parent2, crossPoint ): child1 = Individual( chromosome = numpy.append( parent1[:crossPoint], parent2[crossPoint:] ), function = self.function ) child2 = Individual( chromosome = numpy.append( parent2[:crossPoint], parent1[crossPoint:] ), function = self.function ) return ( child1, child2 )
def lcCrossover( self, parent1, parent2 ): alpha = numpy.random.uniform() c1 = alpha * parent1 + ( 1 - alpha ) * parent2 c2 = ( 1 - alpha ) * parent1 + alpha * parent2 child1 = Individual( chromosome = c1, function = self.function ) child2 = Individual( chromosome = c2, function = self.function ) return ( child1, child2 )
def realMutation( self, chromosome ): pos = randrange( 0, self.dimensionsLength ) lim_min, lim_max = self.limits[0], self.limits[1] sigma = (lim_max - lim_min) / 100 new_chrom = numpy.array( chromosome[:] ) new_chrom[pos] += numpy.random.randn() * sigma if new_chrom[pos] > lim_max or new_chrom[pos] < lim_min: return Individual(chromosome = chromosome, function = self.function) child = Individual( chromosome = new_chrom, function = self.function ) return child
def mutation( self, chromosome ): position = randint( 0, self.chromosomeLength ) child = Individual( chromosome = chromosome ^ 2 ** position, function = self.function ) return child
def crossover( self, parent1, parent2, crossPoint ): mask1 = sum( [2 ** i for i in range( crossPoint )] ) mask2 = sum( [2 ** i for i in range( crossPoint, self.chromosomeLength )] ) crossParent11 = parent1 & mask1 crossParent12 = parent1 & mask2 crossParent21 = parent2 & mask1 crossParent22 = parent2 & mask2 child1 = Individual( chromosome = crossParent11 | crossParent22, function = self.function ) child2 = Individual( chromosome = crossParent21 | crossParent12, function = self.function ) return ( child1, child2 )
def createPopulation( self ): self.population = [Individual( chromosome = randint( 0, sum( [2 ** i for i in range( self.chromosomeLength )] ) ), function = self.function ) for _ in range( self.populationLength )]
def dcCreatePopulation( self ): minLim, maxLim = self.limits[0], self.limits[1] self.population = numpy.array( [Individual( chromosome = numpy.random.uniform( minLim, maxLim, self.dimensionsLength ), function = self.function ) for _ in range( self.populationLength )] )