Added basic database functionallity
This commit is contained in:
@ -44,14 +44,17 @@ class GA(Attributes):
|
|||||||
# If its the first generation
|
# If its the first generation
|
||||||
if self.current_generation == 0:
|
if self.current_generation == 0:
|
||||||
|
|
||||||
# Create the database and tables
|
# Create the database here to allow the user to change
|
||||||
# self.database = database.database()
|
# the database name and structure in the running function.
|
||||||
# self.database.create_data_table(self)
|
self.database = database.database()
|
||||||
|
self.database.create_data_table(self)
|
||||||
|
|
||||||
# Create the initial population
|
# 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)
|
||||||
|
# Save the population to the database
|
||||||
|
self.database.insert_current_population(self)
|
||||||
|
|
||||||
# Otherwise evolve the population
|
# Otherwise evolve the population
|
||||||
else:
|
else:
|
||||||
@ -62,6 +65,8 @@ class GA(Attributes):
|
|||||||
self.mutation_population_impl(self)
|
self.mutation_population_impl(self)
|
||||||
self.set_all_fitness()
|
self.set_all_fitness()
|
||||||
self.population.sort_by_best_fitness(self)
|
self.population.sort_by_best_fitness(self)
|
||||||
|
# Save the population to the database
|
||||||
|
self.database.insert_current_population(self)
|
||||||
|
|
||||||
number_of_generations -= 1
|
number_of_generations -= 1
|
||||||
self.current_generation += 1
|
self.current_generation += 1
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
import sqlite3
|
import sqlite3
|
||||||
from sqlite3 import Error
|
from sqlite3 import Error
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
class database:
|
class database:
|
||||||
"""Main database class that controls all the functionality for input /
|
"""Main database class that controls all the functionality for input /
|
||||||
@ -12,10 +14,8 @@ class database:
|
|||||||
|
|
||||||
def create_connection(self, db_file):
|
def create_connection(self, db_file):
|
||||||
""" create a database connection to the SQLite database
|
""" create a database connection to the SQLite database
|
||||||
specified by db_file
|
specified by db_file."""
|
||||||
:param db_file: database file
|
|
||||||
:return: Connection object or None
|
|
||||||
"""
|
|
||||||
conn = None
|
conn = None
|
||||||
try:
|
try:
|
||||||
conn = sqlite3.connect(db_file)
|
conn = sqlite3.connect(db_file)
|
||||||
@ -38,23 +38,41 @@ class database:
|
|||||||
except Error as e:
|
except Error as e:
|
||||||
print(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 conn:
|
||||||
|
:param generation:
|
||||||
:param chromosome:
|
:param chromosome:
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# Structure the insert data
|
||||||
|
db_chromosome = (generation, chromosome.fitness, '[chromosome]')
|
||||||
|
|
||||||
|
|
||||||
sql = ''' INSERT INTO data(generation,fitness,chromosome)
|
sql = ''' INSERT INTO data(generation,fitness,chromosome)
|
||||||
VALUES(?,?,?) '''
|
VALUES(?,?,?) '''
|
||||||
cur = self.conn.cursor()
|
cur = self.conn.cursor()
|
||||||
cur.execute(sql, chromosome)
|
cur.execute(sql, db_chromosome)
|
||||||
self.conn.commit()
|
self.conn.commit()
|
||||||
return cur.lastrowid
|
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):
|
def create_data_table(self, ga):
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Remove old database if there
|
||||||
|
os.remove(ga.database_name)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
# create a database connection
|
# create a database connection
|
||||||
self.conn = self.create_connection(ga.database_name)
|
self.conn = self.create_connection(ga.database_name)
|
||||||
|
|
||||||
@ -67,13 +85,3 @@ class database:
|
|||||||
# create_table(conn, sql_create_tasks_table)
|
# create_table(conn, sql_create_tasks_table)
|
||||||
else:
|
else:
|
||||||
print("Error! cannot create the database connection.")
|
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)
|
|
||||||
|
|||||||
@ -4,6 +4,8 @@ import EasyGA
|
|||||||
# Create the Genetic algorithm
|
# Create the Genetic algorithm
|
||||||
ga = EasyGA.GA()
|
ga = EasyGA.GA()
|
||||||
|
|
||||||
|
ga.population_size = 3
|
||||||
|
ga.generation_goal = 10
|
||||||
# Evolve the genetic algorithm
|
# Evolve the genetic algorithm
|
||||||
ga.evolve()
|
ga.evolve()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user