UNPKG

intelligence

Version:

Machine learning library written in javascript

196 lines (168 loc) 8.39 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: genetic/linear/registerSet.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/registerSet.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>var utils = require('./../../infrastructure/utils'); var clone = require('clone'); /** * Linear genetic programming register set * @constructor * @param {object} options - Register set options * @param {number} options.numInputs - The number of input registers * @param {number} options.numOutputs - The number of output registers * @param {number} [options.constMin=1] - Minimum constant value * @param {number} [options.constMax=100] - Maxmimum constant value * @param {number} [options.constDivider=1] - Value to divide each constant value by * @param {number} [options.defaultCalculationValue=0] - Value to divide each constant value by * @param {number} [options.defaultOutputValue=0] - Value to divide each constant value by * @param {number} [options.numCalculationRegisters=5] - Value to divide each constant value by * @property {object[]} input - Input registers * @property {number[]} const - Constant registers * @property {number[]} calc - Calculation registers * @property {object[]} out - Output registers */ var RegisterSet = function (options) { this.options = options; this.input = []; this.const = []; this.calc = []; this.out = []; this.validate(); }; /** * Resets all registers * @returns {RegisterSet} Reference to current object for chaining */ RegisterSet.prototype.reset = function () { var i; this.input = []; this.const = []; this.calc = []; this.out = []; for (i = this.options.constMin; i &lt; this.options.constMax; i++) { this.const.push(i / this.options.constDivider); } for (i = 0; i &lt; this.options.numCalculationRegisters; i++) { this.calc.push(this.options.defaultCalculationValue); } for (i = 0; i &lt; this.options.numOutputs; i++) { this.out.push(this.options.defaultOutputValue); } return this; }; /** * Resets all registers * @param {object[]} inputs - An array of inputs * @throws An exception is thrown if the length of the input array does not match options.numInputs * @returns {RegisterSet} Reference to current object for chaining */ RegisterSet.prototype.setInputs = function (inputs) { if (inputs.length !== this.options.numInputs) { throw "length of inputs does not equal expected length"; } this.reset(); this.input = inputs; return this; }; /** * Validates the register set * @throws An exception is thrown if validation fails * @returns {RegisterSet} Reference to current object for chaining */ RegisterSet.prototype.validate = function () { this.setDefaultOptionsIfNotProvided().validateRequiredOptions().validateOptions().reset(); }; /** * Throws an exception if a required option is missing * @throws An exception is thrown if a required option is missing * @returns {RegisterSet} Reference to current object for chaining */ RegisterSet.prototype.validateRequiredOptions = function () { if (!this.options) { throw "options are required"; } else if (!this.options.numInputs) { throw "option 'numInputs is required"; } else if (!this.options.numOutputs) { throw "option 'numOutputs' is required"; } return this; }; /** * Throws an exception if any option values are invalid * @throws An exception is thrown if any option values are invalid * @returns {RegisterSet} Reference to current object for chaining */ RegisterSet.prototype.validateOptions = function () { if (this.options.constMin > this.options.constMax) { throw "value for option 'constMin' cannot be greater than 'constMax'"; } return this; }; /** * Sets default values for options that have not been defined * @returns {RegisterSet} Reference to current object for chaining */ RegisterSet.prototype.setDefaultOptionsIfNotProvided = function () { if (this.options.constMin === undefined) { this.options.constMin = 1; } if (this.options.constMax === undefined) { this.options.constMax = 100; } if (this.options.constDivider === undefined) { this.options.constDivider = 1; } if (this.options.defaultCalculationValue === undefined) { this.options.defaultCalculationValue = 0; } if (this.options.defaultOutputValue === undefined) { this.options.defaultOutputValue = 0; } if (this.options.numCalculationRegisters === undefined) { this.options.numCalculationRegisters = 5; } return this; }; /** * Returns a deep copy of the register set output array * @returns {object[]} A deep copy of the register set output array */ RegisterSet.prototype.getOutputNodes = function () { return clone(this.out); }; /** * Returns the total number of writable registers * @returns {number} The total number of writable registers */ RegisterSet.prototype.getTotalWritableRegisters = function () { return this.calc.length + this.out.length; }; exports.RegisterSet = RegisterSet;</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>