Added graphing capablities. It only graphs total fitness of generation.

This commit is contained in:
danielwilczak101
2020-11-06 02:11:06 -05:00
parent e012f6653c
commit 5c5b645c30
7 changed files with 75 additions and 26 deletions

View File

@ -5,7 +5,7 @@ import os
class database:
"""Main database class that controls all the functionality for input /
out of the database."""
out of the database."""
def __init__(self):
@ -13,7 +13,7 @@ class database:
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."""
conn = None
@ -27,11 +27,8 @@ class database:
def create_table(self, create_table_sql):
""" create a table from the create_table_sql statement
:param conn: Connection object
:param create_table_sql: a CREATE TABLE statement
:return:
"""
"""Create a table from the create_table_sql statement."""
try:
c = self.conn.cursor()
c.execute(create_table_sql)
@ -39,20 +36,15 @@ class database:
print(e)
def insert_chromosome(self, generation, chromosome):
"""
Insert a new chromosome
:param conn:
:param generation:
:param chromosome:
:return:
"""
""" """
# Structure the insert data
db_chromosome = (generation, chromosome.fitness, '[chromosome]')
# Create sql query structure
sql = ''' INSERT INTO data(generation,fitness,chromosome)
VALUES(?,?,?) '''
VALUES(?,?,?) '''
cur = self.conn.cursor()
cur.execute(sql, db_chromosome)
self.conn.commit()
@ -61,16 +53,20 @@ class database:
def insert_current_population(self, ga):
""" Insert current generations population """
# For each chromosome in the population
for chromosome in ga.population.chromosome_list:
# Insert the chromosome into the database
self.insert_chromosome(ga.current_generation, chromosome)
def create_data_table(self, ga):
"""Create the data table that store generation data."""
try:
# Remove old database if there
# Remove old database file if it exists.
os.remove(ga.database_name)
except:
# If the database does not exist continue
pass
# create a database connection
@ -81,7 +77,23 @@ class database:
# create projects table
self.create_table(ga.sql_create_data_structure)
# create tasks table
# create_table(conn, sql_create_tasks_table)
else:
print("Error! cannot create the database connection.")
def query_all(self, query):
"""Query for muliple rows of data"""
cur = self.conn.cursor()
cur.execute(query)
return cur.fetchall()
def query_one_item(self, query):
"""Query for single data point"""
cur = self.conn.cursor()
cur.execute(query)
query_data = cur.fetchone()
return query_data[0]