forked from EMI-Group/GMOEA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspea2_env.py
62 lines (50 loc) · 1.76 KB
/
spea2_env.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
import numpy as np
from scipy.spatial.distance import cdist
def environment_selection(population, n):
"""
environmental selection in SPEA-2
:param population: current population
:param n: number of selected individuals
:return: next generation population
"""
fitness = cal_fit(population[1])
index = np.nonzero(fitness < 1)[0]
if len(index) < n:
rank = np.argsort(fitness)
index = rank[: n]
elif len(index) > n:
del_no = trunc(population[1][index, :], len(index) - n)
index = np.setdiff1d(index, index[del_no])
population = [population[0][index, :], population[1][index, :]]
return population, index
def trunc(pop_obj, k):
n, m = np.shape(pop_obj)
distance = cdist(pop_obj, pop_obj)
distance[np.eye(n) > 0] = np.inf
del_no = np.ones(n) < 0
while np.sum(del_no) < k:
remain = np.nonzero(np.logical_not(del_no))[0]
temp = np.sort(distance[remain, :][:, remain], axis=1)
rank = np.argsort(temp[:, 0])
del_no[remain[rank[0]]] = True
return del_no
def cal_fit(pop_obj):
n, m = np.shape(pop_obj)
dominance = np.ones((n, n)) < 0
for i in range(0, n-1):
for j in range(i+1, n):
k = int(np.any(pop_obj[i, :] < pop_obj[j, :])) - int(np.any(pop_obj[i, :] > pop_obj[j, :]))
if k == 1:
dominance[i, j] = True
elif k == -1:
dominance[j, i] = True
s = np.sum(dominance, axis=1, keepdims=True)
r = np.zeros(n)
for i in range(n):
r[i] = np.sum(s[dominance[:, i]])
distance = cdist(pop_obj, pop_obj)
distance[np.eye(n) > 0] = np.inf
distance = np.sort(dominance, axis=1)
d = 1 / (distance[:, int(np.sqrt(n))] + 2)
fitness = r + d
return fitness