Skip to content

Commit

Permalink
Import error fix
Browse files Browse the repository at this point in the history
  • Loading branch information
blankjul committed Sep 12, 2018
1 parent 56023d0 commit 3575ae0
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 14 deletions.
5 changes: 3 additions & 2 deletions pymoo/algorithms/rnsga3.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from pymoo.algorithms.nsga3 import NSGA3
from pymoo.operators.survival.aspiration_point_survival import AspirationPointSurvival
from pymoo.util.reference_directions import get_ref_dirs_from_n
from pymoo.util.reference_direction import UniformReferenceDirectionFactory


class RNSGA3(NSGA3):
Expand All @@ -19,7 +19,8 @@ def __init__(self,
# add the aspiration point lines
aspiration_ref_dirs = []
for i in range(n_ref_points):
aspiration_ref_dirs.extend(get_ref_dirs_from_n(n_obj, pop_per_ref_point))
ref_dirs = UniformReferenceDirectionFactory(n_dim=n_obj, n_points=pop_per_ref_point).do()
aspiration_ref_dirs.extend(ref_dirs)
aspiration_ref_dirs = np.array(aspiration_ref_dirs)

kwargs['ref_dirs'] = aspiration_ref_dirs
Expand Down
13 changes: 9 additions & 4 deletions pymoo/experimental/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pymoo.algorithms.nsga3 import NSGA3
from pymoo.model.termination import MaximumGenerationTermination
from pymoo.util.plotting import plot, animate, plot_3d
from pymoo.util.reference_directions import get_ref_dirs_from_section
from pymoo.util.reference_direction import UniformReferenceDirectionFactory
from pymop.problem import ScaledProblem, ConvexProblem
from pymop.problems.dtlz import DTLZ2, DTLZ1

Expand All @@ -15,15 +15,18 @@
def run():
start_time = time.time()

problem = ScaledProblem(DTLZ1(n_var=12, n_obj=3), 10)
problem = ScaledProblem(DTLZ2(n_var=12, n_obj=3), 10)

#plot_3d(problem.pareto_front())
#plt.xlabel("X")
#plt.ylabel("Y")
#plt.zlabel("Z")
#plt.show()

algorithm = NSGA3(ref_dirs=get_ref_dirs_from_section(3, 12))
algorithm = NSGA3(ref_dirs=UniformReferenceDirectionFactory(n_dim=3, n_partitions=12).do())

res = algorithm.solve(problem,
termination=MaximumGenerationTermination(600),
termination=MaximumGenerationTermination(250),
seed=2,
save_history=True,
disp=True)
Expand All @@ -32,6 +35,8 @@ def run():

print("--- %s seconds ---" % (time.time() - start_time))

F = (F - problem.ideal_point()) / (problem.nadir_point() - problem.ideal_point())

scatter_plot = True
save_animation = False

Expand Down
76 changes: 74 additions & 2 deletions pymoo/operators/survival/aspiration_point_survival.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
get_extreme_points, get_intercepts
from pymoo.util.mathematics import Mathematics
from pymoo.util.non_dominated_sorting import NonDominatedSorting
from pymoo.util.reference_directions import get_ref_dirs_from_points


class AspirationPointSurvival(Survival):
Expand Down Expand Up @@ -126,4 +125,77 @@ def _do(self, pop, n_survive, D=None, **kwargs):
D['dist_to_niche'] = dist_to_niche

# now truncate the population
pop.filter(survivors)
pop.filter(survivors)


def get_ref_dirs_from_points(ref_point, ref_dirs, mu=0.1):
"""
This function takes user specified reference points, and creates smaller sets of equidistant
Das-Dennis points around the projection of user points on the Das-Dennis hyperplane
:param ref_point: List of user specified reference points
:param n_obj: Number of objectives to consider
:param mu: Shrinkage factor (0-1), Smaller = tigher convergence, Larger= larger convergence
:return: Set of reference points
"""

n_obj = ref_point.shape[1]

val = []
n_vector = np.ones(n_obj) / np.sqrt(n_obj) # Normal vector of Das Dennis plane
point_on_plane = np.eye(n_obj)[0] # Point on Das-Dennis

for point in ref_point:

ref_dir_for_aspiration_point = np.copy(ref_dirs) # Copy of computed reference directions
ref_dir_for_aspiration_point = mu * ref_dir_for_aspiration_point

cent = np.mean(ref_dir_for_aspiration_point, axis=0) # Find centroid of shrunken reference points

# Project shrunken Das-Dennis points back onto original Das-Dennis hyperplane
intercept = line_plane_intersection(np.zeros(n_obj), point, point_on_plane, n_vector)
shift = intercept - cent # shift vector

ref_dir_for_aspiration_point += shift

# If reference directions are located outside of first octant, redefine points onto the border
if not (ref_dir_for_aspiration_point > 0).min():
ref_dir_for_aspiration_point[ref_dir_for_aspiration_point < 0] = 0
ref_dir_for_aspiration_point = ref_dir_for_aspiration_point / np.sum(ref_dir_for_aspiration_point, axis=1)[
:, None]
val.extend(ref_dir_for_aspiration_point)

val.extend(np.eye(n_obj)) # Add extreme points
return np.array(val)


# intersection function

def line_plane_intersection(l0, l1, p0, p_no, epsilon=1e-6):
"""
l0, l1: define the line
p0, p_no: define the plane:
p0 is a point on the plane (plane coordinate).
p_no is a normal vector defining the plane direction;
(does not need to be normalized).
reference: https://en.wikipedia.org/wiki/Line%E2%80%93plane_intersection
return a Vector or None (when the intersection can't be found).
"""

l = l1 - l0
dot = np.dot(l, p_no)

if abs(dot) > epsilon:
# the factor of the point between p0 -> p1 (0 - 1)
# if 'fac' is between (0 - 1) the point intersects with the segment.
# otherwise:
# < 0.0: behind p0.
# > 1.0: infront of p1.
w = p0 - l0
d = np.dot(w, p_no) / dot
l = l * d
return l0 + l
else:
# The segment is parallel to plane then return the perpendicular projection
ref_proj = l1 - (np.dot(l1 - p0, p_no) * p_no)
return ref_proj
2 changes: 0 additions & 2 deletions pymoo/util/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def plot_3d(F):
from mpl_toolkits.mplot3d import Axes3D
ax = fig.add_subplot(111, projection='3d')
ax.scatter(F[:, 0], F[:, 1], F[:, 2])
plt.show()

def plot_2d(F, problem=None):
plt.scatter(F[:, 0], F[:, 1], label="F")
Expand All @@ -36,7 +35,6 @@ def plot_2d(F, problem=None):
plt.scatter(pf[:, 0], pf[:, 1], label='Pareto Front', s=20, facecolors='none', edgecolors='r')
plt.legend()

plt.show()

def animate(path_to_file, history, problem=None, func_iter=None):

Expand Down
9 changes: 7 additions & 2 deletions pymoo/util/reference_direction.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ def _do(self):

@staticmethod
def get_partition_closest_to_points(n_points, n_dim):

# in this case the do method will always return one values anyway
if n_dim == 1:
return 0

n_partitions = 1
_n_points = 0
_n_points = UniformReferenceDirectionFactory.get_n_points(n_partitions, n_dim)
while _n_points <= n_points:
n_partitions += 1
_n_points = UniformReferenceDirectionFactory.get_n_points(n_partitions, n_dim)
Expand Down Expand Up @@ -92,7 +97,7 @@ def do(self):


if __name__ == '__main__':
ref_dirs = UniformReferenceDirectionFactory(3, n_partitions=12).do()
ref_dirs = UniformReferenceDirectionFactory(2, n_points=100).do()
print(np.sum(ref_dirs, axis=1))

multi_layer = MultiLayerReferenceDirectionFactory()
Expand Down
4 changes: 2 additions & 2 deletions tests/test_no_exception.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest

from pymoo.optimize import minimize
from pymoo.util.reference_directions import get_ref_dirs_from_n
from pymoo.util.reference_direction import UniformReferenceDirectionFactory
from pymop.problems.osy import OSY
from pymop.problems.rastrigin import Rastrigin
from pymop.problems.zdt import ZDT1, ZDT4
Expand All @@ -28,7 +28,7 @@ def test_no_exception(self):
try:
minimize(problem,
method=algorithm['name'],
method_args={**algorithm, 'ref_dirs': get_ref_dirs_from_n(problem.n_obj, 100)},
method_args={**algorithm, 'ref_dirs': UniformReferenceDirectionFactory(n_dim=problem.n_obj, n_points=100).do()},
termination=('n_eval', algorithm['n_eval']),
seed=2,
save_history=True,
Expand Down

0 comments on commit 3575ae0

Please sign in to comment.