Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions pygad/utils/nsga.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
this module so neither algorithm has to depend on the other.
"""

import operator

import numpy


Expand Down Expand Up @@ -35,23 +37,20 @@ def get_non_dominated_set(self, curr_solutions):
``curr_solutions`` dominates. These form the current Pareto
front.
"""
# The fitness vectors are tiny, so plain Python comparisons are
# cheaper than numpy calls on every pair of solutions.
rows = [numpy.asarray(sol[1]).tolist() for sol in curr_solutions]
dominated_set = []
non_dominated_set = []
for idx1, sol1 in enumerate(curr_solutions):
is_not_dominated = True
for idx2, sol2 in enumerate(curr_solutions):
if idx1 == idx2:
continue
two_solutions = numpy.array(list(zip(sol1[1], sol2[1])))
for sol, row in zip(curr_solutions, rows):
for other in rows:
# PyGAD maximizes, so domination uses >= and >.
greater_or_equal = two_solutions[:, 1] >= two_solutions[:, 0]
strictly_greater = two_solutions[:, 1] > two_solutions[:, 0]
if greater_or_equal.all() and strictly_greater.any():
is_not_dominated = False
dominated_set.append(sol1)
if (all(map(operator.ge, other, row))
and any(map(operator.gt, other, row))):
dominated_set.append(sol)
break
if is_not_dominated:
non_dominated_set.append(sol1)
else:
non_dominated_set.append(sol)
return dominated_set, non_dominated_set

def non_dominated_sorting(self, fitness):
Expand Down
Loading