NSGA2算法一

pop_size=200
max_gen=300
solution=[]
company = [0,118.719989,32.208781]
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):
    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(solution[a1], solution[b1]))[0])
        solution2.append((crossover(solution[a1], solution[b1]))[1])
        solution2.append(motation(solution))
        # 随机选择,将种群中的个体进行交配,得到子代种群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]
    if gen_now % 50==0:
        print(solution)

    gen_now = gen_now + 1

# Lets plot the final front now
    functionx = [i * 1 for i in function1_values]
    functiony = [j * 1 for j in function2_values]
    plt.xlabel('Function 1', fontsize=15)
    plt.ylabel('Function 2', fontsize=15)
    plt.scatter(functionx, functiony)
    # plt.xlim((0.002, 0.006))
    # plt.ylim((100, 300))
    plt.pause(0.1)
plt.pause(0)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值