Added small database functionality but its commented out of running.

This commit is contained in:
danielwilczak101
2020-11-03 13:43:21 -05:00
parent e99da737b5
commit 172918cad7
2 changed files with 31 additions and 2 deletions

View File

@ -19,6 +19,10 @@ from crossover import Crossover_Methods
# Default Attributes for the GA # Default Attributes for the GA
from attributes import Attributes from attributes import Attributes
# Database class
from database import database
from sqlite3 import Error
class GA(Attributes): class GA(Attributes):
"""GA is the main class in EasyGA. Everything is run through the ga """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 and (not consider_termination # and if consider_termination flag is set
or self.active())): # then also check if termination conditions reached 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: 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.initialize_population()
self.set_all_fitness() self.set_all_fitness()
self.population.sort_by_best_fitness(self) self.population.sort_by_best_fitness(self)

View File

@ -1,4 +1,5 @@
import random import random
import sqlite3
# Import all the data structure prebuilt modules # Import all the data structure prebuilt modules
from structure import Population as create_population from structure import Population as create_population
@ -18,6 +19,10 @@ from survivor_selection import Survivor_Selection
from mutation import Mutation_Methods from mutation import Mutation_Methods
from crossover import Crossover_Methods from crossover import Crossover_Methods
# Database class
from database import database
from sqlite3 import Error
class Attributes: class Attributes:
"""Default GA attributes can be found here. If any attributes have not """Default GA attributes can be found here. If any attributes have not
been set then they will fall back onto the default attribute. All 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_individual_impl' : Mutation_Methods.Individual.single_gene,
'mutation_population_impl' : Mutation_Methods.Population.random_selection, 'mutation_population_impl' : Mutation_Methods.Population.random_selection,
'termination_impl' : Termination_Methods.fitness_and_generation_based, '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 # Filling in the default settings
@ -108,6 +121,12 @@ class Attributes:
# Database varibles # Database varibles
self.database_name = attributes['database_name'] 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 # Getter and setters for all required varibles
@property @property
def chromosome_length(self): def chromosome_length(self):