Skip to content

Commit

Permalink
Fix imports.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jun Hu@lab committed May 7, 2020
1 parent 3fe76c5 commit 673b073
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 47 deletions.
15 changes: 4 additions & 11 deletions extracted_ackley_GA.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from util_plot import AddPlot

fig = plt.figure(figsize=(6, 8))
ax1 = fig.add_subplot(211, projection='3d')
ax2 = fig.add_subplot(212)
ax2.set_title("Learning curve")
ax2.set_xlim(0, 30)
ax2.set_ylim(0, 1)
p = AddPlot(is_3d=True, with_lc=True)
ax1, _ = p.returns

x_min = -4
x_max = 4
Expand Down Expand Up @@ -82,8 +78,6 @@ def plot_ackley():
max_idx = np.argmax(fitness_arr)
max_chromosome = population[max_idx]

old_fitness = best_fitness

if fitness_arr[max_idx] > best_fitness:
best_fitness = fitness_arr[max_idx]
best_goal = np.copy(max_chromosome)
Expand All @@ -93,8 +87,7 @@ def plot_ackley():
ax1.scatter(*population.T, s=50, alpha=0.5)
ax1.scatter(*test_goal, s=200, marker="*", alpha=1.0)
ax1.scatter(*best_goal, s=200, marker="+", alpha=1.0)
if iteration > 0:
ax2.plot((iteration - 1, iteration), (old_fitness, best_fitness), color='C0')
p.plot_curve(iteration, best_fitness)
# We assume converged when arrive early_stop_fitness.
if best_fitness > early_stop_fitness:
ax1.set_title("Stop at iteration %s, best_fitness: %.4f" % (iteration, best_fitness))
Expand Down
4 changes: 2 additions & 2 deletions genetic_algorithm.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import numpy as np
import matplotlib.pyplot as plt
from util_3d import add_plot
from util_plot import AddPlot

is_3d = True
ax, gene_num = add_plot(is_3d)
ax, gene_num = AddPlot(is_3d).returns

chromosome_num = 10
population_shape = (chromosome_num, gene_num)
Expand Down
4 changes: 2 additions & 2 deletions iris_knn.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets
from util_3d import add_plot
from util_plot import AddPlot

is_3d = True
ax, point_dim = add_plot(is_3d)
ax, point_dim = AddPlot(is_3d).returns

# import some data to play with
iris = datasets.load_iris()
Expand Down
4 changes: 2 additions & 2 deletions k_means.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import numpy as np
import matplotlib.pyplot as plt
from util_3d import add_plot
from util_plot import AddPlot

is_3d = True
ax, point_dim = add_plot(is_3d)
ax, point_dim = AddPlot(is_3d).returns

n_cluster_points = 100
cluster_shape = (n_cluster_points, point_dim)
Expand Down
4 changes: 2 additions & 2 deletions k_nearest_neighbor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import matplotlib.pyplot as plt
import numpy as np

from util_3d import add_plot
from util_plot import AddPlot

is_3d = True
ax, point_dim = add_plot(is_3d)
ax, point_dim = AddPlot(is_3d).returns

# Number of points of cluster
n_cluster_points = 20
Expand Down
4 changes: 2 additions & 2 deletions mean_shift.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import numpy as np
import matplotlib.pyplot as plt
from util_3d import add_plot
from util_plot import AddPlot

is_3d = True
ax, point_dim = add_plot(is_3d)
ax, point_dim = AddPlot(is_3d).returns

n_cluster_points = 100
cluster_shape = (n_cluster_points, point_dim)
Expand Down
9 changes: 4 additions & 5 deletions midterm_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ def plot_rastrigin():
iteration = 0
max_iteration = 30
while True:
ax1.clear()

plot_rastrigin()

Expand All @@ -67,7 +66,7 @@ def plot_rastrigin():
fitness_arr = population[:, 2]

max_idx = np.argmax(fitness_arr)
old_fitness = best_fitness
prev_fitness = best_fitness

if fitness_arr[max_idx] > best_fitness:
best_fitness = fitness_arr[max_idx]
Expand All @@ -77,7 +76,7 @@ def plot_rastrigin():
ax1.scatter(*best_goal, s=200, c='blue', marker='*')

if iteration > 0:
ax2.plot((iteration - 1, iteration), (old_fitness, best_fitness), c='C0')
ax2.plot((iteration - 1, iteration), (prev_fitness, best_fitness), c='C0')

if best_fitness < early_stop_fitness:
ax1.set_title("iteration %d, fitness %.4f" % (iteration, best_fitness))
Expand All @@ -90,13 +89,14 @@ def plot_rastrigin():

plt.draw()
plt.pause(0.5)
ax1.clear()

# Genetic Algorithm

# Selection
sorted_idx = np.argsort(fitness_arr)
seled_chromo = population[sorted_idx][-sel_num:]
copy_idx = np.random.choice(seled_chromo.shape[0], copy_num)
copy_idx = np.random.choice(sel_num, copy_num)
copy_chromo = seled_chromo[copy_idx]
population = np.concatenate((seled_chromo, copy_chromo))

Expand All @@ -122,5 +122,4 @@ def plot_rastrigin():
iteration += 1

# Plot forever after finished
plt.draw()
plt.show()
5 changes: 2 additions & 3 deletions path_planning.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import matplotlib.pyplot as plt
import numpy as np

from util_3d import add_plot
from util_plot import AddPlot

is_3d = True
ax, data_dim = add_plot(is_3d)
ax, data_dim = AddPlot(is_3d).returns

# Make goal as gene
gene_num = 5
Expand All @@ -28,7 +28,6 @@

iteration = 0
iter_num = 100
plt.pause(3)
while True:
# Update all paths
paths[:, 1:] = points[1:][population]
Expand Down
4 changes: 2 additions & 2 deletions random_search.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import numpy as np
import matplotlib.pyplot as plt
from util_3d import add_plot
from util_plot import AddPlot

is_3d = True
ax, particle_dim = add_plot(is_3d)
ax, particle_dim = AddPlot(is_3d).returns

n_particles = 10
particles_shape = (n_particles, particle_dim)
Expand Down
16 changes: 0 additions & 16 deletions util_3d.py

This file was deleted.

37 changes: 37 additions & 0 deletions util_plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D


class AddPlot:
def __init__(self, is_3d, pos=111, with_lc=False, is_record=False):
if with_lc:
pos = 211
self.fig = plt.figure(figsize=(6, 8))
self._add_learning_curve()
else:
self.fig = plt.figure()
if is_record:
mngr = plt.get_current_fig_manager()
# to put it into the upper left corner for example:
mngr.window.wm_geometry("+350+100")
if is_3d:
point_dim = 3
ax = self.fig.add_subplot(pos, projection='3d')
else:
point_dim = 2
ax = self.fig.add_subplot(pos)
self.returns = ax, point_dim

def _add_learning_curve(self):
self.ax2 = self.fig.add_subplot(212)
self.ax2.set_title("Learning Curve")
self.ax2.set_xlabel("Iteration")
self.ax2.set_ylabel("Fitness Value")
self.ax2.grid()
self.prev_fitness = None

def plot_curve(self, iteration, best_fitness):
if self.prev_fitness is not None:
self.ax2.plot((iteration - 1, iteration), (self.prev_fitness, best_fitness), c='C0')
self.prev_fitness = best_fitness

0 comments on commit 673b073

Please sign in to comment.