genetic-lab
    TypeScript icon, indicating that this package has built-in type declarations

    1.0.1 • Public • Published

    Genetic Lab

    npm version

    License: MIT

    A lightweight genetic algorithm library.

    Changelog [1.0.1]

    • Added support for custom configuration, ex. custom randomizer function

    For a more detailed list of updates, see CHANGELOG.

    Getting Started

    Installation

    Add to existing project using npm or yarn.

    npm install genetic-lab --save
    
    yarn add genetic-lab
    

    Usage

    Import the library to your project.

    const GeneticLab = require("genetic-lab");
     
    // using ES6 import
    // import GeneticLab from 'genetic-lab';
     
    const { Selection, Mutation, Crossover } = GeneticLab;
     
    const {
      createRoulleteWheelSelection,
      createStochasticUniversalSampling,
      createRankSelection,
      createTournamentSelection 
    = Selection;
     
    const { pointCrossover, uniquePointCrossover } = Crossover;
     
    const { mutateOrder, createMutation } = Mutation;

    Check the examples for additional reference on using this library.

    Fitness

    A fitness function should check the fitness of a single individual and return a number based on it's fitness score.

    For example:

    // A fitness function that optimizes for highest binary string value
    function getFitness(individual) {
      return parseInt(individual, 2);
    }
     
    getFitness("010111"); // => 23

    Selection

    createRoulleteWheelSelection(getFitness, config?)

    Creates a selection function that uses Roullete Wheel method.

    const population = ["000", "111"];
    const selection = createRoulleteWheelSelection(getFitness);
    selection(population); // => ['111']

    Config (Optional)

    • randomizer?: () => number; // function that replaces Math.random()

    createStochasticUniversalSampling(getFitness, config)

    Creates a selection function that uses Stochastic Universal Sampling method. Number of individuals selected is based on count.

    const population = ["000", "111", "101"];
    const selection = createStochasticUniversalSampling(getFitness, { count: 2 });
    selection(population); // => ['111', '101'] or ['101', '111']

    Config

    • randomizer?: () => number; // function that replaces Math.random()
    • count: number;

    createRankSelection(getFitness, config)

    Creates a selection function that uses Rank Selection method. Number of individuals selected is based on count.

    const population = ["000", "111", "101"];
    const selection = createRankSelection(getFitness, { count: 2 });
    selection(population); // => ['111', '101']

    Config

    • randomizer?: () => number; // function that replaces Math.random()
    • count: number;

    createTournamentSelection(getFitness, tournamentSize, config?)

    Creates a selection function that uses Tournament Selection method. Size of tournament is dependent on tournametSize.

    const population = ["000", "111", "101"];
    const selection = createTournamentSelection(getFitness, 2);
    selection(population); // => ['111'] or ['101']

    Config (Optional)

    • randomizer?: () => number; // function that replaces Math.random()

    Mutation

    mutateOrder(individual)

    Swaps two genes randomly.

    mutateOrder("ABC"); // => 'BAC' or 'CBA' or 'BCA'
    mutateOrder("10"); // => '01'

    createMutation(sample)

    Creates a mutation function that randomly changes a gene based on sample.

    const sample = ["1", "0"];
    const mutateBinary = createMutation(sample);
    mutateBinary("10"); // => '11' or '00'

    Crossover

    pointCrossover(parentA, parentB, index)

    Creates two offspring based on parentA and parentB using index as point of crossover. If no index is provided, it would be assigned randomly.

    const pair = ["ABCD", "DCBA"];
    pointCrossover(...pair, 2); // => ['ABBA', 'DCCD']

    uniquePointCrossover(parentA, parentB, index)

    Creates two unique chromosome offsprings based on unique chromosomes of parentA and parentB using index as point of crossover. If no index is provided, it would be assigned randomly.

    const pair = ["ABCD", "DCBA"];
    uniquePointCrossover(...pair, 2); // => ['ABDC', 'DCAB']

    License

    This project is licensed under the MIT License - see the LICENSE file for details.

    Install

    npm i genetic-lab

    DownloadsWeekly Downloads

    7

    Version

    1.0.1

    License

    MIT

    Unpacked Size

    17.5 kB

    Total Files

    28

    Last publish

    Collaborators

    • jerameel