genetic-search
Version:
Multiprocessing genetic algorithm implementation library
301 lines (300 loc) • 12.6 kB
TypeScript
import type { BaseGenome, Population, BaseMutationStrategyConfig, MutationStrategyInterface, PhenomeStrategyInterface, FitnessStrategyInterface, GeneticSearchReferenceConfig, PhenomeStrategyConfig, GenerationPhenomeMatrix, PhenomeRow, GenerationFitnessColumn, PhenomeCacheInterface, SortStrategyInterface, SelectionStrategyInterface, EvaluatedGenome } from "./types";
/**
* Base class for mutation strategies.
*
* @template TGenome The type of genome objects in the population.
* @template TConfig The type of configuration for the mutation strategy.
*
* @category Strategies
* @category Mutation
*/
export declare abstract class BaseMutationStrategy<TGenome extends BaseGenome, TConfig extends BaseMutationStrategyConfig> implements MutationStrategyInterface<TGenome> {
/**
* The configuration for the mutation strategy.
*/
protected readonly config: TConfig;
/**
* Constructs a new instance of the mutation strategy.
*
* @param config The configuration for the mutation strategy.
*/
protected constructor(config: TConfig);
abstract mutate(genome: TGenome, newGenomeId: number): TGenome;
}
/**
* Base class for phenome strategies.
*
* @template TGenome The type of genome objects in the population.
* @template TConfig The type of configuration for the phenome strategy.
* @template TTaskConfig The type of configuration required to execute the task of the calculating phenome.
*
* @category Strategies
* @category Phenome
*/
export declare abstract class BasePhenomeStrategy<TGenome extends BaseGenome, TConfig extends PhenomeStrategyConfig<TTaskConfig>, TTaskConfig> implements PhenomeStrategyInterface<TGenome> {
/**
* The configuration for the phenome strategy.
*/
protected readonly config: TConfig;
/**
* Constructs a new instance of the phenome strategy.
*
* @param config The configuration for the phenome strategy.
*/
constructor(config: TConfig);
/**
* Collects and caches the phenome for a given population of genomes.
*
* @param population The population of genomes to collect phenome for.
* @param cache The cache used to store and retrieve phenomes.
* @returns A promise that resolves to a matrix of phenomes for the generation.
*/
collect(population: Population<TGenome>, cache: PhenomeCacheInterface): Promise<GenerationPhenomeMatrix>;
/**
* Executes the tasks to calculate the phenome of the given genomes.
*
* @param inputs The inputs for the tasks to execute.
* @returns A promise that resolves to an array of phenome for each genome.
*/
protected execTasks(inputs: TTaskConfig[]): Promise<GenerationPhenomeMatrix>;
/**
* Creates the task input required for calculating phenome for
* a given genome.
*
* @param genome The genome for which to create the task input.
* @returns The task configuration for the given genome.
*/
protected abstract createTaskInput(genome: TGenome): TTaskConfig;
}
/**
* A fitness strategy that calculates the fitness of a genome based on a reference loss.
*
* The fitness of a genome is calculated as the negative sum of the absolute differences between the reference loss
* and the loss calculated for the genome.
*
* @category Strategies
* @category Fitness
*/
export declare class ReferenceLossFitnessStrategy implements FitnessStrategyInterface {
/**
* The configuration for the reference loss fitness strategy.
*/
private readonly referenceConfig;
/**
* Constructor of the reference loss fitness strategy.
*
* @param referenceConfig The configuration for the reference loss fitness strategy.
*/
constructor(referenceConfig: GeneticSearchReferenceConfig);
score(results: GenerationPhenomeMatrix): GenerationFitnessColumn;
/**
* Formats the losses by normalizing the phenome matrix and applying weights to each row.
*
* @param results The generation phenome matrix to format.
* @returns A matrix of formatted losses.
*/
protected formatLosses(results: GenerationPhenomeMatrix): GenerationPhenomeMatrix;
/**
* Weighs a row of phenome by multiplying each element with its corresponding weight.
*
* @param result The genome phenome row to weigh.
* @returns A genome phenome row with applied weights.
*/
protected weighRow(result: PhenomeRow): PhenomeRow;
}
/**
* Sorts a given iterable of genomes, fitness scores, and phenome rows in ascending order of their fitness scores.
*
* @param input An iterable containing tuples of genomes, their fitness scores, and their associated phenome rows.
* @returns An array of sorted tuples of genomes, fitness scores, and phenome rows.
*
* @category Strategies
* @category Sorting
*/
export declare class AscendingSortingStrategy<TGenome extends BaseGenome> implements SortStrategyInterface<TGenome> {
/**
* Sorts a given iterable of genomes, fitness scores, and phenome rows in ascending order of their fitness scores.
*
* @param input An iterable containing tuples of genomes, their fitness scores, and their associated phenome rows.
* @returns An array of sorted tuples of genomes, fitness scores, and phenome rows.
*/
sort(input: Array<EvaluatedGenome<TGenome>>): Array<EvaluatedGenome<TGenome>>;
}
/**
* Sorts a given iterable of genomes, fitness scores, and phenome rows in descending order of their fitness scores.
*
* @param input An iterable containing tuples of genomes, their fitness scores, and their associated phenome rows.
* @returns An array of sorted tuples of genomes, fitness scores, and phenome rows.
*
* @category Strategies
* @category Sorting
*/
export declare class DescendingSortingStrategy<TGenome extends BaseGenome> implements SortStrategyInterface<TGenome> {
/**
* Sorts a given iterable of genomes, fitness scores, and phenome rows in descending order of their fitness scores.
*
* @param input An iterable containing tuples of genomes, their fitness scores, and their associated phenome rows.
* @returns An array of sorted tuples of genomes, fitness scores, and phenome rows.
*/
sort(input: Array<EvaluatedGenome<TGenome>>): Array<EvaluatedGenome<TGenome>>;
}
/**
* A random selection strategy.
*
* This selection strategy randomly selects parents for mutation and crossover.
*
* @template TGenome The type of genome objects in the population.
*
* @category Strategies
* @category Selection
*/
export declare class RandomSelectionStrategy<TGenome extends BaseGenome> implements SelectionStrategyInterface<TGenome> {
protected readonly crossoverParentsCount: number;
/**
* Constructor of the random selection strategy.
*
* @param crossoverParentsCount The number of parents to select for crossover.
*/
constructor(crossoverParentsCount: number);
/**
* Selects parents for crossover.
*
* @param input The population extended with fitness scores and phenome to select parents from.
* @param count The number of parents to select.
* @returns An array of parents arrays.
*/
selectForCrossover(input: Array<EvaluatedGenome<TGenome>>, count: number): Array<TGenome[]>;
/**
* Selects parents for mutation.
*
* @param input The population extended with fitness scores and phenome to select parents from.
* @param count The number of parents to select.
* @returns An array of parents.
*/
selectForMutation(input: Array<EvaluatedGenome<TGenome>>, count: number): TGenome[];
}
/**
* A truncation selection strategy.
*
* This selection strategy selects the top `sliceThresholdRate` fraction of the population
* as parents for mutation and crossover.
*
* @template TGenome The type of genome objects in the population.
*
* @category Strategies
* @category Selection
*/
export declare class TruncationSelectionStrategy<TGenome extends BaseGenome> extends RandomSelectionStrategy<TGenome> {
protected readonly sliceThresholdRate: number;
/**
* Constructor of the truncation selection strategy.
*
* @param crossoverParentsCount The number of parents to select for crossover.
* @param sliceThresholdRate The rate at which to slice the population.
*/
constructor(crossoverParentsCount: number, sliceThresholdRate: number);
/**
* Selects parents for crossover.
*
* @param input The population extended with fitness scores and phenome to select parents from.
* @param count The number of parents to select.
* @returns An array of parents arrays.
*/
selectForCrossover(input: Array<EvaluatedGenome<TGenome>>, count: number): Array<TGenome[]>;
/**
* Selects parents for mutation.
*
* @param input The population extended with fitness scores and phenome to select parents from.
* @param count The number of parents to select.
* @returns An array of parents.
*/
selectForMutation(input: Array<EvaluatedGenome<TGenome>>, count: number): TGenome[];
}
/**
* A selection strategy that uses a tournament to select parents.
*
* This selection strategy runs a tournament between random participants from the population,
* and selects the best participant as a parent.
*
* @template TGenome The type of genome objects in the population.
*
* @category Strategies
* @category Selection
*/
export declare class TournamentSelectionStrategy<TGenome extends BaseGenome> implements SelectionStrategyInterface<TGenome> {
protected readonly crossoverParentsCount: number;
protected readonly tournamentSize: number;
/**
* Constructor of the tournament selection strategy.
*
* @param crossoverParentsCount The number of parents to select for crossover.
* @param tournamentSize The number of participants in a tournament.
*/
constructor(crossoverParentsCount: number, tournamentSize: number);
/**
* Selects parents for crossover.
*
* @param input The population extended with fitness scores and phenome to select parents from.
* @param count The number of parents to select.
* @returns An array of parents arrays.
*/
selectForCrossover(input: Array<EvaluatedGenome<TGenome>>, count: number): Array<TGenome[]>;
/**
* Selects parents for mutation.
*
* @param input The population extended with fitness scores and phenome to select parents from.
* @param count The number of parents to select.
* @returns An array of parents.
*/
selectForMutation(input: Array<EvaluatedGenome<TGenome>>, count: number): TGenome[];
/**
* Conducts a tournament and returns the best participant.
*
* @param input The population.
* @returns The best `EvaluatedGenome` from the tournament.
*/
private runTournament;
}
/**
* A proportional selection strategy.
*
* This selection strategy selects parents for mutation and crossover based on their fitness proportion.
*
* @template TGenome The type of genome objects in the population.
*
* @category Strategies
* @category Selection
*/
export declare class ProportionalSelectionStrategy<TGenome extends BaseGenome> implements SelectionStrategyInterface<TGenome> {
protected crossoverParentsCount: number;
/**
* Constructor of the proportional selection strategy.
*
* @param crossoverParentsCount The number of parents to select for crossover.
*/
constructor(crossoverParentsCount: number);
/**
* Selects parents for crossover using proportional selection.
*
* @param input The population extended with fitness scores and phenome to select parents from.
* @param count The number of parents to select.
* @returns An array of parents arrays.
*/
selectForCrossover(input: Array<EvaluatedGenome<TGenome>>, count: number): Array<TGenome[]>;
/**
* Selects parents for mutation using proportional selection.
*
* @param input The population extended with fitness scores and phenome to select parents from.
* @param count The number of parents to select.
* @returns An array of parents.
*/
selectForMutation(input: Array<EvaluatedGenome<TGenome>>, count: number): TGenome[];
/**
* Helper method to select an individual based on fitness proportion.
*
* @param input The population.
* @param totalFitness The sum of all fitness scores in the population.
* @returns A selected `EvaluatedGenome`.
*/
private selectByFitness;
}