More efficient insertion of entire population
Executes the sql command over the entire population at once.
This commit is contained in:
@ -35,6 +35,7 @@ class database:
|
|||||||
except Error as e:
|
except Error as e:
|
||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
|
|
||||||
def insert_chromosome(self, generation, chromosome):
|
def insert_chromosome(self, generation, chromosome):
|
||||||
""" """
|
""" """
|
||||||
|
|
||||||
@ -50,13 +51,24 @@ class database:
|
|||||||
self.conn.commit()
|
self.conn.commit()
|
||||||
return cur.lastrowid
|
return cur.lastrowid
|
||||||
|
|
||||||
|
|
||||||
def insert_current_population(self, ga):
|
def insert_current_population(self, ga):
|
||||||
""" Insert current generations population """
|
""" Insert current generations population """
|
||||||
|
|
||||||
# For each chromosome in the population
|
# Structure the insert data
|
||||||
for chromosome in ga.population.chromosome_list:
|
db_chromosome_list = [
|
||||||
# Insert the chromosome into the database
|
(ga.current_generation, chromosome.fitness, '[chromosome]')
|
||||||
self.insert_chromosome(ga.current_generation, 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):
|
def create_data_table(self, ga):
|
||||||
@ -89,6 +101,7 @@ class database:
|
|||||||
|
|
||||||
return cur.fetchall()
|
return cur.fetchall()
|
||||||
|
|
||||||
|
|
||||||
def query_one_item(self, query):
|
def query_one_item(self, query):
|
||||||
"""Query for single data point"""
|
"""Query for single data point"""
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user