#主函数 pop_size=200 max_gen=300 solution=[] company = [0,118.719989,32.208781] zuhe=[] for n in range(pop_size): location = [[1, 118.77089, 32.242341], [2, 118.77116, 32.242325], [3, 118.758009, 32.236431], [4, 118.753302, 32.25167], [5, 118.743133, 32.24938], [6, 118.756796, 32.251199], [7, 118.745644, 32.21866], [8, 118.747782, 32.222601], [9, 118.754213, 32.230573], [10, 118.756172, 32.234696], [11, 118.751653, 32.211145], [12, 118.724399, 32.181262], [13, 118.732268, 32.168273], [14, 118.741251, 32.154059], [15, 118.739814, 32.149382], [16, 118.741502, 32.144766], [17, 118.753899, 32.156138], [18, 118.75663, 32.151614], [19, 118.722315, 32.136388], [20, 118.705346, 32.182783], [21, 118.640739, 32.082919], [22, 118.766203, 32.236144], [23, 118.82427, 32.339209], [24, 118.711883, 32.100773], [25, 118.624981, 32.078503], [26, 118.791455, 32.120714], [27, 118.77255, 32.113826], [28, 118.7786, 32.108053], [29, 118.806232, 32.115148], [30, 118.780361, 32.099122], [31, 118.772132, 32.113803], [32, 118.826143, 32.119479], [33, 118.770969, 32.104218]] random.shuffle(location) solution.append(location)#产生父代 #迭代 gen_now=0 # while (gen_now <= max_gen): while (True): function1_values = [function1(solution[i]) for i in range(0, pop_size)] function2_values = [function2(solution[i]) for i in range(0, pop_size)] # 生成两个函数值列表,构成一个种群 non_dominated_sorted_solution = fast_non_dominated_sort(function1_values[:], function2_values[:]) # 种群之间进行快速非支配性排序,得到非支配性排序集合0-200 #print(non_dominated_sorted_solution) # print("The best front for Generation number ", gen_now, " is") #print( non_dominated_sorted_solution[0]) # for valuez in non_dominated_sorted_solution[0]: # print((solution[valuez])) # print(function1(solution[valuez])) # print(function2(solution[valuez])) # print("\n") crowding_distance_values = [] # 计算非支配集合中每个个体的拥挤度 for i in range(0, len(non_dominated_sorted_solution)): crowding_distance_values.append( crowding_distance(function1_values[:], function2_values[:], non_dominated_sorted_solution[i][:])) solution2 = solution[:] #没问题 # print( crowding_distance_values) # print(len(crowding_distance_values)) while (len(solution2) < 2 * pop_size): a1 = random.randint(0, pop_size - 1) b1 = random.randint(0, pop_size - 1) # 选择 solution2.append((crossover(solution2[a1], solution2[b1]))[0]) solution2.append((crossover(solution2[a1], solution2[b1]))[1]) solution2.append(motation(solution2)) # 随机选择,将种群中的个体进行交配,得到子代种群2*pop_size # 随机选择,将种群中的个体进行交配,得到子代种群2*pop_size # print(solution2) # print(len(solution2)) if len(solution2)==2*pop_size+1: del(solution2[-1]) if len(solution2)==2*pop_size+2: del (solution2[-1]) del (solution2[-1]) function1_values2 = [function1(solution2[i]) for i in range(0, 2 * pop_size)] function2_values2 = [function2(solution2[i]) for i in range(0, 2 * pop_size)] non_dominated_sorted_solution2 = fast_non_dominated_sort(function1_values2[:], function2_values2[:]) # 将两个目标函数得到的两个种群值value,再进行排序 得到2*pop_size解 crowding_distance_values2 = [] for i in range(0, len(non_dominated_sorted_solution2)): crowding_distance_values2.append( crowding_distance(function1_values2[:], function2_values2[:], non_dominated_sorted_solution2[i][:])) # 计算子代的个体间的距离值 new_solution = [] for i in range(0, len(non_dominated_sorted_solution2)): non_dominated_sorted_solution2_1 = [ index_of(non_dominated_sorted_solution2[i][j], non_dominated_sorted_solution2[i]) for j in range(0, len(non_dominated_sorted_solution2[i]))] # 排序 front22 = sort_by_values(non_dominated_sorted_solution2_1[:], crowding_distance_values2[i][:]) front = [non_dominated_sorted_solution2[i][front22[j]] for j in range(0, len(non_dominated_sorted_solution2[i]))] front.reverse() for value in front: new_solution.append(value) if (len(new_solution) == pop_size): break if (len(new_solution) == pop_size): break # solution = [solution2[i] for i in new_solution] a= [solution2[i] for i in new_solution] if gen_now % 10==0: print(solution) if len(solution[0][0])==1: exit() if gen_now % 100==0: print(zuhe) gen_now = gen_now + 1 import numpy c = numpy.zeros((34, 34)) # c[a[0][0][0]][a[0][1][0]]+=1 for j in range(200): i = 0 while (2 * i + 1) < len(a[0]): c[a[j][2 * i][0]][a[j][i * 2 + 1][0]] += 1 i = i + 1 shan = [] for i in range(len(c)): for j in range(len(c)): if c[i][j] == 200: shan.append(i) shan.append(j) zuhe.append(i) zuhe.append(j) for i in range(len(shan)): a = shaixuan(a, shan[i]) solution=a
NSGA2算法二:加信息素
最新推荐文章于 2023-10-17 23:55:18 发布