UNPKG

intelligence

Version:

Machine learning library written in javascript

170 lines (142 loc) 7.81 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: genetic/linear/registerReference.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/registerReference.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>var utils = require('./../../infrastructure/utils'); /** * Linear genetic programming register reference * @constructor * @param {string} flag - The register set property name * @param {RegisterSet} registerSet - The register set to randomly select an index from * @param {number} index - Optional user specified index * @property {string} flag - The register set property name * @property {number} index - The register set array index */ var RegisterReference = function (flag, registerSet, index) { if (!registerSet[flag]) { throw "Property does not exist"; } this.flag = flag; if (index !== undefined) { this.index = index; } else { this.setRandomIndex(registerSet); } return this; }; /** * Sets the index property to a random value based on the provided register set * @param {RegisterSet} registerSet - A register set instance * @returns {RegisterReference} Reference to current object for chaining */ RegisterReference.prototype.setRandomIndex = function (registerSet) { if (this.flag === exports.INPUT) { this.index = utils.randBetween(0, registerSet.options.numInputs); } else if (this.flag === exports.CONSTANT) { this.index = utils.randBetween(0, registerSet.const.length); } else if (this.flag === exports.CALCULATION) { this.index = utils.randBetween(0, registerSet.calc.length); } else { this.index = utils.randBetween(0, registerSet.out.length); } return this; }; /** * Gets the value that this register reference represents in the given register set * @param {RegisterSet} registerSet - A register set instance * @returns {*} Value from the register set */ RegisterReference.prototype.getValue = function (registerSet) { return registerSet[this.flag][this.index]; }; /** * Sets the value that this register reference represents in the given register set * @param {RegisterSet} registerSet - A register set instance * @param {*} value - A value to set * @returns {RegisterReference} Reference to current object for chaining */ RegisterReference.prototype.setValue = function (registerSet, value) { if (this.flag !== exports.CALCULATION &amp;&amp; this.flag !== exports.OUTPUT) { throw "only calculation or output registers can be set"; } else { registerSet[this.flag][this.index] = value; } return this; }; /** * Returns a string representation of register reference * @param {RegisterSet} registerSet - A register set instance * @returns {string} A string representation of the register reference */ RegisterReference.prototype.toString = function () { return utils.formatString("registerSet.{0}[{1}]", [this.flag, this.index]); }; /** * Returns a new input register reference * @param {RegisterSet} registerSet - A register set instance * @param {number} index - Optional user specified index * @returns {RegisterReference} A new input register reference */ exports.createInput = function (registerSet, index) { return new RegisterReference(exports.INPUT, registerSet, index); }; /** * Returns a new constant register reference * @param {RegisterSet} registerSet - A register set instance * @param {number} index - Optional user specified index * @returns {RegisterReference} A new constant register reference */ exports.createConstant = function (registerSet, index) { return new RegisterReference(exports.CONSTANT, registerSet, index); }; /** * Returns a new calculation register reference * @param {RegisterSet} registerSet - A register set instance * @param {number} index - Optional user specified index * @returns {RegisterReference} A new calculation register reference */ exports.createCalculation = function (registerSet, index) { return new RegisterReference(exports.CALCULATION, registerSet, index); }; /** * Returns a new output register reference * @param {RegisterSet} registerSet - A register set instance * @param {number} index - Optional user specified index * @returns {RegisterReference} A new output register reference */ exports.createOutput = function (registerSet, index) { return new RegisterReference(exports.OUTPUT, registerSet, index); }; exports.INPUT = 'input'; exports.CONSTANT = 'const'; exports.CALCULATION = 'calc'; exports.OUTPUT = 'out'; exports.RegisterReference = RegisterReference;</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>