forked from anyoptimization/pymoo
-
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
Julian Blank
committed
Mar 27, 2020
1 parent
088f24c
commit d8f05eb
Showing
15 changed files
with
230 additions
and
31 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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,26 @@ | ||
import autograd.numpy as anp | ||
|
||
from pymoo.model.problem import Problem | ||
|
||
|
||
def curve(problem, n_points=200): | ||
X = anp.linspace(problem.xl[0], problem.xu[0], n_points)[:, None] | ||
F = problem.evaluate(X) | ||
return anp.column_stack([X, F]) | ||
|
||
|
||
class MultiModalSimple1(Problem): | ||
def __init__(self, n_var=1): | ||
super().__init__(n_var=n_var, n_obj=1, n_constr=0, xl=0, xu=1, type_var=anp.double) | ||
|
||
def _evaluate(self, x, out, *args, **kwargs): | ||
out["F"] = 1 - anp.exp(-x ** 2) * anp.sin(2 * anp.pi * x) ** 2 | ||
|
||
|
||
class MultiModalSimple2(Problem): | ||
def __init__(self, n_var=1): | ||
super().__init__(n_var=n_var, n_obj=1, n_constr=0, xl=-1, xu=0, type_var=anp.double) | ||
|
||
def _evaluate(self, x, out, *args, **kwargs): | ||
x = - x | ||
out["F"] = 1.1 - anp.exp(-2 * x) * anp.sin(5 * anp.pi * x) ** 2 |
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
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,48 @@ | ||
import matplotlib.pyplot as plt | ||
|
||
from pymoo.algorithms.nsga2 import NSGA2 | ||
from pymoo.optimize import minimize | ||
from pymoo.problems.multi import ZDT1 | ||
from pymoo.util.nds.non_dominated_sorting import NonDominatedSorting | ||
from pymoo.visualization.pcp import PCP | ||
from pymoo.visualization.scatter import Scatter | ||
from pyrecorder.recorders.gif import GIF | ||
from pyrecorder.video import Video | ||
|
||
problem = ZDT1(n_var=6) | ||
|
||
algorithm = NSGA2() | ||
|
||
ret = minimize(problem, | ||
algorithm, | ||
termination=('n_gen', 61), | ||
seed=1, | ||
save_history=True, | ||
verbose=False) | ||
|
||
print(ret.F) | ||
|
||
with Video(GIF("animation.gif")) as vid: | ||
for algorithm in ret.history: | ||
|
||
if algorithm.n_gen % 5 == 0: | ||
X, F = algorithm.pop.get("X", "F") | ||
nds = NonDominatedSorting().do(F, only_non_dominated_front=True) | ||
other = [k for k in range(len(F)) if k not in nds] | ||
|
||
fig, (ax1, ax2) = plt.subplots(2, figsize=(8, 6)) | ||
fig.suptitle("%s - %s - Gen %s" % ("ZDT1", "NSGA2", algorithm.n_gen), fontsize=16) | ||
|
||
pcp = PCP(ax=ax1, bounds=(problem.xl, problem.xu)) | ||
pcp.set_axis_style(color="black", alpha=0.7) | ||
pcp.add(X[other], color="blue", linewidth=0.5) | ||
pcp.add(X[nds], color="red", linewidth=2) | ||
pcp.do() | ||
|
||
sc = Scatter(ax=ax2) | ||
sc.add(F[other], color="blue") | ||
sc.add(F[nds], color="red") | ||
sc.add(problem.pareto_front(), plot_type="line", color="black") | ||
sc.do() | ||
|
||
vid.record() |
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,58 @@ | ||
import numpy as np | ||
|
||
|
||
def func_select_by_objective(pop): | ||
F = pop.get("F") | ||
return F[:, 0].argmin() | ||
|
||
|
||
def select_by_clearing(pop, D, n_select, func_select, eps=0.05): | ||
clearing = EpsilonClearing(D, eps) | ||
|
||
while len(clearing.selected()) < n_select: | ||
remaining = clearing.remaining() | ||
|
||
if len(remaining) == 0: | ||
clearing.reset() | ||
|
||
best = remaining[func_select(pop[remaining])] | ||
clearing.select(best) | ||
|
||
S = clearing.selected() | ||
return S | ||
|
||
|
||
class EpsilonClearing: | ||
|
||
def __init__(self, D, | ||
epsilon) -> None: | ||
super().__init__() | ||
self.D = D | ||
self.n = len(D) | ||
self.epsilon = epsilon | ||
|
||
self.S = [] | ||
self.C = np.full(self.n, False) | ||
|
||
def remaining(self): | ||
return np.where(~self.C)[0] | ||
|
||
def has_remaining(self): | ||
return self.C.sum() != self.n | ||
|
||
def cleared(self): | ||
return self.C | ||
|
||
def selected(self): | ||
return self.S | ||
|
||
def reset(self): | ||
self.C = np.full(self.n, False) | ||
self.C[self.S] = True | ||
|
||
def select(self, k): | ||
self.S.append(k) | ||
self.C[k] = True | ||
|
||
less_than_epsilon = self.D[k] < self.epsilon | ||
self.C[less_than_epsilon] = True |
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,27 @@ | ||
import numpy as np | ||
|
||
|
||
class RouletteWheelSelection: | ||
|
||
def __init__(self, val, larger_is_better=True): | ||
super().__init__() | ||
if not larger_is_better: | ||
val = val.max() - val | ||
_sum = val.sum() | ||
self.cumulative = np.array([val[:k].sum() / _sum for k in range(1, len(val))]) | ||
|
||
def next(self, n=None): | ||
if n is None: | ||
X = np.random.random((1, 1)) | ||
else: | ||
X = np.random.random((n, 1)) | ||
if n > 1: | ||
X.repeat(n - 1, axis=1) | ||
|
||
M = self.cumulative[None, :].repeat(len(X), axis=0) | ||
B = X >= M | ||
ret = B.sum(axis=1) | ||
|
||
if n is None: | ||
return ret[0] | ||
return ret |
Oops, something went wrong.