{"id":21874510,"url":"https://github.com/smoren/genetic-search-ts","last_synced_at":"2025-04-15T01:23:58.460Z","repository":{"id":260595424,"uuid":"881785194","full_name":"Smoren/genetic-search-ts","owner":"Smoren","description":"Multiprocessing genetic algorithm implementation library","archived":false,"fork":false,"pushed_at":"2025-01-27T08:25:12.000Z","size":888,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-28T13:21:13.432Z","etag":null,"topics":["evolutionary-algorithms","genetic-algorithm","genetic-programming","genetics","heuristic-search-algorithms","multiprocessing","multiprocessing-library","search-algorithm"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Smoren.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-01T08:15:37.000Z","updated_at":"2025-02-24T14:44:55.000Z","dependencies_parsed_at":"2024-11-15T16:38:30.099Z","dependency_job_id":null,"html_url":"https://github.com/Smoren/genetic-search-ts","commit_stats":null,"previous_names":["smoren/genetic-search-ts"],"tags_count":53,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Smoren%2Fgenetic-search-ts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Smoren%2Fgenetic-search-ts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Smoren%2Fgenetic-search-ts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Smoren%2Fgenetic-search-ts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Smoren","download_url":"https://codeload.github.com/Smoren/genetic-search-ts/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248986689,"owners_count":21194098,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["evolutionary-algorithms","genetic-algorithm","genetic-programming","genetics","heuristic-search-algorithms","multiprocessing","multiprocessing-library","search-algorithm"],"created_at":"2024-11-28T07:12:33.450Z","updated_at":"2025-04-15T01:23:58.453Z","avatar_url":"https://github.com/Smoren.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Multiprocessing Genetic Algorithm Implementation for TypeScript\n\n[![npm](https://img.shields.io/npm/v/genetic-search.svg)](https://www.npmjs.com/package/genetic-search)\n[![npm](https://img.shields.io/npm/dm/genetic-search.svg?style=flat)](https://www.npmjs.com/package/genetic-search)\n[![Coverage Status](https://coveralls.io/repos/github/Smoren/genetic-search-ts/badge.svg?branch=master\u0026rand=222)](https://coveralls.io/github/Smoren/genetic-search-ts?branch=master)\n![Build and test](https://github.com/Smoren/genetic-search-ts/actions/workflows/test.yml/badge.svg)\n[![Minified Size](https://badgen.net/bundlephobia/minzip/genetic-search)](https://bundlephobia.com/result?p=genetic-search)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\n![Logo](docs/images/logo.png)\n\nOverview\n--------\n\nThis project provides a TypeScript implementation of a genetic algorithm that can be used for optimization problems. The algorithm is designed to be highly customizable and can be used for a wide range of applications.\n\nFeatures\n--------\n\n* **Multiprocessing support**: The algorithm can be run in parallel using multiple processes, making it suitable for large-scale optimization problems.\n* **Deep customization**: The algorithm can be customized by providing custom implementations for various components:\n  * **base parameters** (population size, survival rate, crossover rate);\n  * **strategies** (population, phenome, fitness, sorting, selection, mutation, crossover, caching);\n  * **scheduler** for dynamic tuning of all the macro parameters.\n* **Separation of phenome metrics and fitness calculation**: The algorithm separates the computation of phenomes\n  and fitness values, allowing normalization and other operations to be applied to the phenomes of all genomes prior\n  to evaluating the fitness function.\n* **Type-safe**: The project uses TypeScript, which provides type safety and helps catch errors at compile-time.\n\nSetup\n-----\n\nFor single process (including browser apps) use:\n```bash\nnpm i genetic-search\n```\n\nFor multiprocessing in node environment use:\n```bash\nnpm i genetic-search-multiprocess\n```\n\nUsage example\n-------------\n\nLet's get a max value of the parabola: `y = -(x-12)^2 - 3`.\n\n```typescript\nimport type {\n  GeneticSearchConfig,\n  GeneticSearchStrategyConfig,\n} from \"genetic-search\";\nimport {\n  GeneticSearch,\n  SimplePhenomeCache,\n  DescendingSortingStrategy,\n  RandomSelectionStrategy,\n} from \"genetic-search\";\n\nconst config: GeneticSearchConfig = {\n  populationSize: 100,\n  survivalRate: 0.5,\n  crossoverRate: 0.5,\n};\n\nconst strategies: GeneticSearchStrategyConfig\u003cParabolaArgumentGenome\u003e = {\n  populate: new ParabolaPopulateStrategy(),\n  phenome: new ParabolaMultiprocessingPhenomeStrategy({\n    poolSize: 4,\n    task: async (data) =\u003e [-((data[0] - 12)**2) - 3],\n    onTaskResult: () =\u003e void 0,\n  }),\n  fitness: new ParabolaMaxValueFitnessStrategy(),\n  sorting: new DescendingSortingStrategy(),\n  selection: new RandomSelectionStrategy(2),\n  mutation: new ParabolaMutationStrategy(),\n  crossover: new ParabolaCrossoverStrategy(),\n  cache: new SimplePhenomeCache(),\n}\n\nconst search = new GeneticSearch(config, strategies);\n\nexpect(search.partitions).toEqual([50, 25, 25]);\n\nawait search.fit({\n  generationsCount: 100,\n  beforeStep: () =\u003e void 0,\n  afterStep: () =\u003e void 0,\n});\n\nconst bestGenome = search.bestGenome;\nconsole.log('Best genome:', bestGenome);\n```\n\nStrategies implementation:\n\n```typescript\nimport type {\n  BaseGenome,\n  BaseMutationStrategyConfig,\n  CrossoverStrategyInterface,\n  FitnessStrategyInterface,\n  GenerationFitnessColumn,\n  GenerationPhenomeMatrix,\n  IdGeneratorInterface,\n  PopulateStrategyInterface,\n} from \"genetic-search\";\nimport type { MultiprocessingPhenomeStrategyConfig } from \"genetic-search-multiprocess\";\nimport { BaseMutationStrategy } from \"genetic-search\";\nimport { MultiprocessingPhenomeStrategyConfig } from \"genetic-search-multiprocess\";\n\nexport type ParabolaArgumentGenome = BaseGenome \u0026 {\n  id: number;\n  x: number;\n}\n\nexport type ParabolaTaskConfig = [number];\n\nexport class ParabolaPopulateStrategy implements PopulateStrategyInterface\u003cParabolaArgumentGenome\u003e {\n  populate(size: number, idGenerator: IdGeneratorInterface\u003cParabolaArgumentGenome\u003e): ParabolaArgumentGenome[] {\n    const result: ParabolaArgumentGenome[] = [];\n    for (let i=0; i\u003csize; ++i) {\n      result.push({ id: idGenerator.nextId(), x: Math.random() * 200 - 100 });\n    }\n    return result;\n  }\n}\n\nexport class ParabolaMutationStrategy extends BaseMutationStrategy\u003cParabolaArgumentGenome, BaseMutationStrategyConfig\u003e {\n  constructor() {\n    super({ probability: 1 });\n  }\n\n  mutate(genome: ParabolaArgumentGenome, newGenomeId: number): ParabolaArgumentGenome {\n    return { x: genome.x + Math.random() * 10 - 5, id: newGenomeId };\n  }\n}\n\nexport class ParabolaCrossoverStrategy implements CrossoverStrategyInterface\u003cParabolaArgumentGenome\u003e {\n  cross(parents: ParabolaArgumentGenome[], newGenomeId: number): ParabolaArgumentGenome {\n    const [lhs, rhs] = parents;\n    return { x: (lhs.x + rhs.x) / 2, id: newGenomeId };\n  }\n}\n\nexport class ParabolaMultiprocessingPhenomeStrategy extends BaseMultiprocessingPhenomeStrategy\u003cParabolaArgumentGenome, MultiprocessingPhenomeStrategyConfig\u003cParabolaTaskConfig\u003e, ParabolaTaskConfig\u003e {\n  protected createTaskInput(genome: ParabolaArgumentGenome): ParabolaTaskConfig {\n    return [genome.x];\n  }\n}\n\nexport class ParabolaMaxValueFitnessStrategy implements FitnessStrategyInterface {\n  score(results: GenerationPhenomeMatrix): GenerationFitnessColumn {\n    return results.map((result) =\u003e result[0]);\n  }\n}\n```\n\nApi Reference\n-------------\n\nFor detailed documentation and usage examples, please refer to:\n* [Main API documentation](https://smoren.github.io/genetic-search-ts/)\n* [Multiprocess API documentation](https://smoren.github.io/genetic-search-multiprocess-ts/)\n\nUnit testing\n------------\n\n```bash\nnpm i\nnpm run test\n```\n\nLicense\n-------\n\nGenetic Search TS is licensed under the MIT License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmoren%2Fgenetic-search-ts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsmoren%2Fgenetic-search-ts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmoren%2Fgenetic-search-ts/lists"}