From 7b2053d626e84daf6daf497a4201cf7143a0a038 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Thu, 5 Nov 2020 15:38:54 -0500 Subject: [PATCH] Added basic database functionallity --- src/EasyGA.py | 11 ++++++++--- src/database/database.py | 42 ++++++++++++++++++++++++---------------- src/run_testing.py | 2 ++ 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/src/EasyGA.py b/src/EasyGA.py index d14299e..de96100 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -44,14 +44,17 @@ class GA(Attributes): # If its the first generation if self.current_generation == 0: - # Create the database and tables - # self.database = database.database() - # self.database.create_data_table(self) + # Create the database here to allow the user to change + # the database name and structure in the running function. + self.database = database.database() + self.database.create_data_table(self) # Create the initial population self.initialize_population() self.set_all_fitness() self.population.sort_by_best_fitness(self) + # Save the population to the database + self.database.insert_current_population(self) # Otherwise evolve the population else: @@ -62,6 +65,8 @@ class GA(Attributes): self.mutation_population_impl(self) self.set_all_fitness() self.population.sort_by_best_fitness(self) + # Save the population to the database + self.database.insert_current_population(self) number_of_generations -= 1 self.current_generation += 1 diff --git a/src/database/database.py b/src/database/database.py index 2550d34..7596288 100644 --- a/src/database/database.py +++ b/src/database/database.py @@ -1,5 +1,7 @@ import sqlite3 from sqlite3 import Error +import os + class database: """Main database class that controls all the functionality for input / @@ -12,10 +14,8 @@ class database: def create_connection(self, db_file): """ create a database connection to the SQLite database - specified by db_file - :param db_file: database file - :return: Connection object or None - """ + specified by db_file.""" + conn = None try: conn = sqlite3.connect(db_file) @@ -38,23 +38,41 @@ class database: except Error as e: print(e) - def insert_chromosome(self, chromosome): + def insert_chromosome(self, generation, chromosome): """ - Create a new task + Insert a new chromosome :param conn: + :param generation: :param chromosome: :return: """ + # Structure the insert data + db_chromosome = (generation, chromosome.fitness, '[chromosome]') + + sql = ''' INSERT INTO data(generation,fitness,chromosome) VALUES(?,?,?) ''' cur = self.conn.cursor() - cur.execute(sql, chromosome) + cur.execute(sql, db_chromosome) self.conn.commit() return cur.lastrowid + def insert_current_population(self, ga): + """ Insert current generations population """ + + for chromosome in ga.population.chromosome_list: + self.insert_chromosome(ga.current_generation, chromosome) + + def create_data_table(self, ga): + try: + # Remove old database if there + os.remove(ga.database_name) + except: + pass + # create a database connection self.conn = self.create_connection(ga.database_name) @@ -67,13 +85,3 @@ class database: # create_table(conn, sql_create_tasks_table) else: print("Error! cannot create the database connection.") - - with self.conn: - - # Create a fake chromosome - # Generation / Fitness / chromosome - data1 = (0, 99, '[gene,gene,gene,gene]') - data2 = (1, 200, '[gene,gene,gene,gene]') - # Add chromosome to the data table inside the database - self.insert_chromosome(data1) - self.insert_chromosome(data2) diff --git a/src/run_testing.py b/src/run_testing.py index d61f6b5..b5f355a 100644 --- a/src/run_testing.py +++ b/src/run_testing.py @@ -4,6 +4,8 @@ import EasyGA # Create the Genetic algorithm ga = EasyGA.GA() +ga.population_size = 3 +ga.generation_goal = 10 # Evolve the genetic algorithm ga.evolve()