27 lines
43 KiB
JavaScript
27 lines
43 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(t,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?exports.neataptic=n():t.neataptic=n()}(this,function(){return function(t){function n(e){if(o[e])return o[e].exports;var i=o[e]={i:e,l:!1,exports:{}};return t[e].call(i.exports,i,i.exports,n),i.l=!0,i.exports}var o={};return n.m=t,n.c=o,n.i=function(t){return t},n.d=function(t,o,e){n.o(t,o)||Object.defineProperty(t,o,{configurable:!1,enumerable:!0,get:e})},n.n=function(t){var o=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(o,"a",o),o},n.o=function(t,n){return Object.prototype.hasOwnProperty.call(t,n)},n.p="",n(n.s=18)}([function(t,n){t.exports=function(t){return t.webpackPolyfill||(t.deprecate=function(){},t.paths=[],t.children||(t.children=[]),Object.defineProperty(t,"loaded",{enumerable:!0,get:function(){return t.l}}),Object.defineProperty(t,"id",{enumerable:!0,get:function(){return t.i}}),t.webpackPolyfill=1),t}},function(t,n,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(n,e))&&(t.exports=i),void 0!==t&&t.exports&&(t.exports=s),"object"==typeof window&&(!function(){var t=window.methods;s.ninja=function(){return window.methods=t,s}}(),window.methods=s)},function(t,n,o){(function(t){function n(t){this.bias="input"==t?0:.2*Math.random()-.1,this.squash=r.LOGISTIC,this.type=t||"hidden",this.activation=0,this.state=0,this.old=0,this.mask=1,this.previousDeltaBias=0,this.totalDeltaBias=0,this.connections={in:[],out:[],gated:[],self:new e(this,this,0)},this.error={responsibility:0,projected:0,gated:0}}t&&(t.exports=n);var e=o(4),i=o(1),s=(o(5),o(3)),r=i.Activation,a=i.Mutation;n.prototype={activate:function(t){if(void 0!==t)return this.activation=t,this.activation;this.old=this.state,this.state=this.connections.self.gain*this.connections.self.weight*this.state+this.bias;for(var n=0;n<this.connections.in.length;n++){var o=this.connections.in[n];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=[],n=0;n<this.connections.gated.length;n++){var s=this.connections.gated[n],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 n=0;n<this.connections.in.length;n++){var o=this.connections.in[n];o.elegibility=this.connections.self.gain*this.connections.self.weight*o.elegibility+o.from.activation*o.gain;for(var h=0;h<e.length;h++){var r=e[h],c=i[h],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*c:(o.xtrace.nodes.push(r),o.xtrace.values.push(this.derivative*o.elegibility*c))}}return this.activation},propagate:function(t,n,o,e){n=n||0,t=t||.3;var i=0;if("output"==this.type)this.error.responsibility=this.error.projected=e-this.activation;else{for(var s=0;s<this.connections.out.length;s++){var r=this.connections.out[s],a=r.to;i+=a.error.responsibility*r.weight*r.gain}this.error.projected=this.derivative*i,i=0;for(var s=0;s<this.connections.gated.length;s++){var h=this.connections.gated[s],a=h.to,c=a.connections.self.gater==this?a.old:0;c+=h.weight*h.from.activation,i+=a.error.responsibility*c}this.error.gated=this.derivative*i,this.error.responsibility=this.error.projected+this.error.gated}if("constant"!=this.type){for(var s=0;s<this.connections.in.length;s++){for(var r=this.connections.in[s],u=this.error.projected*r.elegibility,f=0;f<r.xtrace.nodes.length;f++){var a=r.xtrace.nodes[f],l=r.xtrace.values[f];u+=a.error.responsibility*l}var p=t*u*this.mask;r.totalDeltaWeight+=p,o&&(r.totalDeltaWeight+=n*r.previousDeltaWeight,r.weight+=r.totalDeltaWeight,r.previousDeltaWeight=r.totalDeltaWeight,r.totalDeltaWeight=0)}var d=t*this.error.responsibility;this.totalDeltaBias+=d,o&&(this.totalDeltaBias+=n*this.previousDeltaBias,this.bias+=this.totalDeltaBias,this.previousDeltaBias=this.totalDeltaBias,this.totalDeltaBias=0)}},connect:function(t,n){var o=[];if(void 0!==t.bias)if(t==this)0!=this.connections.self.weight?s.warnings&&console.warn("This connection already exists!"):this.connections.self.weight=n||1,o.push(this.connections.self);else{if(this.isProjectingTo(t))throw new Error("Already projecting a connection to this node!");var i=new e(this,t,n);t.connections.in.push(i),this.connections.out.push(i),o.push(i)}else for(var r=0;r<t.nodes.length;r++){var i=new e(this,t.nodes[r],n);t.nodes[r].connections.in.push(i),this.connections.out.push(i),t.connections.in.push(i),o.push(i)}return o},disconnect:function(t,n){if(this==t)return void(this.connections.self.weight=0);for(var o=0;o<this.connections.out.length;o++){var e=this.connections.out[o];if(e.to==t){this.connections.out.splice(o,1);var i=e.to.connections.in.indexOf(e);e.to.connections.in.splice(i,1),null!=e.gater&&this.ungate(e);break}}n&&t.disconnect(this)},gate:function(t){Array.isArray(t)||(t=[t]);for(var n=0;n<t.length;n++){var o=t[n];this.connections.gated.push(o),o.gater=this}},ungate:function(t){Array.isArray(t)||(t=[t]);for(var n=t.length-1;n>=0;n--){var o=t[n],e=this.connections.gated.indexOf(o);this.connections.gated.splice(e,1),o.gater=null}},clear:function(){for(var t=0;t<this.connections.in.length;t++){var n=this.connections.in[t];n.elegibility=0,n.xtrace={nodes:[],values:[]}}this.error.responsibility=this.error.projected=this.error.gated=0,this.old=this.state=this.activation=0},mutate:function(t){if(void 0===t)throw new Error("No mutate method given!");if(!t.name in i.Mutation)throw new Error("This method does not exist!");switch(t){case a.MOD_ACTIVATION:var n=t.allowed[(t.allowed.indexOf(this.squash)+Math.floor(Math.random()*(t.allowed.length-1))+1)%t.allowed.length];this.squash=n;break;case a.MOD_BIAS:var o=Math.random()*(t.max-t.min)+t.min;this.bias+=o}},isProjectingTo:function(t){for(var n=0;n<this.connections.out.length;n++){if(this.connections.out[n].to==t)return!0}return t==this&&0!=this.connections.self.weight},isProjectedBy:function(t){for(var n=0;n<this.connections.in.length;n++){if(this.connections.in[n].from==t)return!0}return t==this&&0!=this.connections.self.weight},toJSON:function(){return{bias:this.bias,type:this.type,squash:this.squash.name,mask:this.mask}}},n.fromJSON=function(t){var o=new n;o.bias=t.bias,o.type=t.type,o.mask=t.mask;for(squash in r)if(r[squash].name==t.squash){o.squash=r[squash];break}return o}}).call(n,o(0)(t))},function(t,n,o){(function(t){var n={warnings:!0};t&&(t.exports=n)}).call(n,o(0)(t))},function(t,n,o){(function(t){function n(t,n,o){this.from=t,this.to=n,this.gain=1,this.weight=void 0===o?.2*Math.random()-.1:o,this.gater=null,this.elegibility=0,this.previousDeltaWeight=0,this.totalDeltaWeight=0,this.xtrace={nodes:[],values:[]}}t&&(t.exports=n),n.prototype={toJSON:function(){return{weight:this.weight}}},n.innovationID=function(t,n){return.5*(t+n)*(t+n+1)+n}}).call(n,o(0)(t))},function(t,n,o){(function(t){function n(t){this.nodes=[],this.connections={in:[],out:[],self:[]};for(var n=0;n<t;n++)this.nodes.push(new i)}t&&(t.exports=n);var e=o(1),i=(o(4),o(2)),s=o(3),r=o(6);e.Activation,e.Mutation;n.prototype={activate:function(t){var n=[];if(void 0!==t&&t.length!=this.nodes.length)throw new Error("Array with values should be same as the amount of nodes!");for(var o=0;o<this.nodes.length;o++){if(void 0===t)var e=this.nodes[o].activate();else var e=this.nodes[o].activate(t[o]);n.push(e)}return n},propagate:function(t,n,o){if(void 0!==o&&o.length!=this.nodes.length)throw new Error("Array with values should be same as the amount of nodes!");for(var e=this.nodes.length-1;e>=0;e--)void 0===o?this.nodes[e].propagate(t,n):this.nodes[e].propagate(t,n,o[e])},connect:function(t,o,a){var h=[];if(t instanceof n){if(void 0===o&&(this!=t?(s.warnings&&console.warn("No group connection specified, using ALL_TO_ALL"),o=e.Connection.ALL_TO_ALL):(s.warnings&&console.warn("No group connection specified, using ONE_TO_ONE"),o=e.Connection.ONE_TO_ONE)),o==e.Connection.ALL_TO_ALL||o==e.Connection.ALL_TO_ELSE){for(var c=0;c<this.nodes.length;c++)for(var u=0;u<t.nodes.length;u++)if(o!=e.Connection.ALL_TO_ELSE||this.nodes[c]!=t.nodes[u]){var f=this.nodes[c].connect(t.nodes[u],a);this.connections.out.push(f[0]),t.connections.in.push(f[0]),h.push(f[0])}}else if(o==e.Connection.ONE_TO_ONE){if(this.nodes.length!=t.nodes.length)throw new Error("From and To group must be the same size!");for(var c=0;c<this.nodes.length;c++){var f=this.nodes[c].connect(t.nodes[c],a);this.connections.self.push(f[0]),h.push(f[0])}}}else if(t instanceof r)var h=t.input(this,o,a);else if(t instanceof i)for(var c=0;c<this.nodes.length;c++){var f=this.nodes[c].connect(t,a);this.connections.out.push(f[0]),h.push(f[0])}return h},gate:function(t,n){if(void 0===n)throw new Error("Please specify Gating.INPUT, Gating.OUTPUT");Array.isArray(t)||(t=[t]);for(var o=[],i=[],s=0;s<t.length;s++){var r=t[s];o.includes(r.from)||o.push(r.from),i.includes(r.to)||i.push(r.to)}switch(n){case e.Gating.INPUT:for(var s=0;s<i.length;s++)for(var a=i[s],h=this.nodes[s%this.nodes.length],c=0;c<a.connections.in.length;c++){var u=a.connections.in[c];t.includes(u)&&h.gate(u)}break;case e.Gating.OUTPUT:for(var s=0;s<o.length;s++)for(var a=o[s],h=this.nodes[s%this.nodes.length],c=0;c<a.connections.out.length;c++){var u=a.connections.out[c];t.includes(u)&&h.gate(u)}break;case e.Gating.SELF:for(var s=0;s<o.length;s++){var a=o[s],h=this.nodes[s%this.nodes.length];t.includes(a.connections.self)&&h.gate(a.connections.self)}}},set:function(t){for(var n=0;n<this.nodes.length;n++)void 0!==t.bias&&(this.nodes[n].bias=t.bias),this.nodes[n].squash=t.squash||this.nodes[n].squash,this.nodes[n].type=t.type||this.nodes[n].type},disconnect:function(t,o){if(o=o||!1,t instanceof n)for(var e=0;e<this.nodes.length;e++)for(var s=0;s<t.nodes.length;s++){this.nodes[e].disconnect(t.nodes[s],o);for(var r=this.connections.out.length-1;r>=0;r--){var a=this.connections.out[r];if(a.from==this.nodes[e]&&a.to==t.nodes[s]){this.connections.out.splice(r,1);break}}if(o)for(var r=this.connections.in.length-1;r>=0;r--){var a=this.connections.in[r];if(a.from==t.nodes[s]&&a.to==this.nodes[e]){this.connections.in.splice(r,1);break}}}else if(t instanceof i)for(var e=0;e<this.nodes.length;e++){for(var s=(this.nodes[e].disconnect(t,o),this.connections.out.length-1);s>=0;s--){var a=this.connections.out[s];if(a.from==this.nodes[e]&&a.to==t){this.connections.out.splice(s,1);break}}if(o)for(var s=this.connections.in.length-1;s>=0;s--){var a=this.connections.in[s];if(a.from==t&&a.to==this.nodes[e]){this.connections.in.splice(s,1);break}}}},clear:function(){for(var t=0;t<this.nodes.length;t++)this.nodes[t].clear()}}}).call(n,o(0)(t))},function(t,n,o){(function(t){function n(){this.output=null,this.nodes=[],this.connections={in:[],out:[],self:[]}}t&&(t.exports=n);var e=o(1),i=(o(4),o(2)),s=(o(3),o(8),o(5));e.Activation,e.Mutation;n.prototype={activate:function(t){var n=[];if(void 0!==t&&t.length!=this.nodes.length)throw new Error("Array with values should be same as the amount of nodes!");for(var o=0;o<this.nodes.length;o++){if(void 0===t)var e=this.nodes[o].activate();else var e=this.nodes[o].activate(t[o]);n.push(e)}return n},propagate:function(t,n,o){if(void 0!==o&&o.length!=this.nodes.length)throw new Error("Array with values should be same as the amount of nodes!");for(var e=this.nodes.length-1;e>=0;e--)void 0===o?this.nodes[e].propagate(t,n):this.nodes[e].propagate(t,n,o[e])},connect:function(t,o,e){if(t instanceof s||t instanceof i)var r=this.output.connect(t,o,e);else if(t instanceof n)var r=t.input(this,o,e);return r},gate:function(t,n){this.output.gate(t,n)},set:function(t){for(var n=0;n<this.nodes.length;n++){var o=this.nodes[n];o instanceof i?(void 0!==t.bias&&(o.bias=t.bias),o.squash=t.squash||o.squash,o.type=t.type||o.type):o instanceof s&&o.set(t)}},disconnect:function(t,n){if(n=n||!1,t instanceof s)for(var o=0;o<this.nodes.length;o++)for(var e=0;e<t.nodes.length;e++){this.nodes[o].disconnect(t.nodes[e],n);for(var r=this.connections.out.length-1;r>=0;r--){var a=this.connections.out[r];if(a.from==this.nodes[o]&&a.to==t.nodes[e]){this.connections.out.splice(r,1);break}}if(n)for(var r=this.connections.in.length-1;r>=0;r--){var a=this.connections.in[r];if(a.from==t.nodes[e]&&a.to==this.nodes[o]){this.connections.in.splice(r,1);break}}}else if(t instanceof i)for(var o=0;o<this.nodes.length;o++){for(var e=(this.nodes[o].disconnect(t,n),this.connections.out.length-1);e>=0;e--){var a=this.connections.out[e];if(a.from==this.nodes[o]&&a.to==t){this.connections.out.splice(e,1);break}}if(n)for(var r=this.connections.in.length-1;r>=0;r--){var a=this.connections.in[r];if(a.from==t&&a.to==this.nodes[o]){this.connections.in.splice(r,1);break}}}},clear:function(){for(var t=0;t<this.nodes.length;t++)this.nodes[t].clear()}},n.Dense=function(t){var o=new n,i=new s(t);return o.nodes.push(i),o.output=i,o.input=function(t,o,s){return t instanceof n&&(t=t.output),o=o||e.Connection.ALL_TO_ALL,t.connect(i,o,s)},o},n.LSTM=function(t){var o=new n,i=new s(t),r=new s(t),a=new s(t),h=new s(t),c=new s(t);i.set({bias:1}),r.set({bias:1}),h.set({bias:1}),a.connect(i,e.Connection.ALL_TO_ALL),a.connect(r,e.Connection.ALL_TO_ALL),a.connect(h,e.Connection.ALL_TO_ALL);var u=a.connect(a,e.Connection.ONE_TO_ONE),f=a.connect(c,e.Connection.ALL_TO_ALL);return r.gate(u,e.Gating.SELF),h.gate(f,e.Gating.OUTPUT),o.nodes=[i,r,a,h,c],o.output=c,o.input=function(t,o,s){t instanceof n&&(t=t.output),o=o||e.Connection.ALL_TO_ALL;var c=[],u=t.connect(a,o,s);return c=c.concat(u),c=c.concat(t.connect(i,o,s)),c=c.concat(t.connect(h,o,s)),c=c.concat(t.connect(r,o,s)),i.gate(u,e.Gating.INPUT),c},o},n.GRU=function(t){var o=new n,i=new s(t),r=new s(t),a=new s(t),h=new s(t),c=new s(t),u=new s(t);u.set({bias:0,squash:e.Activation.IDENTITY,type:"constant"}),h.set({squash:e.Activation.TANH}),r.set({bias:0,squash:e.Activation.INVERSE,type:"constant"}),i.set({bias:1}),a.set({bias:0}),u.connect(i,e.Connection.ALL_TO_ALL),i.connect(r,e.Connection.ONE_TO_ONE,1),u.connect(a,e.Connection.ALL_TO_ALL);var f=u.connect(h,e.Connection.ALL_TO_ALL);a.gate(f,e.Gating.OUTPUT);var l=u.connect(c,e.Connection.ALL_TO_ALL),p=h.connect(c,e.Connection.ALL_TO_ALL);return i.gate(l,e.Gating.OUTPUT),r.gate(p,e.Gating.OUTPUT),c.connect(u,e.Connection.ONE_TO_ONE,1),o.nodes=[i,r,a,h,c,u],o.output=c,o.input=function(t,o,s){t instanceof n&&(t=t.output),o=o||e.Connection.ALL_TO_ALL;var r=[];return r=r.concat(t.connect(i,o,s)),r=r.concat(t.connect(a,o,s)),r=r.concat(t.connect(h,o,s))},o},n.Memory=function(t,o){for(var i=new n,r=null,a=0;a<o;a++){var h=new s(t);h.set({squash:e.Activation.IDENTITY,bias:0,type:"constant"}),null!=r&&r.connect(h,e.Connection.ONE_TO_ONE,1),i.nodes.push(h),r=h}i.nodes.reverse();for(var a=0;a<i.nodes.length;a++)i.nodes[a].nodes.reverse();var c=new s(0);for(var u in i.nodes)c.nodes=c.nodes.concat(i.nodes[u].nodes);return i.output=c,i.input=function(t,o,s){if(t instanceof n&&(t=t.output),o=o||e.Connection.ALL_TO_ALL,t.nodes.length!=i.nodes[i.nodes.length-1].nodes.length)throw new Error("Previous layer size must be same as memory size");return t.connect(i.nodes[i.nodes.length-1],e.Connection.ONE_TO_ONE,1)},i}}).call(n,o(0)(t))},function(t,n,o){(function(t){function n(t,n){if(void 0===t||void 0===n)throw new Error("No input or output size given");this.input=t,this.output=n,this.nodes=[],this.connections=[],this.gates=[],this.selfconns=[],this.dropout=0;for(var o=0;o<this.input+this.output;o++){var i=o<this.input?"input":"output";this.nodes.push(new e(i,this.nodes.length))}for(var o=0;o<this.input;o++)for(var s=this.input;s<this.output+this.input;s++){var r=Math.random()*this.input*Math.sqrt(2/this.input);this.connect(this.nodes[o],this.nodes[s],r)}}t&&(t.exports=n);var e=o(2),i=o(4),s=o(1),r=o(3),a=o(9),h=(s.Activation,s.Mutation);n.prototype={activate:function(t,n){for(var o=[],e=0;e<this.nodes.length;e++)if("input"==this.nodes[e].type)this.nodes[e].activate(t[e]);else if("output"==this.nodes[e].type){var i=this.nodes[e].activate();o.push(i)}else n&&(this.nodes[e].mask=Math.random()<this.dropout?0:1),this.nodes[e].activate();return o},propagate:function(t,n,o,e){if(void 0!==e&&e.length!=this.output)throw new Error("Output target length should match network output length");for(var i=e.length,s=this.nodes.length-1;s>=this.nodes.length-this.output;s--)this.nodes[s].propagate(t,n,o,e[--i]);for(var s=this.nodes.length-this.output-1;s>=this.input;s--)this.nodes[s].propagate(t,n,o)},clear:function(){for(var t=0;t<this.nodes.length;t++)this.nodes[t].clear()},connect:function(t,n,o){for(var e=t.connect(n,o),i=0;i<e.length;i++){var s=e[i];t!=n?this.connections.push(s):this.selfconns.push(s)}return e},disconnect:function(t,n){for(var o=t==n?this.selfconns:this.connections,e=0;e<o.length;e++){var i=o[e];if(i.from==t&&i.to==n){null!=i.gater&&this.ungate(i),o.splice(e,1);break}}t.disconnect(n)},gate:function(t,n){if(-1==this.nodes.indexOf(t))throw new Error("This node is not part of the network!");if(null!=n.gater)return void(r.warnings&&console.warn("This connection is already gated!"));t.gate(n),this.gates.push(n)},ungate:function(t){var n=this.gates.indexOf(t);if(-1==n)throw new Error("This connection is not gated!");this.gates.splice(n,1),t.gater.ungate(t)},remove:function(t){var n=this.nodes.indexOf(t);if(-1==n)throw new Error("This node does not exist in the network!");var o=[];this.disconnect(t,t);for(var e=[],i=t.connections.in.length-1;i>=0;i--){var r=t.connections.in[i];s.Mutation.SUB_NODE.keep_gates&&null!=r.gater&&r.gater!=t&&o.push(r.gater),e.push(r.from),this.disconnect(r.from,t)}for(var a=[],i=t.connections.out.length-1;i>=0;i--){var r=t.connections.out[i];s.Mutation.SUB_NODE.keep_gates&&null!=r.gater&&r.gater!=t&&o.push(r.gater),a.push(r.to),this.disconnect(t,r.to)}for(var h=[],i=0;i<e.length;i++)for(var c=e[i],u=0;u<a.length;u++){var f=a[u];if(!c.isProjectingTo(f)){var l=this.connect(c,f);h.push(l[0])}}for(var i=0;i<o.length&&0!=h.length;i++){var p=o[i],d=Math.floor(Math.random()*h.length),l=h[d];this.gate(p,h[d]),h.splice(d,1)}for(var i=t.connections.gated.length-1;i>=0;i--){var l=t.connections.gated[i];this.ungate(l)}this.disconnect(t,t),this.nodes.splice(n,1)},mutate:function(t){if(void 0===t)throw new Error("No (correct) mutate method given!");switch(t){case h.ADD_NODE:var n=this.connections[Math.floor(Math.random()*this.connections.length)],o=n.gater;this.disconnect(n.from,n.to);var i=this.nodes.indexOf(n.to),s=new e("hidden",this.nodes.length);s.mutate(h.MOD_ACTIVATION);var a=Math.min(i,this.nodes.length-this.output);this.nodes.splice(a,0,s);var c=this.connect(n.from,s)[0],u=this.connect(s,n.to)[0];null!=o&&this.gate(o,Math.random()>=.5?c:u);break;case h.SUB_NODE:if(this.nodes.length==this.input+this.output){r.warnings&&console.warn("No more nodes left to remove!");break}var f=Math.floor(Math.random()*(this.nodes.length-this.output-this.input)+this.input);this.remove(this.nodes[f]);break;case h.ADD_CONN:for(var l=[],p=0;p<this.nodes.length-this.output;p++)for(var d=this.nodes[p],g=Math.max(p+1,this.input);g<this.nodes.length;g++){var v=this.nodes[g];d.isProjectingTo(v)||l.push([d,v])}if(0==l.length){r.warnings&&console.warn("No more connections to be made!");break}var m=l[Math.floor(Math.random()*l.length)];this.connect(m[0],m[1]);break;case h.SUB_CONN:for(var O=[],p=0;p<this.connections.length;p++){var w=this.connections[p];w.from.connections.out.length>1&&w.to.connections.in.length>1&&this.nodes.indexOf(w.to)>this.nodes.indexOf(w.from)&&O.push(w)}if(0==O.length){r.warnings&&console.warn("No connections to remove!");break}var _=O[Math.floor(Math.random()*O.length)];this.disconnect(_.from,_.to);break;case h.MOD_WEIGHT:var n=this.connections[Math.floor(Math.random()*this.connections.length)],A=Math.random()*(t.max-t.min)+t.min;n.weight+=A;break;case h.MOD_BIAS:var f=Math.floor(Math.random()*(this.nodes.length-this.input)+this.input),s=this.nodes[f];s.mutate(t);break;case h.MOD_ACTIVATION:if(!t.mutateOutput&&this.input+this.output==this.nodes.length){r.warnings&&console.warn("No nodes that allow mutation of activation function");break}var f=Math.floor(Math.random()*(this.nodes.length-(t.mutateOutput?0:this.output)-this.input)+this.input),s=this.nodes[f];s.mutate(t);break;case h.ADD_SELF_CONN:for(var O=[],p=this.input;p<this.nodes.length;p++){var s=this.nodes[p];-1==this.selfconns.indexOf(s.connections.self)&&O.push(s)}if(0==O.length){r.warnings&&console.warn("No more self-connections to add!");break}var s=O[Math.floor(Math.random()*O.length)];this.connect(s,s);break;case h.SUB_SELF_CONN:if(0==this.selfconns.length){r.warnings&&console.warn("No more self-connections to remove!");break}var w=this.selfconns[Math.floor(Math.random()*this.selfconns.length)];this.disconnect(w.from,w.to);break;case h.ADD_GATE:for(var N=this.connections.concat(this.selfconns),O=[],p=0;p<N.length;p++){var w=N[p];null==w.gater&&O.push(w)}if(0==O.length){r.warnings&&console.warn("No more connections to gate!");break}var s=this.nodes[Math.floor(Math.random()*this.nodes.length)],w=O[Math.floor(Math.random()*O.length)];this.gate(s,w);break;case h.SUB_GATE:if(0==this.gates.length){r.warnings&&console.warn("No more connections to ungate!");break}var f=Math.floor(Math.random()*this.gates.length),L=this.gates[f];this.ungate(L);break;case h.ADD_BACK_CONN:for(var l=[],p=this.input;p<this.nodes.length;p++)for(var d=this.nodes[p],g=this.input;g<p;g++){var v=this.nodes[g];d.isProjectingTo(v)||l.push([d,v])}if(0==l.length){r.warnings&&console.warn("No more connections to be made!");break}var m=l[Math.floor(Math.random()*l.length)];this.connect(m[0],m[1]);break;case h.SUB_BACK_CONN:for(var O=[],p=0;p<this.connections.length;p++){var w=this.connections[p];w.from.connections.out.length>1&&w.to.connections.in.length>1&&this.nodes.indexOf(w.from)>this.nodes.indexOf(w.to)&&O.push(w)}if(0==O.length){r.warnings&&console.warn("No connections to remove!");break}var _=O[Math.floor(Math.random()*O.length)];this.disconnect(_.from,_.to);break;case h.SWAP_NODES:var f=Math.floor(Math.random()*(this.nodes.length-this.input)+this.input),d=this.nodes[f],f=Math.floor(Math.random()*(this.nodes.length-this.input)+this.input),v=this.nodes[f],T=d.bias,M=d.squash;d.bias=v.bias,d.squash=v.squash,v.bias=T,v.squash=M}},train:function(t,n){n=n||{},void 0===n.rate&&r.warnings&&console.warn("Using default learning rate, please define a rate!"),void 0===n.iterations&&r.warnings&&console.warn("No target iterations given, running until error is reached!");var o=Date.now(),e=n.log||!1,i=n.error||.05,a=n.cost||s.Cost.MSE,h=n.rate||.3,c=n.shuffle||!1,u=n.iterations||0,f=n.crossValidate||!1,l=n.clear||!1,p=n.dropout||0,d=n.momentum||0,g=n.batchSize||1,v=n.ratePolicy||s.Rate.FIXED(),m=n.schedule;if(g>t.length)throw new Error("Batch size must be smaller or equal to dataset length!");if(void 0===n.iterations&&void 0===n.error?r.warnings&&console.warn("At least one of the following options must be specified: error, iterations"):void 0===n.error&&(i=-1),this.dropout=p,f)var O=n.crossValidate.testSize,w=n.crossValidate.testError,_=Math.ceil((1-O)*t.length),A=t.slice(0,_),N=t.slice(_);for(var L=h,T=0,M=1;M>i&&(0==u||T<u)&&!(f&&M<=w);){if(T++,L=v(h,T),M=0,f?(this._trainSet(A,g,L,d,a),l&&this.clear(),M+=this.test(N,a).error,l&&this.clear()):(M+=this._trainSet(t,g,L,d,a),l&&this.clear()),c)for(var E,D,b=t.length;b;E=Math.floor(Math.random()*b),D=t[--b],t[b]=t[E],t[E]=D);e&&T%e==0&&console.log("iteration",T,"error",M,"rate",L),m&&T%m.iterations==0&&m.function({error:M,iteration:T})}if(l&&this.clear(),p)for(var b=0;b<this.nodes.length;b++)"hidden"!=this.nodes[b].type&&"constant"!=this.nodes[b].type||(this.nodes[b].mask=1-this.dropout);return{error:M,iterations:T,time:Date.now()-o}},_trainSet:function(t,n,o,e,i){for(var s=0,r=0;r<t.length;r++){var a=t[r].input,h=t[r].output,c=(r+1)%n==0||r+1==t.length,u=this.activate(a,!0);this.propagate(o,e,c,h),s+=i(h,u)}return s/r},test:function(t,n){if(this.dropout)for(var o=0;o<this.nodes.length;o++)"hidden"!=this.nodes[o].type&&"constant"!=this.nodes[o].type||(this.nodes[o].mask=1-this.dropout);n=n||s.Cost.MSE;for(var e,i,r,a=0,h=Date.now(),o=0;o<t.length;o++)e=t[o].input,r=t[o].output,i=this.activate(e),a+=n(r,i);return a/=t.length,{error:a,time:Date.now()-h}},graph:function(t,n){for(var o=0,e=0,i={nodes:[],links:[],constraints:[{type:"alignment",axis:"x",offsets:[]},{type:"alignment",axis:"y",offsets:[]}]},s=0;s<this.nodes.length;s++){var r=this.nodes[s];"input"==r.type?(1==this.input?i.constraints[0].offsets.push({node:s,offset:0}):i.constraints[0].offsets.push({node:s,offset:.8*t/(this.input-1)*o++}),i.constraints[1].offsets.push({node:s,offset:0})):"output"==r.type&&(1==this.output?i.constraints[0].offsets.push({node:s,offset:0}):i.constraints[0].offsets.push({node:s,offset:.8*t/(this.output-1)*e++}),i.constraints[1].offsets.push({node:s,offset:-.8*n})),i.nodes.push({id:s,name:"hidden"==r.type?r.squash.name:r.type.toUpperCase(),activation:r.activation,bias:r.bias})}for(var a=this.connections.concat(this.selfconns),s=0;s<a.length;s++){var h=a[s];if(null==h.gater)i.links.push({source:this.nodes.indexOf(h.from),target:this.nodes.indexOf(h.to),weight:h.weight});else{var c=i.nodes.length;i.nodes.push({id:c,activation:h.gater.activation,name:"GATE"}),i.links.push({source:this.nodes.indexOf(h.from),target:c,weight:.5*h.weight}),i.links.push({source:c,target:this.nodes.indexOf(h.to),weight:.5*h.weight}),i.links.push({source:this.nodes.indexOf(h.gater),target:c,weight:h.gater.activation,gate:!0})}}return i},toJSON:function(){for(var t={nodes:[],connections:[],input:this.input,output:this.output,dropout:this.dropout},n=0;n<this.nodes.length;n++){var o=this.nodes[n],e=o.toJSON();if(e.index=n,t.nodes.push(e),0!=o.connections.self.weight){var e=o.connections.self.toJSON();e.from=n,e.to=n,e.gater=null!=o.connections.self.gater?this.nodes.indexOf(o.connections.self.gater):null,t.connections.push(e)}}for(var n=0;n<this.connections.length;n++){var i=this.connections[n],e=i.toJSON();e.from=this.nodes.indexOf(i.from),e.to=this.nodes.indexOf(i.to),e.gater=null!=i.gater?this.nodes.indexOf(i.gater):null,t.connections.push(e)}return t},set:function(t){for(var n=0;n<this.nodes.length;n++)this.nodes[n].bias=t.bias||this.nodes[n].bias,this.nodes[n].squash=t.squash||this.nodes[n].squash},evolve:function(t,n){function o(n){for(var o=0,s=0;s<i;s++)f&&n.clear(),o-=n.test(t,e).error;return o-=(n.nodes.length+n.connections.length+n.gates.length)*r,(o=isNaN(o)?-1/0:o)/i}var e=n.cost||s.Cost.MSE,i=n.amount||1,r=void 0!==n.growth?n.growth:1e-4,h=n.iterations||0,c=n.error||.005,u=n.log||0,f=n.clear||!1,l=n.schedule,p=Date.now();n.network=this;for(var d=new a(0,0,o,n),g=-1/0,v=-1/0,m=null;g<-c&&(0==h||d.generation<h);){d.evolve();var O=d.getFittest();f&&O.clear(),g=-O.test(t).error-(O.nodes.length+O.connections.length+O.gates.length)*r,g>v&&(v=g,m=O),u&&d.generation%u==0&&console.log("generation",d.generation,"error",O.score,"cost error",g),l&&d.generation%l.iterations==0&&l.function({error:g,iteration:d.generation})}f&&m.clear();var w={error:v,generations:d.generation,time:Date.now()-p};return this.nodes=m.nodes,this.connections=m.connections,this.gates=m.gates,this.selfconns=m.selfconns,w}},n.fromJSON=function(t){var o=new n(t.input,t.output);o.dropout=t.dropout,o.nodes=[],o.connections=[];for(var i=0;i<t.nodes.length;i++)o.nodes.push(e.fromJSON(t.nodes[i]));for(var i=0;i<t.connections.length;i++){var s=t.connections[i],r=o.connect(o.nodes[s.from],o.nodes[s.to])[0];r.weight=s.weight,null!=s.gater&&o.gate(o.nodes[s.gater],r)}return o},n.merge=function(t,o){if(t=n.fromJSON(t.toJSON()),o=n.fromJSON(o.toJSON()),t.output!=o.input)throw new Error("Output size of network1 should be the same as the input size of network2!");for(var e=0;e<o.connections.length;e++){var i=o.connections[e];if("input"==i.from.type){var s=o.nodes.indexOf(i.from);o.nodes[s];i.from=t.nodes[t.nodes.length-1-s]}}for(var e=o.input-1;e>=0;e--)o.nodes.splice(e,1);for(var e=t.nodes.length-t.output;e<t.nodes.length;e++)t.nodes[e].type="hidden";return t.connections=t.connections.concat(o.connections),t.nodes=t.nodes.concat(o.nodes),t},n.crossOver=function(t,o,s){if(t.input!=o.input||t.output!=o.output)throw new Error("Networks don't have the same input/output size!");var r=new n(t.input,t.output);r.connections=[],r.nodes=[];var a=t.score||0,h=o.score||0;if(a==h)var c=Math.max(t.nodes.length,o.nodes.length),u=Math.min(t.nodes.length,o.nodes.length),f=Math.floor(Math.random()*(c-u+1)+u);else if(a>h)var f=t.nodes.length;else var f=o.nodes.length;for(var l=t.output,p=0;p<f;p++){if(p<f-l){var d=Math.random(),g=d>=.5?t.nodes[p]:o.nodes[p],v=d<.5?t.nodes[p]:o.nodes[p];void 0!==g&&"output"!=g.type||(g=v)}else if(Math.random()>=.5)var g=t.nodes[t.nodes.length+p-f];else var g=o.nodes[o.nodes.length+p-f];var m=new e;m.bias=g.bias,m.squash=g.squash,m.type=g.type,r.nodes.push(m)}for(var O=[],w=[],p=0;p<t.connections.length;p++){var _=t.connections[p],A={weight:_.weight,from:t.nodes.indexOf(_.from),to:t.nodes.indexOf(_.to),gater:t.nodes.indexOf(_.gater)};i.innovationID(A.from,A.to);O.push(A)}for(var p=0;p<t.selfconns.length;p++){var _=t.selfconns[p],A={weight:_.weight,from:t.nodes.indexOf(_.from),to:t.nodes.indexOf(_.to),gater:t.nodes.indexOf(_.gater)};i.innovationID(A.from,A.to);O.push(A)}for(var p=0;p<o.connections.length;p++){var _=o.connections[p],A={weight:_.weight,from:o.nodes.indexOf(_.from),to:o.nodes.indexOf(_.to),gater:o.nodes.indexOf(_.gater)};i.innovationID(A.from,A.to);w.push(A)}for(var p=0;p<o.selfconns.length;p++){var _=o.selfconns[p],A={weight:_.weight,from:o.nodes.indexOf(_.from),to:o.nodes.indexOf(_.to),gater:o.nodes.indexOf(_.gater)};i.innovationID(A.from,A.to);w.push(A)}for(var N=[],p=O.length-1;p>=0;p--){for(var L=!1,T=w.length-1;T>=0;T--)if(O[p].from==w[T].from&&O[p].to==w[T].to){var _=Math.random()>=.5?O[p]:w[T];N.push(_),w[T]=[-1,-1];var L=!0;break}!L&&a>=h&&N.push(O[p])}if(h>=a)for(var p=0;p<w.length;p++)-1!=w[p][0]&&-1!=w[p][1]&&N.push(w[p]);for(var p=0;p<N.length;p++){var M=N[p];if(M.to<f&&M.from<f){var E=r.nodes[M.from],D=r.nodes[M.to],_=r.connect(E,D)[0];_.weight=M.weight,-1!=M.gater&&M.gater<f&&r.gate(r.nodes[M.gater],_)}}return r}}).call(n,o(0)(t))},function(t,n,o){(function(t){var n=o(7),e=o(1),i=o(2),s=o(5),r=o(6),a={Construct:function(t){for(var o=new n(0,0),e=[],a=0;a<t.length;a++)if(t[a]instanceof s)for(var h=0;h<t[a].nodes.length;h++)e.push(t[a].nodes[h]);else if(t[a]instanceof r)for(var h=0;h<t[a].nodes.length;h++)for(var c=0;c<t[a].nodes[h].nodes.length;c++)e.push(t[a].nodes[h].nodes[c]);else t[a]instanceof i&&e.push(t[a]);for(var u=[],f=[],a=e.length-1;a>=0;a--)"output"==e[a].type||e[a].connections.out.length+e[a].connections.gated.length==0?(e[a].type="output",o.output++,f.push(e[a]),e.splice(a,1)):"input"!=e[a].type&&e[a].connections.in.length||(e[a].type="input",o.input++,u.push(e[a]),e.splice(a,1));if(e=u.concat(e).concat(f),0==o.input||0==o.output)throw new Error("Given nodes have no clear input/output node!");for(var a=0;a<e.length;a++){for(var h=0;h<e[a].connections.out.length;h++)o.connections.push(e[a].connections.out[h]);for(var h=0;h<e[a].connections.gated.length;h++)o.gates.push(e[a].connections.gated[h]);0!=e[a].connections.self.weight&&o.selfconns.push(e[a].connections.self)}return o.nodes=e,o},Perceptron:function(){var t=Array.prototype.slice.call(arguments);if(t.length<3)throw new Error("not enough layers (minimum 3) !!");var n=[];n.push(new s(t[0]));for(var o=1;o<t.length;o++){var i=t[o],i=new s(i);n.push(i),n[o-1].connect(n[o],e.Connection.ALL_TO_ALL)}return a.Construct(n)},Random:function(t,o,i,s){s=s||{};for(var r=s.connections||2*o,a=s.backconnections||0,h=s.selfconnections||0,c=s.gates||0,u=new n(t,i),f=0;f<o;f++)u.mutate(e.Mutation.ADD_NODE);for(var f=0;f<r-o;f++)u.mutate(e.Mutation.ADD_CONN);for(var f=0;f<a;f++)u.mutate(e.Mutation.ADD_BACK_CONN);for(var f=0;f<h;f++)u.mutate(e.Mutation.ADD_SELF_CONN);for(var f=0;f<c;f++)u.mutate(e.Mutation.ADD_GATE);return u},LSTM:function(){var t=Array.prototype.slice.call(arguments);if(t.length<3)throw new Error("not enough layers (minimum 3) !!");var n=t.pop();if("number"==typeof n){var o=new s(n);n={}}else var o=new s(t.pop());o.set({type:"output"});var i={};i.memoryToMemory=n.memoryToMemory||!1,i.outputToMemory=n.outputToMemory||!1,i.outputToGates=n.outputToGates||!1,i.inputToOutput=void 0==n.inputToOutput||n.inputToOutput,i.inputToDeep=void 0==n.inputToDeep||n.inputToDeep;var r=new s(t.shift());r.set({type:"input"});var h=t,c=[];c.push(r);for(var u=r,f=0;f<h.length;f++){var l=h[f],p=new s(l),d=new s(l),g=new s(l),v=new s(l),m=f==h.length-1?o:new s(l);p.set({bias:1}),d.set({bias:1}),v.set({bias:1});var O=u.connect(g,e.Connection.ALL_TO_ALL);u.connect(p,e.Connection.ALL_TO_ALL),u.connect(v,e.Connection.ALL_TO_ALL),u.connect(d,e.Connection.ALL_TO_ALL),g.connect(p,e.Connection.ALL_TO_ALL),g.connect(d,e.Connection.ALL_TO_ALL),g.connect(v,e.Connection.ALL_TO_ALL);var w=g.connect(g,e.Connection.ONE_TO_ONE),_=g.connect(m,e.Connection.ALL_TO_ALL);if(p.gate(O,e.Gating.INPUT),d.gate(w,e.Gating.SELF),v.gate(_,e.Gating.OUTPUT),i.inputToDeep&&f>0){var O=r.connect(g,e.Connection.ALL_TO_ALL);p.gate(O,e.Gating.INPUT)}if(i.memoryToMemory){var O=g.connect(g,e.Connection.ALL_TO_ELSE);p.gate(O,e.Gating.INPUT)}if(i.outputToMemory){var O=o.connect(g,e.Connection.ALL_TO_ALL);p.gate(O,e.Gating.INPUT)}i.outputToGates&&(o.connect(p,e.Connection.ALL_TO_ALL),o.connect(d,e.Connection.ALL_TO_ALL),o.connect(v,e.Connection.ALL_TO_ALL)),c.push(p),c.push(d),c.push(g),c.push(v),f!=h.length-1&&c.push(m),u=m}return i.inputToOutput&&r.connect(o,e.Connection.ALL_TO_ALL),c.push(o),a.Construct(c)},GRU:function(){var t=Array.prototype.slice.call(arguments);if(t.length<3)throw new Error("not enough layers (minimum 3) !!");var n=new s(t.shift()),o=new s(t.pop()),e=t,i=[];i.push(n);for(var h=n,c=0;c<e.length;c++){var u=new r.GRU(e[c]);h.connect(u),h=u,i.push(u)}return h.connect(o),i.push(o),a.Construct(i)},Hopfield:function(t){var n=new s(t),o=new s(t);return n.connect(o,e.Connection.ALL_TO_ALL),n.set({type:"input"}),o.set({squash:e.Activation.STEP,type:"output"}),new a.Construct([n,o])},NARX:function(t,n,o,i,s){Array.isArray(n)||(n=[n]);var h=[],c=new r.Dense(t),u=new r.Memory(t,i),f=[],l=new r.Dense(o),p=new r.Memory(o,s);h.push(c),h.push(p);for(var d=0;d<n.length;d++){var g=new r.Dense(n[d]);f.push(g),h.push(g),void 0!==f[d-1]&&f[d-1].connect(g,e.Connection.ALL_TO_ALL)}return h.push(u),h.push(l),c.connect(f[0],e.Connection.ALL_TO_ALL),c.connect(u,e.Connection.ONE_TO_ONE,1),u.connect(f[0],e.Connection.ALL_TO_ALL),f[f.length-1].connect(l,e.Connection.ALL_TO_ALL),l.connect(p,e.Connection.ONE_TO_ONE,1),p.connect(f[0],e.Connection.ALL_TO_ALL),c.set({type:"input"}),l.set({type:"output"}),a.Construct(h)}};t&&(t.exports=a)}).call(n,o(0)(t))},function(t,n,o){(function(t){function n(t,n,o,s){this.input=t,this.output=n,this.fitness=o,s=s||{},this.equal=s.equal||!1,this.popsize=s.popsize||50,this.elitism=s.elitism||0,this.mutationRate=s.mutationRate||.3,this.mutationAmount=s.mutationAmount||1,this.selection=s.selection||i.Selection.POWER,this.crossover=s.crossover||[i.Crossover.SINGLE_POINT,i.Crossover.TWO_POINT,i.Crossover.UNIFORM,i.Crossover.AVERAGE],this.mutation=s.mutation||i.Mutation.FFW,this.generation=0,this.createPool(s.network||new e(this.input,this.output))}t&&(t.exports=n);var e=(o(2),o(7)),i=o(1),s=(i.Activation,i.Mutation,i.Selection);i.Crossover;n.prototype={createPool:function(t){this.population=[];for(var n=0;n<this.popsize;n++){var o=e.fromJSON(t.toJSON());o.score=null,this.population.push(o)}},evolve:function(){null==this.population[this.population.length-1].score&&this.evaluate(),this.sort();for(var t=[],n=0;n<this.elitism;n++)t.push(this.population[n]);for(var n=0;n<this.popsize-this.elitism;n++)t.push(this.getOffspring());this.population=t,this.mutate();for(var n=0;n<this.popsize;n++)this.population[n].score=null;this.generation++},getOffspring:function(){var t=this.getParent(),n=this.getParent();return this.equal&&(t.score=0,n.score=0),e.crossOver(t,n)},mutate:function(){for(var t=this.elitism;t<this.popsize;t++)if(Math.random()<=this.mutationRate)for(var n=0;n<this.mutationAmount;n++){var o=this.mutation[Math.floor(Math.random()*this.mutation.length)];this.population[t].mutate(o)}},evaluate:function(){for(var t=0;t<this.popsize;t++){var n=this.fitness(this.population[t]);this.population[t].score=n}},sort:function(){this.population.sort(function(t,n){return n.score-t.score})},getFittest:function(){return null==this.population[this.popsize-1].score&&this.evaluate(),this.sort(),this.population[0]},getAverage:function(){null==this.population[this.popsize-1].score&&this.evaluate();for(var t=0,n=0;n<this.popsize;n++)t+=this.population[n].score;return t/this.popsize},getParent:function(){switch(this.selection){case s.POWER:this.population[0].score<this.population[1].score&&this.sort();var t=Math.floor(Math.pow(Math.random(),this.selection.power)*this.popsize);return this.population[t];case s.FITNESS_PROPORTIONATE:for(var n=0,o=0,e=0;e<this.popsize;e++){var i=this.population[e].score;o=i<o?i:o,n+=i}o=Math.abs(o),n+=o*this.popsize;for(var r=Math.random()*n,a=0,e=0;e<this.popsize;e++){var h=this.population[e];if(a+=h.score+o,r<a)return h}return this.population[Math.floor(Math.random()*this.population.length)];case s.TOURNAMENT:if(this.selection.size>this.popsize)throw new Error("Your tournament size should be lower than the population size, please change Methods.Selection.TOURNAMENT.size");for(var c=[],e=0;e<this.selection.size;e++){var r=this.population[Math.floor(Math.random()*this.popsize)];c.push(r)}c.sort(function(t,n){return n.score-t.score});for(var e=0;e<this.selection.size;e++)if(Math.random()<this.selection.probability||e==this.selection.size-1)return c[e]}},export:function(){for(var t=[],n=0;n<this.popsize;n++){var o=this.population[o];t.push(o.toJSON())}return t},import:function(t){for(var n=[],o=0;o<t.length;o++){var i=t[o];n.push(e.fromJSON(i))}this.population=n,this.popsize=n.length}}}).call(n,o(0)(t))},function(t,n,o){(function(t){var n={LOGISTIC:function(t,n){var o=1/(1+Math.exp(-t));return n?o*(1-o):o},TANH:function(t,n){return n?1-Math.pow(Math.tanh(t),2):Math.tanh(t)},IDENTITY:function(t,n){return n?1:t},STEP:function(t,n){return n?0:t>0?1:0},RELU:function(t,n){return n?t>0?1:0:t>0?t:0},SOFTSIGN:function(t,n){var o=1+Math.abs(t);return n?t/Math.pow(o,2):t/o},SINUSOID:function(t,n){return n?Math.cos(t):Math.sin(t)},GAUSSIAN:function(t,n){var o=Math.exp(-Math.pow(t,2));return n?-2*t*o:o},BENT_IDENTITY:function(t,n){var o=Math.sqrt(Math.pow(t,2)+1);return n?t/(2*o)+1:(o-1)/2+t},BIPOLAR:function(t,n){return n?0:t>0?1:-1},BIPOLAR_SIGMOID:function(t,n){var o=2/(1+Math.exp(-t))-1;return n?.5*(1+o)*(1-o):o},HARD_TANH:function(t,n){return n?t>-1&&t<1?1:0:Math.max(-1,Math.min(1,t))},ABSOLUTE:function(t,n){return n?t<0?-1:1:Math.abs(t)},INVERSE:function(t,n){return n?-1:1-t},SELU:function(t,n){var o=1.6732632423543772,e=1.0507009873554805,i=(t>0?t:o*Math.exp(t)-o)*e;return n?t>0?e:i+o:i}};t&&(t.exports=n)}).call(n,o(0)(t))},function(t,n,o){(function(t){var n={ALL_TO_ALL:{name:"OUTPUT"},ALL_TO_ELSE:{name:"INPUT"},ONE_TO_ONE:{name:"SELF"}};t&&(t.exports=n)}).call(n,o(0)(t))},function(t,n,o){(function(t){var n={CROSS_ENTROPY:function(t,n){for(var o=0,e=0;e<n.length;e++)o-=t[e]*Math.log(Math.max(n[e],1e-15))+(1-t[e])*Math.log(1-Math.max(n[e],1e-15));return o},MSE:function(t,n){for(var o=0,e=0;e<n.length;e++)o+=Math.pow(t[e]-n[e],2);return o/n.length},BINARY:function(t,n){for(var o=0,e=0;e<n.length;e++)o+=Math.round(2*t[e])!=Math.round(2*n[e]);return o},MAE:function(t,n){for(var o=0,e=0;e<n.length;e++)o+=Math.abs(t[e]-n[e]);return o/n.length},MAPE:function(t,n){for(var o=0,e=0;e<n.length;e++)o+=Math.abs((n[e]-t[e])/Math.max(t[e],1e-15));return o/n.length},MSLE:function(t,n){for(var o=0,e=0;e<n.length;e++)o+=Math.log(Math.max(t[e],1e-15))-Math.log(Math.max(n[e],1e-15));return o},HINGE:function(t,n){for(var o=0,e=0;e<n.length;e++)o+=Math.max(0,1-t[e]*n[e]);return o}};t&&(t.exports=n)}).call(n,o(0)(t))},function(t,n,o){(function(t){var n={SINGLE_POINT:{name:"SINGLE_POINT",config:[.4]},TWO_POINT:{name:"TWO_POINT",config:[.4,.9]},UNIFORM:{name:"UNIFORM"},AVERAGE:{name:"AVERAGE"}};t&&(t.exports=n)}).call(n,o(0)(t))},function(t,n,o){(function(t){var n={OUTPUT:{name:"OUTPUT"},INPUT:{name:"INPUT"},SELF:{name:"SELF"}};t&&(t.exports=n)}).call(n,o(0)(t))},function(t,n,o){(function(t){var n=o(10),e={ADD_NODE:{name:"ADD_NODE"},SUB_NODE:{name:"SUB_NODE",keep_gates:!0},ADD_CONN:{name:"ADD_CONN"},SUB_CONN:{name:"REMOVE_CONN"},MOD_WEIGHT:{name:"MOD_WEIGHT",min:-1,max:1},MOD_BIAS:{name:"MOD_BIAS",min:-1,max:1},MOD_ACTIVATION:{name:"MOD_ACTIVATION",mutateOutput:!0,allowed:[n.LOGISTIC,n.TANH,n.RELU,n.IDENTITY,n.STEP,n.SOFTSIGN,n.SINUSOID,n.GAUSSIAN,n.BENT_IDENTITY,n.BIPOLAR,n.BIPOLAR_SIGMOID,n.HARD_TANH,n.ABSOLUTE,n.INVERSE,n.SELU]},ADD_SELF_CONN:{name:"ADD_SELF_CONN"},SUB_SELF_CONN:{name:"SUB_SELF_CONN"},ADD_GATE:{name:"ADD_GATE"},SUB_GATE:{name:"SUB_GATE"},ADD_BACK_CONN:{name:"ADD_BACK_CONN"},SUB_BACK_CONN:{name:"SUB_BACK_CONN"},SWAP_NODES:{name:"SWAP_NODES"}};e.ALL=[e.ADD_NODE,e.SUB_NODE,e.ADD_CONN,e.SUB_CONN,e.MOD_WEIGHT,e.MOD_BIAS,e.MOD_ACTIVATION,e.ADD_GATE,e.SUB_GATE,e.ADD_SELF_CONN,e.SUB_SELF_CONN,e.ADD_BACK_CONN,e.SUB_BACK_CONN],e.FFW=[e.ADD_NODE,e.SUB_NODE,e.ADD_CONN,e.SUB_CONN,e.MOD_WEIGHT,e.MOD_BIAS,e.MOD_ACTIVATION],t&&(t.exports=e)}).call(n,o(0)(t))},function(t,n,o){(function(t){var n={FIXED:function(){return function(t,n){return t}},STEP:function(t,n){return t=t||.9,n=n||100,function(o,e){return o*Math.pow(t,Math.floor(e/n))}},EXP:function(t){return t=t||.999,function(n,o){return n*Math.pow(t,o)}},INV:function(t,n){return t=t||.001,n=n||2,function(o,e){return o*Math.pow(1+t*e,-n)}}};t&&(t.exports=n)}).call(n,o(0)(t))},function(t,n,o){(function(t){var n={FITNESS_PROPORTIONATE:{name:"FITNESS_PROPORTIONATE"},POWER:{name:"POWER",power:4},TOURNAMENT:{name:"TOURNAMENT",size:5,probability:.5}};t&&(t.exports=n)}).call(n,o(0)(t))},function(t,n,o){var e,i,s={Node:o(2),Neat:o(9),Network:o(7),Methods:o(1),Architect:o(8),Group:o(5),Connection:o(4),Config:o(3),Layer:o(6)};e=[],void 0!==(i=function(){return s}.apply(n,e))&&(t.exports=i),void 0!==t&&t.exports&&(t.exports=s),"object"==typeof window&&(!function(){var t=window.neataptic;s.ninja=function(){return window.neataptic=t,s}}(),window.neataptic=s)}])}); |