{"id":13520460,"url":"https://github.com/coin-unknown/async-genetic","last_synced_at":"2026-01-23T15:29:31.927Z","repository":{"id":37485296,"uuid":"256233354","full_name":"coin-unknown/async-genetic","owner":"coin-unknown","description":"A blazing fast and fully async genetic algorithm","archived":false,"fork":false,"pushed_at":"2025-06-26T14:27:27.000Z","size":358,"stargazers_count":33,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-10-22T18:56:33.153Z","etag":null,"topics":["genetic","genetic-algorithm","node","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/coin-unknown.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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,"zenodo":null}},"created_at":"2020-04-16T14:08:45.000Z","updated_at":"2025-10-09T23:30:12.000Z","dependencies_parsed_at":"2022-08-23T15:50:36.046Z","dependency_job_id":"39b2e03f-ba30-4ec4-b1cf-a4ce37d08de2","html_url":"https://github.com/coin-unknown/async-genetic","commit_stats":{"total_commits":128,"total_committers":4,"mean_commits":32.0,"dds":0.4140625,"last_synced_commit":"d0797c21bd3abe42954702436e02a9031a77abdb"},"previous_names":["coin-unknown/async-genetic","businessduck/async-genetic"],"tags_count":50,"template":false,"template_full_name":null,"purl":"pkg:github/coin-unknown/async-genetic","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coin-unknown%2Fasync-genetic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coin-unknown%2Fasync-genetic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coin-unknown%2Fasync-genetic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coin-unknown%2Fasync-genetic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/coin-unknown","download_url":"https://codeload.github.com/coin-unknown/async-genetic/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coin-unknown%2Fasync-genetic/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28694564,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-23T14:15:13.573Z","status":"ssl_error","status_checked_at":"2026-01-23T14:09:05.534Z","response_time":59,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["genetic","genetic-algorithm","node","typescript"],"created_at":"2024-08-01T05:02:21.430Z","updated_at":"2026-01-23T15:29:31.909Z","avatar_url":"https://github.com/coin-unknown.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# Blazing fast Genetic Algorithm\n\n**Async Genetic** its crossplatform implementation of genetic algorithms. It's pretty asyncronous and use `Promises`. Genetic algorithms allow solving problems such as game balance optimization, solving equations, creating visual effects, optimizing system parameters, and others.\n\n\u003cimg src=\"./.github/logo.png\" width=\"500\" /\u003e\n\n# Abstract\n\nGenetic Algorithm (GA) is one of the most well-regarded evolutionary algorithms in the history. This algorithm mimics Darwinian theory of survival of the fittest in nature. This chapter presents the most fundamental concepts, operators, and mathematical models of this algorithm. The most popular improvements in the main component of this algorithm (selection, crossover, and mutation) are given too. The chapter also investigates the application of this technique in the field of image processing. In fact, the GA algorithm is employed to reconstruct a binary image from a completely random image.\n\n# Island Model\n\nThe simulation model of the behavior of population settlement on islands helps to create species diversity. On the islands, the degree of mutation and isolation of the population from the main part allows the creation of local dominant genes.\n\nIn the local implementation of this model, the mainland is also used to cross all populations. You can manually manipulate the population migrations to the mainland and islands as often as you like.\n\n\u003cimg src=\"./.github/island.png\" width=\"400\" /\u003e\n\n## Installation\n\nReleases are available under Node Package Manager (npm):\n\n    npm install async-genetic\n\n## Examples\n\n**Gnetic guess text phrase**\n\n[Classic Model Test](./test/genetic.ts)\n[Island Model Test](./test/island.ts)\n\n![Genetic console](./.github/genetic-classic-console.png)\n## How to use\n\n### GeneticAlgorithm constructor\n```js\nimport { Genetic } from 'async-genetic';\n\nconst config = {...};\nconst population = [...];\nconst genetic = new Genetic(config);\nawait genetic.seed(population);\n\n```\nThe minimal configuration for constructing an GeneticAlgorithm calculator is like so:\n\n```js\nconst config = {\n    mutationFunction: (phenotype: T) =\u003e Promise\u003cT\u003e; // you custom mutation fn\n    crossoverFunction: (a: T, b: T) =\u003e Promise\u003cArray\u003cT\u003e\u003e; // you custom crossover fn\n    fitnessFunction: (phenotype: T, isLast: boolean) =\u003e Promise\u003c{ fitness: number, state?: any }\u003e; // // you custom fitness fn\n    randomFunction: () =\u003e Promise\u003cT\u003e; // you custom random phenotype generator fn\n    populationSize: number; // constant size of population\n    mutateProbablity?: number; // perturb prob random phenotype DNA\n    crossoverProbablity?: number; // crossover prob\n    fittestNSurvives?: number; // good old boys, fittest are not crossing in current generation\n    select1?: (pop) =\u003e T; // Select one phenotype by Selection method e.g. Select.Random or Select.Fittest\n    select2?: (pop) =\u003e T; // Select for crossover by Selection method e.g. Select.Tournament2 or Select.Tournament3\n    deduplicate?: (phenotype: T) =\u003e boolean; // Remove duplicates (not recommended to use)\n}\n\nconst settings = {...};\nconst population = [...];\nconst genetic = new Genetic(config);\n```\n\nThat creates one instance of an GeneticAlgorithm calculator which uses the initial configuration you supply.  All configuration options are optional except *population*.  If you don't specify a crossover function then GeneticAlgorithm will only do mutations and similarly if you don't specify the mutation function it will only do crossovers.  If you don't specify either then no evolution will happen, go figure.\n\n### genetic.estimate( )\nEstimate current generation by fitnessFunction\n```js\nawait geneticalgorithm.estimate( )\n```\nThe *.estimate()* add score number per each phenotype in population\n### genetic.breed(); \n```js\nasync function solve() {\n    await genetic.seed(); // filled by random function or passed pre defined population T[]\n\n    for (let i = 0; i \u003c= GENERATIONS; i++) {\n        console.count('gen');\n        await genetic.estimate(); // estimate i generation\n        await genetic.breed(); // breed (apply crossover or mutations)\n\n        const bestOne = genetic.best()[0]; // get best one\n        console.log(bestOne);\n\n        if (bestOne.entity === solution) {\n            break;\n        }\n    }\n}\n```\nto do two evolutions and then get the best N phenoTypes with scores (see *.scoredPopulation(N)* below).\n\n### genetic.best(N)\nRetrieve the Phenotype with the highest fitness score like so. You can get directly N best scored items\n```js\nconst best = genetic.best(1)\n// best = [{...}];\n```\n\n# Functions\nThis is the specification of the configuration functions you pass to GeneticAlgorithm\n\n### mutationFunction(phenotype)\n\u003e Must return a phenotype\n\nThe mutation function that you provide.  It is a synchronous function that mutates the phenotype that you provide like so:\n```js\nasync function mutationFunction (oldPhenotype) {\n\tvar resultPhenotype = {}\n\t// use oldPhenotype and some random\n\t// function to make a change to your\n\t// phenotype\n\treturn resultPhenotype\n}\n```\n\n### crossoverFunction (phenoTypeA, phenoTypeB)\n\u003e Must return an array [] with 2 phenotypes\n\nThe crossover function that you provide.  It is a synchronous function that swaps random sections between two phenotypes.  Construct it like so:\n```js\nasync function crossoverFunction(phenoTypeA, phenoTypeB) {\n\tvar result = {}\n\t//  result should me created by merge phenoTypeA and phenoTypeB in custom rules\n\treturn result;\n}\n```\n\n###  fitnessFunction (phenotype) [async]\n\u003e Must return a promise with number\n\n```js\nasync function fitnessFunction(phenotype) {\n\tvar fitness = 0\n\t// use phenotype and possibly some other information\n\t// to determine the fitness number.  Higher is better, lower is worse.\n\treturn { fitness, state: { foo: 'bar' } };\n}\n```\n\n### crossoverFunction (phenotypeA, phenotypeB)\n\u003e Must return childs phenotypes after breeding phenotypeA and phenotypeB\n\n```js\nasync function crossoverFunction(mother: string, father: string) {\n    // two-point crossover\n    const len = mother.length;\n    let ca = Math.floor(Math.random() * len);\n    let cb = Math.floor(Math.random() * len);\n    if (ca \u003e cb) {\n\t\t[ca, cb] = [cb, ca];\n    }\n\n    const son = father.substr(0, ca) + mother.substr(ca, cb - ca) + father.substr(cb);\n    const daughter = mother.substr(0, ca) + father.substr(ca, cb - ca) + mother.substr(cb);\n\n    return [son, daughter];\n}\n```\n\n### Configuring\n\u003e Next T - is your custom phenotype\n\n| Parameter  | Type | Description |\n| ------------- | ------------- | ------------- |\n| mutationFunction | (phenotype: T) =\u003e Promise\u003cT\u003e  | Mutate you phenotype as you describe  |\n| crossoverFunction | (a: T, b: T) =\u003e Promise\u003cArray\u003cT\u003e\u003e | Cross two different phenotypes in to once (merge)  |\n| fitnessFunction | (phenotype: T) =\u003e Promise\u003cnumber\u003e | Train you phenotype to get result (scores more - better) |\n| randomFunction | () =\u003e Promise\u003cT\u003e | Function generate random phenotype to complete the generation |\n| populationSize | number | Number phenotypes in population |\n| mutateProbablity | number [0...1] | Each crossover may be changed to mutation with this chance |\n| fittestNSurvives | number [0...population.length -1] | Each generation fittest guys will survive |\n| select1 | Select | select one phenotype from population for mutate or cloning |\n| select2 | Select | select two or more phenotype from population for crossing over |\n| optimize | (a: T, b:T) =\u003e boolean  | order function for popultaion |\n| deduplicate | boolean | Remove duplicates from phenotypes |\n\n\n### Selection method\n\u003e Should be used for select1, select2 parameters\n\n| Type | Description |\n| ------------- | ------------- |\n| Select.Random | Select random phenotype from population |\n| Select.RandomLinear | Select random phenotype from population |\n| Select.Fittest | Select best one phenotype from population |\n| Select.FittestLinear | Select linear best one phenotypes from population |\n| Select.Tournament2 | Select 2 random phenotypes from population and take best of 2 |\n| Select.Tournament3 | Select 3 random phenotype from population and take best of 3|\n| Select.RandomLinearRank | Select random phenotype from population with linear rank |\n| Select.Sequential | Select phenotype from population by linear function |\n\n\n# Island Model \n\nIsland model have absolutely same interface with classic genetic.\n\n```typescript\n// Use Island model imports\nimport { IslandGeneticModel, IslandGeneticModelOptions, Migrate, GeneticOptions } from 'async-genetic';\n\n// Island configuration\nconst islandOptions: IslandGeneticModelOptions\u003cstring\u003e = {\n    islandCount: 8, // count of islands\n    islandMutationProbability: 0.8, // mutation on island are different from continental\n    islandCrossoverProbability: 0.8, // same for crossover, because island area are small\n    migrationProbability: 0.1, // migration to another island chance\n    migrationFunction: Migrate.FittestLinear, // select migrated phenotype\n};\n\n// Move to continent after each 50 generations\nconst continentBreedAfter = 50;\n// How many generations to breed at continent left\nlet continentGenerationsCount = 0;\n\nconst genetic = new IslandGeneticModel\u003cstring\u003e(islandOptions, geneticOptions);\nawait genetic.seed();\n\nfor (let i = 0; i \u003c= GENERATIONS; i++) {\n    if (log) {\n        console.count('gen');\n    }\n\n    if (i !== 0 \u0026\u0026 i % continentBreedAfter === 0) {\n        // Move to continent\n        genetic.moveAllToContinent();\n        // Setup next 10 generations to breed at continent\n        continentGenerationsCount = 10;\n    }\n\n    if (continentGenerationsCount) {\n        // Reduce continent generations\n        continentGenerationsCount--;\n\n        // If continent generations over, move to islands\n        if (continentGenerationsCount === 0) {\n            // Move to islands\n            genetic.migrateToIslands();\n        }\n    }\n\n    // Estimate on island or continent, by configuration\n    await genetic.estimate();\n\n    const bestOne = genetic.best()[0];\n\n    if (log) {\n        console.log(`${bestOne.entity} - ${bestOne.fitness}`);\n    }\n\n    await genetic.breed();\n\n    if (bestOne.entity === solution) {\n        return i;\n    }\n}\n\n```\n\n### Migration method\n\u003e Should be used for selection Phenotype and move to another island (migrate)\n\n| Type | Description |\n| ------------- | ------------- |\n| Migrate.Random | Select random phenotype from population |\n| Migrate.RandomLinearelect random phenotype from population |\n| Migrate.Fittest | Select best one phenotype from population |\n| Migrate.FittestLinear | Select linear best one phenotypes from population |\n\n\n\n```javascript\n// Move to continent, islands has no populations after that\ngenetic.moveAllToContinent();\n// Split population and move to islands (each island got same of total population part)\ngenetic.migrateToIslands();\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoin-unknown%2Fasync-genetic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcoin-unknown%2Fasync-genetic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoin-unknown%2Fasync-genetic/lists"}