genetic-search
Version:
Multiprocessing genetic algorithm implementation library
275 lines (274 loc) • 10.3 kB
TypeScript
import type { BaseGenome, EvaluatedGenome, GenerationFitnessColumn, GenerationPhenomeMatrix, PhenomeRow, GroupedStatSummary, IdGeneratorInterface, Population, RangeStatSummary, StatSummary, ArrayManagerInterface } from "./types";
/**
* Generates unique identifiers for genomes.
*
* @template TGenome The type of genome objects in the population.
*
* @category Utils
*/
export declare class IdGenerator<TGenome extends BaseGenome> implements IdGeneratorInterface<TGenome> {
private id;
nextId(): number;
reset(population: TGenome[]): void;
}
/**
* Manages an array of `T` objects.
*
* @template T The type of the objects to manage.
*/
export declare class ArrayManager<T> implements ArrayManagerInterface<T> {
protected readonly _data: T[];
/**
* Creates a new `ArrayManager` from an array of `T` objects.
*
* @param data The array of `T` objects to manage.
*/
constructor(data: T[]);
/**
* Updates elements of the managed array that match the given filter.
*
* @param filter A function that returns true if the element should be updated.
* @param update A function that updates the element.
*
* @returns The updated items.
*/
update(filter: (genome: T) => boolean, update: (genome: T) => void): T[];
/**
* Removes elements of the managed array that match the given filter and optionally sorts the rest of the array.
*
* @param filter A function that returns true if the element should be removed.
* @param maxCount The maximum number of elements to remove.
* @param order The order to sort the remaining elements.
*
* @returns The removed items.
*/
remove(filter: (genome: T) => boolean, maxCount?: number, order?: 'asc' | 'desc'): T[];
}
/**
* Creates a deep copy of an object.
*
* @template T The type of the object to copy.
* @param obj The object to copy.
* @returns A deep copy of the object.
*
* @category Utils
*/
export declare const fullCopyObject: <T extends Record<string, any>>(obj: T) => T;
/**
* Rounds a number to a given precision.
*
* @param value The number to round.
* @param precision The precision to round to.
* @returns The rounded number.
*
* @category Utils
*/
export declare function round(value: number, precision: number): number;
/**
* Generates an array of a given length, filled with a specified value.
*
* @template T The type of the values in the array.
* @param length The length of the array to generate.
* @param value The value to fill the array with.
* @returns An array of the specified length, filled with the specified value.
*
* @category Utils
*/
export declare function createFilledArray<T>(length: number, value: T): T[];
/**
* Calculates the sum of an array of numbers.
*
* @param input The array of numbers to sum.
* @returns The sum of the input array.
*
* @category Utils
*/
export declare function arraySum(input: number[]): number;
/**
* Calculates the mean of an array of numbers.
*
* @param input The array of numbers to calculate the mean of.
* @returns The mean of the input array.
*
* @category Utils
*/
export declare function arrayMean(input: number[]): number;
/**
* Calculates the median of a sorted array of numbers.
*
* @param sortedInput The sorted array of numbers to find the median of.
* @returns The median value of the input array.
*
* @category Utils
*/
export declare function arrayMedian(sortedInput: number[]): number;
/**
* Applies a binary operator to two arrays of the same length.
*
* @template T The type of the values in the arrays.
* @param lhs The left-hand side array.
* @param rhs The right-hand side array.
* @param operator The binary operator to apply.
* @returns A new array with the result of applying the operator to each pair of values.
*
* @category Utils
*/
export declare function arrayBinaryOperation<T>(lhs: Array<T>, rhs: Array<T>, operator: (lhs: T, rhs: T) => T): Array<T>;
/**
* Returns a random element from the input array.
*
* @template T The type of the values in the array.
* @param input The array to select a random element from.
* @returns A random element from the input array.
*
* @category Utils
*/
export declare function getRandomArrayItem<T>(input: T[]): T;
/**
* Normalizes an array of numbers to the range from -1 to 1, where the `reference` value is mapped to 0.
*
* @param input The array of numbers to normalize.
* @param reference The reference value to map to 0.
* @returns The normalized array of numbers.
*
* @category Utils
*/
export declare function normalizePhenomeRow(input: PhenomeRow, reference: number): PhenomeRow;
/**
* Normalizes the columns of a matrix of phenome to the range from -1 to 1, where the `reference` value is mapped to 0.
*
* @param input The matrix of phenome to normalize.
* @param reference The reference value to map to 0.
* @returns The normalized matrix of phenome.
*
* @category Utils
*/
export declare function normalizePhenomeMatrixColumns(input: GenerationPhenomeMatrix, reference: PhenomeRow): GenerationPhenomeMatrix;
/**
* Normalizes the columns of a matrix of phenome to the range from -1 to 1, where the `reference` value is mapped to 0.
*
* @param matrix The matrix of phenome to normalize.
* @param reference The reference value to map to 0.
* @param abs Whether to take the absolute value of the normalized values.
* @returns The normalized matrix of phenome.
*
* @category Utils
*/
export declare function normalizePhenomeMatrix(matrix: GenerationPhenomeMatrix, reference: PhenomeRow, abs?: boolean): GenerationPhenomeMatrix;
/**
* Creates an empty `StatSummary` object.
*
* A `StatSummary` object contains the count of genomes in the population,
* as well as the best, second best, mean, median, and worst values.
* This function initializes a `StatSummary` with all values set to zero.
*
* @returns An initialized `StatSummary` object with all fields set to zero.
*
* @category Utils
*/
export declare function createEmptyStatSummary(): StatSummary;
/**
* Creates an empty `GroupedStatSummary` object.
*
* A `GroupedStatSummary` object contains a summary of the statistics of a population of genomes,
* grouped by origin into three categories: initial, crossover, and mutation.
* This function initializes a `GroupedStatSummary` with all values set to zero.
*
* @returns An initialized `GroupedStatSummary` object with all fields set to zero.
*
* @category Utils
*/
export declare function createEmptyGroupedStatSummary(): GroupedStatSummary;
/**
* Creates an empty `RangeStatSummary` object.
*
* A `RangeStatSummary` object contains the minimum, mean, and maximum values
* for a set of numerical data.
* This function initializes a `RangeStatSummary` with all values set to zero.
*
* @returns An initialized `RangeStatSummary` object with all fields set to zero.
*
* @category Utils
*/
export declare function createEmptyRangeStatSummary(): RangeStatSummary;
/**
* Calculates a summary of the statistics for a sorted array of numbers.
*
* The summary includes the count of numbers in the array,
* as well as the best, second best, mean, median, and worst values.
*
* @param sortedSource The sorted array of numbers.
* @returns A summary of the statistics for the array of numbers.
*
* @category Utils
*/
export declare function calcStatSummary(sortedSource: number[]): StatSummary;
/**
* Calculates a summary of the statistics for a sorted array of numbers.
*
* The summary includes the minimum, mean, and maximum values
* for a set of numerical data.
*
* @param source The array of numbers.
* @returns A summary of the statistics for the array of numbers.
*
* @category Utils
*/
export declare function calcRangeStatSummary(source: number[]): RangeStatSummary;
/**
* Rounds the fields of a StatSummary object to a given precision.
*
* @param summary The StatSummary object to round.
* @param precision The number of decimal places to round to.
* @returns A new StatSummary object with rounded fields.
*
* @category Utils
*/
export declare function roundStatSummary(summary: StatSummary, precision: number): StatSummary;
/**
* Rounds the fields of a GroupedStatSummary object to a given precision.
*
* This function rounds the statistics in each category of the GroupedStatSummary
* (initial, crossover, mutation) to the specified number of decimal places.
*
* @param summary The GroupedStatSummary object to round.
* @param precision The number of decimal places to round to.
* @returns A new GroupedStatSummary object with rounded fields.
*
* @category Utils
*/
export declare function roundGroupedStatSummary(summary: GroupedStatSummary, precision: number): GroupedStatSummary;
/**
* Rounds the fields of a RangeStatSummary object to a given precision.
*
* This function rounds the minimum, mean, and maximum values of the RangeStatSummary
* to the specified number of decimal places.
*
* @param summary The RangeStatSummary object to round.
* @param precision The number of decimal places to round to.
* @returns A new RangeStatSummary object with rounded fields.
*
* @category Utils
*/
export declare function roundRangeStatSummary(summary: RangeStatSummary, precision: number): RangeStatSummary;
/**
* Creates an array of `EvaluatedGenome` objects from a population and its fitness and phenome.
*
* @param population The population of genomes.
* @param fitnessColumn The fitness column of the population.
* @param phenomeMatrix The phenome matrix of the population.
* @returns An array of EvaluatedGenome objects.
*
* @category Utils
*/
export declare function createEvaluatedPopulation<TGenome extends BaseGenome>(population: Population<TGenome>, fitnessColumn: GenerationFitnessColumn, phenomeMatrix: GenerationPhenomeMatrix): Array<EvaluatedGenome<TGenome>>;
/**
* Extracts the population, fitness column, and phenome matrix from an array of
* [[EvaluatedGenome]] objects.
*
* @param input The array of [[EvaluatedGenome]] objects.
* @returns An array of three elements: the population, fitness column, and phenome matrix.
*
* @category Utils
*/
export declare function extractEvaluatedPopulation<TGenome extends BaseGenome>(input: Array<EvaluatedGenome<TGenome>>): [Population<TGenome>, GenerationFitnessColumn, GenerationPhenomeMatrix];