Newfangled Package Modernizer

    charles.darwin
    TypeScript icon, indicating that this package has built-in type declarations

    1.3.0 • Public • Published

    NPM Package Build Size

    Darwin

    Flexible genetic algorithm implementation in TypeScript.

    Installation

    npm install charles.darwin

    Darwin is compatible with ES6 modules

    Demos

    Usage

    One must first choose a way of encoding the desired behavior as an array of some type (a chromosome) and of evaluating the fitness of any such chromosome.

    For instance the 'Typing Monkeys' demo tries to evolve towards a target string, the fitness is simply the number of correct characters present in the randomly generated string.

    On the other hand, the 'Smart Eaters' demo evolves the weights and biases of an artificial neural network, the fitness of an 'Eater' is the number of nutriments it eats in one generation (2000 ticks).

      const population = new Darwin<T>({
        populationSize: number,
        chromosomeLength: number,
        randomGene: () => T,
        crossoverRate?: number = 0.7,
        mutationRate?: number = 1 / populationSize,
        crossoverMethod?: CrossoverFunction<T> = crossoverMethod.singlePoint,
        mutationMethod?: MutationFunction<T> = mutationMethod.flip,
        eliteCount?: number = Math.ceil(populationSize / 25),
        eliteCopies?: number = 1,
        randomNumber?: () => number = Math.random
      });

    The constructor of the Darwin class initializes a population of random chromosomes, we can now assign a score (or fitness) to each of those chromosomes based on their genes represented by an array of the type returned by the randomGene function :

      for (const chromo of population.getPopulation()) {
        chromo.setFitness(evalFitness(chromo.getGenes()));
      }
    
      // shorthand:
      population.updateFitness(genes => evalFitness(genes));

    A new generation can then be generated by calling the mate() method:

      population.mate();

    To observe the evolution of the population, one can call:

      const {
        fittest,
        fittestIndex,
        averageFitness,
        totalFitness
    } = population.updateStats();

    Install

    npm i charles.darwin

    DownloadsWeekly Downloads

    3

    Version

    1.3.0

    License

    MIT

    Unpacked Size

    123 kB

    Total Files

    56

    Last publish

    Collaborators

    • avatar