From 172918cad74ac5b1361b4cb56a49d8ea19baddf2 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Tue, 3 Nov 2020 13:43:21 -0500 Subject: [PATCH] Added small database functionality but its commented out of running. --- src/EasyGA.py | 12 +++++++++++- src/attributes.py | 21 ++++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/EasyGA.py b/src/EasyGA.py index c2f9b02..d7f8180 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -19,6 +19,10 @@ from crossover import Crossover_Methods # Default Attributes for the GA from attributes import Attributes +# Database class +from database import database +from sqlite3 import Error + class GA(Attributes): """GA is the main class in EasyGA. Everything is run through the ga @@ -40,8 +44,14 @@ class GA(Attributes): and (not consider_termination # and if consider_termination flag is set or self.active())): # then also check if termination conditions reached - # If its the first generation then initialize the population + # 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 initial population self.initialize_population() self.set_all_fitness() self.population.sort_by_best_fitness(self) diff --git a/src/attributes.py b/src/attributes.py index 1239d1c..254614d 100644 --- a/src/attributes.py +++ b/src/attributes.py @@ -1,4 +1,5 @@ import random +import sqlite3 # Import all the data structure prebuilt modules from structure import Population as create_population @@ -18,6 +19,10 @@ from survivor_selection import Survivor_Selection from mutation import Mutation_Methods from crossover import Crossover_Methods +# Database class +from database import database +from sqlite3 import Error + class Attributes: """Default GA attributes can be found here. If any attributes have not been set then they will fall back onto the default attribute. All @@ -55,7 +60,15 @@ class Attributes: 'mutation_individual_impl' : Mutation_Methods.Individual.single_gene, 'mutation_population_impl' : Mutation_Methods.Population.random_selection, 'termination_impl' : Termination_Methods.fitness_and_generation_based, - 'database_name' : 'database.db' + 'database' : None, + 'database_name' : r"database.db", + 'sql_create_data_structure' : """ CREATE TABLE IF NOT EXISTS data ( + id integer PRIMARY KEY, + generation integer NOT NULL, + fitness DOUBLE, + chromosome text + ); """ + } # Filling in the default settings @@ -108,6 +121,12 @@ class Attributes: # Database varibles self.database_name = attributes['database_name'] + # Database varibles + self.database = attributes['database'] + self.database_name = attributes['database_name'] + self.sql_create_data_structure = attributes['sql_create_data_structure'] + + # Getter and setters for all required varibles @property def chromosome_length(self):