1.1.0 • Published 5 years ago

genalgo v1.1.0

Weekly downloads
8
License
MIT
Repository
github
Last release
5 years ago

GenAlgo.js

npm version gzipped size

logo

Genetic and evolutionary algorithms in Javascript made simple !

Motivation

This package was written in order to provide a JS genetic algorithm boilerplate. Older libraries were not maintained, had flaws in their documentation or implementation.

This library is inspired by :

Installation

npm install genalgo

Examples

Website with examples in progress (polynomial extrema, bin packing, maybe travelling salesman)

Polynomial extrema is available in the storybook :

npm install
cd examples/storybook
npm install
npm run storybook

Usage

Creation of a GenAlgo object

const algo = new GenAlgo({})

Parameters

All of the following parameters can be set during the GenAlgo object creation, or using the corresponding setter.

Crossover function and pair selection function are linked and must both be set in order to create a genetic algorithm.

Doing only crossovers or mutations can result in slow convergence or being stuck in local extrema.

ParameterTypeDefaultRequiredDescription
crossoverFunction(Individual, Individual) => Individual, Individualundefinedfunction used when crossover of two Individuals occurs
crossoverProbabilitynumber0.8probability of crossover
fitnessComparator(number, number) => booleanGreaterfunction used to compare two fitness and return whether the first is better or not
fitnessEvaluator(Individual) => numberundefinedXfunction used to evaluate fitness of an Individual
iterationCallback({iterationNumber:number, elapsedTime:number, bestIndividual: {entity:Individual, fitness:number}}) => booleanundefinedXfunction called after each iteration giving some information to the main program. Should return false if the algorithm has to stop, true otherwise
iterationNumbernumber1000number of iterations to do
mutationFunction(Individual) => IndividualundefinedXfunction used when mutation of an Individual occurs
mutationProbabilitynumber0.8probability of mutation
resultSizenumberundefinedthe number of individuals that should be returned. If it is not set, GenAlgo will return the entire population
selectPairFunction(Array<{entity:Individual, fitness:number}>, (number, number) => boolean) => Individual, Individualtournament3function taking the population and a fitnessComparator as parameters, returning a pair of individual
selectSingleFunction(Array<{entity:Individual, fitness:number}>, (number, number) => boolean) => IndividualfittestXfunction taking the population and a fitnessComparator as parameters, returning a single individual
spareFittestbooleantruespare the fittest individual during each iteration or not
seedArray<Individual>undefinedXfirst population of the algorithm

Selection Function

GenAlgo provides a set of selection function that can be used by setting them to the corresponding attribute of the GenAlgo object.

Single Selectors

Select a single individual.

SelectorDescription
fittestselect the fittest individual
randomselect a random individual
randomLinearRankselect a random individual based on a linear ranking
sequentialselect the individual in the order of the array
tournament2select the best individual between two
tournament3select the best individual between two
Pair Selectors

Select a pair of individuals.

SelectorDescription
fittestRandomselect a pair composed of the fittest and a random individual
randomselect a pair of random individuals
randomLinearRankselect a pair of random individuals based on a linear ranking
sequentialselect a pair of individuals in the order of the array
tournament2select the best two individuals of two tournament2
tournament3select the best two individuals of two tournament3

Fitness Comparators

GenAlgo allows you to maximize or minimize the fitness by setting the comparator to the corresponding attribute

ComparatorDescription
greatermaximize the fitness
lesserminimize the fitness

Polynomial extremum example

import { GenAlgo, lesser, tournament3Single, tournament3Pair } from "genalgo";
import rangeStep from "lodash/fp/rangeStep";

function tryToFindPolynomialExtremum(func: number => number, min: boolean) {
  // Create a GenAlgo object with simple parameters
  const algo = new GenAlgo({
    mutationProbability: 0.2,
    crossoverProbability: 0.8,
    iterationNumber: 100
  });

  // Function used to mutate an individual
  const mutation = number => {
    return number + Math.random();
  };

  // Function used to crossover two individuals
  const crossover = (number1, number2) => {
    return [(number1 + number2) / 2, number1 + number2];
  };

  // Will be called at each iteration
  const iterationCallback = ({
    bestIndividual,
    elapsedTime,
    iterationNumber
  }) => {
    console.log("Iteration " + iterationNumber);
    console.log("Best fitness : " + bestIndividual.fitness);
    console.log("Elapsed time : " + elapsedTime);
    return true;
  };

  // Seed generation
  const seed = rangeStep(10, -10000, 10000);

  algo.setSeed(seed);

  algo.setFitnessEvaluator(func);

  if (min) {
    algo.setFitnessComparator(lesser);
  }

  algo.setMutationFunction(mutation);

  algo.setCrossoverFunction(crossover);

  algo.setSelectSingleFunction(tournament3Single);

  algo.setSelectPairFunction(tournament3Pair);

  algo.setIterationCallback(iterationCallback);

  algo.start();
}
1.1.0

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

6 years ago

1.0.0

6 years ago