From e22ff559722b66e04049b210b3e102b8aef955a5 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Tue, 10 Nov 2020 18:12:37 -0500 Subject: [PATCH] Worked on graphing class --- src/database/matplotlib_graph.py | 108 ++++++++++++++++++++++--------- src/structure/population.py | 9 ++- 2 files changed, 84 insertions(+), 33 deletions(-) diff --git a/src/database/matplotlib_graph.py b/src/database/matplotlib_graph.py index e446dc5..6b9824e 100644 --- a/src/database/matplotlib_graph.py +++ b/src/database/matplotlib_graph.py @@ -7,20 +7,12 @@ class Matplotlib_Graph: def __init__(self, database): self.database = database - - - def make_plot(self, type_of_plot, size, X, Y): - """Create the plot""" - - # Set the plot size - plt.figure(figsize = size) - - if(type_of_plot == "line"): - plt.plot(X, Y) - elif(type_of_plot == "scatter"): - plt.scatter(X, Y) - elif(type_of_plot == "bar"): - plt.bar(X, Y) + self.type_of_plot = plt.plot + self.size = [6,6] + self.xlabel = None + self.ylabel = None + self.title = None + self.yscale = "linear" def generation_total_fitness(self, type_of_plot = "line", size = [6,6]): @@ -33,14 +25,26 @@ class Matplotlib_Graph: X = list(range(0, generations)) # Query for Y data - Y = self.database.get_generation_total_fitness() + Y_list = self.database.get_generation_total_fitness() - self.make_plot(type_of_plot, size, X, Y) + # Set the y scale + plt.yscale(self.yscale) + + if(self.yscale == "log"): + # If using log then the values have to be positive numbers + Y = [abs(ele) for ele in Y_list] + + # Setup data + plt.figure(figsize = self.size) + self.type_of_plot(X,Y_list) # x and y labels - plt.xlabel('Generation') - plt.ylabel('Generation Total Fitness') - plt.title('Relationship Between Generations and Generation Total Fitness') + if(self.xlabel == None): + plt.xlabel('Generation') + if(self.ylabel == None): + plt.ylabel('Generation Total Fitness') + if(self.title == None): + plt.title('Relationship Between Generations and Generation Total Fitness') # Show the plot plt.show() @@ -56,15 +60,26 @@ class Matplotlib_Graph: X = list(range(0, generations)) # Query for Y data - Y = self.database.get_highest_chromosome() + Y_list = self.database.get_highest_chromosome() - self.make_plot(type_of_plot, size, X, Y) + # Set the y scale + plt.yscale(self.yscale) + + if(self.yscale == "log"): + # If using log then the values have to be positive numbers + Y = [abs(ele) for ele in Y_list] + + # Setup data + plt.figure(figsize = self.size) + self.type_of_plot(X,Y_list) # x and y labels - plt.xlabel('Generation') - plt.ylabel('Generation Highest Fitness Chromosome') - plt.title('Relationship Between Generations and Highest Value Chromosome') - + if(self.xlabel == None): + plt.xlabel('Generation') + if(self.ylabel == None): + plt.ylabel('Generation Total Fitness') + if(self.title == None): + plt.title('Relationship Between Generations and Generation Total Fitness') # Show the plot plt.show() @@ -79,14 +94,45 @@ class Matplotlib_Graph: X = list(range(0, generations)) # Query for Y data - Y = self.database.get_lowest_chromosome() + Y_list = self.database.get_lowest_chromosome() - self.make_plot(type_of_plot, size, X, Y) + # Set the y scale + plt.yscale(self.yscale) + + if(self.yscale == "log"): + # If using log then the values have to be positive numbers + Y = [abs(ele) for ele in Y_list] + + # Setup data + plt.figure(figsize = self.size) + self.type_of_plot(X,Y_list) # x and y labels - plt.xlabel('Generation') - plt.ylabel('Generation Highest Fitness Chromosome') - plt.title('Relationship Between Generations and Lowest Value Chromosome') - + if(self.xlabel == None): + plt.xlabel('Generation') + if(self.ylabel == None): + plt.ylabel('Generation Total Fitness') + if(self.title == None): + plt.title('Relationship Between Generations and Generation Total Fitness') # Show the plot plt.show() + + # Getter and setters + @property + def type_of_plot(self): + return self._type_of_plot + + + @type_of_plot.setter + def type_of_plot(self, value_input): + + # Defults type of ploting functions + if(value_input == "line"): + self._type_of_plot = plt.plot + elif(value_input == "scatter"): + self._type_of_plot = plt.scatter + elif(value_input == "bar"): + self._type_of_plot = plt.bar + else: + # If its none of the defaults then use what the user provided. + self._type_of_plot = value_input diff --git a/src/structure/population.py b/src/structure/population.py index cd7e5be..b532bef 100644 --- a/src/structure/population.py +++ b/src/structure/population.py @@ -1,7 +1,8 @@ class Population: def __init__(self, chromosome_list = None): - """Intiialize the population with fitness of value None, and a set of chromosomes dependant on user-passed parameter""" + """Intiialize the population with fitness of value None, and a + set of chromosomes dependant on user-passed parameter""" if chromosome_list is None: self.chromosome_list = [] @@ -14,6 +15,8 @@ class Population: def update(self): + """Sets all the population variables to what they should be at + the end of the generation """ self.set_chromosome_list(self.next_population) self.reset_mating_pool() self.reset_next_population() @@ -70,7 +73,9 @@ class Population: def add_chromosome(self, chromosome, index = None): - """Adds a chromosome to the population at the input index, defaulted to the end of the chromosome set""" + """Adds a chromosome to the population at the input index, defaulted + to the end of the chromosome set""" + if index is None: index = self.size() self.chromosome_list.insert(index, chromosome)