/*! * 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(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=a.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),r=o(3),a=i.Activation,c=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-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-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=0;t--){var o=n[t],e=this.connections.gated.indexOf(o);this.connections.gated.splice(e,1),o.gater=null}},clear:function(){for(var n=0;n=0;e--)void 0===o?this.nodes[e].propagate(n,t):this.nodes[e].propagate(n,t,o[e])},connect:function(n,o,a){var c=[];if(n instanceof t){if(void 0===o&&(this!=n?(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(n,t):this.nodes[e].propagate(n,t,o[e])},connect:function(n,o,e){if(n instanceof s||n instanceof i)var r=this.output.connect(n,o,e);else if(n instanceof t)var r=n.input(this,o,e);return r},gate:function(n,t){this.output.gate(n,t)},set:function(n){for(var t in this.nodes)t=this.nodes[t],t instanceof i?(void 0!==n.bias&&(t.bias=n.bias),t.squash=n.squash||t.squash,t.type=n.type||t.type):t instanceof s&&t.set(n)},disconnect:function(n,t){if(t=t||!1,n instanceof s)for(var o=0;o=this.nodes.length-this.output;i--)this.nodes[i].propagate(n,t,o[--e]);for(var i=this.nodes.length-this.output-1;i>=this.input;i--)this.nodes[i].propagate(n,t)},clear:function(){for(var n=0;n=0;i--){var r=n.connections.in[i];s.Mutation.SUB_NODE.keep_gates&&null!=r.gater&&r.gater!=n&&o.push(r.gater),e.push(r.from),this.disconnect(r.from,n)}for(var a=[],i=n.connections.out.length-1;i>=0;i--){var r=n.connections.out[i];s.Mutation.SUB_NODE.keep_gates&&null!=r.gater&&r.gater!=n&&o.push(r.gater),a.push(r.to),this.disconnect(n,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=n.connections.gated.length-1;i>=0;i--){var f=n.connections.gated[i];this.ungate(f)}this.disconnect(n,n),this.nodes.splice(t,1)},mutate:function(n){if(void 0===n)throw new Error("No (correct) mutate method given!");switch(n){case c.ADD_NODE:var t=this.connections[Math.floor(Math.random()*this.connections.length)],o=t.gater;this.disconnect(t.from,t.to);var i=this.nodes.indexOf(t.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(t.from,s)[0],u=this.connect(s,t.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 t=this.connections[Math.floor(Math.random()*this.connections.length)],_=Math.random()*(n.max-n.min)+n.min;t.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(n);break;case c.MOD_ACTIVATION:if(!n.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-(n.mutateOutput?0:this.output)-this.input)+this.input),s=this.nodes[f];s.mutate(n);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(n,t){t=t||{},void 0===t.rate&&r.warnings&&console.warn("Using default learning rate, please define a rate!"),void 0===t.iterations&&r.warnings&&console.warn("No target iterations given, running until error is reached!");var o=Date.now(),e=t.log||!1,i=t.error||.05,a=t.cost||s.Cost.MSE,c=t.rate||.3,h=t.shuffle||!1,u=t.iterations||0,f=t.crossValidate||!1,l=t.clear||!1,p=t.dropout||0,d=t.momentum||0,g=t.ratePolicy||s.Rate.FIXED(),v=t.schedule;if(void 0===t.iterations&&void 0===t.error?r.warnings&&console.warn("At least one of the following options must be specified: error, iterations"):void 0===t.error&&(i=-1),this.dropout=p,f)var m=t.crossValidate.testSize,O=t.crossValidate.testError,w=Math.ceil((1-m)*n.length),_=n.slice(0,w),A=n.slice(w);for(var N=c,T=0,L=1;L>i&&(0==u||Tv&&(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:c})}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}},t.fromJSON=function(n){var o=new t(n.input,n.output);o.dropout=n.dropout,o.nodes=[],o.connections=[];for(node in n.nodes)o.nodes.push(e.fromJSON(n.nodes[node]));for(i in n.connections){var i=n.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},t.merge=function(n,o){if(n=t.fromJSON(n.toJSON()),o=t.fromJSON(o.toJSON()),n.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=n.nodes[n.nodes.length-1-e]}for(var i=o.input-1;i>=0;i--)o.nodes.splice(i,1);for(var i=n.nodes.length-n.output;ic)var f=n.nodes.length;else var f=o.nodes.length;for(var l=n.output,p=0;p=.5?n.nodes[p]:o.nodes[p],v=d<.5?n.nodes[p]:o.nodes[p];void 0!==g&&"output"!=g.type||(g=v)}else if(Math.random()>=.5)var g=n.nodes[n.nodes.length+p-f];else var g=o.nodes[o.nodes.length+p-f];r.nodes.push(g)}for(g in r.nodes)r.nodes[g]=e.fromJSON(r.nodes[g].toJSON());var m={},O={},w=n.connections.concat(n.selfconns);for(_ in w){var _=w[_],A={weight:_.weight,from:n.nodes.indexOf(_.from),to:n.nodes.indexOf(_.to),gater:n.nodes.indexOf(_.gater)},N=i.innovationID(A.from,A.to);m[N]=A}var T=o.connections.concat(o.selfconns);for(_ in T){var _=T[_],A={weight:_.weight,from:o.nodes.indexOf(_.from),to:o.nodes.indexOf(_.to),gater:o.nodes.indexOf(_.gater)},N=i.innovationID(A.from,A.to);O[N]=A}var L={};for(var N in m)N in O&&(L[N]=[m[N],O[N]],delete m[N],delete O[N]);var M=[];for(_ in L){var _=Math.random()>=.5?L[_][0]:L[_][1];M.push(_)}if(a==c){for(_ in m)M.push(m[_]);for(_ in O)M.push(O[_])}else if(a>c)for(_ in m)M.push(m[_]);else for(_ in O)M.push(O[_]);for(_ in M){var E=M[_];if(E.to=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 n=Array.prototype.slice.call(arguments);if(n.length<3)throw new Error("not enough layers (minimum 3) !!");var t=new s(n.shift()),o=new s(n.pop()),e=n,i=[];i.push(t);for(var c=t,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 c=[],h=0;h0?1:0},RELU:function(n,t){return t?n>0?1:0:n>0?n:0},SOFTSIGN:function(n,t){var o=1+Math.abs(n);return t?n/Math.pow(o,2):n/o},SINUSOID:function(n,t){return t?Math.cos(n):Math.sin(n)},GAUSSIAN:function(n,t){var o=Math.exp(-Math.pow(n,2));return t?-2*n*o:o},BENT_IDENTITY:function(n,t){var o=Math.sqrt(Math.pow(n,2)+1);return t?n/(2*o)+1:(o-1)/2+n},BIPOLAR:function(n,t){return t?0:n>0?1:-1},BIPOLAR_SIGMOID:function(n,t){var o=2/(1+Math.exp(-n))-1;return t?.5*(1+o)*(1-o):o},HARD_TANH:function(n,t){return t?n>-1&&n<1?1:0:Math.max(-1,Math.min(1,n))},ABSOLUTE:function(n,t){return t?n<0?-1:1:Math.abs(n)},INVERSE:function(n,t){return t?-1:1-n}};n&&(n.exports=t)}).call(t,o(0)(n))},function(n,t,o){(function(n){var t={ALL_TO_ALL:{name:"OUTPUT"},ALL_TO_ELSE:{name:"INPUT"},ONE_TO_ONE:{name:"SELF"}};n&&(n.exports=t)}).call(t,o(0)(n))},function(n,t,o){(function(n){var t={CROSS_ENTROPY:function(n,t){for(var o=0,e=0;e