def generate_new_population(population, weights, size, mutation_rate=0.1): dna_set = [1, 2, 3, 4, 5, 6, 7, 8] new_population = [] for _ in range(size): ind_x = random_selection(population, weights) ind_y = random_selection(population, weights) xy = reproduce(ind_x, ind_y) mutated_xy = mutation(xy, dna_set, mutation_rate) new_population.append(mutated_xy) return new_population
temp=pd.DataFrame([[interations,MinFunctionValue,AverageFunctionValue,MaxFitness,AverageFitness,BestIndex]], columns=metaDfVariableHeadings) metaDf = pd.concat([metaDf,temp]) # print(metaDf,"meta") #'BestFitnessValue','AverageFitness','BestIndexNumber'] # plotting # metaDf.plot(, ax = ax) # Pass the axes to plot. # print("max fitness value",df['FitnessValue'].idxmax()) mating_pool = utils.suss(fitness_scores,population,listOfVarStringLengths) # selection - mating pool generation # crossover of mating pool utils.generalCrossover(crossoverProbability,mating_pool,utils.singlePointCrossover,listOfVarStringLengths,listOfPrecisionDigits) # mutation of new mating pool utils.mutation(mating_pool,mutationProbability,listOfVarStringLengths,listOfPrecisionDigits) population = mating_pool interations = interations + 1 f1 = plt.figure(1) ax = plt.gca() metaDf.plot(kind='line',x='GenerationNumber',y='AverageFunctionValue',color='blue',ax=ax) metaDf.plot(kind='line',x='GenerationNumber',y='MinFunctionValue',color='red',ax=ax) f2 = plt.figure(2) ax = plt.gca() metaDf.plot(kind='line',x='GenerationNumber',y='AverageFitness',color='blue',ax=ax) metaDf.plot(kind='line',x='GenerationNumber',y='BestFitnessValue',color='red',ax=ax) plt.show()
def LL_static(initial_solution, instance, lda1, lda2, k, c, crossover_choice, max_evaluation, ioh_output=None): x = initial_solution n = len(x.chromossome) T = 0 function_value = [] neval = [] counter = 0 f = None if ioh_output is not None: f = open(ioh_output, 'at') f.write( '"function evaluation" "original f(x)" "best original f(x)" "transformed f(x) " "best transformed f(x)" "mutation_rate" "crossover_bias"' ) while (x.value != instance.optimum): #---------- mutation phase ------------ #sample l from the binomial p = k / float(n) l = np.random.binomial(n, p) while l == 0: l = np.random.binomial(n, p) x_prime = utils.mutation(x, l) x_prime.value = getattr(evaluate, instance.name)(x_prime, instance) T += 1 for i in range(lda1 - 1): x2_prime = utils.mutation(x, l) x2_prime.value = getattr(evaluate, instance.name)(x2_prime, instance) T += 1 if x2_prime.value > x_prime.value: x_prime = x2_prime #-------- crossover phase y, Ty = LL_crossover_phase(x=x, x_prime=x_prime, instance=instance, implementation_choice=crossover_choice, chromossome_length=n, n_offsprings=lda2, crossover_bias=c) T += Ty #-------- selection if y.value > x.value: x = y if f is not None: ls = [T, x.value, x.value, x.value, x.value, p, c] # IOH output s = ' '.join([str(val) for val in ls]) f.write('\n' + s) function_value.append(x.value) neval.append(T) counter += 1 if max_evaluation > 0 and T >= max_evaluation: break #End while if f is not None: f.write('\n' + str(T)) f.close() return (T, counter, function_value, neval)
def LL_dynamic_02(initial_solution, instance, alpha, beta, gamma, a, b, crossover_choice, max_evaluation, ioh_output=None): x = initial_solution n = len(x.chromossome) lda = float(1) T = 0 ldas = [] function_value = [] neval = [] counter = 0 min_prob = 1.0 / n max_prob = 0.99 f = None if ioh_output is not None: f = open(ioh_output, 'at') f.write( '"function evaluation" "original f(x)" "best original f(x)" "transformed f(x) " "best transformed f(x)" "mutation_rate" "crossover_bias" "n_mutations" "n_offsprings" "iteration"' ) while (x.value != instance.optimum): p = max([min([alpha * lda / float(n), max_prob]), min_prob]) lda1 = int(round(lda)) lda2 = int(round(beta * lda)) c = max([min([gamma / lda, max_prob]), min_prob]) #----------- mutation phase l = np.random.binomial(n, p) while l == 0: l = np.random.binomial(n, p) x_prime = utils.mutation(x, l) x_prime.value = getattr(evaluate, instance.name)(x_prime, instance) T += 1 for i in range(int(round(lda1) - 1)): x2_prime = utils.mutation(x, l) x2_prime.value = getattr(evaluate, instance.name)(x2_prime, instance) T += 1 if x2_prime.value > x_prime.value: x_prime = x2_prime #---------- crossover phase y, Ty = LL_crossover_phase(x=x, x_prime=x_prime, instance=instance, implementation_choice=crossover_choice, chromossome_length=n, n_offsprings=lda2, crossover_bias=c) T += Ty #---------- selection and update phase if (y.value <= x.value): #x = y lda = min([lda * a, n - 1]) if (y.value > x.value): x = y lda = max([lda * b, 1]) if f is not None: ls = [ T, x.value, x.value, x.value, x.value, p, c, lda1, lda2, counter ] # IOH output s = ' '.join([str(val) for val in ls]) f.write('\n' + s) ldas.append(lda) function_value.append(x.value) neval.append(T) counter += 1 if max_evaluation > 0 and T >= max_evaluation: break #End while if f is not None: f.write('\n' + str(T)) f.close() return (T, counter, function_value, neval)
def LL_static_02(initial_solution, instance, lda, alpha, beta, crossover_choice, max_evaluation, ioh_output=None): x = initial_solution n = len(x.chromossome) T = 0 function_value = [] neval = [] counter = 0 while (x.value != instance.optimum): #---------- mutation phase ------------ #sample l from the binomial p = alpha / float(n) l = np.random.binomial(n, p) while l == 0: l = np.random.binomial(n, p) x_prime = utils.mutation(x, l) x_prime.value = getattr(evaluate, instance.name)(x_prime, instance) T += 1 for i in range(lda - 1): x2_prime = utils.mutation(x, l) x2_prime.value = getattr(evaluate, instance.name)(x2_prime, instance) T += 1 if x2_prime.value > x_prime.value: x_prime = x2_prime #-------- crossover phase c = 1.0 / beta y, Ty = LL_crossover_phase(x=x, x_prime=x_prime, instance=instance, implementation_choice=crossover_choice, chromossome_length=n, n_offsprings=lda, crossover_bias=c) T += Ty #-------- selection if y.value > x.value: x = y function_value.append(x.value) neval.append(T) #counter += 1 if max_evaluation > 0 and T >= max_evaluation: break #if T % 1000 == 0: # print(T) # print(str(x.value) + '/' + str(instance.optimum)) #End while return (T, counter, function_value, neval)