Fixed graphing functions and added optional config_id to graphing functions

This commit is contained in:
danielwilczak101
2020-11-21 15:32:01 -05:00
parent f3d4712097
commit 610b23dc3a
4 changed files with 48 additions and 28 deletions

View File

@ -2,7 +2,9 @@
# EasyGA - Genetic Algorithms made Easy # EasyGA - Genetic Algorithms made Easy
EasyGA is a python package designed to provide an easy-to-use Genetic Algorithm. The package is designed to work right out of the box, while also allowing the user to customize features as they see fit. Check out our [wiki](https://github.com/danielwilczak101/EasyGA/wiki) for more information. EasyGA is a python package designed to provide an easy-to-use Genetic Algorithm. The package is designed to work right out of the box, while also allowing the user to customize features as they see fit.
### Check out our [wiki](https://github.com/danielwilczak101/EasyGA/wiki) for more information.
## Installation: ## Installation:

View File

@ -6,8 +6,8 @@ with open("README.md", "r") as fh:
setup( setup(
name='EasyGA', name='EasyGA',
version='0.0.30', version='0.0.31',
description='A ubiquitous or general purpuse GA', description='EasyGA is a python package designed to provide an easy-to-use Genetic Algorithm. The package is designed to work right out of the box, while also allowing the user to customize features as they see fit.',
py_modules=["EasyGA","attributes","test_EasyGA"], py_modules=["EasyGA","attributes","test_EasyGA"],
packages=find_packages(where='EasyGA'), packages=find_packages(where='EasyGA'),
package_dir={ package_dir={
@ -27,10 +27,7 @@ setup(
"Operating System :: OS Independent", "Operating System :: OS Independent",
], ],
install_requires = ["matplotlib ~= 3.3.2", install_requires = ["matplotlib ~= 3.3.2",
"pyserial ~= 3.4",
"pytest>=3.7"
], ],
extra_require = {
"dev": [
"pytest>=3.7",
],
},
) )

View File

@ -40,7 +40,6 @@ class Matplotlib_Graph:
plt.ylabel('Generation Total Fitness') plt.ylabel('Generation Total Fitness')
plt.title('Relationship Between Generations and Generation Total Fitness') plt.title('Relationship Between Generations and Generation Total Fitness')
def highest_value_chromosome(self): def highest_value_chromosome(self):
"""Generation by Max value chromosome """ """Generation by Max value chromosome """
@ -84,6 +83,9 @@ class Matplotlib_Graph:
plt.ylabel('Lowest Fitness') plt.ylabel('Lowest Fitness')
plt.title('Relationship Between Generations and Lowest Fitness') plt.title('Relationship Between Generations and Lowest Fitness')
def show(self):
"""Used to show the matplot lib graph."""
plt.show()
# Getter and setters # Getter and setters
@property @property

View File

@ -11,8 +11,13 @@ class SQL_Database:
def __init__(self): def __init__(self):
self.conn = None self.conn = None
self.config_id = None
def get_current_config(self):
"""Get the current config_id from the config table."""
return self.query_one_item("SELECT MAX(id) FROM config")
def sql_type_of(self, obj): def sql_type_of(self, obj):
"""Returns the sql type for the object""" """Returns the sql type for the object"""
@ -52,11 +57,11 @@ class SQL_Database:
""" Insert one chromosome into the database""" """ Insert one chromosome into the database"""
# Structure the insert data # Structure the insert data
db_chromosome = (generation, chromosome.fitness, repr(chromosome)) db_chromosome = (self.config_id,generation, chromosome.fitness, repr(chromosome))
# Create sql query structure # Create sql query structure
sql = ''' INSERT INTO data(generation, fitness, chromosome) sql = ''' INSERT INTO data(config_id, generation, fitness, chromosome)
VALUES(?,?,?) ''' VALUES(?,?,?,?) '''
cur = self.conn.cursor() cur = self.conn.cursor()
cur.execute(sql, db_chromosome) cur.execute(sql, db_chromosome)
@ -68,14 +73,19 @@ class SQL_Database:
""" Insert current generations population """ """ Insert current generations population """
# Structure the insert data # Structure the insert data
db_chromosome_list = [(ga.current_generation, db_chromosome_list = [
(
self.config_id,
ga.current_generation,
chromosome.fitness, chromosome.fitness,
repr(chromosome)) repr(chromosome)
for chromosome in ga.population.get_chromosome_list() ] )
for chromosome in ga.population.get_chromosome_list()
]
# Create sql query structure # Create sql query structure
sql = ''' INSERT INTO data(generation,fitness,chromosome) sql = ''' INSERT INTO data(config_id,generation,fitness,chromosome)
VALUES(?,?,?) ''' VALUES(?,?,?,?) '''
cur = self.conn.cursor() cur = self.conn.cursor()
cur.executemany(sql, db_chromosome_list) cur.executemany(sql, db_chromosome_list)
@ -101,8 +111,8 @@ class SQL_Database:
tables.""" tables."""
try: try:
# Remove old database file if it exists. # if the database file already exists.
os.remove(ga.database_name) self.config = self.get_current_config()
except: except:
# If the database does not exist continue # If the database does not exist continue
pass pass
@ -163,6 +173,7 @@ class SQL_Database:
cur = self.conn.cursor() cur = self.conn.cursor()
cur.executemany(sql, db_config_list) cur.executemany(sql, db_config_list)
self.conn.commit() self.conn.commit()
self.config_id = self.get_current_config()
return cur.lastrowid return cur.lastrowid
@ -185,10 +196,12 @@ class SQL_Database:
return query_data[0] return query_data[0]
def get_generation_total_fitness(self): def get_generation_total_fitness(self,config_id = None):
"""Get each generations total fitness sum from the database """ """Get each generations total fitness sum from the database """
query_data = self.query_all("SELECT SUM(fitness) FROM data GROUP BY generation;") if config_id == None: config_id = self.config_id
query_data = self.query_all(f"SELECT SUM(fitness) FROM data WHERE config_id={config_id} GROUP BY generation;")
# Format the fitness data into one list # Format the fitness data into one list
formated_query_data = [i[0] for i in query_data] formated_query_data = [i[0] for i in query_data]
@ -196,18 +209,22 @@ class SQL_Database:
return formated_query_data return formated_query_data
def get_total_generations(self): def get_total_generations(self,config_id = None):
"""Get the total generations from the database""" """Get the total generations from the database"""
query_data = self.query_one_item("SELECT COUNT(DISTINCT generation) FROM data;") if config_id == None: config_id = self.config_id
query_data = self.query_one_item(f"SELECT COUNT(DISTINCT generation) FROM data WHERE config_id={config_id};")
return query_data return query_data
def get_highest_chromosome(self): def get_highest_chromosome(self,config_id = None):
"""Get the highest fitness of each generation""" """Get the highest fitness of each generation"""
query_data = self.query_all("select fitness, max(fitness) from data group by generation") if config_id == None: config_id = self.config_id
query_data = self.query_all(f"SELECT fitness, max(fitness) FROM data WHERE config_id={config_id} GROUP by generation;")
# Format the fitness data into one list # Format the fitness data into one list
formated_query_data = [i[0] for i in query_data] formated_query_data = [i[0] for i in query_data]
@ -215,10 +232,12 @@ class SQL_Database:
return formated_query_data; return formated_query_data;
def get_lowest_chromosome(self): def get_lowest_chromosome(self,config_id = None):
"""Get the lowest fitness of each generation""" """Get the lowest fitness of each generation"""
query_data = self.query_all("select fitness, min(fitness) from data group by generation") if config_id == None: config_id = self.config_id
query_data = self.query_all(f"SELECT fitness, min(fitness) FROM data WHERE config_id={config_id} GROUP by generation;")
# Format the fitness data into one list # Format the fitness data into one list
formated_query_data = [i[0] for i in query_data] formated_query_data = [i[0] for i in query_data]