From 80377b58e799f6ae505c007a9c862d13a2fd4fa6 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Thu, 12 Nov 2020 16:34:07 -0500 Subject: [PATCH] Cleaned up graph code --- src/database/matplotlib_graph.py | 59 +++++++++++++++++++------------- src/run_testing.py | 14 +------- 2 files changed, 36 insertions(+), 37 deletions(-) diff --git a/src/database/matplotlib_graph.py b/src/database/matplotlib_graph.py index 5100858..8724d30 100644 --- a/src/database/matplotlib_graph.py +++ b/src/database/matplotlib_graph.py @@ -12,30 +12,37 @@ class Matplotlib_Graph: def __init__(self, database): self.database = database + # Type self.type_of_plot = plt.plot + # Size self.size = [6,6] + # Labels self.xlabel = None self.ylabel = None self.title = None + # Scale self.yscale = "linear" + # Data points + self.x = None + self.y = None - def plt_setup(self, X, Y, yscale, xlabel, ylabel, title, type_of_plot, size): - """Setup for plt""" + def plot(self): + """Plot all the graph attributes""" - if yscale == "log": + if self.yscale == "log": # If using log then the values have to be positive numbers - Y = [abs(ele) for ele in Y] + self.y = [abs(ele) for ele in self.y] # Setup data - plt.figure(figsize = size) + plt.figure(figsize = self.size) plt.yscale(self.yscale) - type_of_plot(X, Y) + self.type_of_plot(self.x, self.y) # labels - plt.xlabel(xlabel) - plt.ylabel(ylabel) - plt.title(title) + plt.xlabel(self.xlabel) + plt.ylabel(self.ylabel) + plt.title(self.title) # Show the plot plt.show() @@ -48,12 +55,16 @@ class Matplotlib_Graph: generations = self.database.get_total_generations() # Create the generations list - [0,1,2,etc] - X = list(range(0, generations)) + self.x = list(range(0, generations)) # Query for Y data - Y = self.database.get_generation_total_fitness() + self.y = self.database.get_generation_total_fitness() - self.plt_setup(X, Y, self.yscale, 'Generation', 'Generation Total Fitness', 'Relationship Between Generations and Generation Total Fitness', self.type_of_plot, self.size) + self.xlabel = 'Generation' + self.ylabel = 'Generation Total Fitness' + self.title = 'Relationship Between Generations and Generation Total Fitness' + + self.plot() def highest_value_chromosome(self): @@ -63,13 +74,16 @@ class Matplotlib_Graph: generations = self.database.get_total_generations() # Create the generations list - [0,1,2,etc] - X = list(range(0, generations)) + self.x = list(range(0, generations)) # Query for Y data - Y = self.database.get_highest_chromosome() + self.y = self.database.get_highest_chromosome() - self.plt_setup(X, Y, self.yscale, 'Generation', 'Highest Fitness', 'Relationship Between Generations and Highest Fitness', self.type_of_plot, self.size) + self.xlabel = 'Generation' + self.ylabel = 'Highest Fitness' + self.title = 'Relationship Between Generations and Highest Fitness' + self.plot() def lowest_value_chromosome(self): """Generation by Min value Chromosome """ @@ -78,19 +92,16 @@ class Matplotlib_Graph: generations = self.database.get_total_generations() # Create the generations list - [0,1,2,etc] - X = list(range(0, generations)) - - print(X) + self.x = list(range(0, generations)) # Query for Y data - Y = self.database.get_lowest_chromosome() + self.y = self.database.get_lowest_chromosome() - if(self.yscale == "log"): - # If using log then the values have to be positive numbers - Y = [abs(ele) for ele in Y] - - self.plt_setup(X, Y, self.yscale, 'Generation', 'Lowest Fitness', 'Relationship Between Generations and Lowest Fitness', self.type_of_plot, self.size) + self.xlabel = 'Generation' + self.ylabel = 'Lowest Fitness' + self.title = 'Relationship Between Generations and Lowest Fitness' + self.plot() # Getter and setters @property diff --git a/src/run_testing.py b/src/run_testing.py index 9cec020..6a5e6e8 100644 --- a/src/run_testing.py +++ b/src/run_testing.py @@ -9,18 +9,6 @@ ga.population_size = 100 ga.chromosome_length = 10 ga.generation_goal = 150 -# Create random genes from 0 to 10 -ga.gene_impl = lambda: random.randint(0, 10) - -# Minimize the sum of the genes -ga.fitness_function_impl = lambda chromosome: sum(gene.get_value() for gene in chromosome.get_gene_list()) -ga.target_fitness_type = 'min' - ga.evolve() -ga.print_generation() -ga.print_population() - -ga.graph.type_of_plot = "line" -ga.graph.yscale = "log" -ga.graph.lowest_value_chromosome() +ga.graph.highest_value_chromosome()