SimpleGA (1) Genetic Algorithms in Javascript/Node.js

Two week ago, I was writing:

a Simple Genetic Algorithm base framework, that supports population, evaluation and mutator operators (I should add crossover operators). The base algorithm is based on

A Genetic Algorithm Tutorial (pdf)

You can create a population of genotypes, each one with an evaluation function. The library evaluates each genotypes and prepares another population, based on the value of each genotype, copying the best ones, and mutating some of the initial specimen based on fitness. The fitness value is v = fi / fa, where fi is the individual fitness evaluation, and fa is the fitness average. The integer part of v determines the count of copies of an individual that survives to the next generation. The fraction part is the probability of having a mutated copy in the next generation set of genotypes:

var newpopulation = [];

for (var k = 0; k < l; k++) {
	if (values[k] < 0)
	var fitness = values[k] / total;
	if (fitness < 0)
	var ntimes = Math.floor(fitness);
	var fraction = fitness - ntimes;
	for (var j = 0; j < ntimes; j++)
	if (fraction > 0 && Math.random() <= fraction)

if (mutators && mutators.length > 0) {
	l = newpopulation.length;
	var lm = mutators.length;
	for (k = 0; k < l; k++) {
		var mutator = mutators[Math.floor(Math.random() * lm)];
		newpopulation[k] = mutator.mutate(newpopulation[k]);

(I should add crossover operator support). There is a sample implementing the Travelling Saleman Problem

running in console using

It can run from the console, or you can launch a local web page:

I wrote client/server version (web page at browser, Node.js server program) and a distributed version (the evaluation of many population running in many server nodes, coordinated by a Node.js server, showing results in a browser).

Next steps: add crossover operators, sample short descriptions, more samples.

Keep tuned!

Angel “Java” Lopez

3 thoughts on “SimpleGA (1) Genetic Algorithms in Javascript/Node.js

  1. Pingback: Genetic Algorithms: Links And Resources (2) « Angel ”Java” Lopez on Blog

  2. Pingback: Working on AjTalk: Smalltalk VM, C# version « Angel ”Java” Lopez on Blog

  3. Pingback: Trabajando de nuevo en on AjTalk: Smalltalk Virtual Machine, en C# - Angel "Java" Lopez

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s