Skip to content

Commit

Permalink
zdana
Browse files Browse the repository at this point in the history
  • Loading branch information
kkulczak committed Apr 30, 2019
1 parent ae52458 commit 51a51f9
Show file tree
Hide file tree
Showing 4 changed files with 334 additions and 133 deletions.
259 changes: 160 additions & 99 deletions list04/AE2018_ES_Demo.ipynb

Large diffs are not rendered by default.

159 changes: 132 additions & 27 deletions list04/Mutation_Types.ipynb

Large diffs are not rendered by default.

44 changes: 39 additions & 5 deletions list04/mutations.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from matplotlib.patches import Ellipse
from scipy.stats import norm, chi2
import numpy as np
import matplotlib.pyplot as plt

Expand All @@ -12,16 +14,19 @@ def plot_mutation(
domain_Y=np.arange(-5, 5, 0.25),
objective_function=objective_function_F1a,
original_individual=np.array([[1, 1]]),
conv_elipse=False,
):
X, Y = np.meshgrid(domain_X, domain_Y)
Z = - objective_function(np.vstack([X.ravel(),
Y.ravel()]).T).reshape(X.shape[0], X.shape[1])
plt.figure(figsize=(6, 6))
plt.contour(X, Y, Z, 50)
plt.plot(mutations[:, 0], mutations[:, 1], 'ro')
plt.contour(X, Y, Z, 50, alpha=0.5)
plt.plot(mutations[:, 0], mutations[:, 1], 'ro', markersize=2)
plt.plot(original_individual[0, 0],
original_individual[0, 1], 'k*', markersize=24)
# plt.title('Mutation 1')
if conv_elipse:
plot_point_cov(mutations)
plt.show()


Expand Down Expand Up @@ -53,9 +58,10 @@ def benchmark_mutation(
original_individual=np.array([[1, 1]])
):
indv_res = objective_function(original_individual)[0]

x = np.array([objective_function(mutate(sigma)) for i in range(100)])
better_than_indv = (x < indv_res).sum(axis=1)
mutations = np.array([(mutate(sigma)) for i in range(100)])
x = np.array([objective_function(i) for i in mutations])
plot_mutation(mutations[0], objective_function=objective_function, conv_elipse=True)
better_than_indv = (x < indv_res).sum(axis=1)
best_from_pop = x.max(axis=1)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(6, 3))
Expand All @@ -73,3 +79,31 @@ def benchmark_mutation(
)
fig.tight_layout()
plt.show()


def plot_point_cov(points, q=0.95, ax=None, **kwargs):
pos = points.mean(axis=0)
cov = np.cov(points, rowvar=False)
return plot_cov_ellipse(cov, pos, q, ax, **kwargs)


def plot_cov_ellipse(cov, pos, q=0.95, ax=None, **kwargs):
def eigsorted(cov):
vals, vecs = np.linalg.eigh(cov)
order = vals.argsort()[::-1]
return vals[order], vecs[:, order]

if ax is None:
ax = plt.gca()

q = np.asarray(q)
r2 = chi2.ppf(q, 2)
val, vec = np.linalg.eigh(cov)
width, height = 2 * np.sqrt(val[:, None] * r2)
rotation = np.degrees(np.arctan2(*vec[::-1, 0]))

ellip = Ellipse(xy=pos, width=width, height=height,
angle=rotation, color='green', **kwargs)

ax.add_artist(ellip)
return width, height
5 changes: 3 additions & 2 deletions list04/notebook_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ def run_es(
plot=True,
sigma=50.0,
verbose=True,
K=1

):
result = es(
Expand All @@ -172,8 +173,8 @@ def run_es(
number_of_offspring=2*N,
number_of_parents=number_of_parents,
sigma=sigma,
tau=1/np.sqrt(2*d),
tau_0=1/np.sqrt(2*np.sqrt(d)),
tau=K/np.sqrt(2*d),
tau_0=K/np.sqrt(2*np.sqrt(d)),
log_frequency=log_frequency,
verbose=verbose
)
Expand Down

0 comments on commit 51a51f9

Please sign in to comment.