genetic-search
v5.0.0
Published
Multiprocessing genetic algorithm implementation library
Downloads
1,505
Maintainers
Readme
Multiprocessing Genetic Algorithm Implementation for TypeScript
Setup
npm i genetic-search
Usage example
Let's get a max value of the parabola: y = -(x-12)^2 - 3
.
import type {
GeneticSearchConfig,
GeneticSearchStrategyConfig,
} from "genetic-search";
import {
GeneticSearch,
IdGenerator,
SimpleMetricsCache,
} from "genetic-search";
const config: GeneticSearchConfig = {
populationSize: 100,
survivalRate: 0.5,
crossoverRate: 0.5,
};
const strategies: GeneticSearchStrategyConfig<ParabolaArgumentGenome> = {
populate: new ParabolaPopulateStrategy(),
metrics: new ParabolaMultiprocessingMetricsStrategy({
poolSize: 4,
task: async (data) => [-((data[0] - 12)**2) - 3],
onTaskResult: () => void 0,
}),
fitness: new ParabolaMaxValueFitnessStrategy(),
mutation: new ParabolaMutationStrategy(),
crossover: new ParabolaCrossoverStrategy(),
cache: new SimpleMetricsCache(),
}
const search = new GeneticSearch(config, strategies, new IdGenerator());
expect(search.partitions).toEqual([50, 25, 25]);
await search.fit({
generationsCount: 100,
beforeStep: () => void 0,
afterStep: () => void 0,
});
const bestGenome = search.bestGenome;
console.log('Best genome:', bestGenome);
Strategies implementation:
import type {
BaseGenome,
BaseMutationStrategyConfig,
CrossoverStrategyInterface,
FitnessStrategyInterface,
GenerationFitnessColumn,
GenerationMetricsMatrix,
IdGeneratorInterface,
MultiprocessingMetricsStrategyConfig,
PopulateStrategyInterface,
} from "genetic-search";
import {
BaseMultiprocessingMetricsStrategy,
BaseMutationStrategy,
} from "genetic-search";
export type ParabolaArgumentGenome = BaseGenome & {
id: number;
x: number;
}
export type ParabolaTaskConfig = [number];
export class ParabolaPopulateStrategy implements PopulateStrategyInterface<ParabolaArgumentGenome> {
populate(size: number, idGenerator: IdGeneratorInterface<ParabolaArgumentGenome>): ParabolaArgumentGenome[] {
const result: ParabolaArgumentGenome[] = [];
for (let i=0; i<size; ++i) {
result.push({ id: idGenerator.nextId(), x: Math.random() * 200 - 100 });
}
return result;
}
}
export class ParabolaMutationStrategy extends BaseMutationStrategy<ParabolaArgumentGenome, BaseMutationStrategyConfig> {
constructor() {
super({ probability: 1 });
}
mutate(genome: ParabolaArgumentGenome, newGenomeId: number): ParabolaArgumentGenome {
return { x: genome.x + Math.random() * 10 - 5, id: newGenomeId };
}
}
export class ParabolaCrossoverStrategy implements CrossoverStrategyInterface<ParabolaArgumentGenome> {
cross(lhs: ParabolaArgumentGenome, rhs: ParabolaArgumentGenome, newGenomeId: number): ParabolaArgumentGenome {
return { x: (lhs.x + rhs.x) / 2, id: newGenomeId };
}
}
export class ParabolaMultiprocessingMetricsStrategy extends BaseMultiprocessingMetricsStrategy<ParabolaArgumentGenome, MultiprocessingMetricsStrategyConfig<ParabolaTaskConfig>, ParabolaTaskConfig> {
protected createTaskInput(genome: ParabolaArgumentGenome): ParabolaTaskConfig {
return [genome.x];
}
}
export class ParabolaMaxValueFitnessStrategy implements FitnessStrategyInterface {
score(results: GenerationMetricsMatrix): GenerationFitnessColumn {
return results.map((result) => result[0]);
}
}
Unit testing
npm i
npm run test
License
Genetic Search TS is licensed under the MIT License.