pop_int = convertPOP2Reseau(pop=pop, reseau_BIT_size=r_bit_len, reseau_amount=r_amount, reseau_bound=X_BOUND) fitness_input = list(map(lambda x: (x, sh_index), pop_int)) # 计算种群的适应度 pop_fitness = pool.starmap(fitness, fitness_input) # 可以放入可迭代对象,自动分配进程 # pop_fitness = list(map(lambda x: fitness(x, sh_index), pop_int)) # 调整适应度 last_fitness = get_fitness(pop_fitness) # 根据适应度进行选择 pop = select(pop, last_fitness) # 交叉变异 pop = C_M(pop, CROSS_RATE, DNA_SIZE, POP_SIZE, MUTATION_RATE) print('本次最高的适应度为:' + str(np.max(pop_fitness)) + ' 耗时:' + str(round(time.time() - t_s, 1)) + '秒!') print('最终的网格:' + str(pop_int[list(last_fitness).index(np.max(last_fitness))])) result_money.append(np.max(pop_fitness)) # 训练结束,找出最好的 print('收益提高情况:' + str(result_money)) print('最终的网格:' + str(pop_int[list(last_fitness).index(np.max(last_fitness))]))
for _ in range(N_GENERATIONS): F_values = F(translateDNA( pop, DNA_SIZE, X_BOUND)) # 计算适应度的值? compute function value by extracting DNA # 画图相关 if 'sca' in globals(): sca.remove() sca = plt.scatter(translateDNA(pop, DNA_SIZE, X_BOUND), F_values, s=200, lw=0, c='red', alpha=0.5) plt.pause(0.05) # 进行进化 fitness = get_fitness(F_values) print("Most fitted DNA: ", pop[np.argmax(fitness), :]) pop = select(pop, fitness, len(pop)) # pop_copy = pop.copy() pop = C_M(pop, CROSS_RATE, DNA_SIZE, POP_SIZE, MUTATION_RATE) # for parent in pop: # child = crossover(parent, pop_copy) # child = mutate(child) # parent[:] = child # 子孙代替父辈 plt.ioff() plt.show()