NSGA2演算法一

2021-10-09 09:50:24 字數 4553 閱讀 2482

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)

#迭代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(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)

# 選擇

# 隨機選擇,將種群中的個體進行交配,得到子代種群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(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:

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)

nsga2演算法 NSGA2 遺傳演算法解決多目標優化

進行多目標優化時,通常面臨多個目標函式無法同時達到最優的情況,為了解決這一矛盾,引入pareto optimality的概念 通常,多目標優化的一般形式為 經過處理,可以化為以下形式 其中f1 x f2 x fn x 為目標函式,其全部都是求最小值的形式 以下針對兩個目標函式進行討論 有幾個目標函式...

NSGA 2學習筆記

分類 演算法 2010 04 17 21 05 341人閱讀收藏 舉報 nsga2主要是對nsga演算法的改進。nsga是n.srinivas 和 k.deb在1995年發表的一篇名為 multiobjective function optimization using nondominated s...

NSGA 2學習筆記

nsga2主要是對nsga演算法的改進。nsga是n.srinivas 和 k.deb在1995年發表的一篇名為 multiobjective function optimization using nondominated sorting genetic algorithms 的 中提出的。該演算...