alogos._optimization.ea.database
¶
Classes¶
Database wrapper for easily storing algorithm results. |
|
Convert database entries back to Python objects. |
Detailed object descriptions¶
- class alogos._optimization.ea.database.Database(location, system=None)[source]¶
Database wrapper for easily storing algorithm results.
- __init__(self, location, system=None)[source]¶
Create a database object referring to a file-based or in-memory SQLite3 database.
- export_sql(self, filepath, ext='sqlite3')[source]¶
Export the evolutionary search by storing the current database to an SQL file.
All tables and views are exported to a single SQL file that adheres to SQLite version 3. It can be used for a later import in order to continue and analyze a run, or it may be opened with an external tools such as DB Browser for SQLite.
- Parameters:
filepath (str) – The given filepath may automatically be modified in two ways:
It is ensured to end with the extension defined by the
ext
argument.It is ensured to be a filepath that does not exist yet by adding a numerical suffix.
Example: If
some_file.sqlite3
exists, it usessome_file_1.sqlite3
or if that also exists thensome_file_2.sqlite3
and so on.
ext (str) – The extension that the filepath is ensured to end with.
If
None
, no extension is added.If
db
, the filepath is ensured to end with.db
.If
.sql
, the filepath is ensured to end with.sql
.
- Returns:
filepath_used (str)
- export_csv(self, filepath, ext='csv')[source]¶
Export the database of this evolutionary search as CSV file.
Only the main view, which gathers information from all individual tables, is exported to a single CSV file. As such it provides all information stored in the database in a redundant manner. Currently it can not be used for a later import, but it can be opened with external tools that can read CSV files, such as LibreOffice Calc or Tad.
- Parameters:
filepath (str) – The given filepath may automatically be modified in two ways:
It is ensured to end with the extension defined by the
ext
argument.It is ensured to be a filepath that does not exist yet by adding a numerical suffix.
Example: If
some_file.sqlite3
exists, it usessome_file_1.sqlite3
or if that also exists thensome_file_2.sqlite3
and so on.
ext (str) – The extension that the filepath is ensured to end with.
If it is
None
, no extension is added.If it is
csv
, the filepath is ensured to end with.csv
.If it is
.csv
, the filepath is ensured to end with.csv
.
- Returns:
filepath_used (str)
- import_sql(self, filepath, generation_range=None)[source]¶
Import an evolutionary search by loading the SQL file of a previous run.
Either all generations (default) or only a subset defined by the interval
[first_generation, last_generation]
can be loaded.Caution: The method
reset()
is called, so that the current state and database are dropped and can be replaced in a clean fashion by new data from the SQL file.- Parameters:
Examples
Using
first_generation=0
andlast_generation=2
loads the first three generations of a previous run. The last generation is reconstructed as population in memory, so that the search can be continued from this point. Ifrun_step()
is called, the last generation loaded from the database (2) is used to construct the next generation (3). The resulting search state can again be exported to an SQL file if desired.
- import_sql_evaluations(self, filepath, verbose=False)[source]¶
Import only phenotype-to-fitness evaluation data from an SQL file.
This method allows to load phenotype-to-fitness calculations from a previous run. It is relevant when the objective funtion is computationally demanding and prevention of some recalculations may speed up the search significantly.
- Parameters:
filepath (str) – Filepath of an SQLite3 file exported by a previous run.
- num_individuals(self, generation_range=None, only_main=False)[source]¶
Get the number of individuals stored in the database.
- num_genotypes(self, generation_range=None, only_main=False)[source]¶
Get the number of unique genotypes stored in the database.
- num_phenotypes(self, generation_range=None, only_main=False)[source]¶
Get the number of unique phenotypes stored in the database.
- num_fitnesses(self, generation_range=None, only_main=False)[source]¶
Get the number of unique fitness values stored in the database.
NaN values are not counted. These values appear in individuals that were not evaluated, e.g. those generated by crossover but then modified by mutation before being evaluated and selected.
- num_details(self, generation_range=None, only_main=False)[source]¶
Get the number of unique details stored in the database.
- num_phe_to_fit_evaluations(self, only_unique=True)[source]¶
Get the number of phenotype-to-fitness evaluations.
Note: It assumes that no phenotype was evaluated more than once, which depends on the parametrization (cache and/or database lookups).
- generation_first(self)[source]¶
Get the first generation stored in the database.
Raises an error if the database does not contain any entries yet.
- generation_last(self)[source]¶
Get the last generation stored in the database.
Raises an error if the database does not contain any entries yet.
- individuals(self, generation_range=None, only_main=False)[source]¶
Get all individuals stored in the database.
- individuals_with_given_fitness(self, fitness, generation_range=None, only_main=False)[source]¶
Get all individuals that have the same user-provided fitness value.
- individuals_with_min_fitness(self, generation_range=None, only_main=False)[source]¶
Get all individuals that have the same minimum fitness value.
- individuals_with_max_fitness(self, generation_range=None, only_main=False)[source]¶
Get all individuals that have the same maximum fitness value.
- individuals_with_low_fitness(self, n=10, generation_range=None, only_main=False)[source]¶
Get the first n elements from a list of individuals sorted by lowest fitness.
- individuals_with_high_fitness(self, n=10, generation_range=None, only_main=False)[source]¶
Load the first n individuals, when all of them are sorted by hightest fitness.
- population_size_min(self)[source]¶
Get smallest population size of any generation stored in the database.
Raises an error if the database does not contain any entries yet.
- population_size_max(self)[source]¶
Get largest population size of any generation stored in the database.
Raises an error if the database does not contain any entries yet.
- genotypes(self, generation_range=None, only_main=False)[source]¶
Get unique genotypes stored in the database.
- genotypes_with_given_fitness(self, fitness, generation_range=None, only_main=False)[source]¶
Get unique genotypes that have the same user-provided fitness value.
- genotypes_with_min_fitness(self, generation_range=None, only_main=False)[source]¶
Load unique genotypes that have the same minimum fitness value.
- genotypes_with_max_fitness(self, generation_range=None, only_main=False)[source]¶
Load unique genotypes that have the same maximum fitness value.
- phenotypes_with_given_fitness(self, fitness, generation_range=None, only_main=False)[source]¶
Load unique phenotypes that have the same user-provided fitness value.
- phenotypes_with_min_fitness(self, generation_range=None, only_main=False)[source]¶
Load unique phenotypes that have the same minimum fitness value.
- phenotypes_with_max_fitness(self, generation_range=None, only_main=False)[source]¶
Load unique phenotypes that have the same minimum fitness value.
- details(self, generation_range=None, only_main=False)[source]¶
Load unique details (returned by the objective function).
- details_with_given_fitness(self, fitness, generation_range=None, only_main=False)[source]¶
Load unique details that have the same user-provided fitness value.
- details_with_min_fitness(self, generation_range=None, only_main=False)[source]¶
Load unique details that have the same minimum fitness value.
- details_with_max_fitness(self, generation_range=None, only_main=False)[source]¶
Load unique details that have the same maximum fitness value.
- fitness_min(self, generation_range=None, only_main=False)[source]¶
Load the minimum fitness value that was found.
- fitness_max(self, generation_range=None, only_main=False)[source]¶
Load the maximum fitness value that was found.
- fitness_min_after_num_evals(self, num_evaluations)[source]¶
Load the minimum fitness value that was found after a number of fitness evaluations.
- fitness_max_after_num_evals(self, num_evaluations)[source]¶
Load the maximum fitness value that was found after a number of fitness evaluations.
- gen_to_phe_evaluations(self, num_evaluations=None)[source]¶
Get genotype-to-phenotype evaluations that were performed during the search.
Guaranteed: - The order of the list is the order of performed evaluations.
Not guaranteed: - The same evaluations may have been performed multiple times during the run,
depending on cache settings, which is not available as information in the database.
- phe_to_fit_evaluations(self, num_evaluations=None, with_details=False)[source]¶
Get phenotype-to-fitness evaluations that were performed during the search.
Guaranteed: - The order of the list is the order of performed evaluations. - Genotype-phenotype pairs that were loaded from previous runs are not considered.
Not guaranteed: - The same evaluations may have been performed multiple times during the run,
depending on cache and database lookup settings, which is not available as information in the database.
- to_list(self, generation_range=None, only_main=False)[source]¶
Convert the database entries to a list of rows and add some derived information.
It uses lazy loading, i.e. it is only constructed again if the database has changed since the last call.
The first list entry contains the column names.
- to_dataframe(self, generation_range=None, only_main=False)[source]¶
Convert the database entries to a Pandas DataFrame.
Some derived information is added during the conversion.
- Parameters:
only_main_populations (bool) – From the complete dataframe, only main populations are kept and intermediate ones (selected parents, crossed-over or mutated populations) are filtered out. Note that the crossed-over population is not evaluated if a mutation operator is provided, so that they have no associated phenotype and fitness values.
References
- to_network(self, generation_range=None, only_main=False)[source]¶
Convert the database entries to NetworkX graph and add some derived information.
- class alogos._optimization.ea.database.Deserializer(system)[source]¶
Convert database entries back to Python objects.
- genotype(self, data)[source]¶
Reconstruct a genotype.
This generates a Genotype object. Its exact type depends on the grammar-based genetic programming system being used.