evoJS
evoJS is a genetic and evolutionary algorithm tool, making it easy to incorporate evolutionary algorithms in your JavaScript.
Installation
To install run npm install evo-js
.
To run on a browser, include evo.min.js
in your scripts.
Basic Usage
The tool is used through a population
object. Here is a simple example
// Instantiate a population object var population = evo; var { /* Use genes to return a score */ } // Define a method to evaluate genes and return the fitness // 'genes' is an array of floats population; // Run with a stopping criteria population; // Get the best genes var result = population;
Pool Configuration
As an argument to evo.population
a configuration object can be passed to override the
defaults. Here are the defaults.
var config = genes: 200 // Number of genes for each member size: 100 // Number of members in the gene population cross_rate: 005 // The frequency of gene "twists" in two parents genes mutate_rate: 005 // The frequency of mutations in a parent gene mutate_amount: 10 // The amount a mutated gene can deviate // Each generation is made from a mix of different breeding strategies // Ratios defines the ratio of each in the next generation ratios: top: 025 // Survivors from last generation mutate: 025 // Created by randomly altering genes cross: 025 // Created from crossing parents random: 010 // Random survivors from last generation average:005 // Created by averaging parents together fresh: 010 // new, totally random gene sets ); var population = evo;
Stopping conditions
As an argument to population.run
a configuration object can be passed to defined the
stopping conditions
var config = iterations: 1000 // How many times run will be called generations: 100 // How many generations will be run score: 100 // Minimum score to be reached (higher score is better) // A while function can be supplied // when false is returned sim stops { return true; } // If true, a method will be attempted to stop when the score // stops changing across generations auto_run: false ); population;
'Manual' simulations
While population provides the run method to run simulations, the gene population can be accessed manually for custom simulations.
population = evo whilepopulationgeneration < 100 // Grab next genes var genes = population; //Evaluate genes var score = ; //Report genes back to population population;
When the gene population is empty, a new generation will be created.
Member construction
You may provide a member constructor to automate gene evaluation, and use a more object oriented approach
population = evo // Supply a 'constructor' to use the genes population; // Run will now give your constructed member instead of a gene list population;