forked from Lianghao93/NSGA-II
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crowding_distance.py
28 lines (26 loc) · 1.02 KB
/
crowding_distance.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
#!/usr/bin/env python
# encoding: utf-8
import numpy as np
def crowding_distance(pop_obj, front_no):
"""
The crowding distance of each Pareto front
:param pop_obj: objective vectors
:param front_no: front numbers
:return: crowding distance
"""
n, M = np.shape(pop_obj)
crowd_dis = np.zeros(n)
front = np.unique(front_no)
Fronts = front[front != np.inf]
for f in range(len(Fronts)):
Front = np.array([k for k in range(len(front_no)) if front_no[k] == Fronts[f]])
Fmax = pop_obj[Front, :].max(0)
Fmin = pop_obj[Front, :].min(0)
for i in range(M):
rank = np.argsort(pop_obj[Front, i])
crowd_dis[Front[rank[0]]] = np.inf
crowd_dis[Front[rank[-1]]] = np.inf
for j in range(1, len(Front) - 1):
crowd_dis[Front[rank[j]]] = crowd_dis[Front[rank[j]]] + (pop_obj[(Front[rank[j + 1]], i)] - pop_obj[
(Front[rank[j - 1]], i)]) / (Fmax[i] - Fmin[i])
return crowd_dis