Genetic algorithm library for Typescript
npm install typenetic
* Installation
* Example usage
* Decorators
* Genetic operators examples
* Selection
* Tournament selection
* Crossover
* Single point
* Mutation
1. Install module:
```
npm install typenetic --save
2. You need to set these options in tsconfig.json file in your project:
`json`
{
"emitDecoratorMetadata": true,
"experimentalDecorators": true
}
1. Create GeneticOperators.ts file:
`typescript
import {Selection, Crossover, Mutation} from "typenetic";
export class GeneticOperators {
@Selection()
selection(population: Array
// perform elities selection
return elities;
}
@Crossover()
crossover(parentA: any, parentB: any): any {
// perform crossover
return offspring;
}
@Mutation()
mutation(offspring: any): any {
// perform mutation
return offspring;
}
}
`
2. Create index.ts file:
`typescript
import {evolve} from "typenetic";
import {GeneticOperators} from "./GeneticOperators";
// create population
const population: Array
// evolve population
let evolved: Array
`
| Signature | Description |
|-----------------------------|----------------------------------------|
| @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). |@Crossover()
| | Crossover operator is responsible for crossing two units. |@Mutation()
| | Mutation operator randomly modify genes in the unit. |
Tournament selection chooses best units from population, based on unit's fitness.
`typescript
import {Selection} from "typenetic";
export class GeneticOperators {
// select 3 elities from population
@Selection(3)
selection(population: Array
return population.sort((unitA: any, unitB: any) => {
return unitB.fitness - unitA.fitness;
});
}
}
`
In 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.
`typescript
import {Crossover} from "typenetic";
export class GeneticOperators {
@Crossover()
crossover(parentA: Array
const cutPoint = this.random(0, parentA.neurons.length - 1);
for (let i = cutPoint; i < parentA.neurons.length; i++) {
let biasFromParentA = parentA.neurons[i].bias;
parentA.neurons[i].bias = parentB.neurons[i].bias;
parentB.neurons[i].bias = biasFromParentA;
}
return this.random(0, 1) === 1 ? parentA : parentB;
}
private random(min: number, max: number): number {
return Math.floor(Math.random() * (max - min + 1) + min);
};
}
`
Mutation operator randolmy modify genes of units. The probability of mutation depends on mutation rate.
`typescript
import {Mutation} from "typenetic";
export class GeneticOperators {
@Mutation()
mutation(offspring: Array
for (let i = 0; i < offspring.neurons.length; i++) {
offspring.neurons[i].bias = this.mutate(offspring.neurons[i].bias);
}
for (let i = 0; i < mutated.connections.length; i++) {
offspring.connections[i].weight = this.mutate(offspring.connections[i].weight);
}
return offspring;
}
private mutate(gene) {
// mutation rate at 0.5 (50%)
if (Math.random() < 0.5) {
const mutateFactor = 1 + ((Math.random() - 0.5) * 3 + (Math.random() - 0.5));
return gene *= mutateFactor;
}
return gene;
}
}
``