Genetic JS is a port of GeneticSharp done in TypeScript.
Genetic JS attempts to be a fast, extensible, multi-platform and multi-threading JavaScript Genetic Algorithm library that simples the development of applications using Genetic Algorithm.
Credits to the original implementation goes to the author of Genetic Sharp which serves as the entire basis of this project.
Currently the project is ongoing
and there will be plans to release a npm module with instructions on how to run it.
The current landscape of Genetic Algorithm libraries are vast, however this library aims to provide a simple way in which various selection and crossover criteria can be introduced. This library also aims to provide implementations of the vastly different crossover, mutation and other criteria that exist.
The easiest way to start using the library is by importing the module.
<script type="module" src="./geneticjs.umd.min.mjs"></script>
After that you can use the module by
The global namespace for geneticjs is geneticjs
const gjs = geneticjs;
const ga = new gjs.GeneticAlgorithm();
Before you start, you will need to define your own Chromosome. There are several chromosomes predefined for common problems.
- Defining your chromosome.
class MyProblemChromosome extends ChromosomeBase {
constructor() {
super(10);
this.createGenes();
}
public generateGene(geneIndex: number): Gene {
// Fill in how to generate the gene
}
createNew(): IChromosome {
return new MyProblemChromosome();
}
}
- Provide a fitness function. By default the fitness will be maximized.
const fitnessFunction = (chromosome: IChromosome): number => {
let genes = chromosome.getGenes();
let fitness = 0;
for (let i = 0; i < genes.length; i++) {
fitness += parseInt(genes[i].mValue.toString());
}
return fitness;
};
If you would prefer the fitness to be minimized, you can choose tho calculate the fitness via (1 / 1 - fitness).
- Running your GA
var selection = new EliteSelection();
var crossover = new OrderedCrossover();
var mutation = new ReverseSequenceMutation();
var fitness = new MyProblemFitness();
var chromosome = new MyProblemChromosome();
var population = new Population(50, 70, chromosome);
var ga = new GeneticAlgorithm(
population,
fitness,
selection,
crossover,
mutation
);
ga.start();
- Function maximization
- NQueen Problem
- Documentation of usage with example code
- Full port of GeneticJS
- Node.js support
- Browser support
- Require.js support
- Universal Module Definition
- Multi-threading