def GenOffspring(self, pop): """ This function generates the offspring by applying crossover, mutation **or** reproduction. The sum of both probabilities self.cxpb and self.mutpb must be in [0,1] The reproduction probability is 1 - cxpb - mutpb The new offspring goes for fitness evaluation Inputs: pop (dict): population in dictionary structure Returns: offspring (dict): new modified population in dictionary structure """ pop_indices = list(range(0, len(pop))) offspring = defaultdict(list) for i in range(self.lambda_): alpha = random.random() #------------------------------ # Crossover #------------------------------ if alpha < self.cxpb: index1, index2 = random.sample(pop_indices, 2) ind1, ind2, strat1, strat2 = cxES2point( ind1=list(pop[index1][0]), ind2=list(pop[index2][0]), strat1=list(pop[index1][1]), strat2=list(pop[index2][1])) offspring[i].append(ind1) offspring[i].append(strat1) #print('crossover is done for sample {} between {} and {}'.format(i,index1,index2)) #------------------------------ # Mutation #------------------------------ elif alpha < (self.cxpb + self.mutpb): # Apply mutation index = random.choice(pop_indices) ind, strat = mutES(ind=list(pop[index][0]), strat=list(pop[index][1]), lb=self.lbound, ub=self.ubound, datatype=self.datatype, smax=self.smax, smin=self.smin) offspring[i].append(ind) offspring[i].append(strat) #print('mutation is done for sample {} based on {}'.format(i,index)) #------------------------------ # Reproduction from population #------------------------------ else: index = random.choice(pop_indices) offspring[i].append(pop[index][0]) offspring[i].append(pop[index][1]) #print('reproduction is done for sample {} based on {}'.format(i,index)) return offspring
def GenOffspring(self, pop): #""" # #This function generates the offspring by applying crossover, mutation **or** reproduction. #The sum of both probabilities self.cxpb and self.mutpb must be in [0,1] #The reproduction probability is 1 - cxpb - mutpb #The new offspring goes for fitness evaluation #Inputs: # pop (dict): population in dictionary structure #Returns: # offspring (dict): new modified population in dictionary structure #""" pop_indices = list(range(0, len(pop))) offspring = defaultdict(list) for i in range(self.lambda_): rn = random.random() #------------------------------ # Crossover #------------------------------ if rn < self.cxpb: index1, index2 = random.sample(pop_indices, 2) if self.cxmode.strip() == 'cx2point': ind1, ind2, strat1, strat2 = cxES2point( ind1=list(pop[index1][0]), ind2=list(pop[index2][0]), strat1=list(pop[index1][1]), strat2=list(pop[index2][1])) elif self.cxmode.strip() == 'blend': ind1, ind2, strat1, strat2 = cxESBlend( ind1=list(pop[index1][0]), ind2=list(pop[index2][0]), strat1=list(pop[index1][1]), strat2=list(pop[index2][1]), alpha=self.alpha) else: raise ValueError( '--error: the cxmode selected (`{}`) is not available in ES, either choose `cx2point` or `blend`' .format(self.cxmode)) offspring[i].append(ind1) offspring[i].append(strat1) #print('crossover is done for sample {} between {} and {}'.format(i,index1,index2)) #------------------------------ # Mutation #------------------------------ elif rn < self.cxpb + self.mutpb: # Apply mutation index = random.choice(pop_indices) ind, strat = self.mutES(ind=list(pop[index][0]), strat=list(pop[index][1])) offspring[i].append(ind) offspring[i].append(strat) #print('mutation is done for sample {} based on {}'.format(i,index)) #------------------------------ # Reproduction from population #------------------------------ else: index = random.choice(pop_indices) offspring[i].append(pop[index][0]) offspring[i].append(pop[index][1]) #print('reproduction is done for sample {} based on {}'.format(i,index)) return offspring