diff --git a/src/database/matplotlib_graph.py b/src/database/matplotlib_graph.py index a372913..23a6509 100644 --- a/src/database/matplotlib_graph.py +++ b/src/database/matplotlib_graph.py @@ -19,17 +19,17 @@ class Matplotlib_Graph: self.yscale = "linear" - def generation_total_fitness(self): + 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() + generations = self.database.get_total_generations(config_id) # Create the generations list - [0,1,2,etc] self.x = list(range(0, generations)) # Query for Y data - self.y = self.database.get_generation_total_fitness() + self.y = self.database.get_generation_total_fitness(config_id) if self.yscale == "log": # If using log then the values have to be positive numbers @@ -40,17 +40,17 @@ class Matplotlib_Graph: plt.ylabel('Generation Total Fitness') plt.title('Relationship Between Generations and Generation Total Fitness') - def highest_value_chromosome(self): + def highest_value_chromosome(self,config_id = None): """Generation by Max value chromosome """ # Query the X data - generations = self.database.get_total_generations() + generations = self.database.get_total_generations(config_id) # Create the generations list - [0,1,2,etc] self.x = list(range(0, generations)) # Query for Y data - self.y = self.database.get_highest_chromosome() + self.y = self.database.get_highest_chromosome(config_id) if self.yscale == "log": # If using log then the values have to be positive numbers @@ -62,17 +62,17 @@ class Matplotlib_Graph: plt.title('Relationship Between Generations and Highest Fitness') - def lowest_value_chromosome(self): + def lowest_value_chromosome(self,config_id = None): """Generation by Min value Chromosome """ # Query the X data - generations = self.database.get_total_generations() + generations = self.database.get_total_generations(config_id) # Create the generations list - [0,1,2,etc] self.x = list(range(0, generations)) # Query for Y data - self.y = self.database.get_lowest_chromosome() + self.y = self.database.get_lowest_chromosome(config_id) if self.yscale == "log": # If using log then the values have to be positive numbers diff --git a/src/database/sql_database.py b/src/database/sql_database.py index 2b1bbbb..e6e9306 100644 --- a/src/database/sql_database.py +++ b/src/database/sql_database.py @@ -18,6 +18,7 @@ class SQL_Database: """Get the current config_id from the config table.""" return self.query_one_item("SELECT MAX(id) FROM config") + def sql_type_of(self, obj): """Returns the sql type for the object""" @@ -196,11 +197,19 @@ class SQL_Database: return query_data[0] - def get_generation_total_fitness(self,config_id = None): + def past_runs(self,database_name = 'database.db'): + """Show a summerization of the past runs that the user has done.""" + + self.conn = self.create_connection(database_name) + query_data = self.query_all(f"SELECT id,generation_goal,chromosome_length FROM config;") + print(query_data) + + def get_generation_total_fitness(self,config_id = None,database_name = 'database.db'): """Get each generations total fitness sum from the database """ if config_id == None: config_id = self.config_id + self.conn = self.create_connection(database_name) query_data = self.query_all(f"SELECT SUM(fitness) FROM data WHERE config_id={config_id} GROUP BY generation;") # Format the fitness data into one list @@ -209,21 +218,22 @@ class SQL_Database: return formated_query_data - def get_total_generations(self,config_id = None): + def get_total_generations(self,config_id = None,database_name = 'database.db'): """Get the total generations from the database""" if config_id == None: config_id = self.config_id - + self.conn = self.create_connection(database_name) query_data = self.query_one_item(f"SELECT COUNT(DISTINCT generation) FROM data WHERE config_id={config_id};") return query_data - def get_highest_chromosome(self,config_id = None): + def get_highest_chromosome(self,config_id = None,database_name = 'database.db'): """Get the highest fitness of each generation""" if config_id == None: config_id = self.config_id + self.conn = self.create_connection(database_name) query_data = self.query_all(f"SELECT fitness, max(fitness) FROM data WHERE config_id={config_id} GROUP by generation;") # Format the fitness data into one list @@ -232,14 +242,51 @@ class SQL_Database: return formated_query_data; - def get_lowest_chromosome(self,config_id = None): + def get_lowest_chromosome(self,config_id = None,database_name = 'database.db'): """Get the lowest fitness of each generation""" if config_id == None: config_id = self.config_id + self.conn = self.create_connection(database_name) query_data = self.query_all(f"SELECT fitness, min(fitness) FROM data WHERE config_id={config_id} GROUP by generation;") # Format the fitness data into one list formated_query_data = [i[0] for i in query_data] return formated_query_data; + + + # Getters and setter for class + + def get_config_id(self): + + try: + # return the config id if its already set + self.__config_id = "something" + except: + # Get the most resent run from config table. + pass + except: + # config_id and config table dont exist: Tell the user + print("""You are required to run a ga before you + can connect to the database. Run ga.evolve()""") + break + + + def get_conn(self): + """Conn """ + try: + # Return if the connection has already been set + return self.__conn + except: + # Check if you can connect to the database named in ga + self.__conn = self.create_connection(ga.database_name) + + except: + # See if the default database exists + self.__conn = self.create_connection("database.db") + except: + # if the connection + print("""You are required to run a ga before you + can connect to the database. Run ga.evolve()""") + break diff --git a/src/run.py b/src/run.py index ceaa595..63c9006 100644 --- a/src/run.py +++ b/src/run.py @@ -4,21 +4,11 @@ import matplotlib.pyplot as plt # Create the genetic algorithm ga = EasyGA.GA() -# Create 25 chromosomes each with 10 genes and 200 generations -ga.population_size = 200 -ga.chromosome_length = 10 -ga.generation_goal = 150 - -# Evolve the genetic algorithm ga.evolve() -# Print generation and population -ga.print_generation() -ga.print_population() -# Plot the data from the genetic algorithm -plt.figure(figsize = [6, 6]) -ga.graph.highest_value_chromosome() # Change this so it doesn't make its own figure or show -plt.xlabel('My datas generations') # override the xlabel -plt.ylabel('How well the fitness is') # override the ylabel -plt.title('My GA fitness x generations') # override the title -plt.show() +ga.database.past_runs() + + + +ga.graph.highest_value_chromosome(1) # Change this so it doesn't make its own figure or show +ga.graph.show()