Store Access Data - danielwilczak101/EasyGA GitHub Wiki

Store and Access Data from local database

The default database used is in EasyGA is a SQLite3 database.

Saving the genetic algorithm

All data is stored on a local database. The file is created when you first run your genetic algorithm. To change the name of the file created. Just change the database_name attribute.

# Default file name
ga.database_name = 'database.db'

Automated

Saving to the local database is completely automated while using ga.evolve() to evolve the genetic algorithm.

What does it save?

There are two major sections that EasyGA saves. This can be broken down into two tables in the EasyGA database.

data:

Stores all the meta data of the running genetic algorithm. To see more detail see the data table page.

config:

Stores all the configuration data the genetic algorithm was setup on. To see more detail see the config table page.

Want to see past runs?

Check out the ga.past_runs() function on the config page to see a summery of your past ga runs.

Getting Data from the Database

EasyGA comes with some pre-built in functions and the ability to run SQL commands directly within the framework. This doesn't stop you from accessing the database.db file directly it just makes it easier.

EasyGA built in functions

import EasyGA

# Create the genetic algorithm
ga = EasyGA.GA()

# Evolve the genetic algorithm
ga.evolve()

# Get the total number of generations ran
print(ga.database.get_total_generations())
# Get the highest fitness of each generation
print(ga.database.get_highest_chromosome())
# Get the lowest fitness of each generation
print(ga.database.get_lowest_chromosome())
# Get each generations total fitness summed
print(ga.database.get_generation_total_fitness())

Output

15
[3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0]
[0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 2.0]
[15.0, 18.0, 21.0, 23.0, 25.0, 26.0, 28.0, 31.0, 32.0, 34.0, 34.0, 34.0, 35.0, 36.0, 36.0]

Direct SQL commands

There are two main functions to access using sql statements. For more information on the SQL language. W3School is a good resource.

import EasyGA

# Create the genetic algorithm
ga = EasyGA.GA()

# Evolve the genetic algorithm
ga.evolve()

# Return only one value
print(ga.database.query_one_item("SELECT COUNT(DISTINCT generation) FROM data;"))
# Returns all the rows
print(ga.database.query_all("""SELECT * FROM data WHERE generation = 1;"""))

Output

15
[(11, None, 1, 3.0, '[5][8][6][10][4][5][10][5][10][1]'),
 (12, None, 1, 3.0, '[1][5][6][6][5][4][8][3][1][5]'),
 (13, None, 1, 2.0, '[4][10][10][7][8][5][7][2][5][2]'),
 (14, None, 1, 2.0, '[4][10][10][7][8][5][7][9][5][2]'),
 (15, None, 1, 2.0, '[5][9][5][2][3][2][3][8][4][10]'),
 (16, None, 1, 2.0, '[6][10][10][9][8][5][2][3][5][10]'),
 (17, None, 1, 1.0, '[4][9][5][3][8][8][6][9][9][4]'),
 (18, None, 1, 1.0, '[7][9][3][3][7][3][5][1][4][8]'),
 (19, None, 1, 0.0, '[3][10][10][2][10][4][9][10][3][7]'),
 (20, None, 1, 0.0, '[6][4][7][9][1][7][1][2][7][9]')]

Manually Access Data

You can always enter the sqlite3 database manually to look,query or change the database. A good resource to learn this is the python tutorial for sqllite3 https://www.sqlitetutorial.net/sqlite-python/. A nice GUI (Graphical User Interface) to use for the sqlite3 database is Sqlite studio.