{"id":24380486,"url":"https://github.com/gorzechowski/typenetic","last_synced_at":"2026-04-29T03:31:18.979Z","repository":{"id":57383611,"uuid":"119036204","full_name":"gorzechowski/typenetic","owner":"gorzechowski","description":"Genetic algorithm library for Typescript","archived":false,"fork":false,"pushed_at":"2018-02-07T09:24:22.000Z","size":28,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-19T10:18:45.865Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/gorzechowski.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-01-26T10:02:49.000Z","updated_at":"2019-04-05T12:09:13.000Z","dependencies_parsed_at":"2022-09-14T00:21:20.643Z","dependency_job_id":null,"html_url":"https://github.com/gorzechowski/typenetic","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gorzechowski%2Ftypenetic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gorzechowski%2Ftypenetic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gorzechowski%2Ftypenetic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gorzechowski%2Ftypenetic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gorzechowski","download_url":"https://codeload.github.com/gorzechowski/typenetic/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243238970,"owners_count":20259126,"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":[],"created_at":"2025-01-19T08:19:30.320Z","updated_at":"2025-12-29T03:23:50.033Z","avatar_url":"https://github.com/gorzechowski.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Typenetic - Genetic algorithm for Typescript\n\n[![Build Status](https://travis-ci.org/gorzechowski/typenetic.svg?branch=master)](https://travis-ci.org/gorzechowski/typenetic)\n\n# Table of Contents\n\n* [Installation](#installation)\n* [Example usage](#example-usage)\n* [Decorators](#decorators)\n* [Genetic operators examples](#genetic-operators-examples)\n    * [Selection](#selection)\n        * [Tournament selection](#tournament-selection)\n    * [Crossover](#crossover)\n        * [Single point](#single-point)\n    * [Mutation](#mutation)\n\n# Installation\n\n1. Install module:\n\n    ```\n    npm install typenetic --save\n    ```\n\n2. You need to set these options in `tsconfig.json` file in your project:\n\n    ```json\n    {\n        \"emitDecoratorMetadata\": true,\n        \"experimentalDecorators\": true\n    }\n    ```\n\n# Example usage\n\n1. Create `GeneticOperators.ts` file:\n\n    ```typescript\n    import {Selection, Crossover, Mutation} from \"typenetic\";\n\n    export class GeneticOperators {\n\n        @Selection()\n        selection(population: Array\u003cany\u003e): Array\u003cany\u003e {\n            // perform elities selection\n\n            return elities;\n        }\n\n        @Crossover()\n        crossover(parentA: any, parentB: any): any {\n            // perform crossover\n\n            return offspring;\n        }\n\n        @Mutation()\n        mutation(offspring: any): any {\n            // perform mutation\n\n            return offspring;\n        }\n    }\n    ```\n\n2. Create `index.ts` file:\n\n    ```typescript\n    import {evolve} from \"typenetic\";\n    import {GeneticOperators} from \"./GeneticOperators\";\n\n    // create population\n    const population: Array\u003cany\u003e = [];\n\n    // evolve population\n    let evolved: Array\u003cany\u003e = evolve(population);\n    ```\n\n# Decorators\n\n| Signature                   | Description                            |\n|-----------------------------|----------------------------------------|\n| `@Selection(size?: number)` | Selection operator, which task is to select elities from population. If `size` provided, then decorated function result will be sliced: `result.slice(0, size)` (useful in eg. tournament selection). |\n| `@Crossover()`              | Crossover operator is responsible for crossing two units. |\n| `@Mutation()`  | Mutation operator randomly modify genes in the unit. |\n\n# Genetic operators examples\n\n## Selection\n\n### Tournament selection\n\nTournament selection chooses best units from population, based on unit's fitness.\n\n```typescript\nimport {Selection} from \"typenetic\";\n\nexport class GeneticOperators {\n\n    // select 3 elities from population\n    @Selection(3)\n    selection(population: Array\u003cany\u003e): Array\u003cany\u003e {\n        return population.sort((unitA: any, unitB: any) =\u003e {\n            return unitB.fitness - unitA.fitness;\n        });\n    }\n\n}\n```\n\n## Crossover\n\n### Single point\n\nIn single point crossover, one point, randomly chosen is used to split parents and create new unit with genes from first parent before chosen point and with genes from second parent, beyond chosen point.\n\n```typescript\nimport {Crossover} from \"typenetic\";\n\nexport class GeneticOperators {\n\n    @Crossover()\n    crossover(parentA: Array\u003cany\u003e, parentB: Array\u003cany\u003e): Array\u003cany\u003e {\n        const cutPoint = this.random(0, parentA.neurons.length - 1);\n\n        for (let i = cutPoint; i \u003c parentA.neurons.length; i++) {\n            let biasFromParentA = parentA.neurons[i].bias;\n\n            parentA.neurons[i].bias = parentB.neurons[i].bias;\n            parentB.neurons[i].bias = biasFromParentA;\n        }\n\n        return this.random(0, 1) === 1 ? parentA : parentB;\n    }\n\n    private random(min: number, max: number): number {\n        return Math.floor(Math.random() * (max - min + 1) + min);\n    };\n\n}\n```\n\n## Mutation\n\nMutation operator randolmy modify genes of units. The probability of mutation depends on mutation rate.\n\n```typescript\nimport {Mutation} from \"typenetic\";\n\nexport class GeneticOperators {\n\n    @Mutation()\n    mutation(offspring: Array\u003cany\u003e): Array\u003cany\u003e {\n        for (let i = 0; i \u003c offspring.neurons.length; i++) {\n            offspring.neurons[i].bias = this.mutate(offspring.neurons[i].bias);\n        }\n\n        for (let i = 0; i \u003c mutated.connections.length; i++) {\n            offspring.connections[i].weight = this.mutate(offspring.connections[i].weight);\n        }\n\n        return offspring;\n    }\n\n    private mutate(gene) {\n        // mutation rate at 0.5 (50%)\n        if (Math.random() \u003c 0.5) {\n            const mutateFactor = 1 + ((Math.random() - 0.5) * 3 + (Math.random() - 0.5));\n\n            return gene *= mutateFactor;\n        }\n\n        return gene;\n    }\n\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgorzechowski%2Ftypenetic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgorzechowski%2Ftypenetic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgorzechowski%2Ftypenetic/lists"}