From 690fa9712ede5e9650f4cf6a73d23dbda0b9834a Mon Sep 17 00:00:00 2001 From: SimpleArt <71458112+SimpleArt@users.noreply.github.com> Date: Fri, 6 Nov 2020 21:31:20 -0500 Subject: [PATCH] More efficient insertion of entire population Executes the sql command over the entire population at once. --- src/database/database.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/database/database.py b/src/database/database.py index 78cd66a..5680f8b 100644 --- a/src/database/database.py +++ b/src/database/database.py @@ -35,6 +35,7 @@ class database: except Error as e: print(e) + def insert_chromosome(self, generation, chromosome): """ """ @@ -50,13 +51,24 @@ class database: self.conn.commit() return cur.lastrowid + 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) + # Structure the insert data + db_chromosome_list = [ + (ga.current_generation, chromosome.fitness, '[chromosome]') + for chromosome in ga.population.get_chromosome_list() + ] + + # Create sql query structure + sql = ''' INSERT INTO data(generation,fitness,chromosome) + VALUES(?,?,?) ''' + + cur = self.conn.cursor() + cur.executemany(sql, db_chromosome_list) + self.conn.commit() + return cur.lastrowid def create_data_table(self, ga): @@ -89,6 +101,7 @@ class database: return cur.fetchall() + def query_one_item(self, query): """Query for single data point"""