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 🙏

© 2025 – Pkg Stats / Ryan Hefner

genetic.ts

v2.0.0

Published

Simple and clear JavaScript class for genetic algorithms. Contains helpful functions to get you going.

Readme

Genetic.ts

A simple yet powerful and hackable Genetic Algorithm library. Handles your parent finding, crossover and mutation. Contains also some helpful functions to get you quickly started.


installation

As a module:

npm i genetic.ts
# or
yarn add genetic.ts

For browser:

<script src="https://cdn.jsdelivr.net/npm/genetic.ts/dist/genetic.web.js"></script>

usage

See examples. Source code can be found in docs/.

import * as genetic from 'genetic.ts' /* import the library, this object will be available globally if imported through HTML */

const population = [
  {
    dna: [1, 2, 4],
    fitness() {
      return this.dna.reduce((a, b) => a + b)
    }
  },
  {
    dna: [4, 4, 8],
    fitness() {
      return this.dna.reduce((a, b) => a + b)
    }
  },
  {
    dna: [11, 3, 7],
    fitness() {
      return this.dna.reduce((a, b) => a + b)
    }
  }
]

/* create your genetic object */
const ga = new genetic.Instance({
  population /* set your population */,
  mutationFunction: genetic.chance(
    genetic.add(-0.5, 0.5)
  ) /* add mutation function */,
  modes: {
    crossover:
      genetic.CrossoverModes.clone /* overwrite default modes with enums */
  }
})

/* All Genetic's methods are chainable */
ga.findParents() /* finds parents using the passed mode */
  .crossover() /* creates new genes using the passed mode */
  .mutate() /* mutates the genes using the passed function */
  .finishGeneration() /* Overwrites your population's dna and increments the generation counter */

/* or use the `nextGeneration` method to do the above all at once */
ga.nextGeneration()

configuration

The genetic.Instance class accepts a configuration object in the constructor. Genetic instance will follow the same structure. Here's the object it accepts with its defaults (those that do not have a default require a value to be passed):

  • population: array containing your members that satisfy the IPopMember interface
  • mutationFunction: function to be used when mutating the genes
  • mutationRate: mutation rate of the algorithm (default: 0.1)
  • amountOfParents: amount of parents to be chosen from the mating pool (default: 2)
  • modes: object containing properties specifying the modes:
    • parentsSelection: method of choosing the parents (default: 'best')
    • crossover: method of crossing parents' genes (default: 'random')
  • preserveParents: preservation of parents' dna in the new generation. If you set this to true and have modes.parentsSelection = 'best' you will ensure the next generations wont get worse (default: false)
  • keepHistory: saves history of parents of every generation in this.history

population

A population is considered correct when:

interface IPopMember<DNA> {
  fitness(): number
  dna: DNA
}
  • it is an array
  • each element in the array is an object implementing IPopMember:
    • contains a fitness method that returns the fitness (number)
    • contains a dna property:
      • can be any data structure as long as it ends with a number
      • the structure is the same for every member in the array

If you're unsure whether your population is correct you can always use genetic.validatePopulation(pop) that will throw an error if something is wrong.


modes

Parent selection modes:

methods of choosing the parents

best: takes members with highest fitness scores

probability: selects members based on their fitness scores that will correspond to the chance of being chosen

probability2: selects members based on their fitness scores squared that will correspond to the chance of being chosen

probability3: selects members based on their fitness scores cubed that will correspond to the chance of being chosen

Crossover modes:

method of crossing parents' genes

random: randomly choosing a parent for each gene

average: averaging all parents' dna

clone: randomly selecting a parent and cloning his dna


mutating

A mutation function accepts data about the current gene and will return a number that will be added to the gene.

A mutation function is considered correct when:

type MutationFunction = (mutationRate: number) => number
  • will accept a mutationRate
  • will return a number

history

If enabled history is stored under ga.history. It is saved each time ga.findParents() is called. It is an array of arrays of all previous parents with their fitness and dna:

type HistoryRecord<DNA> = {
  fitness: number
  dna: DNA
}[]

premade mutation functions

Genetic.ts provides some pre-made functions for mutations:

chance

If you'd like to mutate only some properties (based on the mutation rate) wrap your function in chance(yourFunction), like so:

const mutFunc = chance(mRate => 2 * mRate)

add

If you'd like to mutate values by some random number in a range use add(min, max):

const mutFunc = add(-0.3, 0.3) /* min inclusive, max exclusive */