/*! * The MIT License (MIT) * * Copyright 2017 Thomas Wagenaar . 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.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-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-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(t,n,o){n=n||0,t=t||.3;var e=0;if("output"==this.type)this.error.responsibility=this.error.projected=o-this.activation;else{for(var i=0;i=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=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 c=[];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 h=0;h=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 in this.nodes)n=this.nodes[n],n instanceof i?(void 0!==t.bias&&(n.bias=t.bias),n.squash=t.squash||n.squash,n.type=t.type||n.type):n instanceof s&&n.set(t)},disconnect:function(t,n){if(n=n||!1,t instanceof s)for(var o=0;o=this.nodes.length-this.output;i--)this.nodes[i].propagate(t,n,o[--e]);for(var i=this.nodes.length-this.output-1;i>=this.input;i--)this.nodes[i].propagate(t,n)},clear:function(){for(var t=0;t=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)}var c=[];for(var h in e){h=e[h];for(var u in a)if(u=a[u],!h.isProjectingTo(u)){var f=this.connect(h,u);c.push(f[0])}}for(var l in o){if(0==c.length)break;l=o[l];var p=Math.floor(Math.random()*c.length),f=c[p];this.gate(l,c[p]),c.splice(p,1)}for(var i=t.connections.gated.length-1;i>=0;i--){var f=t.connections.gated[i];this.ungate(f)}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 c.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(c.MOD_ACTIVATION);var a=Math.min(i,this.nodes.length-this.output);this.nodes.splice(a,0,s);var h=this.connect(n.from,s)[0],u=this.connect(s,n.to)[0];null!=o&&this.gate(o,Math.random()>=.5?h:u);break;case c.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 c.ADD_CONN:for(var l=[],p=0;p1&&A.to.connections.in.length>1&&this.nodes.indexOf(A.to)>this.nodes.indexOf(A.from)&&O.push(A);if(0==O.length){r.warnings&&console.warn("No connections to remove!");break}var w=O[Math.floor(Math.random()*O.length)];this.disconnect(w.from,w.to);break;case c.MOD_WEIGHT:var n=this.connections[Math.floor(Math.random()*this.connections.length)],_=Math.random()*(t.max-t.min)+t.min;n.weight+=_;break;case c.MOD_BIAS:var f=Math.floor(Math.random()*(this.nodes.length-this.input)+this.input),s=this.nodes[f];s.mutate(t);break;case c.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 c.ADD_SELF_CONN:for(var O=[],p=this.input;p1&&A.to.connections.in.length>1&&this.nodes.indexOf(A.from)>this.nodes.indexOf(A.to)&&O.push(A);if(0==O.length){r.warnings&&console.warn("No connections to remove!");break}var w=O[Math.floor(Math.random()*O.length)];this.disconnect(w.from,w.to);break;case c.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],L=d.bias,M=d.squash;d.bias=v.bias,d.squash=v.squash,v.bias=L,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,c=n.rate||.3,h=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(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 T=c,L=0,M=1;M>i&&(0==u||Lv&&(v=g,m=O),u&&d.generation%u==0&&console.log("generation",d.generation,"error",O.score,"cost error",g),l&&iteration%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(node in t.nodes)o.nodes.push(e.fromJSON(t.nodes[node]));for(i in t.connections){var i=t.connections[i],s=o.connect(o.nodes[i.from],o.nodes[i.to])[0];s.weight=i.weight,null!=i.gater&&o.gate(o.nodes[i.gater],s)}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(conn in o.connections)if(conn=o.connections[conn],"input"==conn.from.type){var e=o.nodes.indexOf(conn.from);o.nodes[e];conn.from=t.nodes[t.nodes.length-1-e]}for(var i=o.input-1;i>=0;i--)o.nodes.splice(i,1);for(var i=t.nodes.length-t.output;ic)var f=t.nodes.length;else var f=o.nodes.length;for(var l=t.output,p=0;p=.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=0;p--){for(var T=!1,L=w.length-1;L>=0;L--)if(O[p].from==w[L].from&&O[p].to==w[L].to){var _=Math.random()>=.5?O[p]:w[L];N.push(_),w[L]=[-1,-1];var T=!0;break}!T&&a>=c&&N.push(O[p])}if(c>=a)for(var p=0;p=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;a0){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)),h.push(p),h.push(d),h.push(g),h.push(v),f!=c.length-1&&h.push(m),u=m}return i.inputToOutput&&r.connect(o,e.Connection.ALL_TO_ALL),h.push(o),a.Construct(h)},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 c=n,h=0;hthis.popsize)throw new Error("Your tournament size should be lower than the population size, please change Methods.Selection.TOURNAMENT.size");for(var h=[],e=0;e0?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