27 lines
42 KiB
JavaScript
27 lines
42 KiB
JavaScript
|
/*!
|
||
|
* The MIT License (MIT)
|
||
|
*
|
||
|
* Copyright 2017 Thomas Wagenaar <wagenaartje@protonmail.com>. Copyright for
|
||
|
* portions of Neataptic are held by Copyright 2017 Juan Cazala - cazala.com, as a
|
||
|
* part of project Synaptic.
|
||
|
*
|
||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||
|
* of this software and associated documentation files (the "Software"), to deal
|
||
|
* in the Software without restriction, including without limitation the rights
|
||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||
|
* copies of the Software, and to permit persons to whom the Software is
|
||
|
* furnished to do so, subject to the following conditions:
|
||
|
*
|
||
|
* The above copyright notice and this permission notice shall be included in
|
||
|
* all copies or substantial portions of the Software.
|
||
|
*
|
||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||
|
* THE SOFTWARE
|
||
|
*
|
||
|
*/
|
||
|
!function(n,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.neataptic=t():n.neataptic=t()}(this,function(){return function(n){function t(e){if(o[e])return o[e].exports;var i=o[e]={i:e,l:!1,exports:{}};return n[e].call(i.exports,i,i.exports,t),i.l=!0,i.exports}var o={};return t.m=n,t.c=o,t.i=function(n){return n},t.d=function(n,o,e){t.o(n,o)||Object.defineProperty(n,o,{configurable:!1,enumerable:!0,get:e})},t.n=function(n){var o=n&&n.__esModule?function(){return n.default}:function(){return n};return t.d(o,"a",o),o},t.o=function(n,t){return Object.prototype.hasOwnProperty.call(n,t)},t.p="",t(t.s=18)}([function(n,t){n.exports=function(n){return n.webpackPolyfill||(n.deprecate=function(){},n.paths=[],n.children||(n.children=[]),Object.defineProperty(n,"loaded",{enumerable:!0,get:function(){return n.l}}),Object.defineProperty(n,"id",{enumerable:!0,get:function(){return n.i}}),n.webpackPolyfill=1),n}},function(n,t,o){var e,i,s={Activation:o(10),Mutation:o(15),Selection:o(17),Crossover:o(13),Cost:o(12),Gating:o(14),Connection:o(11),Rate:o(16)};e=[],void 0!==(i=function(){return s}.apply(t,e))&&(n.exports=i),void 0!==n&&n.exports&&(n.exports=s),"object"==typeof window&&(!function(){var n=window.methods;s.ninja=function(){return window.methods=n,s}}(),window.methods=s)},function(n,t,o){(function(n){function t(n){this.bias="input"==n?0:.2*Math.random()-.1,this.squash=r.LOGISTIC,this.type=n||"hidden",this.activation=0,this.state=0,this.old=0,this.mask=1,this.connections={in:[],out:[],gated:[],self:new e(this,this,0)},this.error={responsibility:0,projected:0,gated:0}}n&&(n.exports=t);var e=o(4),i=o(1),s=(o(5),o(3)),r=i.Activation,a=i.Mutation;t.prototype={activate:function(n){if(void 0!==n)return this.activation=n,this.activation;this.old=this.state,this.state=this.connections.self.gain*this.connections.self.weight*this.state+this.bias;for(var t=0;t<this.connections.in.length;t++){var o=this.connections.in[t];this.state+=o.from.activation*o.weight*o.gain}this.activation=this.squash(this.state)*this.mask,this.derivative=this.squash(this.state,!0);for(var e=[],i=[],t=0;t<this.connections.gated.length;t++){var s=this.connections.gated[t],r=s.to,a=e.indexOf(r);a>-1?i[a]+=s.weight*s.from.activation:(e.push(r),i.push(s.weight*s.from.activation+(r.connections.self.gater==this?r.old:0))),s.gain=this.activation}for(var t=0;t<this.connections.in.length;t++){var o=this.connections.in[t];o.elegibility=this.connections.self.gain*this.connections.self.weight*o.elegibility+o.from.activation*o.gain;for(var c=0;c<e.length;c++){var r=e[c],h=i[c],a=o.xtrace.nodes.indexOf(r);a>-1?o.xtrace.values[a]=r.connections.self.gain*r.connections.self.weight*o.xtrace.values[a]+this.derivative*o.elegibility*h:(o.xtrace.nodes.push(r),o.xtrace.values.push(this.derivative*o.elegibility*h))}}return this.activation},propagate:function(n,t,o){t=t||0,n=n||.3;var e=0;if("output"==this.type)this.error.responsibility=this.error.projected=o-this.activation;else{for(var i=0;i<this.connections.out.length;i++){var s=this.connections.out[i],r=s.to;e+=r.error.responsibility*s.weight*s.gain}this.error.projected=this.derivative*e,e=0;for(var i=0;i<this.connections.gated.length;i++){var a=this.connections.gated[i],r=a.to,c=r.connections.self.gater==this?r.old:0;c+=a.weight*a.from.activation,e+=r.error.responsibility*c}this.error.gated=this.derivative*e,this.error.responsibility=this.error.projected+this.error.gated}if("constant"!=this.type){for(var i=0;i<this.connections.in.length;i++){for(var s=this.connections.in[i],h=this.error.projected*s.elegibility,u=0;u<s.xtrace.nodes.length;u++){var r=s.xtrace.nodes[u],f=s.xtrace.values[u];h+=r.error.responsibility*f}var l=n*h*this.mask+t*s.previousDeltaWeight;s.weight+=l,s.previousDeltaWeight=l}this.bias+=n*this.error.responsibility}},connect:function(n,t){var o=[];if(void 0!==n.bias)if(n==this)0!=this.connections.self.weight?s.warnings&&console.warn("This connection already exists!"):this.connections.self.weight=t||1,o.push(this.co
|