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

evolutionary

v0.1.1

Published

A functional genetic algorithm library

Downloads

33

Readme

evolutionary Build Status

A (more) functional genetic algorithm library. Designed to make as few assumptions as possible about how you want your genetic algorithm library to run.

Warning: I wrote this in a day without tests in a kinda functional style. So it's probably buggy and not as performant as it could be. Use at your own risk.

Installing

npm install evolutionary

Usage

var Evolutionary = require('evolutionary')
var Select1 = require('evolutionary/select1')
var Select2 = require('evolutionary/select2')

// Initialize it the way you want
var evolve = Evolutionary({
  optimize: Math.max,
  select1: Select1.bestOf2,
  select2: Select2.bestOf2,
  mutate: x => x,
  crossover: (a, b) => [a, b],
  seed: Math.random,
  fitness: (a) => a * 2

  populationSize: 250,
  crossoverChance: 0.9,
  mutateChance: 0.2,
  fittestAlwaysSurvives: true
})

var population = evolve()
var nextPopulation = evolve(population)

// Population is sorted by fitness
var fittest = population[0]

// keep evolving to your heart's content

The only required option is seed and fitness. But this probably isn't going to be useful if you don't define at least mutate and crossover.

Options

seed (required)

A function that returns a random individual when called.

fitness (required)

A function that accepts an individual and returns that individual's fitness score.

optimize

A function that accepts two fitness scores, and returns whichever one is the better one. Defaults to Math.max.

mutate

A function that accepts an individual, and returns a mutated version of that individual. Defaults to x => x, so you should probably change it.

crossover

A function that accepts two parent individuals, and returns an array containing two children individuals created from those parents. Defaults to (a, b) => [a, b], so you should probably change it.

select1

A function that selects a single individual out of a population. See the Selection section below. Defaults to Select1.bestOf2.

select2

A function that selects a two individuals out of a population. See the Selection section below. Defaults to Select2.bestOf2.

populationSize

How many individuals there should be in the population. Defaults to 250.

crossoverChance

The chance for a crossover to happen. Defaults to 0.9.

mutationChance

The chance for a mutation to happen. Defaults to 0.2.

fittestAlwaysSurvives

Whether or not the fittest individual should always move on to the next generation. Defaults to true.

Selection

There are a number of selection behaviors pre-written if the default isn't what you want.

const Select1 = reqiure('evolutionary/select1')
const Select2 = reqiure('evolutionary/select2')

Select1.random

Selects a random individual from the population.

Select1.bestOf2

Picks two random individuals from the population, and selects the fitter one.

Select1.bestOf3

Picks three random individuals from the population, and selects the fittest one.

Select1.bestOfN(n)

Picks n random individuals from the population, and selects the fittest one.

Select1.fittest

Selects the fittest individual from the population.

Select2.random

Selects two random individuals from the population.

Select2.bestOf2

Selects two individuals using the Select1.bestOf2 selection behavior.

Select2.bestOf3

Selects two individuals using the Select1.bestOf3 selection behavior.

Select2.bestOfN(n)

Selects two individuals using the Select1.bestOfN(n) selection behavior.

Select2.fittestAndRandom

Selects the fittest individual and a random individual from the population.

Generator

This package also includes a convenience function to wrap your genetic algorithm as a generator:

const Evolutionary = require('evolutionary')
const makeGenerator = require('evolutionary/generator')

const evolve = Evolutionary({ ... })
const done = (pop) => pop[0] == desiredSolution
const generator = makeGenerator(evolve, done)

generator.next()
generator.next()
generator.next()
generator.next()

// ...