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

genemo

v1.3.4

Published

Simple to use genetic algorithms library. Supports both NodeJS and web browsers.

Downloads

30

Readme

GeneMO - Genetic Algorithm Library

Build Status Coverage Status npm version MIT Licence

Simple to use genetic algorithm library. It enables fast prototyping by providing a number of universal genetic operators (for selection, crossover, mutation, etc.) and easy customization. Supports both NodeJS and web browsers.

Table of Contents

Installation Example Usage Getting Started API Reference How To Contribute

Installation

npm i genemo

Example Usage

Full examples with comments can be found in the ./examples directory. Here is a shorter version:

const Genemo = require('genemo');

Genemo.run({
  generateInitialPopulation: Genemo.generateInitialPopulation({
    generateIndividual, // Here, provide a function which generates an individual
    size: 200,
  }),
  selection: Genemo.selection.roulette({ minimizeFitness: false }),
  reproduce: Genemo.reproduce({
    crossover: Genemo.crossover.singlePoint(),
    mutate: Genemo.mutation.transformRandomGene(Genemo.mutation.flipBit()),
    mutationProbability: 0.02,
  }),
  evaluatePopulation: Genemo.evaluatePopulation({ fitnessFunction }), // You need to provide your own fitness function
  stopCondition: Genemo.stopCondition({ maxIterations: 100 }),
}).then(({ evaluatedPopulation }) => {
  // ...
}).catch(console.error);

Example of using GeneMO in the browser environment without blocking browser's main thread can be found in genemo-web-demo repository.

Getting Started

A single most important element of GeneMO library is a Genemo.run function. It runs a genetic algorithm by executing a number of user-specified functions, like for example: generation of initial population, selection, fitness function, etc.

By providing a rich collection of universal genetic operators, GeneMO lets you build an initial version of your genetic algorithm very quickly. Then you can refine your program by gradually replacing GeneMO's universal genetic operators with custom, problem specific operators.

Usually, it is enough to implement a custom function for generating random individual/solution and a fitness function. Rest of the required functions can be taken from the GeneMO library. However, keep in mind that problem specific operators usually give better results.

Read API Reference for detailed description of all the options required by Genemo.run. See Example Usage to quickly get familiar with basic usage of GeneMO.

Genemo.run(options).then(result => {
  // ...
});

Genemo.run returns a promise, which resolves to an object:

{ evaluatedPopulation: EvaluatedPopulation, iteration: number, logs: object }

It contains information about the population (along with fitness values) from the final iteration, number of iterations and an object with logs (mostly with performance data).

Asynchronous execution

Each function passed to Genemo.run (selection, reproduce, fitness, etc.) can return a Promise (synchronous functions work as well). From time to time Genemo.run runs next iteration asynchronously to avoid blocking browser's main thread. However, if a single iteration takes too long to complete, it will still block the main thread. To control the frequency of asynchronous iteration executions, use maxBlockingTime option.

Multithreading

Currently, GeneMO does not have a lot of functionalities for multithreading built-in, but thanks to its highly flexible architecture, you can easily implement multithreading on your own. In the ./examples/example3-parallelExecution directory there is a full example of a genetic algorithm, which performs fitness function and crossover operation in parallel processes, using NodeJS child_process module.

Usually, the most computationaly intensive elements of a genetic algorithm are fitness function, crossover and mutation. evaluatePopulation parameter of Genemo.run is a function, which maps an array of individuals to an array of their fitness values. You can divide this array into a few chunks and evaluate each of them in parallel. To do the same with crossover and mutation, you can use reproduceBatch instead of reproduce. Instead of calling the crossover function for each pair of parents, it calls it only once, passing an array of pairs as an argument. Same with mutation - it is called only once with an array of individuals which should be mutated. Thanks to that, you can divide those arrays into chunks an process them in parallel.

I plan to add to GeneMO more utilities that make implementing multithreading easier, but I don't want to include any code which is strictly dependend on the environment. Therefore, things like WebWorkers or NodeJS child_process are not likely to appear in the library.

API Reference

GeneMO exports a Genemo object with properties listed in the hierarchy below. Full description of each property can be found in API.md.

How To Contribute

If you found a bug, you have a feature request, or you just have a question, please open a new issue on Github. You can also contact me on Twitter (@lukaszjenczmyk). Linter and tests are configured to run on the CI (and locally with Husky). I appreciate your input :heart: