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

evolve-ga

v2.0.1

Published

Genetic Algorithm

Downloads

3

Readme

Build Status Coverage Status npm

Evolve - Genetic Algorithm

Simple implementation of a Genetic Algorithm.

Installation

Install it with NPM:

npm i evolve-ga

Download from the releases section and include it on a webpage:

<script src="dist/evolve-ga.iife.min.js"></script>

Include it via CDN:

<script src="https://unpkg.com/[email protected]/dist/evolve-ga.iife.min.js"></script>

Usage

import { evolve } from "evolve-ga";
const algorithm = evolve(config);
evolve.run();

Provide a config object to the evolve method, After which the run method can be used to create a new generation of candidate solutions. All configuration parameters are optional and have defaults provided for them, except for the Fitness Function.

When running the algorithm for the first time, it will generate a random population of chromosomes and determine their fitness value. Each consecutive run will select the fittest chromosomes from the population, use them to generate offspring, mutate the offspring, and add the surviving population and offspring together to create the new population. Afterwards the fitness values of the new population will be calculated.

Configuration

import { evolve } from "evolve-ga";

const algorithm = evolve({
    populationSize: 10000,
    chromosomeLength: 100,
    possibleGenes: [0, 1, 2, 3],
    mutationChance: 0.1,
    fitnessFunction: fitnessFunction,
    selectionFunction: selectionFunction,
    crossOverFunction: crossOverFunction,
    mutationFunction: mutationFunction
});
  • Population Size: The amount of chromosomes the initial population should consist of.
  • Chromosome Length: The amount of genes a chromosome should contain.
  • Possible Genes: An array of strings or numbers. These values are used to generate and mutate chromosomes.
  • Mutation Chance: A number between 0 and 1. The higher the number, the higher the chance of a mutation occuring.

Fitness Function

The function that's used to determine a chromosomes fitness value. It receives a chromosome as parameter and returns a number (fitness value).

Signature:

(chromosome: Chromosome) => number

Selection Function

The function that's used for selecting chromosomes to keep for the next generation and to generate offspring with. It receives the current population and returns an array of the fittest chromosomes.

Signature:

(chromosomes: Chromosome[]) => Chromosome[]

The default Selection Function is as follows:

const selectionFunction = (chromosomes: Chromosome[]): Chromosome[] => {
    return chromosomes
        .sort((a: Chromosome, b: Chromosome): number => b.fitness - a.fitness)
        .slice(0, Math.ceil(chromosomes.length / 2));
    }
}

CrossOver Function

The function that's used for generating offspring from selected chromosomes. It receives the chromosomes that resulted from the Selection Function and returns an array of offspring.

Signature:

(chromosomes: Chromosome[]) => Chromosome[]

The default CrossOver Function is as follows:

const crossOverFunction = (chromosomes: Chromosome[]): Chromosome[] => {
    let offspring: Chromosome[] = [];
    for (let i = 0; i < chromosomes.length; i++) {
        const crossOverPoint = Math.floor(Math.random() * chromosomes[i].genes.length);
        const parentA = chromosomes[Math.floor(Math.random() * chromosomes.length)];
        const parentB = chromosomes[Math.floor(Math.random() * chromosomes.length)];
        offspring[i] = {
            fitness: 0,
            genes: [...parentA.genes.slice(0, crossOverPoint), ...parentB.genes.slice(crossOverPoint)]
        }
    }
    return offspring;
}

Mutation Function

The function that's used for mutating chromosomes. It receives chromosomes that result from the CrossOver Function and trigger a mutation, and returns a mutated chromosome.

Signature:

(chromosome: Chromosome, possibleGenes: (number | string)[]) => Chromosome

The default Mutation Function is as follows:

const mutationFunction = (chromosome: Chromosome, possibleGenes: (number | string)[]): Chromosome => {
    let mutatedGenes = [...chromosome.genes];
    const geneToMutateIndex = Math.floor(Math.random() * mutatedGenes.length);
    const possibleGenesFiltered = possibleGenes.filter((gene: (number | string)): boolean => {
        return gene !== mutatedGenes[geneToMutateIndex];
    });
    mutatedGenes[geneToMutateIndex] = possibleGenesFiltered[
        Math.floor(Math.random() * possibleGenesFiltered.length)
    ];
    return {
        fitness: chromosome.fitness,
        genes: mutatedGenes
    }
}

Typescript Example

import { evolve, Chromosome, GeneticAlgorithm } from "evolve-ga";

const target = ['A', 'B', 'A', 'B', 'A', 'B', 'A'];
let solved = false;

const algorithm: GeneticAlgorithm = evolve({
    possibleGenes: ['A', 'B', 'C', 'D', 'E', 'F', 'G'],
    chromosomeLength: target.length,
    fitnessFunction: (chromosome: Chromosome): number => {
        let fitness = 0;
        for (let i = 0; i < target.length; i++) {
            if (target[i] === chromosome.genes[i]) {
                fitness += 1;
            }
        }
        if (fitness === target.length) {
            solved = true;
        }
        return fitness;
    }
});

while (!solved) {
    algorithm.run();
}