UNPKG

intelligence

Version:

Machine learning library written in javascript

166 lines (139 loc) 7.62 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: genetic/linear/linearIndividual.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/linear/linearIndividual.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>var clone = require('clone'); var utils = require('./../../infrastructure/utils'); var Individual = require('./../individual').Individual; var RegisterSet = require('./registerSet').RegisterSet; var LinearFunctionNode = require('./linearFunctionNode').LinearFunctionNode; var LinearConditionalNode = require('./linearConditionalNode').LinearConditionalNode; /** * Linear genetic programming individual * @constructor * @extends Individual * @param {object} options - Linear indivdual options * @param {number} options.numInputs - The number of inputs that the individual accepts * @param {number} options.numOutputs - The number of output the individual should return * @param {function[]} options.functionSet - An array of functions that are made available to the individual * @param {boolean} [options.removeIntrons=true] - Specify whether introns should be removed before execution * @param {function[]} [options.conditionalSet=[]] - An array of functions that can be used to control logic * @property {object} options - Linear indivdual options */ var LinearIndividual = function (options) { this.options = options; this.options.registerSet = new RegisterSet(clone(this.options)); this.setDefaultOptionsIfNotProvided(); Individual.call(this, options); return this; }; utils.inherits(LinearIndividual, Individual); /** * Validates the linear individuals current options * @throws An exception will occur if a required option is missing * @returns {LinearIndividual} Reference to current object for chaining */ LinearIndividual.prototype.validateRequiredOptions = function () { Individual.prototype.validateRequiredOptions.call(this); if (!this.options.numInputs) { throw "option 'numInputs' is required"; } else if (!this.options.numOutputs) { throw "option 'numOutputs' is required"; } else if (!this.options.functionSet) { throw "option 'functionSet' is required"; } return this; }; /** * Sets default values for options that have not been defined * @returns {LinearIndividual} Reference to current object for chaining */ LinearIndividual.prototype.setDefaultOptionsIfNotProvided = function () { if (this.options.removeIntrons === undefined) { this.options.removeIntrons = true; } if (!this.options.conditionalSet) { this.options.conditionalSet = []; } }; /** * Executes the liner code represented by the indivduals body * @param {object[]} An array of inputs * @returns {object[]} An array of outputs */ LinearIndividual.prototype.execute = function (inputs) { var i = 0; this.options.registerSet.setInputs(inputs); while (i &lt; this.body.length) { var node = this.body[i]; if (node instanceof LinearFunctionNode) { node.execute(this.options.registerSet); } else if (node instanceof LinearConditionalNode) { if (!node.execute(this.options.registerSet)) { while (this.body[i] instanceof LinearConditionalNode) { i++; } } } else { throw "unknown node type"; } i++; } return this.options.registerSet.getOutputNodes(); }; /** * Removes all structurally noneffective code until the body length reaches the minimum allowed length * @returns {LinearIndividual} Reference to current object for chaining */ LinearIndividual.prototype.removeIntrons = function () { }; /** * Returns a string containing an executable representation of the individual * @returns {string} A string containing an executable representation of the individual */ LinearIndividual.prototype.toString = function () { var toReturn = ""; var numIndents = 0; for (var i = 0; i &lt; this.body.length; i++) { var node = this.body[i]; for (var j = 0; j &lt; numIndents; j++) { toReturn += "\t"; } toReturn += node.toString() + "\n"; if (node instanceof LinearFunctionNode) { numIndents = 0; } else { numIndents += 1; } } return toReturn; }; exports.LinearIndividual = LinearIndividual;</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>