genometrics
v2.0.1
Published
Lightweight Nodejs package for genetic algorithms <3
Downloads
1
Maintainers
Readme
Genometrics
Lightweight genetic algorithm helper for NodeJS.
Installing
You can install this package with NPM!npm install genometrics
A quick primer on Populations and Citizens
- Each Population comes with its own
members
array. That's where your Citizens live so be careful with it! - Each Citizen comes with its own
genome
array. That's quite literally your Citizen so be super careful with it!!! genomes
can consist of floats and strings. They can be whatever data you want!- Citizens also have a
fitness
value assigned to them, this can be any float greater than zero.
Getting Started
Citizens and Populations are the main building blocks of your genetic algorithm. This is how you initiate them!
const { Citizen, Population } = require('genometrics');
let genome = [/* Your array values here. You must at minimum pass in an empty array. */];
let citizen = new Citizen(genome);
let population = new Population();
population.members.push(citizen);
It's that easy!
You can also initiate random genomes
like this:
let citizen = new Citizen([]);
citizen.randomGenome();
That'll give your Citizen a random default genome
to work with! Default genomes
have two values: a number and a string.
The number will be a float in the range [0, 1)
and the string will be a random alpha-numeric
string of length 8
.
If you want to initiate random genomes
your way though, you'll need to supply two parameters:\
- genomeLength
- opt
genomeLength
is simply the amount of values you want in yourgenome
.opt
is a little trickier. It's an array of objects that'll define the rules in how Genometrics will create your randomgenome
. Here's how to create it:
[
{
type: 'number',
min: 0,
max: 1
},
{
type: 'string',
len: 8,
chars: 'abcdefghijklmnopqrstuvwxyz'
}
]
And so on for each item. type
will always be number
or string
too.
This package also includes a nice little character set helper for the chars
setting. Its usage is covered below.
You can also initiate whole random Populations as well, instead of going through Citizens one by one:
/* genomeLength and opt are optional params, just like they are with Citizens. */
const population = new Population(populationSize, genomeLength, opt);
Social science is the easiest science! :+1:
Healthy Citizens are Happy Citizens
Each Citizen comes with its own fitness
value, set at 1
by default when they're born.
However, this package doesn't include a default function to calculate fitness
, that part's up to you!
Citizen.prototype.calculateFitness = function(){
/* Your fitness logic here! */
this.fitness = /* Some value in the range (0, +inf). */
}
Calling citizen.calculateFitness()
will automagically update its fitness
value. If only going to the gym were this easy in real life!
The Future is Now Old Man
Now that your Citizens are in tip top shape, let's see who has the chops to make it in the real world!
Luckily for us, progressing to the next generation is super easy:
population.selection();
population.crossover();
population.mutation(rate, citizen => {
/* Your mutation logic here. */
citizen.genome[0] = /* Some value. */
citizen.genome[1] = /* Some value. */
/* ... */
});
And that's it! selection()
and crossover()
handle everything for you, but you'll have to supply your own mutation()
logic. mutation()
passes in your Citizens automatically in its callback though, so you don't have to worry about that part!
Also, rate should be a float between [0, 1]
, but you've probably figured that out. Looks like you're pretty fit too!
Memories Last a Lifetime
Want to take a snapshot of your members
array? Or maybe bring back the good old times? You don't need a time machine for this one, a simple function call on your Population will do!
population.exportToJSON(filename);
/* ... */
population2.importFromJSON(filename);
Random-Number-String
Remember that character set helper mentioned earlier? That's part of the random-number-string
package, also made by me!
It's just a nice little thing that provides cryptographically secure random generators for numbers and strings, but it also has a character set object that can make declaring your opt
arrays a bit easier.
Installing
const { charSet, randFlt, randInt, randStr } = require('random-number-string');
You don't need to include every object if you don't want to use them, but I've used them all here for your reference!
You can check out the full details of this package here