-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArchive_Update.py
78 lines (68 loc) · 2.27 KB
/
Archive_Update.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import Immune_Search
import pygmo as pg
import math
import numpy as np
def archive_update(S, A, N, f_num):
for i in range(len(S)):
rmlist = []
for j in range(len(A)):
flag = CheckDominance(S[i], A[j])
if flag == 1:
rmlist.append(A[j])
elif flag == -1:
break
for j in range(len(rmlist)):
A.remove(rmlist[j])
if flag != -1:
A.append(S[i])
if len(A) > N:
fitnesss = []
for i in range(len(A)):
fitnesss.append(A[i].fitness)
CDA = pg.crowding_distance(fitnesss)
for i in range(len(A)):
A[i].crowding_distance = CDA[i]
A = sort_by_crowding_distance(A)
del A[-1]
return A
def Dominate(y1, y2):
less = 0 # y1的目标函数值小于y2个体的目标函数值数目
equal = 0 # y1的目标函数值等于y2个体的目标函数值数目
greater = 0 # y1的目标函数值大于y2个体的目标函数值数目
for i in range(len(y1.fitness)):
if y1.fitness[i] > y2.fitness[i]:
greater = greater + 1
elif y1.fitness[i] == y2.fitness[i]:
equal = equal + 1
else:
less = less + 1
if (greater == 0 and equal != len(y1.fitness)):
return 1 # y1支配y2返回正确
elif (less == 0 and equal != len(y1.fitness)):
return -1 # y2支配y1返回false
else:
return 0
def CheckDominance(s, a):
less = 0
great = 0
for k in range(len(s.fitness)):
if (s.fitness[k] <= a.fitness[k]):
less += 1
if s.fitness[k] >= a.fitness[k]:
great += 1
if less == len(s.fitness):
return 1
elif great == len(s.fitness):
return -1
return 0
def sort_by_crowding_distance(population): # selection sort, which can be replaced with quick sort
p_list = []
for p in population:
p_list.append(p)
for i in range(0, len(p_list) - 1):
for j in range(i + 1, len(p_list)):
if p_list[i].crowding_distance < p_list[j].crowding_distance:
temp = p_list[i]
p_list[i] = p_list[j]
p_list[j] = temp
return p_list