UNPKG

intelligence

Version:

Machine learning library written in javascript

264 lines (231 loc) 10.8 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: genetic/population.js</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Source: genetic/population.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>var events = require('events'); var utils = require('./../infrastructure/utils'); var selectionStrategies = require('./selectionStrategies'); /** * Genetic algorithm population * @constructor * @param {object} options - Population options * @param {Individual} options.baseIndividual - * @property {object} options - Population options */ var Population = function (options) { this.options = options; this.individuals = null; this.validateRequiredOptions(); this.setDefaultOptionsIfNotProvided(); this.initialise(); events.EventEmitter.call(this); return this; }; utils.inherits(Population, events.EventEmitter); /** * Throws an exception if a required option is missing * @throws An exception is thrown if a required option is missing * @returns {Population} Reference to current object for chaining */ Population.prototype.validateRequiredOptions = function () { if (!this.options) { throw "options are required"; } else if (!this.options.baseIndividual) { throw "option 'baseIndividual' is required"; } else if (!this.options.crossoverStrategy) { throw "option 'crossoverStrategy' is required"; } else if (!this.options.fitnessFunction) { throw "options 'fitnessFunction' is required"; } return this; }; /** * Sets default values for options that have not been defined * @returns {Population} Reference to current object for chaining */ Population.prototype.setDefaultOptionsIfNotProvided = function () { if (!this.options.populationSize) { this.options.populationSize = 100; } if (this.options.crossoverRate === undefined) { this.options.crossoverRate = 0.75; } if (this.options.mutationRate === undefined) { this.options.mutationRate = 0.2; } if (!this.options.tournamentSize) { this.options.tournamentSize = Math.ceil(this.options.populationSize * 0.05); } if (!this.options.selectionStrategy) { this.options.selectionStrategy = selectionStrategies.tournament; } return this; }; /** * Creates a randomly generated population of individuals * @returns {Population} Reference to current object for chaining */ Population.prototype.initialise = function () { this.individuals = []; for (var i = 0; i &lt; this.options.populationSize; i++) { this.individuals.push(this.options.baseIndividual.createNew()); } return this; }; /** * Calculates the fitness of each individual where the fitness value is null * @returns {Population} Reference to current object for chaining */ Population.prototype.evaluateFitness = function () { for (var i = 0; i &lt; this.individuals.length; i++) { var individual = this.individuals[i]; if (individual.fitness === null) { individual.fitness = this.options.fitnessFunction(individual); } } return this.filterNanFitness(); }; /** * Performs crossover using the crossoverStrategy function defined in the population options * @returns {Population} Reference to current object for chaining */ Population.prototype.crossover = function () { this.evaluateFitness(); var limbo = []; if (this.options.elitism) { var elite = this.getFittestIndividuals(this.options.elitism); for (var i = 0; i &lt; elite.length; i++) { limbo.push(elite[i].copy()); } } while (limbo.length &lt; this.individuals.length) { var selections = this.options.selectionStrategy(this.individuals, this.options); if (utils.random() &lt; this.options.crossoverRate) { var elections = this.options.crossoverStrategy(selections, this.options); selections = elections; for (var i = 0; i &lt; selections.length; i++) selections[i].fitness = null; } limbo = limbo.concat(selections); } this.individuals = limbo; return this; }; /** * Mutates the population based on the mutationRate property in each individuals options * @returns {Population} Reference to current object for chaining */ Population.prototype.mutate = function () { this.evaluateFitness(); var elite = this.options.elitism ? this.getFittestIndividuals(this.options.elitism) : null; for (var i = 0; i &lt; this.individuals.length; i++) { if (!elite || elite.indexOf(this.individuals[i]) === -1) { if (utils.random() &lt; this.options.mutationRate) { this.individuals[i].mutate(); } } } return this; }; /** * Returns a specified number of individuals with the best fitness rating in the population * @param {number} [numIndividuals=1] - The number of individuals to return * @returns {Individual[]} An array of the fittest individuals in the population */ Population.prototype.getFittestIndividuals = function (numIndividuals) { this.evaluateFitness(); var self = this; this.evaluateFitness(); if (!numIndividuals) numIndividuals = 1; return this.individuals.sort(function (a, b) { if (a.fitness === null &amp;&amp; b.fitness === null) { return 0; } else if (a.fitness === null) { return -1; } else if (b.fitness === null) { return 1; } else { return self.options.isMinimise ? a.fitness - b.fitness : b.fitness - a.fitness; } }).slice(0, numIndividuals); }; /** * Calculate the average fitness of all individuals in the population (exluding infinite values) * @returns {number} Average fitness of the population */ Population.prototype.getAverageFitness = function () { this.evaluateFitness(); var sum = 0; for (var i = 0; i &lt; this.individuals.length; i++) { if (isFinite(this.individuals[i].fitness)) { sum += this.individuals[i].fitness; } }; return sum / this.individuals.length; }; /** * Applies a single iteration of crossover and mutation to the population * @returns {Population} Reference to current object for chaining */ Population.prototype.step = function () { return this.evaluateFitness().crossover().mutate().evaluateFitness(); }; /** * Trains the population over a specified number of generations * @param {number} numGenerations - The number of generations to train the population over * @returns {Population} Reference to current object for chaining */ Population.prototype.train = function (numGenerations) { if (numGenerations &lt;= 0) { throw "'numGenerations' must greater than 0"; } else { for (var i = 0; i &lt; numGenerations; i++) { this.step(); this.emit('generationCompleted', this, i); } } this.emit('trainingCompleted', this); return this; }; /** * Subsitutes an individuals fitness to positive or negative infinity if it isNaN * @returns {Population} Reference to current object for chaining */ Population.prototype.filterNanFitness = function () { var value = this.options.isMinimise ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY; for (var i = 0; i &lt; this.individuals.length; i++) { var individual = this.individuals[i]; if (isNaN(individual.fitness)) { individual.fitness = value } } return this; }; exports.Population = Population;</code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Index</a></h2><h3>Classes</h3><ul><li><a href="Individual.html">Individual</a></li><li><a href="LinearConditionalNode.html">LinearConditionalNode</a></li><li><a href="LinearFunctionNode.html">LinearFunctionNode</a></li><li><a href="LinearGPNode.html">LinearGPNode</a></li><li><a href="LinearIndividual.html">LinearIndividual</a></li><li><a href="Population.html">Population</a></li><li><a href="RegisterReference.html">RegisterReference</a></li><li><a href="RegisterSet.html">RegisterSet</a></li></ul><h3>Global</h3><ul><li><a href="global.html#alphabet">alphabet</a></li><li><a href="global.html#arrayEqual">arrayEqual</a></li><li><a href="global.html#binaryNumber">binaryNumber</a></li><li><a href="global.html#binaryString">binaryString</a></li><li><a href="global.html#createCalculation">createCalculation</a></li><li><a href="global.html#createConstant">createConstant</a></li><li><a href="global.html#createInput">createInput</a></li><li><a href="global.html#createOutput">createOutput</a></li><li><a href="global.html#formatString">formatString</a></li><li><a href="global.html#fpEqual">fpEqual</a></li><li><a href="global.html#inherits">inherits</a></li><li><a href="global.html#linearNode">linearNode</a></li><li><a href="global.html#onePointFixed">onePointFixed</a></li><li><a href="global.html#onePointVariable">onePointVariable</a></li><li><a href="global.html#randBetween">randBetween</a></li><li><a href="global.html#random">random</a></li><li><a href="global.html#randomNumber">randomNumber</a></li><li><a href="global.html#rank">rank</a></li><li><a href="global.html#rouletteWheel">rouletteWheel</a></li><li><a href="global.html#selectRandom">selectRandom</a></li><li><a href="global.html#swapGenes">swapGenes</a></li><li><a href="global.html#tournament">tournament</a></li><li><a href="global.html#twoPointFixed">twoPointFixed</a></li><li><a href="global.html#twoPointVariable">twoPointVariable</a></li><li><a href="global.html#uniform">uniform</a></li><li><a href="global.html#validateFixedLength">validateFixedLength</a></li><li><a href="global.html#validateMinimumLength">validateMinimumLength</a></li><li><a href="global.html#validateVariableLength">validateVariableLength</a></li></ul> </nav> <br clear="both"> <footer> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-alpha9</a> on Sat Aug 16 2014 23:33:39 GMT+0100 (GMT Summer Time) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>