-
Notifications
You must be signed in to change notification settings - Fork 20
/
multi_utils.py
48 lines (36 loc) · 1.58 KB
/
multi_utils.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
import operator
import numpy as np
HYP_REF = (11, 11) # reference point for hypervolume (do not change - results will not be comparable)
def hypervolume(pop, ref=HYP_REF):
non_dom = [pop[i] for i in get_first_nondominated(pop)]
f1_sorted = list(sorted(non_dom, key=lambda x: x.fitness[0]))
volume = 0
for (i, j) in zip(f1_sorted, f1_sorted[1:]):
volume += (ref[1] - i.fitness[1])*(j.fitness[0] - i.fitness[0])
volume += (ref[1] - f1_sorted[-1].fitness[1])*(ref[0] - f1_sorted[-1].fitness[0])
return volume
def assign_crowding_distances(front):
front = list(sorted(front, key=operator.attrgetter('fitness')))
front[0].ssc = np.inf # first and last one have infinite crowding distance
front[-1].ssc = np.inf
for i in range(1, len(front) - 1):
front[i].ssc = (front[i + 1].fitness[0] - front[i - 1].fitness[0] +
front[i - 1].fitness[1] - front[i + 1].fitness[1])
# returns true if i1 dominates i2
def dominates(fits1, fits2):
return (all(map(lambda x: x[0] <= x[1], zip(fits1, fits2))) and
any(map(lambda x: x[0] < x[1], zip(fits1, fits2))))
def get_first_nondominated(pop):
non_dom = []
for i, p in enumerate(pop):
if not any(map(lambda x: dominates(x.fitness, pop[i].fitness), pop)):
non_dom.append(i)
return non_dom
def divide_fronts(pop):
fronts = []
while pop:
non_dom = get_first_nondominated(pop)
front = [pop[i] for i in non_dom]
pop = [p for i,p in enumerate(pop) if i not in non_dom]
fronts.append(front)
return fronts