https://github.com/nathsou/darwin
Flexible genetic algorithm implementation in TypeScript.
https://github.com/nathsou/darwin
algorithms darwin evolution genetic genetic-algorithm typescript
Last synced: 7 months ago
JSON representation
Flexible genetic algorithm implementation in TypeScript.
- Host: GitHub
- URL: https://github.com/nathsou/darwin
- Owner: nathsou
- Created: 2016-12-24T06:06:44.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2023-03-04T03:05:02.000Z (over 2 years ago)
- Last Synced: 2024-10-19T09:07:43.929Z (12 months ago)
- Topics: algorithms, darwin, evolution, genetic, genetic-algorithm, typescript
- Language: TypeScript
- Homepage: https://nathsou.github.io/Darwin/SmartEaters/
- Size: 1.24 MB
- Stars: 28
- Watchers: 2
- Forks: 4
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[![NPM Package][npm]][npm-url]
[![Build Size][build-size]][build-size-url]# Darwin
Flexible genetic algorithm implementation in TypeScript.
## Installation
```bash
npm install charles.darwin
```Darwin is compatible with ES6 modules
### Demos
- [Smart Eaters](https://nathsou.github.io/Darwin/SmartEaters/) inspired by [Mat Buckland](http://www.ai-junkie.com/ann/evolved/nnt1.html)
- [Typing Monkeys](https://nathsou.github.io/Darwin/TypingMonkeys/) inspired by [Daniel Shiffman](http://natureofcode.com/book/chapter-9-the-evolution-of-code/)
- [Traveling Salesman](https://nathsou.github.io/Darwin/TSP/)
- [Text Deblurring](https://nathsou.github.io/Darwin/DeblurText/)## Usage
One must first choose a way of encoding the desired behavior as an array of some type (a chromosome) and of evaluating the fitness of any such chromosome.
For instance the 'Typing Monkeys' demo tries to evolve towards a target string, the fitness is simply the number of correct characters present in the randomly generated string.
On the other hand, the 'Smart Eaters' demo evolves the weights and biases of an artificial neural network, the fitness of an 'Eater' is the number of nutriments it eats in one generation (2000 ticks).
```typescript
const population = new Darwin({
populationSize: number,
chromosomeLength: number,
randomGene: () => T,
crossoverRate?: number = 0.7,
mutationRate?: number = 1 / populationSize,
crossoverMethod?: CrossoverFunction = crossoverMethod.singlePoint,
mutationMethod?: MutationFunction = mutationMethod.flip,
eliteCount?: number = Math.ceil(populationSize / 25),
eliteCopies?: number = 1,
randomNumber?: () => number = Math.random
});
```The constructor of the Darwin class initializes a population of random chromosomes, we can now assign a score (or fitness) to each of those chromosomes based on their genes represented by an array of the type returned by the randomGene function :
```typescript
for (const chromo of population.getPopulation()) {
chromo.setFitness(evalFitness(chromo.getGenes()));
}// shorthand:
population.updateFitness(genes => evalFitness(genes));
```A new generation can then be generated by calling the mate() method:
```typescript
population.mate();
```To observe the evolution of the population, one can call:
```typescript
const {
fittest,
fittestIndex,
averageFitness,
totalFitness
} = population.updateStats();
```[npm]: https://img.shields.io/npm/v/charles.darwin
[npm-url]: https://www.npmjs.com/package/charles.darwin
[build-size]: https://badgen.net/bundlephobia/minzip/charles.darwin
[build-size-url]: https://bundlephobia.com/result?p=charles.darwin