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

genetic-lib

v1.1.0

Published

General-purpose Genetic Algorithm library

Downloads

14

Readme

genetic-lib

General-purpose Genetic Algorithm library.

Define your genetic functions, and I will do the rest.

genetic-lib allows you to focus on the implementation of your genetic algorithm, without having to worry about running and controlling the simulation.

Installation

npm install genetic-lib

Highlights

  • Written in ES6, transpiled to ES5.
  • Supports asynchronous seed, fitness and notification functions.
  • Supports pausing and stopping the simulation.
  • Configurable, e.g. number of mutation iterations, number of fittest entities to select for reproduction.
  • Passes data to the notification function after each generation.

Usage

import Genetic from 'genetic-lib';

// (Define relevant functions to be used in the settings object)

const settings = {
  seed, // [Function, required] Genetic function to create an entity
  mutate, // [Function, required] Genetic function to apply mutation to an entity
  crossover, // [Function, required] Genetic function to apply crossover to an entity
  fitness, // [Function, required] Genetic function to calculate the fitness of an entity
  notification, // [function, required] Called after every generation (unless specified otherwise in the `skip` setting) with the stats
  isFinished, // [Function, required] Called with the stats. Should return a Boolean, which is the result of the condition to end the simulation, e.g, `return stats.generation >= 500`
  init, // [Function, optional] Will be called when genetic-lib has initialised and ready to start the simulation. Use this if you need to do some initialisation before starting the simulation
  onFinished, // [Function, optional] Will be called with the stats when the simulation has completed
  populationSize: 100 // [Number, optional, default: 20]
  mutationIterations: 1, // [Number, optional, default: 1] How many times to run the mutation function on each entity
  skip: 5, // [Number - 1 or more, optional, default: 1] Number of generations to skip in calling the notification function, e.g. `5` means the notification function would be called every 5 generations
  optimise: 'min', // [String - 'min'/'max', optional, default: 'max'] Whether the fittest entity is defined as the one with the lowest score (min) or highest score (max)
  initialFitness: 1111, // [Number, optional, default: 0] Initial fitness to assign to entity, before measuring its fitness
  numberOfFittestToSelect: 4, // [Number, optional, default: 2] The number of fittest entities to select for reproduction from each generation
  shouldKillTheWeak: true // [Boolean, optional, default: false] Whether or not to kill the weak entities after the fittest have been identified. This will only work if your method to identify the fittest entities is based on shortest-time-based, meaning the entities that completed the task the most quickly are the fittest, and thus there is no need to measure the fitness of the rest of the entities in the generation, as they won't be selected and are thus irrelevant.
}

const genetic = new Genetic(settings)
genetic.solve()

Stats

The stats is an object passed to the notification, isFinished and onFinished settings functions. It contains the following data:

  • population: [Array] the entities in the current generation
  • generation: [Number] generation number, 0-based
  • mean: [Number] mean fitness
  • fittestEver: [Object] the fittest entity so far
  • isFinished: [Boolean] is simulation finished

Public methods

solve()

Runs the simulation

getMeanFitness()

Returns a number representing the mean fitness of the current generation

setFittestEver({ DNA, fitness, generation })

Allows to manually set the fittest entity so far

stop()

Stops the simulation

togglePaused()

Pauses/resumes the simulation

Example projects using this library

  • Genetic Fly in Maze:

    • Repo: https://github.com/liranh85/genetic-fly-in-maze
    • Demo: http://www.liran.co.uk/ga/genetic-fly-in-maze
  • Genetic String Solver:

    • Repo: https://github.com/liranh85/genetic-string-solver
    • Demo: http://www.liran.co.uk/ga/genetic-string-solver