-
Notifications
You must be signed in to change notification settings - Fork 1
/
multi.py
71 lines (57 loc) · 2.36 KB
/
multi.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
import numpy as np
class Multi:
params = None
def __init__(self, params):
self.params = params
def get_functions(self):
return ['NSGA-II']
def NSGA_II(self, pop, pop_size, sel_size):
sel = []
fronts = self.fast_nondominated_sort(pop, pop_size)
j = 0
while len(sel) + len(fronts[j]) < sel_size:
for ind in fronts[j]:
sel.append(ind)
j += 1
np.random.shuffle(fronts[j])
k = 0
while len(sel) < sel_size:
sel.append(fronts[j][k])
k += 1
return sel
def fast_nondominated_sort(self, pop, pop_size):
fronts = [[] for _ in range(pop_size)]
for i in range(pop_size):
pop[i]['dominates'], pop[i]['dominated'] = set(), 0
for j in range(pop_size):
results = []
for k in range(self.params['num_objs']):
if self.params['objs'][k] == min:
if pop[i]['fitness'][k] < pop[j]['fitness'][k]:
results.append(True)
elif pop[j]['fitness'][k] < pop[i]['fitness'][k]:
results.append(False)
else:
results.append(None)
elif self.params['objs'][k] == max:
if pop[i]['fitness'][k] > pop[j]['fitness'][k]:
results.append(True)
elif pop[j]['fitness'][k] > pop[i]['fitness'][k]:
results.append(False)
else:
results.append(None)
if results == [True for _ in range(self.params['num_objs'])]:
pop[i]['dominates'].add(j)
elif results == [False for _ in range(self.params['num_objs'])]:
pop[i]['dominated'] += 1
if pop[i]['dominated'] == 0:
fronts[0].append(pop[i])
k = 0
while len(fronts[k]) > 0:
for i in range(len(fronts[k])):
for j in fronts[k][i]['dominates']:
pop[j]['dominated'] -= 1
if pop[j]['dominated'] == 0:
fronts[k + 1].append(pop[j])
k += 1
return fronts