-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
372 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ build/ | |
dist/ | ||
doc/_build/ | ||
.vscode/ | ||
.pytest_cache/ | ||
__pycache__/ | ||
env/ | ||
*.egg-info/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
# NSGA-3 is kindly provided by Luis Marti (IC/UFF) http://lmarti.com | ||
|
||
from __future__ import division # making it work with Python 2.x | ||
import copy | ||
import random | ||
|
||
import numpy | ||
from deap import tools | ||
|
||
|
||
class ReferencePoint(list): | ||
"""A reference point exists in objective space an has a set of individuals | ||
associated to it.""" | ||
def __init__(self, *args): | ||
list.__init__(self, *args) | ||
self.associations_count = 0 | ||
self.associations = [] | ||
|
||
|
||
def generate_reference_points(num_objs, num_divisions_per_obj=4): | ||
"""Generates reference points for NSGA-III selection. This code is based on | ||
`jMetal NSGA-III implementation <https://github.com/jMetal/jMetal>`_. | ||
""" | ||
def gen_refs_recursive(work_point, num_objs, left, total, depth): | ||
if depth == num_objs - 1: | ||
work_point[depth] = left/total | ||
ref = ReferencePoint(copy.deepcopy(work_point)) | ||
return [ref] | ||
else: | ||
res = [] | ||
for i in range(left): | ||
work_point[depth] = i/total | ||
res = res + gen_refs_recursive(work_point, num_objs, left-i, total, depth+1) | ||
return res | ||
return gen_refs_recursive([0]*num_objs, num_objs, num_objs*num_divisions_per_obj, | ||
num_objs*num_divisions_per_obj, 0) | ||
|
||
def find_ideal_point(individuals): | ||
"""Finds the ideal point from a set individuals.""" | ||
current_ideal = [numpy.infty] * len(individuals[0].fitness.values) | ||
for ind in individuals: | ||
# Use wvalues to accomodate for maximization and minimization problems. | ||
current_ideal = numpy.minimum(current_ideal, | ||
numpy.multiply(ind.fitness.wvalues, -1)) | ||
return current_ideal | ||
|
||
def find_extreme_points(individuals): | ||
"""Finds the individuals with extreme values for each objective function.""" | ||
return [sorted(individuals, key=lambda ind:ind.fitness.wvalues[o] * -1)[-1] | ||
for o in range(len(individuals[0].fitness.values))] | ||
|
||
def construct_hyperplane(individuals, extreme_points): | ||
"""Calculates the axis intersects for a set of individuals and its extremes.""" | ||
def has_duplicate_individuals(individuals): | ||
for i in range(len(individuals)): | ||
for j in range(i+1, len(individuals)): | ||
if individuals[i].fitness.values == individuals[j].fitness.values: | ||
return True | ||
return False | ||
|
||
num_objs = len(individuals[0].fitness.values) | ||
|
||
if has_duplicate_individuals(extreme_points): | ||
intercepts = [extreme_points[m].fitness.values[m] for m in range(num_objs)] | ||
else: | ||
b = numpy.ones(num_objs) | ||
A = [point.fitness.values for point in extreme_points] | ||
x = numpy.linalg.solve(A,b) | ||
intercepts = 1/x | ||
return intercepts | ||
|
||
def normalize_objective(individual, m, intercepts, ideal_point, epsilon=1e-20): | ||
"""Normalizes an objective.""" | ||
# Numeric trick present in JMetal implementation. | ||
if numpy.abs(intercepts[m]-ideal_point[m] > epsilon): | ||
return individual.fitness.values[m] / (intercepts[m]-ideal_point[m]) | ||
else: | ||
return individual.fitness.values[m] / epsilon | ||
|
||
def normalize_objectives(individuals, intercepts, ideal_point): | ||
"""Normalizes individuals using the hyperplane defined by the intercepts as | ||
reference. Corresponds to Algorithm 2 of Deb & Jain (2014).""" | ||
num_objs = len(individuals[0].fitness.values) | ||
|
||
for ind in individuals: | ||
ind.fitness.normalized_values = list([normalize_objective(ind, m, | ||
intercepts, ideal_point) | ||
for m in range(num_objs)]) | ||
return individuals | ||
|
||
def perpendicular_distance(direction, point): | ||
k = numpy.dot(direction, point) / numpy.sum(numpy.power(direction, 2)) | ||
d = numpy.sum(numpy.power(numpy.subtract(numpy.multiply(direction, [k] * len(direction)), point) , 2)) | ||
return numpy.sqrt(d) | ||
|
||
def associate(individuals, reference_points): | ||
"""Associates individuals to reference points and calculates niche number. | ||
Corresponds to Algorithm 3 of Deb & Jain (2014).""" | ||
pareto_fronts = tools.sortLogNondominated(individuals, len(individuals)) | ||
num_objs = len(individuals[0].fitness.values) | ||
|
||
for ind in individuals: | ||
rp_dists = [(rp, perpendicular_distance(ind.fitness.normalized_values, rp)) | ||
for rp in reference_points] | ||
best_rp, best_dist = sorted(rp_dists, key=lambda rpd:rpd[1])[0] | ||
ind.reference_point = best_rp | ||
ind.ref_point_distance = best_dist | ||
best_rp.associations_count +=1 # update de niche number | ||
best_rp.associations += [ind] | ||
|
||
def selNiching(individuals, k): | ||
"""Secondary niched selection based on reference points. Corresponds to | ||
steps 13-17 of Algorithm 1 and to Algorithm 4.""" | ||
if len(individuals) == k: | ||
return individuals | ||
|
||
#individuals = copy.deepcopy(individuals) | ||
|
||
ideal_point = find_ideal_point(individuals) | ||
extremes = find_extreme_points(individuals) | ||
intercepts = construct_hyperplane(individuals, extremes) | ||
normalize_objectives(individuals, intercepts, ideal_point) | ||
|
||
reference_points = generate_reference_points(len(individuals[0].fitness.values)) | ||
|
||
associate(individuals, reference_points) | ||
|
||
res = [] | ||
while len(res) < k: | ||
min_assoc_rp = min(reference_points, key=lambda rp: rp.associations_count) | ||
min_assoc_rps = [rp for rp in reference_points if rp.associations_count == min_assoc_rp.associations_count] | ||
chosen_rp = min_assoc_rps[random.randint(0, len(min_assoc_rps)-1)] | ||
|
||
#print('Rps',min_assoc_rp.associations_count, chosen_rp.associations_count, len(min_assoc_rps)) | ||
|
||
associated_inds = chosen_rp.associations | ||
|
||
if chosen_rp.associations: | ||
if chosen_rp.associations_count == 0: | ||
sel = min(chosen_rp.associations, key=lambda ind: ind.ref_point_distance) | ||
else: | ||
sel = chosen_rp.associations[random.randint(0, len(chosen_rp.associations)-1)] | ||
res += [sel] | ||
chosen_rp.associations.remove(sel) | ||
chosen_rp.associations_count += 1 | ||
individuals.remove(sel) | ||
else: | ||
reference_points.remove(chosen_rp) | ||
return res | ||
|
||
|
||
__all__ = ["selNiching"] |
Oops, something went wrong.