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 🙏

© 2026 – Pkg Stats / Ryan Hefner

ts-genetic

v1.0.3

Published

A flexible genetic algorithm implementation in TypeScript

Downloads

13

Readme

ts-genetic

A flexible genetic algorithm implementation in TypeScript that can be used to solve various optimization problems.

Source code is available on GitHub.

Installation

npm install ts-genetic

Usage

The package provides a flexible GeneticAlgorithm class that can be configured with custom operators for different optimization problems.

Basic Example

import { GeneticAlgorithm } from 'ts-genetic';

const config = {
    populationSize: 100,
    mutationRate: 0.01,
    generationLimit: 1000,
    elitismCount: 5
};

// target is to find a number as close to 0.5 as possible
const geneticAlgorithm = new GeneticAlgorithm(config, {
    createGene: () => Math.random(),
    mutateGene: (gene) => gene + (Math.random() - 0.5) * 0.1,
    calculateFitness: (individual) => {
        // Calculate fitness based on your problem
        return 1 / (1 + Math.abs(individual.genes[0] - 0.5));
    },
    isTerminationConditionMet: (population, generation) => {
        const bestFitness = Math.max(...population.map(ind => ind.fitness));
        return bestFitness > 0.99;
    }
});

const result = await geneticAlgorithm.evolve(1);
const population = result.population.sort((a, b) => b.fitness - a.fitness);

console.log('Best solution:', population[0].genes[0]);

Real-World Example: Sudoku Solver

See it here: Sudoku Solver Example.

To see a more complex example, check out the example folder in the repository. It contains a Sudoku solver that uses the genetic algorithm to solve Sudoku puzzles.

cd example
npm install
npm run dev

API Reference

GeneticAlgorithm

The main class for implementing genetic algorithms.

Constructor

constructor(config: GeneticConfig, operators: GeneticOperators<T>)

GeneticConfig

interface GeneticConfig {
  populationSize: number;    // Size of the population in each generation
  mutationRate: number;      // Probability of mutation (0-1)
  generationLimit: number;   // Maximum number of generations
  elitismCount: number;      // Number of best individuals to preserve
}

GeneticOperators

interface GeneticOperators<T> {
  // Required operators
  calculateFitness: (individual: Individual<T>) => Promise<number> | number;
  isTerminationConditionMet: (population: Individual<T>[], generation: number) => Promise<boolean> | boolean;

  // Optional operators - provide either the high-level or low-level operators
  createIndividual?: (length: number) => Promise<Individual<T>> | Individual<T>;
  crossover?: (parent1: Individual<T>, parent2: Individual<T>) => Promise<Individual<T>> | Individual<T>;
  mutate?: (individual: Individual<T>, mutationRate: number) => Promise<Individual<T>> | Individual<T>;

  // Low-level operators (used if high-level ones are not provided)
  createGene?: () => Promise<T> | T;
  mutateGene?: (gene: T) => Promise<T> | T;
}

Methods

async evolve(
  individualLength: number,
  onGenerationComplete?: (generation: number, bestFitness: number) => void
): Promise<{
  population: Individual<T>[];
  generations: number;
}>

License

MIT