def run(self): population = [] best_genome = None data = [] for _ in range(population_total): genome_dna = generate(problem_grid) population.append(Genome(genome_dna)) for i in range(simulations): population_fitness = 0 for genome in population: genome.fitness() population_fitness += genome.getFitness() sorted_population = population.copy() best_genome = max(population, key=operator.attrgetter('fit')) best_fitness = round(1 / best_genome.getFitness()) data.append(best_fitness) if i % 1000 == 0: self.show(i, best_genome) if best_fitness <= limit: print('DONE\n') population.clear() population.append(best_genome) while len(population) < population_total: new_genome = tournamentSelection(sorted_population) option_2 = tournamentSelection(sorted_population) if npR.uniform() < crossover_rate: option_3 = tournamentSelection(sorted_population) dna_1 = crossover(new_genome, option_2, option_3) new_genome = Genome(dna_1) if npR.uniform() < mutation_rate: new_genome.mutateSell(problem_grid) population.append(new_genome) self.show(i, best_genome) return data
def run(self): for _ in range(population_total): scramble = shuffle(key) self.population.append(Genome(scramble)) for _ in range(100000): #print('GENERATION :', i, round(1/population[0].getFitness())) population_fitness = 0 for genome in self.population: genome.fitness(row_score, col_score) population_fitness += genome.getFitness() for genome in self.population: genome.setFitness2Population(population_fitness) sorted_population = sorted( self.population, key=operator.attrgetter('fitness_ratio')) sorted_population.reverse() best = sorted_population[0] sorted_population.append(best) if 1 / best.getFitness() <= 10: break self.population.clear() for _ in range(population_total - 1): new_genome = rouletteSelection(sorted_population) option_2 = rouletteSelection(sorted_population) if npR.uniform() < crossover_rate: new_genome = Genome(crossover(new_genome, option_2)) if npR.uniform() < mutation_rate: new_genome.mutate() self.population.append(new_genome)
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()
sorted_population = population.copy() best_genome = max(population, key=operator.attrgetter('fit')) if i%1000 == 0: show(i) image_output(puzzle_img, best_genome.getDNA(), digits) if round(1/best_genome.getFitness()) <= 1: print('Done') break population.clear() population.append(best_genome) while len(population) < population_total: new_genome = tournamentSelection(sorted_population) option_2 = tournamentSelection(sorted_population) if npR.uniform() < crossover_rate: option_3 = tournamentSelection(sorted_population) dna_1 = crossover(new_genome, option_2, option_3) new_genome = Genome(dna_1) if npR.uniform() < mutation_rate: new_genome.mutate(problem_grid) population.append(new_genome) show(i) image_output(puzzle_img, best_genome.getDNA(), digits)
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
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() end = time.time() print(end-start)
# sorted_population.reverse() sorted_population = population.copy() best_genome = max(population, key=operator.attrgetter('fit')) if i % 10000 == 0: print('Gen:', i, '--> ', round(1 / best_genome.getFitness())) show() if round(1 / best_genome.getFitness()) <= 200: print('Done') break population.clear() population.append(best_genome) while len(population) < population_total: new_genome = tournamentSelection(sorted_population) option_2 = tournamentSelection(sorted_population) if npR.uniform() < crossover_rate: dna_1, dna_2 = crossover(new_genome, option_2) new_genome = Genome(dna_1) new_genome_2 = Genome(dna_2) population.append(new_genome_2) if npR.uniform() < mutation_rate: new_genome.mutate() population.append(new_genome) show() print('Complete')