diff --git a/extracted_ackley_GA.py b/extracted_ackley_GA.py index 6664705..19ff95b 100644 --- a/extracted_ackley_GA.py +++ b/extracted_ackley_GA.py @@ -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 @@ -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) @@ -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)) diff --git a/genetic_algorithm.py b/genetic_algorithm.py index b7cfb84..20c4c86 100644 --- a/genetic_algorithm.py +++ b/genetic_algorithm.py @@ -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) diff --git a/iris_knn.py b/iris_knn.py index 9c860e3..1baa013 100644 --- a/iris_knn.py +++ b/iris_knn.py @@ -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() diff --git a/k_means.py b/k_means.py index 3fd3f52..1f189e7 100644 --- a/k_means.py +++ b/k_means.py @@ -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) diff --git a/k_nearest_neighbor.py b/k_nearest_neighbor.py index dc1b64f..436ab3b 100644 --- a/k_nearest_neighbor.py +++ b/k_nearest_neighbor.py @@ -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 diff --git a/mean_shift.py b/mean_shift.py index 3d6f6ac..230eca7 100644 --- a/mean_shift.py +++ b/mean_shift.py @@ -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) diff --git a/midterm_2.py b/midterm_2.py index c028a62..2230893 100644 --- a/midterm_2.py +++ b/midterm_2.py @@ -58,7 +58,6 @@ def plot_rastrigin(): iteration = 0 max_iteration = 30 while True: - ax1.clear() plot_rastrigin() @@ -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] @@ -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)) @@ -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)) @@ -122,5 +122,4 @@ def plot_rastrigin(): iteration += 1 # Plot forever after finished -plt.draw() plt.show() diff --git a/path_planning.py b/path_planning.py index d2ab475..ed69eda 100644 --- a/path_planning.py +++ b/path_planning.py @@ -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 @@ -28,7 +28,6 @@ iteration = 0 iter_num = 100 -plt.pause(3) while True: # Update all paths paths[:, 1:] = points[1:][population] diff --git a/random_search.py b/random_search.py index 0147ea2..1fe6f02 100644 --- a/random_search.py +++ b/random_search.py @@ -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) diff --git a/util_3d.py b/util_3d.py deleted file mode 100644 index a8c23b0..0000000 --- a/util_3d.py +++ /dev/null @@ -1,16 +0,0 @@ -from matplotlib import pyplot as plt -from mpl_toolkits.mplot3d import Axes3D - - -def add_plot(is_3d, pos=111): - fig = plt.figure() - 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 = fig.add_subplot(pos, projection='3d') - else: - point_dim = 2 - ax = fig.add_subplot(pos) - return ax, point_dim diff --git a/util_plot.py b/util_plot.py new file mode 100644 index 0000000..e81c111 --- /dev/null +++ b/util_plot.py @@ -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 +