diff --git a/EasyGA/database/matplotlib_graph.py b/EasyGA/database/matplotlib_graph.py index 9cca0b7..04ff286 100644 --- a/EasyGA/database/matplotlib_graph.py +++ b/EasyGA/database/matplotlib_graph.py @@ -18,21 +18,44 @@ class Matplotlib_Graph: self.x = None self.y = None self.yscale = "linear" + self.legend = False + + def all_config_id(self,function): + """Graph each config_id's data stored in the database + using multiple different colored lines.""" + # Get all the config's + config_ids = self.database.get_all_config_id() + + # Turn on the legend + self.legend = True + + # Get the x and y data for each config_id + for config_id in config_ids: + + # Get x and y data + x = self.database.get_each_generation_number(config_id) + y = function(config_id) + # Graph the line but dont show + self.type_of_graph(x, y, label=f"Config_id - {config_id}") + def generation_total_fitness(self, config_id = None): """Show a plot of generation by generation total fitness.""" - # Query the X data - generations = self.database.get_total_generations(config_id) + if config_id == "all": + # If the user want to plot all the config_id's + self.all_config_id(self.database.get_generation_total_fitness) + else: + # Query the X data + generations = self.database.get_total_generations(config_id) + # Create the generations list - [0,1,2,etc] + self.x = list(range(generations)) + # Query for Y data + self.y = self.database.get_generation_total_fitness(config_id) + self.type_of_graph(self.x, self.y) - # Create the generations list - [0,1,2,etc] - self.x = list(range(generations)) - # Query for Y data - self.y = self.database.get_generation_total_fitness(config_id) - - self.type_of_graph(self.x, self.y) plt.yscale(self.yscale) plt.xlabel('Generation') plt.ylabel('Generation Total Fitness') @@ -42,16 +65,18 @@ class Matplotlib_Graph: def highest_value_chromosome(self,config_id = None): """Generation by Max value chromosome """ - # Query the X data - generations = self.database.get_total_generations(config_id) + if config_id == "all": + # If the user want to plot all the config_id's + self.all_config_id(self.database.get_highest_chromosome) + else: + # Query the X data + generations = self.database.get_total_generations(config_id) + # Create the generations list - [0,1,2,etc] + self.x = list(range(generations)) + # Query for Y data + self.y = self.database.get_highest_chromosome(config_id) + self.type_of_graph(self.x, self.y) - # Create the generations list - [0,1,2,etc] - self.x = list(range(generations)) - - # Query for Y data - self.y = self.database.get_highest_chromosome(config_id) - - self.type_of_graph(self.x, self.y) plt.yscale(self.yscale) plt.xlabel('Generation') plt.ylabel('Highest Fitness') @@ -61,16 +86,18 @@ class Matplotlib_Graph: def lowest_value_chromosome(self,config_id = None): """Generation by Min value Chromosome """ - # Query the X data - generations = self.database.get_total_generations(config_id) + if config_id == "all": + # If the user want to plot all the config_id's + self.all_config_id(self.database.get_lowest_chromosome) + else: + # Query the X data + generations = self.database.get_total_generations(config_id) + # Create the generations list - [0,1,2,etc] + self.x = list(range(generations)) + # Query for Y data + self.y = self.database.get_lowest_chromosome(config_id) + self.type_of_graph(self.x, self.y) - # Create the generations list - [0,1,2,etc] - self.x = list(range(generations)) - - # Query for Y data - self.y = self.database.get_lowest_chromosome(config_id) - - self.type_of_graph(self.x, self.y) plt.yscale(self.yscale) plt.xlabel('Generation') plt.ylabel('Lowest Fitness') @@ -79,6 +106,9 @@ class Matplotlib_Graph: def show(self): """Used to show the matplot lib graph.""" + if self.legend == True: + plt.legend() + plt.show() diff --git a/EasyGA/database/sql_database.py b/EasyGA/database/sql_database.py index 2ab70eb..c9b5e0a 100644 --- a/EasyGA/database/sql_database.py +++ b/EasyGA/database/sql_database.py @@ -178,7 +178,7 @@ class SQL_Database: """Get the highest fitness of each generation""" return self.query_all(f""" - SELECT fitness, max(fitness) + SELECT max(fitness) FROM data WHERE config_id={config_id} GROUP by generation;""") @@ -189,18 +189,34 @@ class SQL_Database: """Get the lowest fitness of each generation""" return self.query_all(f""" - SELECT fitness, min(fitness) + SELECT min(fitness) FROM data WHERE config_id={config_id} GROUP by generation;""") + def get_all_config_id(self): + """Get an array of all the DISTINCT config_id in the database""" + + return self.query_all(f""" + SELECT DISTINCT config_id + FROM config;""") + + def get_each_generation_number(self,config_id): + """Get an array of all the generation numbers""" + + return self.query_all(f""" + SELECT DISTINCT generation + FROM data + WHERE config_id={config_id};""") + + + #=====================================# # Input information Queries: # #=====================================# - def insert_chromosome(self, generation, chromosome): """ Insert one chromosome into the database"""