npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

genometrics

v2.0.1

Published

Lightweight Nodejs package for genetic algorithms <3

Downloads

12

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 your genome. opt is a little trickier. It's an array of objects that'll define the rules in how Genometrics will create your random genome. 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