def rastrigins_minimization(population_size, max_generations, k_tournament_participants, crossover_probability, mutation_probability, n_genes_mutated): print(colored('\nInitial Population...\n', attrs=['bold'])) initial_population = population(population_size) population_fitness = fitness(initial_population) avg = [] best = [] avg_fitness = avg_performance(population_fitness) best_fitness = best_performance(population_fitness) avg.append(avg_fitness) best.append(best_fitness) generations = 0 while (generations < max_generations) and (minimum_found(population_fitness) is False): print(colored(f'\nGeneration {generations+1}...\n', attrs=['bold'])) new_population = [] for i in range(0, population_size): parents_selected = selection(population_fitness, k_tournament_participants) if crossover_probability >= cloning_probability(): child = single_point_crossover(parents_selected) else: child = cloning_best_parent(parents_selected) if mutation_chance(mutation_probability): mutation(child, n_genes_mutated) new_population.append(child[:]) population_fitness = fitness(new_population) generations += 1 avg_fitness = avg_performance(population_fitness) best_fitness = best_performance(population_fitness) avg.append(avg_fitness) best.append(best_fitness) return minimum(population_fitness), generations, avg, best
pop = geneEncoding(pop_size, chrom_length) # 初始化二进制种群基因编码 for i in range(iterations): # print('NO. ', i + 1) fit_value = calobjValue(pop, chrom_length, max_value) # 适应度计算 # print('初始', fit_value) # fit_value = calfitValue(obj_value) # 筛选淘汰 best_indiviual, best_fit = best(pop, fit_value) # 最优解集 best_indiviual_ten = decoding(best_indiviual, chrom_length, max_value) # 最优解集进制转换 results.append([best_indiviual_ten, best_fit]) pop, fit_value = selection(pop, fit_value, pop_size, chrom_length, max_value) # print('选择', fit_value) pop, fit_value = crossover(pop, pc, fit_value, chrom_length, max_value) # print('交叉', fit_value) pop, fit_value = mutation(pop, pm, chrom_length, max_value) # print('变异', fit_value) print(results[iterations - 1][1]) X = [] Y = [] for i in range(iterations): X.append(i) Y.append(results[i][1]) plt.plot(X, Y) plt.show()
CR = sys.argv[5] iteration = sys.argv[6] Ld = float(Ud) * (-1) start = time.time() # Initial populationDataOriginal = np.zeros((int(population), int(dim))) for eachpopulationData_colum in range(int(population)): for eachpopulationData_row in range(int(dim)): r = rand.random() populationDataOriginal[eachpopulationData_colum][eachpopulationData_row] = ((float(Ud) - Ld) * r) + Ld populationData = populationDataOriginal.copy() # copy populationDataOriginal to populationData best = 99999 for eachiteration in range(int(iteration)): # Mutation mutationData = mutation(populationData, float(F)) # Crossover crossoverData = crossover(populationDataOriginal, mutationData, float(CR)) # Selection selectionData = selection(populationDataOriginal, crossoverData, int(dim)) count = [] for i in range(np.size(selectionData, 0)): count.append(fitness(selectionData[i][:], int(dim))) if count[i] < best: best = count[i] print(eachiteration, best) # reset populationDataOriginal = selectionData.copy() populationData = selectionData.copy()
def SPSOGA(Task): # Initial popution particle_swarm_server = [] particle_swarm_order = [] for i in range(numberOfParticle): particle_server = [] particle_order = [] for j in range(len(Task.subTask)): particle_server.append(random.randint(0, numberOfServer - 1)) particle_order.append(j) random.shuffle(particle_order) particle_swarm_server.append(particle_server) particle_swarm_order.append(particle_order) # Initial pbest pbest_particles_server = particle_swarm_server pbest_particles_order = particle_swarm_order pbest_energy_cons = [] for i in range(numberOfParticle): pbest_energy_cons.append( particle2energy(Task, pbest_particles_server[i], pbest_particles_order[i])) # Initial gbest gbest_paticle_server = [] gbest_paticle_order = [] gbest_energy_cons = 9999999 for i in range(len(pbest_particles_server)): energy_cons = particle2energy(Task, pbest_particles_server[i], pbest_particles_order[i]) if energy_cons < gbest_energy_cons: gbest_energy_cons = energy_cons gbest_paticle_server = pbest_particles_server[i] gbest_paticle_order = pbest_particles_order[i] for i in range(iterations_max): # inertia weight w = 0.5 for j in range(numberOfParticle): # inertia particle_swarm_server[j], particle_swarm_order[j] = mutation( particle_swarm_server[j], particle_swarm_order[j], w) # personal cognition c_1 = random.random() particle_swarm_server[j], particle_swarm_order[j] = crossover( particle_swarm_server[j], particle_swarm_order[j], pbest_particles_server[j], pbest_particles_order[j], c_1) # social cognition c_2 = random.random() particle_swarm_server[j], particle_swarm_order[j] = crossover( particle_swarm_server[j], particle_swarm_order[j], gbest_paticle_server, gbest_paticle_order, c_2) # update pbest energy_cons = particle2energy(Task, pbest_particles_server[j], pbest_particles_order[j]) if energy_cons < pbest_energy_cons[j]: pbest_energy_cons[j] = energy_cons pbest_particles_server = pbest_particles_server[j] pbest_particles_order = pbest_particles_order[j] # update gbest if energy_cons < gbest_energy_cons: gbest_energy_cons = energy_cons gbest_paticle_server = pbest_particles_server[j] gbest_paticle_order = pbest_particles_order[j] return gbest_energy_cons
ddr2 = brain_player[j] temp = ddr1 ddr1 = ddr2 ddr2 = temp brain_player[i] = ddr1 brain_player[j] = ddr2 #print(brain_player) return brain_player, False found_the_best = False mr = 0 while True: mr, done = get_the_fitness(pops) if done: break new_pops = new_population(mr) mutated_pops = mutation(new_pops) populated = build_population(mutated_pops) pops = populated.make_pop() #print(pops) for i in range(len(mr)): if mr[i][0] == 0: mr[i] = (100000000, mr[i][1], mr[i][2]) print(mr) pygame.quit()