Genetic algorithm¶
Modules under eso.ga implement the genetic algorithm. The representation is split across Gene, Chromosome, and Population. The variation machinery lives in SelectionOperator and GeneticOperator. For the algorithmic explanation, see Genes and chromosomes and Evolution.
| Symbol | File | Role |
|---|---|---|
Gene |
eso/ga/gene.py |
One horizontal band, encoded as (band_position, band_height). |
Chromosome |
eso/ga/chromosome.py |
Set of genes plus a trained CNN. Knows its own fitness. |
Population |
eso/ga/population.py |
A generation of chromosomes. Trains, scores, replaces. |
SelectionOperator |
eso/ga/selection.py |
Tournament selection of parents. |
GeneticOperator |
eso/ga/operator.py |
Reproduction, mutation, crossover. |
eso.ga.gene¶
The atom of the algorithm. A Gene carries no weights — it is a pointer to a strip of the mel-spectrogram. Position and height are expressed as integer indices on the frequency axis (0 to spec_height). The constructor supports four modes: fully random, fixed position and free height, fixed height and free position, or fully fixed.
gene_1
module-attribute
¶
gene_1 = Gene(
spec_height=200, min_position=0, max_position=128, min_height=1, max_height=10
)
gene_2
module-attribute
¶
gene_2 = Gene(
spec_height=200,
min_position=0,
max_position=128,
min_height=1,
max_height=10,
band_position=80,
band_height=7,
)
gene_3
module-attribute
¶
gene_3 = Gene(
spec_height=200,
min_position=0,
max_position=128,
min_height=1,
max_height=10,
band_position=100,
)
gene_4
module-attribute
¶
gene_4 = Gene(
spec_height=200,
min_position=0,
max_position=128,
min_height=1,
max_height=10,
band_height=9,
)
Gene
¶
Gene(
spec_height: int,
min_position: int,
max_position: int,
min_height: int,
max_height: int,
minimum_gene_height: int,
band_position: int = None,
band_height: int = None,
)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec_height
|
int
|
The height of the spectogram |
required |
min_position
|
int
|
The minimal position of a band in the spectogram |
required |
max_position
|
int
|
The maximal position of a band in the spectogram. |
required |
min_height
|
int
|
The minimal height of a band in the spectogram. |
required |
max_height
|
int
|
The maximal height of a band in the spectogram. |
required |
band_position
|
int
|
The position of the band in the spectogram. The default is None. |
None
|
band_height
|
int
|
The height of the band in the spectogram. The default is None. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in eso/ga/gene.py
get_band_position
¶
get_band_height
¶
set_band_position
¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_band_position
|
int
|
The new position at which the band should be set. |
required |
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in eso/ga/gene.py
set_band_height
¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_band_height
|
int
|
The new height of the band. |
required |
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in eso/ga/gene.py
eso.ga.chromosome¶
The candidate solution. A Chromosome is an ordered list of genes plus the CNN trained on the bands those genes describe. It exposes train(), get_fitness(), get_metric(), and get_genes(). Fitness is computed relative to the baseline F1 and parameter count, weighted by lambda_1 and lambda_2 from ChromosomeConfig.
Created on Wed Sep 20 12:17:35 2023
@author: ljeantet, ufuk-cakir
Chromosome
¶
Chromosome(
results_path,
num_genes: int,
min_num_genes: int,
max_num_genes: int,
baseline_metric: float,
baseline_parameters: int,
gene_args: dict,
model_args: dict,
architecture_args: dict,
lambda_1: float = 0.5,
lambda_2: float = 0.5,
stack: bool = False,
logger=None,
)
Chromosome class
This class represents a chromosome in the genetic algorithm and is a collection of genes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_genes
|
int
|
The number of genes in the chromosome. |
required |
min_num_genes
|
int
|
The minimum number of genes in the chromosome. |
required |
max_num_genes
|
int
|
The maximum number of genes in the chromosome. |
required |
baseline_metric
|
float
|
The baseline metric to compare the accuracy to. |
required |
baseline_parameters
|
int
|
The baseline number of parameters to compare the number of trainable parameters to. |
required |
gene_args
|
dict
|
The arguments to pass to the gene class. |
required |
model_args
|
dict
|
The arguments to pass to the model class. |
required |
lambda_1
|
float
|
The lambda_1 parameter for the fitness function. |
0.5
|
lambda_2
|
float
|
The lambda_2 parameter for the fitness function. |
0.5
|
logger
|
Logger
|
The logger to use. |
None
|
Source code in eso/ga/chromosome.py
sort
¶
get_genes
¶
Get the genes in the chromosome
Returns a list of the genes in the chromosome.
Returns:
| Type | Description |
|---|---|
list
|
The genes in the chromosome. |
set_gene
¶
Set the gene at a specific position
Set the gene at a specific position in the chromosome. Either the band position or the band height must be specified. This is used for crossover.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
position
|
int
|
The position of the gene to set. |
required |
band_position
|
int
|
The position of the band to set. |
None
|
band_height
|
int
|
The height of the band to set. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the position is greater than the number of genes. |
ValueError
|
If the position is less than 0. |
ValueError
|
If neither the band position or the band height is specified. |
Source code in eso/ga/chromosome.py
get_info
¶
Get the information about the chromosome
Returns a string with the information about the chromosome.
Source code in eso/ga/chromosome.py
get_metric
¶
get_trainable_parameters
¶
train
¶
Train the chromosome
Create the sliced dataset from the encoded bands from the genes and train the model on this dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X_train
|
array
|
The training dataset. |
required |
Y_train
|
array
|
The training labels. |
required |
X_val
|
array
|
The validation dataset. |
required |
Y_val
|
array
|
The validation labels. |
required |
Source code in eso/ga/chromosome.py
get_fitness
¶
save
¶
Save the chromosome to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The path to the directory where the chromosome should be saved. |
required |
name
|
str
|
The name of the file. |
required |
Source code in eso/ga/chromosome.py
save_model
¶
predict
¶
Source code in eso/ga/chromosome.py
evaluate
¶
Source code in eso/ga/chromosome.py
eso.ga.population¶
A Population is a fixed-size set of chromosomes. It exposes methods to evaluate all chromosomes in a generation, locate the best, replace the weakest with offspring, and serialise itself to disk between generations.
Population
¶
Population(
results_path,
pop_size: int,
chromosome_args: dict,
model_args: dict,
gene_args: dict,
architecture_args: dict,
logger=None,
chromsomes: list = None,
data: Data = None,
)
Initialize a population.
Source code in eso/ga/population.py
reset_trained_flags
¶
save
¶
Save the population to a file.
:param path: The path to the file. :param save_as_pickle: Whether to save as a pickle file or not.
Source code in eso/ga/population.py
load
staticmethod
¶
Source code in eso/ga/population.py
train_population
¶
Train the population of chromosomes.
:param epochs: The number of epochs to train each chromosome.
Source code in eso/ga/population.py
get_chromosomes
¶
replace_chromosomes
¶
Replace the chromosomes in the population with new ones.
:param new_chromosomes: The new chromosomes to replace the old ones with.
get_best_chromosome
¶
get_best_chromosome() -> Chromosome
Get the chromosome with the highest fitness value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
None
|
|
required |
Returns:
| Type | Description |
|---|---|
Chromosome
|
The chromosome with the highest fitness value |
Source code in eso/ga/population.py
eso.ga.selection¶
Tournament selection. The only parameter is the tournament size t. Each call samples t chromosomes with replacement and returns the one with the highest fitness. Crossover requires two parents, so the operator is invoked twice for each crossover event.
SelectionOperator
¶
Initialize the SelectionOperator with a tournament size.
Source code in eso/ga/selection.py
select_parents
¶
select_parents(population: Population) -> list
Source code in eso/ga/selection.py
select_one_parent
¶
select_one_parent(population: Population)
Select a parent individual from the population using tournament selection.
:param population: The population from which to select a parent. :return: The best individual selected through tournament selection.
Source code in eso/ga/selection.py
eso.ga.operator¶
The genetic operators. mutate adds a bounded random delta to a gene's position, height, or both. crossover swaps a randomly chosen range of genes between two parents. reproduce copies a parent unchanged into the next generation. Rates are configured in GeneticOperatorConfig and must sum to one.
Created on Fri Oct 6 10:38:46 2023
@author: aaron-joel
GeneticOperator
¶
GeneticOperator(
band_height_fixed,
band_position_fixed,
spec_height,
crossover_rate,
mutation_rate,
reproduction_rate,
mutation_height_range,
mutation_position_range,
)
Initialize the GeneticOperator class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict
|
Configuration file for input setting. |
required |
Returns:
| Type | Description |
|---|---|
None
|
DESCRIPTION. |
Source code in eso/ga/operator.py
set_crossover_rate
¶
Set the crossover rate to the specified value of 'rate'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rate
|
float
|
Propotion of population that should be crossover. |
required |
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in eso/ga/operator.py
set_mutation_rate
¶
Set the mutation rate to the specified value of 'rate'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rate
|
float
|
Proportion of population that should be mutated. |
required |
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in eso/ga/operator.py
get_crossover_rate
¶
get_mutation_rate
¶
random_crossover
¶
random_crossover(parent1: Chromosome, parent2: Chromosome) -> tuple
Takes two individuals from the population and generates two offsprings by exchanging a random number of genes at randomly selected position in both individual chromosomes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent1
|
Chromosome
|
The first parent. |
required |
parent2
|
Chromosome
|
The second parent. |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
A tuple of offsprings (Chromosome, Chromosome). |
Source code in eso/ga/operator.py
crossover
¶
crossover(parent1: Chromosome, parent2: Chromosome)
Takes two individuals from the population and generates two offsprings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent1
|
Chromosome
|
The first parent. |
required |
parent2
|
Chromosome
|
The second parent. |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
A tuple of offsprings (Chromosome, Chromosome). |
Source code in eso/ga/operator.py
mutate
¶
mutate(chromosome: Chromosome) -> Chromosome
Randomly mutate the chromosome.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chromosome
|
Chromosome
|
Chromosome to be mutated. |
required |
Returns:
| Type | Description |
|---|---|
Chromosome
|
The mutated Chromosome. |
Source code in eso/ga/operator.py
reproduce
¶
reproduce(population: Population) -> Chromosome
Randomly selects an individual from the current population and moves it to the next generation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
population
|
Population
|
The current population to select from. |
required |
Returns:
| Type | Description |
|---|---|
Chromosome
|
The randomly selected chromosome. |