diff --git a/dist/neataptic.js b/dist/neataptic.js index 54e8295..77a61a7 100644 --- a/dist/neataptic.js +++ b/dist/neataptic.js @@ -2004,17 +2004,26 @@ var Architect = { } } - // Calculate input and output size - for(var node in nodes){ - if(nodes[node].connections.out.length + nodes[node].connections.gated.length == 0){ - nodes[node].type = 'output'; + // Determine input and output nodes + var inputs = []; + var outputs = []; + for(var i = nodes.length - 1; i >= 0; i--){ + if(nodes[i].connections.out.length + nodes[i].connections.gated.length == 0){ + nodes[i].type = 'output'; network.output++; - } else if(!nodes[node].connections.in.length){ - nodes[node].type = 'input'; + outputs.push(nodes[i]); + nodes.splice(i, 1); + } else if(!nodes[i].connections.in.length){ + nodes[i].type = 'input'; network.input++; + inputs.push(nodes[i]); + nodes.splice(i, 1); } } + // Input nodes are always first, output nodes are always last + nodes = inputs.concat(nodes).concat(outputs); + if(network.input == 0 || network.output == 0){ throw new Error('Given nodes have no clear input/output node!'); } diff --git a/dist/neataptic.min.js b/dist/neataptic.min.js index 4acb4aa..0efa113 100644 --- a/dist/neataptic.min.js +++ b/dist/neataptic.min.js @@ -22,4 +22,4 @@ * THE SOFTWARE * */ -!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={};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=16)}([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(7),Mutation:o(14),Selection:o(15),Crossover:o(12),Cost:o(11),Gating:o(13),Connection:o(10)};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.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.derivative=0,this.bias=0,this.activation;this.old=this.state,this.state=this.connections.self.gain*this.connections.self.weight*this.state+this.bias;for(var t in this.connections.in)t=this.connections.in[t],this.state+=t.from.activation*t.weight*t.gain;this.activation=this.squash(this.state),this.derivative=this.squash(this.state,!0);var o=[],e=[];for(var i in this.connections.gated){i=this.connections.gated[i];var s=i.to,r=o.indexOf(s);r>-1?e[r]+=i.weight*i.from.activation:(o.push(s),e.push(s.connections.self.gater==this?s.old:0),e[e.length-1]+=i.weight*i.from.activation)}for(var t in this.connections.in){t=this.connections.in[t],t.elegibility=this.connections.self.gain*this.connections.self.weight*t.elegibility+t.from.activation*t.gain;for(var a=0;a-1?t.xtrace.values[r]=s.connections.self.gain*s.connections.self.weight*t.xtrace.values[r]+this.derivative*t.elegibility*c:(t.xtrace.nodes.push(s),t.xtrace.values.push(this.derivative*t.elegibility*c))}}for(var t in this.connections.gated)this.connections.gated[t].gain=this.activation;return this.activation},propagate:function(n,t){var o=0;if("output"==this.type)this.error.responsibility=this.error.projected=t-this.activation;else{for(var e in this.connections.out){var e=this.connections.out[e],i=e.to;o+=i.error.responsibility*e.weight*e.gain}this.error.projected=this.derivative*o,o=0;for(var s in this.connections.gated){s=this.connections.gated[s];var i=s.to,r=i.connections.self.gater==this?i.old:0;r+=s.weight*s.from.activation,o+=i.error.responsibility*r}this.error.gated=this.derivative*o,this.error.responsibility=this.error.projected+this.error.gated}n=n||.1;for(var e in this.connections.in){for(var e=this.connections.in[e],a=this.error.projected*e.elegibility,c=0;c=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 in this.connections.in)n=this.connections.in[n],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(n){if(void 0===n)throw new Error("No mutate method given!");if(!n.name in i.Mutation)throw new Error("This method does not exist!");switch(n){case c.MOD_ACTIVATION:for(var t=Math.floor(Math.random()*c.MOD_ACTIVATION.allowed.length);c.MOD_ACTIVATION.allowed[t]==this.squash;)t=Math.floor(Math.random()*c.MOD_ACTIVATION.allowed.length);this.squash=c.MOD_ACTIVATION.allowed[t];break;case c.MOD_BIAS:var o=Math.random()*(c.MOD_BIAS.config.max-c.MOD_BIAS.config.min)+c.MOD_BIAS.config.min;this.bias+=o}},isProjectingTo:function(n){for(conn in this.connections.out)if(conn=this.connections.out[conn],conn.to==n)return!0;return!1},isProjectedBy:function(n){for(conn in this.connections.in)if(conn=this.connections.in[conn],conn.from==n)return!0;return!1},toJSON:function(){return{bias:this.bias,type:this.type,squash:this.squash.name}}},t.fromJSON=function(n){var o=new t;o.bias=n.bias,o.type=n.type;for(squash in a)if(a[squash].name==n.squash){o.squash=a[squash];break}return o}}).call(t,o(0)(n))},function(n,t,o){(function(n){var t={warnings:!0};n&&(n.exports=t)}).call(t,o(0)(n))},function(n,t,o){(function(n){function t(n,t,o){this.from=n,this.to=t,this.gain=1,this.weight=void 0===o?.2*Math.random()-.1:o,this.gater=null,this.elegibility=0,this.xtrace={nodes:[],values:[]}}n&&(n.exports=t),t.prototype={toJSON:function(){return{weight:this.weight}}},t.innovationID=function(n,t){return.5*(n+t)*(n+t+1)+t}}).call(t,o(0)(n))},function(n,t,o){(function(n){function t(n){this.nodes=[],this.connections={in:[],out:[],self:[]};for(var t=0;t=0;o--)void 0===t?this.nodes[o].propagate(n):this.nodes[o].propagate(n,t[o])},connect:function(n,o){var r=[];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 a=0;a=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 d in o){if(0==c.length)break;d=o[d];var l=Math.floor(Math.random()*c.length),f=c[l];this.gate(d,c[l]),c.splice(l,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 a.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(a.MOD_ACTIVATION);var c=Math.min(i,this.nodes.length-this.output);this.nodes.splice(c,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 a.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),s=this.nodes[f];this.remove(s);break;case a.ADD_CONN:for(var d=[],l=0;l1&&A.to.connections.in.length>1&&this.nodes.indexOf(A.to)>this.nodes.indexOf(A.from)&&N.push(A);if(0==N.length){r.warnings&&console.warn("No connections to remove!");break}var M=N[Math.floor(Math.random()*N.length)];this.disconnect(M.from,M.to);break;case a.MOD_WEIGHT:var t=this.connections[Math.floor(Math.random()*this.connections.length)],T=Math.random()*(a.MOD_WEIGHT.config.max-a.MOD_WEIGHT.config.min)+a.MOD_WEIGHT.config.min;t.weight+=T;break;case a.MOD_BIAS:var f=Math.floor(Math.random()*(this.nodes.length-this.input)+this.input),s=this.nodes[f];s.mutate(a.MOD_BIAS);break;case a.MOD_ACTIVATION:var f=Math.floor(Math.random()*(this.nodes.length-this.input)+this.input),s=this.nodes[f];s.mutate(a.MOD_ACTIVATION);break;case a.ADD_SELF_CONN:for(var N=[],l=this.input;l1&&A.to.connections.in.length>1&&this.nodes.indexOf(A.from)>this.nodes.indexOf(A.to)&&N.push(A);if(0==N.length){r.warnings&&console.warn("No connections to remove!");break}var M=N[Math.floor(Math.random()*N.length)];this.disconnect(M.from,M.to);break;case a.SWAP_NODES:var f=Math.floor(Math.random()*(this.nodes.length-this.input)+this.input),p=this.nodes[f],f=Math.floor(Math.random()*(this.nodes.length-this.input)+this.input),g=this.nodes[f],E=p.bias,x=p.squash;p.bias=g.bias,p.squash=g.squash,g.bias=E,g.squash=x}},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||.005,a=t.cost||s.Cost.MSE,c=t.rate||.3,h=t.shuffle||!1,u=t.iterations||0,f=t.crossValidate||!1,d=t.clear||!1;if(f)var l=t.crossValidate.testSize,p=t.crossValidate.testError,v=Math.ceil((1-l)*n.length),g=n.slice(0,v),m=n.slice(v);var O=c,w=0;Array.isArray(c)&&(w=Math.floor(u/c.length));for(var N=0,M=1;M>i&&(0==u||N0){O=c[Math.floor(u/w)]||O}if(f?(this._trainSet(g,O,a),d&&this.clear(),M+=this.test(m,a).error,d&&this.clear()):(M+=this._trainSet(n,O,a),d&&this.clear(),M/=n.length),h)for(var T,A,_=n.length;_;T=Math.floor(Math.random()*_),A=n[--_],n[_]=n[T],n[T]=A);e&&N%e==0&&console.log("iterations",N,"error",M,"rate",O)}return{error:M,iterations:N,time:Date.now()-o}},_trainSet:function(n,t,o){var e=0;for(var i in n){var s=n[i].input,r=n[i].output,a=this.activate(s);this.propagate(t,r),e+=o(r,a)}return e},test:function(n,t){t=t||s.Cost.MSE;var o,e,i,r=0,a=Date.now();for(var c in n)o=n[c].input,i=n[c].output,e=this.activate(o),r+=t(i,e);return r/=n.length,{error:r,time:Date.now()-a}},graph:function(n,t){var o=0,e=0,i={nodes:[],links:[],constraints:[{type:"alignment",axis:"x",offsets:[]},{type:"alignment",axis:"y",offsets:[]}]};for(a in this.nodes){var s=this.nodes[a];"input"==s.type?(1==this.input?i.constraints[0].offsets.push({node:a,offset:0}):i.constraints[0].offsets.push({node:a,offset:.8*n/(this.input-1)*o++}),i.constraints[1].offsets.push({node:a,offset:0})):"output"==s.type&&(1==this.output?i.constraints[0].offsets.push({node:a,offset:0}):i.constraints[0].offsets.push({node:a,offset:.8*n/(this.output-1)*e++}),i.constraints[1].offsets.push({node:a,offset:-.8*t})),i.nodes.push({id:a,name:"hidden"==s.type?s.squash.name:s.type.toUpperCase(),activation:s.activation})}var r=this.connections.concat(this.selfconns);for(connection in r)if(connection=r[connection],null==connection.gater)i.links.push({source:this.nodes.indexOf(connection.from),target:this.nodes.indexOf(connection.to),weight:connection.weight});else{var a=i.nodes.length;i.nodes.push({id:a,activation:connection.gater.activation,name:"GATE"}),i.links.push({source:this.nodes.indexOf(connection.from),target:a,weight:.5*connection.weight}),i.links.push({source:a,target:this.nodes.indexOf(connection.to),weight:.5*connection.weight}),i.links.push({source:this.nodes.indexOf(connection.gater),target:a,weight:connection.gater.activation,gate:!0})}return i},toJSON:function(){var n={nodes:[],connections:[],input:this.input,output:this.output};for(index in this.nodes){var t=this.nodes[index],o=t.toJSON();if(o.index=index,n.nodes.push(o),0!=t.connections.self.weight){var o=t.connections.self.toJSON();o.from=index,o.to=index,o.gater=null!=t.connections.self.gater?this.nodes.indexOf(t.connections.self.gater):null,n.connections.push(o)}}for(e in this.connections){var e=this.connections[e],o=e.toJSON();o.from=this.nodes.indexOf(e.from),o.to=this.nodes.indexOf(e.to),o.gater=null!=e.gater?this.nodes.indexOf(e.gater):null,n.connections.push(o)}return n},set:function(n){for(var t in this.nodes)this.nodes[t].bias=n.bias||this.nodes[t].bias,this.nodes[t].squash=n.squash||this.nodes[t].squash}},t.fromJSON=function(n){var o=new t(n.input,n.output);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=n.nodes.length-1-n.output;ic)var f=n.nodes.length;else var f=o.nodes.length;for(var d=0;d=f-n.output)for(;null==l||"output"!=l.type;)l=Math.random()>=.5?n.nodes[d]:o.nodes[d];else for(;null==l||d=.5?n.nodes[d]:o.nodes[d];r.nodes.push(l)}else d=f-n.output?r.nodes.push(n.nodes[n.nodes.length-(d-(f-n.output-1))]):r.nodes.push(n.nodes[d]):d=f-n.output?r.nodes.push(o.nodes[o.nodes.length-(d-(f-n.output-1))]):r.nodes.push(o.nodes[d]));for(l in r.nodes)r.nodes[l]=e.fromJSON(r.nodes[l].toJSON());var p={},v={},g=n.connections.concat(n.selfconns);for(m in g){var m=g[m],O={weight:m.weight,from:n.nodes.indexOf(m.from),to:n.nodes.indexOf(m.to),gater:n.nodes.indexOf(m.gater)};if(O.to==n.nodes.length-1){if(!(O.from=.5?M[m][0]:M[m][1];T.push(m)}if(a==c){for(m in p)T.push(p[m]);for(m in v)T.push(v[m])}else if(a>c)for(m in p)T.push(p[m]);else for(m in v)T.push(v[m]);for(m in T){var A=T[m];if(A.to0?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)}};n&&(n.exports=t)}).call(t,o(0)(n))},function(n,t,o){(function(n){var t=o(6),e=o(1),i=o(2),s=o(5),r={Construct:function(n){var o=new t(0,0),e=[];for(item in n)if(n[item]instanceof s)for(var r in n[item].nodes)e.push(n[item].nodes[r]);else n[item]instanceof i&&e.push(n[item]);for(var r in e)e[r].connections.out.length+e[r].connections.gated.length==0?(e[r].type="output",o.output++):e[r].connections.in.length||(e[r].type="input",o.input++);if(0==o.input||0==o.output)throw new Error("Given nodes have no clear input/output node!");for(var r in e){for(var a in e[r].connections.out)o.connections.push(e[r].connections.out[a]);for(var a in e[r].connections.gated)o.gates.push(e[r].connections.gated[a]);0!=e[r].connections.self.weight&&o.selfconns.push(e[r].connections.self)}return o.nodes=e,o},Perceptron:function(){var n=Array.prototype.slice.call(arguments);if(n.length<3)throw new Error("not enough layers (minimum 3) !!");var t=[];t.push(new s(n[0]));for(var o=1;o=.5?1:0;return n},o}};n&&(n.exports=r)}).call(t,o(0)(n))},function(n,t,o){(function(n){function t(n,t,o,s){this.input=n,this.output=t,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.FITNESS_PROPORTIONATE],this.crossover=s.crossover||[i.Crossover.SINGLE_POINT,i.Crossover.TWO_POINT,i.Crossover.UNIFORM,i.Crossover.AVERAGE],this.mutation=s.mutation||[i.Mutation.ADD_CONN,i.Mutation.SUB_CONN,i.Mutation.ADD_NODE,i.Mutation.SUB_NODE,i.Mutation.MOD_BIAS,i.Mutation.MOD_WEIGHT,i.Mutation.MOD_ACTIVATION],this.generation=0,this.createPool(s.network||new e(this.input,this.output))}n&&(n.exports=t);var e=(o(2),o(6)),i=o(1),s=(i.Activation,i.Mutation,i.Selection);i.Crossover;t.prototype={createPool:function(n){this.population=[];for(var t=0;t-1?e[r]+=i.weight*i.from.activation:(o.push(s),e.push(s.connections.self.gater==this?s.old:0),e[e.length-1]+=i.weight*i.from.activation)}for(var t in this.connections.in){t=this.connections.in[t],t.elegibility=this.connections.self.gain*this.connections.self.weight*t.elegibility+t.from.activation*t.gain;for(var a=0;a-1?t.xtrace.values[r]=s.connections.self.gain*s.connections.self.weight*t.xtrace.values[r]+this.derivative*t.elegibility*c:(t.xtrace.nodes.push(s),t.xtrace.values.push(this.derivative*t.elegibility*c))}}for(var t in this.connections.gated)this.connections.gated[t].gain=this.activation;return this.activation},propagate:function(n,t){var o=0;if("output"==this.type)this.error.responsibility=this.error.projected=t-this.activation;else{for(var e in this.connections.out){var e=this.connections.out[e],i=e.to;o+=i.error.responsibility*e.weight*e.gain}this.error.projected=this.derivative*o,o=0;for(var s in this.connections.gated){s=this.connections.gated[s];var i=s.to,r=i.connections.self.gater==this?i.old:0;r+=s.weight*s.from.activation,o+=i.error.responsibility*r}this.error.gated=this.derivative*o,this.error.responsibility=this.error.projected+this.error.gated}n=n||.1;for(var e in this.connections.in){for(var e=this.connections.in[e],a=this.error.projected*e.elegibility,c=0;c=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 in this.connections.in)n=this.connections.in[n],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(n){if(void 0===n)throw new Error("No mutate method given!");if(!n.name in i.Mutation)throw new Error("This method does not exist!");switch(n){case c.MOD_ACTIVATION:for(var t=Math.floor(Math.random()*c.MOD_ACTIVATION.allowed.length);c.MOD_ACTIVATION.allowed[t]==this.squash;)t=Math.floor(Math.random()*c.MOD_ACTIVATION.allowed.length);this.squash=c.MOD_ACTIVATION.allowed[t];break;case c.MOD_BIAS:var o=Math.random()*(c.MOD_BIAS.config.max-c.MOD_BIAS.config.min)+c.MOD_BIAS.config.min;this.bias+=o}},isProjectingTo:function(n){for(conn in this.connections.out)if(conn=this.connections.out[conn],conn.to==n)return!0;return!1},isProjectedBy:function(n){for(conn in this.connections.in)if(conn=this.connections.in[conn],conn.from==n)return!0;return!1},toJSON:function(){return{bias:this.bias,type:this.type,squash:this.squash.name}}},t.fromJSON=function(n){var o=new t;o.bias=n.bias,o.type=n.type;for(squash in a)if(a[squash].name==n.squash){o.squash=a[squash];break}return o}}).call(t,o(0)(n))},function(n,t,o){(function(n){var t={warnings:!0};n&&(n.exports=t)}).call(t,o(0)(n))},function(n,t,o){(function(n){function t(n,t,o){this.from=n,this.to=t,this.gain=1,this.weight=void 0===o?.2*Math.random()-.1:o,this.gater=null,this.elegibility=0,this.xtrace={nodes:[],values:[]}}n&&(n.exports=t),t.prototype={toJSON:function(){return{weight:this.weight}}},t.innovationID=function(n,t){return.5*(n+t)*(n+t+1)+t}}).call(t,o(0)(n))},function(n,t,o){(function(n){function t(n){this.nodes=[],this.connections={in:[],out:[],self:[]};for(var t=0;t=0;o--)void 0===t?this.nodes[o].propagate(n):this.nodes[o].propagate(n,t[o])},connect:function(n,o){var r=[];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 a=0;a=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 d=Math.floor(Math.random()*c.length),f=c[d];this.gate(l,c[d]),c.splice(d,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 a.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(a.MOD_ACTIVATION);var c=Math.min(i,this.nodes.length-this.output);this.nodes.splice(c,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 a.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),s=this.nodes[f];this.remove(s);break;case a.ADD_CONN:for(var l=[],d=0;d1&&A.to.connections.in.length>1&&this.nodes.indexOf(A.to)>this.nodes.indexOf(A.from)&&N.push(A);if(0==N.length){r.warnings&&console.warn("No connections to remove!");break}var M=N[Math.floor(Math.random()*N.length)];this.disconnect(M.from,M.to);break;case a.MOD_WEIGHT:var t=this.connections[Math.floor(Math.random()*this.connections.length)],T=Math.random()*(a.MOD_WEIGHT.config.max-a.MOD_WEIGHT.config.min)+a.MOD_WEIGHT.config.min;t.weight+=T;break;case a.MOD_BIAS:var f=Math.floor(Math.random()*(this.nodes.length-this.input)+this.input),s=this.nodes[f];s.mutate(a.MOD_BIAS);break;case a.MOD_ACTIVATION:var f=Math.floor(Math.random()*(this.nodes.length-this.input)+this.input),s=this.nodes[f];s.mutate(a.MOD_ACTIVATION);break;case a.ADD_SELF_CONN:for(var N=[],d=this.input;d1&&A.to.connections.in.length>1&&this.nodes.indexOf(A.from)>this.nodes.indexOf(A.to)&&N.push(A);if(0==N.length){r.warnings&&console.warn("No connections to remove!");break}var M=N[Math.floor(Math.random()*N.length)];this.disconnect(M.from,M.to);break;case a.SWAP_NODES:var f=Math.floor(Math.random()*(this.nodes.length-this.input)+this.input),p=this.nodes[f],f=Math.floor(Math.random()*(this.nodes.length-this.input)+this.input),g=this.nodes[f],E=p.bias,x=p.squash;p.bias=g.bias,p.squash=g.squash,g.bias=E,g.squash=x}},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||.005,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;if(f)var d=t.crossValidate.testSize,p=t.crossValidate.testError,v=Math.ceil((1-d)*n.length),g=n.slice(0,v),m=n.slice(v);var O=c,w=0;Array.isArray(c)&&(w=Math.floor(u/c.length));for(var N=0,M=1;M>i&&(0==u||N0){O=c[Math.floor(u/w)]||O}if(f?(this._trainSet(g,O,a),l&&this.clear(),M+=this.test(m,a).error,l&&this.clear()):(M+=this._trainSet(n,O,a),l&&this.clear(),M/=n.length),h)for(var T,A,_=n.length;_;T=Math.floor(Math.random()*_),A=n[--_],n[_]=n[T],n[T]=A);e&&N%e==0&&console.log("iterations",N,"error",M,"rate",O)}return{error:M,iterations:N,time:Date.now()-o}},_trainSet:function(n,t,o){var e=0;for(var i in n){var s=n[i].input,r=n[i].output,a=this.activate(s);this.propagate(t,r),e+=o(r,a)}return e},test:function(n,t){t=t||s.Cost.MSE;var o,e,i,r=0,a=Date.now();for(var c in n)o=n[c].input,i=n[c].output,e=this.activate(o),r+=t(i,e);return r/=n.length,{error:r,time:Date.now()-a}},graph:function(n,t){var o=0,e=0,i={nodes:[],links:[],constraints:[{type:"alignment",axis:"x",offsets:[]},{type:"alignment",axis:"y",offsets:[]}]};for(a in this.nodes){var s=this.nodes[a];"input"==s.type?(1==this.input?i.constraints[0].offsets.push({node:a,offset:0}):i.constraints[0].offsets.push({node:a,offset:.8*n/(this.input-1)*o++}),i.constraints[1].offsets.push({node:a,offset:0})):"output"==s.type&&(1==this.output?i.constraints[0].offsets.push({node:a,offset:0}):i.constraints[0].offsets.push({node:a,offset:.8*n/(this.output-1)*e++}),i.constraints[1].offsets.push({node:a,offset:-.8*t})),i.nodes.push({id:a,name:"hidden"==s.type?s.squash.name:s.type.toUpperCase(),activation:s.activation})}var r=this.connections.concat(this.selfconns);for(connection in r)if(connection=r[connection],null==connection.gater)i.links.push({source:this.nodes.indexOf(connection.from),target:this.nodes.indexOf(connection.to),weight:connection.weight});else{var a=i.nodes.length;i.nodes.push({id:a,activation:connection.gater.activation,name:"GATE"}),i.links.push({source:this.nodes.indexOf(connection.from),target:a,weight:.5*connection.weight}),i.links.push({source:a,target:this.nodes.indexOf(connection.to),weight:.5*connection.weight}),i.links.push({source:this.nodes.indexOf(connection.gater),target:a,weight:connection.gater.activation,gate:!0})}return i},toJSON:function(){var n={nodes:[],connections:[],input:this.input,output:this.output};for(index in this.nodes){var t=this.nodes[index],o=t.toJSON();if(o.index=index,n.nodes.push(o),0!=t.connections.self.weight){var o=t.connections.self.toJSON();o.from=index,o.to=index,o.gater=null!=t.connections.self.gater?this.nodes.indexOf(t.connections.self.gater):null,n.connections.push(o)}}for(e in this.connections){var e=this.connections[e],o=e.toJSON();o.from=this.nodes.indexOf(e.from),o.to=this.nodes.indexOf(e.to),o.gater=null!=e.gater?this.nodes.indexOf(e.gater):null,n.connections.push(o)}return n},set:function(n){for(var t in this.nodes)this.nodes[t].bias=n.bias||this.nodes[t].bias,this.nodes[t].squash=n.squash||this.nodes[t].squash}},t.fromJSON=function(n){var o=new t(n.input,n.output);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=n.nodes.length-1-n.output;ic)var f=n.nodes.length;else var f=o.nodes.length;for(var l=0;l=f-n.output)for(;null==d||"output"!=d.type;)d=Math.random()>=.5?n.nodes[l]:o.nodes[l];else for(;null==d||l=.5?n.nodes[l]:o.nodes[l];r.nodes.push(d)}else l=f-n.output?r.nodes.push(n.nodes[n.nodes.length-(l-(f-n.output-1))]):r.nodes.push(n.nodes[l]):l=f-n.output?r.nodes.push(o.nodes[o.nodes.length-(l-(f-n.output-1))]):r.nodes.push(o.nodes[l]));for(d in r.nodes)r.nodes[d]=e.fromJSON(r.nodes[d].toJSON());var p={},v={},g=n.connections.concat(n.selfconns);for(m in g){var m=g[m],O={weight:m.weight,from:n.nodes.indexOf(m.from),to:n.nodes.indexOf(m.to),gater:n.nodes.indexOf(m.gater)};if(O.to==n.nodes.length-1){if(!(O.from=.5?M[m][0]:M[m][1];T.push(m)}if(a==c){for(m in p)T.push(p[m]);for(m in v)T.push(v[m])}else if(a>c)for(m in p)T.push(p[m]);else for(m in v)T.push(v[m]);for(m in T){var A=T[m];if(A.to0?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)}};n&&(n.exports=t)}).call(t,o(0)(n))},function(n,t,o){(function(n){var t=o(6),e=o(1),i=o(2),s=o(5),r={Construct:function(n){var o=new t(0,0),e=[];for(item in n)if(n[item]instanceof s)for(var r in n[item].nodes)e.push(n[item].nodes[r]);else n[item]instanceof i&&e.push(n[item]);for(var a=[],c=[],h=e.length-1;h>=0;h--)e[h].connections.out.length+e[h].connections.gated.length==0?(e[h].type="output",o.output++,c.push(e[h]),e.splice(h,1)):e[h].connections.in.length||(e[h].type="input",o.input++,a.push(e[h]),e.splice(h,1));if(e=a.concat(e).concat(c),0==o.input||0==o.output)throw new Error("Given nodes have no clear input/output node!");for(var r in e){for(var u in e[r].connections.out)o.connections.push(e[r].connections.out[u]);for(var u in e[r].connections.gated)o.gates.push(e[r].connections.gated[u]);0!=e[r].connections.self.weight&&o.selfconns.push(e[r].connections.self)}return o.nodes=e,o},Perceptron:function(){var n=Array.prototype.slice.call(arguments);if(n.length<3)throw new Error("not enough layers (minimum 3) !!");var t=[];t.push(new s(n[0]));for(var o=1;o=.5?1:0;return n},o}};n&&(n.exports=r)}).call(t,o(0)(n))},function(n,t,o){(function(n){function t(n,t,o,s){this.input=n,this.output=t,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.FITNESS_PROPORTIONATE],this.crossover=s.crossover||[i.Crossover.SINGLE_POINT,i.Crossover.TWO_POINT,i.Crossover.UNIFORM,i.Crossover.AVERAGE],this.mutation=s.mutation||[i.Mutation.ADD_CONN,i.Mutation.SUB_CONN,i.Mutation.ADD_NODE,i.Mutation.SUB_NODE,i.Mutation.MOD_BIAS,i.Mutation.MOD_WEIGHT,i.Mutation.MOD_ACTIVATION],this.generation=0,this.createPool(s.network||new e(this.input,this.output))}n&&(n.exports=t);var e=(o(2),o(6)),i=o(1),s=(i.Activation,i.Mutation,i.Selection);i.Crossover;t.prototype={createPool:function(n){this.population=[];for(var t=0;t= 0; i--){ + if(nodes[i].connections.out.length + nodes[i].connections.gated.length == 0){ + nodes[i].type = 'output'; network.output++; - } else if(!nodes[node].connections.in.length){ - nodes[node].type = 'input'; + outputs.push(nodes[i]); + nodes.splice(i, 1); + } else if(!nodes[i].connections.in.length){ + nodes[i].type = 'input'; network.input++; + inputs.push(nodes[i]); + nodes.splice(i, 1); } } + // Input nodes are always first, output nodes are always last + nodes = inputs.concat(nodes).concat(outputs); + if(network.input == 0 || network.output == 0){ throw new Error('Given nodes have no clear input/output node!'); } diff --git a/docs/cdn/neataptic.min.js b/docs/cdn/neataptic.min.js index 4acb4aa..0efa113 100644 --- a/docs/cdn/neataptic.min.js +++ b/docs/cdn/neataptic.min.js @@ -22,4 +22,4 @@ * THE SOFTWARE * */ -!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={};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=16)}([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(7),Mutation:o(14),Selection:o(15),Crossover:o(12),Cost:o(11),Gating:o(13),Connection:o(10)};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.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.derivative=0,this.bias=0,this.activation;this.old=this.state,this.state=this.connections.self.gain*this.connections.self.weight*this.state+this.bias;for(var t in this.connections.in)t=this.connections.in[t],this.state+=t.from.activation*t.weight*t.gain;this.activation=this.squash(this.state),this.derivative=this.squash(this.state,!0);var o=[],e=[];for(var i in this.connections.gated){i=this.connections.gated[i];var s=i.to,r=o.indexOf(s);r>-1?e[r]+=i.weight*i.from.activation:(o.push(s),e.push(s.connections.self.gater==this?s.old:0),e[e.length-1]+=i.weight*i.from.activation)}for(var t in this.connections.in){t=this.connections.in[t],t.elegibility=this.connections.self.gain*this.connections.self.weight*t.elegibility+t.from.activation*t.gain;for(var a=0;a-1?t.xtrace.values[r]=s.connections.self.gain*s.connections.self.weight*t.xtrace.values[r]+this.derivative*t.elegibility*c:(t.xtrace.nodes.push(s),t.xtrace.values.push(this.derivative*t.elegibility*c))}}for(var t in this.connections.gated)this.connections.gated[t].gain=this.activation;return this.activation},propagate:function(n,t){var o=0;if("output"==this.type)this.error.responsibility=this.error.projected=t-this.activation;else{for(var e in this.connections.out){var e=this.connections.out[e],i=e.to;o+=i.error.responsibility*e.weight*e.gain}this.error.projected=this.derivative*o,o=0;for(var s in this.connections.gated){s=this.connections.gated[s];var i=s.to,r=i.connections.self.gater==this?i.old:0;r+=s.weight*s.from.activation,o+=i.error.responsibility*r}this.error.gated=this.derivative*o,this.error.responsibility=this.error.projected+this.error.gated}n=n||.1;for(var e in this.connections.in){for(var e=this.connections.in[e],a=this.error.projected*e.elegibility,c=0;c=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 in this.connections.in)n=this.connections.in[n],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(n){if(void 0===n)throw new Error("No mutate method given!");if(!n.name in i.Mutation)throw new Error("This method does not exist!");switch(n){case c.MOD_ACTIVATION:for(var t=Math.floor(Math.random()*c.MOD_ACTIVATION.allowed.length);c.MOD_ACTIVATION.allowed[t]==this.squash;)t=Math.floor(Math.random()*c.MOD_ACTIVATION.allowed.length);this.squash=c.MOD_ACTIVATION.allowed[t];break;case c.MOD_BIAS:var o=Math.random()*(c.MOD_BIAS.config.max-c.MOD_BIAS.config.min)+c.MOD_BIAS.config.min;this.bias+=o}},isProjectingTo:function(n){for(conn in this.connections.out)if(conn=this.connections.out[conn],conn.to==n)return!0;return!1},isProjectedBy:function(n){for(conn in this.connections.in)if(conn=this.connections.in[conn],conn.from==n)return!0;return!1},toJSON:function(){return{bias:this.bias,type:this.type,squash:this.squash.name}}},t.fromJSON=function(n){var o=new t;o.bias=n.bias,o.type=n.type;for(squash in a)if(a[squash].name==n.squash){o.squash=a[squash];break}return o}}).call(t,o(0)(n))},function(n,t,o){(function(n){var t={warnings:!0};n&&(n.exports=t)}).call(t,o(0)(n))},function(n,t,o){(function(n){function t(n,t,o){this.from=n,this.to=t,this.gain=1,this.weight=void 0===o?.2*Math.random()-.1:o,this.gater=null,this.elegibility=0,this.xtrace={nodes:[],values:[]}}n&&(n.exports=t),t.prototype={toJSON:function(){return{weight:this.weight}}},t.innovationID=function(n,t){return.5*(n+t)*(n+t+1)+t}}).call(t,o(0)(n))},function(n,t,o){(function(n){function t(n){this.nodes=[],this.connections={in:[],out:[],self:[]};for(var t=0;t=0;o--)void 0===t?this.nodes[o].propagate(n):this.nodes[o].propagate(n,t[o])},connect:function(n,o){var r=[];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 a=0;a=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 d in o){if(0==c.length)break;d=o[d];var l=Math.floor(Math.random()*c.length),f=c[l];this.gate(d,c[l]),c.splice(l,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 a.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(a.MOD_ACTIVATION);var c=Math.min(i,this.nodes.length-this.output);this.nodes.splice(c,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 a.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),s=this.nodes[f];this.remove(s);break;case a.ADD_CONN:for(var d=[],l=0;l1&&A.to.connections.in.length>1&&this.nodes.indexOf(A.to)>this.nodes.indexOf(A.from)&&N.push(A);if(0==N.length){r.warnings&&console.warn("No connections to remove!");break}var M=N[Math.floor(Math.random()*N.length)];this.disconnect(M.from,M.to);break;case a.MOD_WEIGHT:var t=this.connections[Math.floor(Math.random()*this.connections.length)],T=Math.random()*(a.MOD_WEIGHT.config.max-a.MOD_WEIGHT.config.min)+a.MOD_WEIGHT.config.min;t.weight+=T;break;case a.MOD_BIAS:var f=Math.floor(Math.random()*(this.nodes.length-this.input)+this.input),s=this.nodes[f];s.mutate(a.MOD_BIAS);break;case a.MOD_ACTIVATION:var f=Math.floor(Math.random()*(this.nodes.length-this.input)+this.input),s=this.nodes[f];s.mutate(a.MOD_ACTIVATION);break;case a.ADD_SELF_CONN:for(var N=[],l=this.input;l1&&A.to.connections.in.length>1&&this.nodes.indexOf(A.from)>this.nodes.indexOf(A.to)&&N.push(A);if(0==N.length){r.warnings&&console.warn("No connections to remove!");break}var M=N[Math.floor(Math.random()*N.length)];this.disconnect(M.from,M.to);break;case a.SWAP_NODES:var f=Math.floor(Math.random()*(this.nodes.length-this.input)+this.input),p=this.nodes[f],f=Math.floor(Math.random()*(this.nodes.length-this.input)+this.input),g=this.nodes[f],E=p.bias,x=p.squash;p.bias=g.bias,p.squash=g.squash,g.bias=E,g.squash=x}},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||.005,a=t.cost||s.Cost.MSE,c=t.rate||.3,h=t.shuffle||!1,u=t.iterations||0,f=t.crossValidate||!1,d=t.clear||!1;if(f)var l=t.crossValidate.testSize,p=t.crossValidate.testError,v=Math.ceil((1-l)*n.length),g=n.slice(0,v),m=n.slice(v);var O=c,w=0;Array.isArray(c)&&(w=Math.floor(u/c.length));for(var N=0,M=1;M>i&&(0==u||N0){O=c[Math.floor(u/w)]||O}if(f?(this._trainSet(g,O,a),d&&this.clear(),M+=this.test(m,a).error,d&&this.clear()):(M+=this._trainSet(n,O,a),d&&this.clear(),M/=n.length),h)for(var T,A,_=n.length;_;T=Math.floor(Math.random()*_),A=n[--_],n[_]=n[T],n[T]=A);e&&N%e==0&&console.log("iterations",N,"error",M,"rate",O)}return{error:M,iterations:N,time:Date.now()-o}},_trainSet:function(n,t,o){var e=0;for(var i in n){var s=n[i].input,r=n[i].output,a=this.activate(s);this.propagate(t,r),e+=o(r,a)}return e},test:function(n,t){t=t||s.Cost.MSE;var o,e,i,r=0,a=Date.now();for(var c in n)o=n[c].input,i=n[c].output,e=this.activate(o),r+=t(i,e);return r/=n.length,{error:r,time:Date.now()-a}},graph:function(n,t){var o=0,e=0,i={nodes:[],links:[],constraints:[{type:"alignment",axis:"x",offsets:[]},{type:"alignment",axis:"y",offsets:[]}]};for(a in this.nodes){var s=this.nodes[a];"input"==s.type?(1==this.input?i.constraints[0].offsets.push({node:a,offset:0}):i.constraints[0].offsets.push({node:a,offset:.8*n/(this.input-1)*o++}),i.constraints[1].offsets.push({node:a,offset:0})):"output"==s.type&&(1==this.output?i.constraints[0].offsets.push({node:a,offset:0}):i.constraints[0].offsets.push({node:a,offset:.8*n/(this.output-1)*e++}),i.constraints[1].offsets.push({node:a,offset:-.8*t})),i.nodes.push({id:a,name:"hidden"==s.type?s.squash.name:s.type.toUpperCase(),activation:s.activation})}var r=this.connections.concat(this.selfconns);for(connection in r)if(connection=r[connection],null==connection.gater)i.links.push({source:this.nodes.indexOf(connection.from),target:this.nodes.indexOf(connection.to),weight:connection.weight});else{var a=i.nodes.length;i.nodes.push({id:a,activation:connection.gater.activation,name:"GATE"}),i.links.push({source:this.nodes.indexOf(connection.from),target:a,weight:.5*connection.weight}),i.links.push({source:a,target:this.nodes.indexOf(connection.to),weight:.5*connection.weight}),i.links.push({source:this.nodes.indexOf(connection.gater),target:a,weight:connection.gater.activation,gate:!0})}return i},toJSON:function(){var n={nodes:[],connections:[],input:this.input,output:this.output};for(index in this.nodes){var t=this.nodes[index],o=t.toJSON();if(o.index=index,n.nodes.push(o),0!=t.connections.self.weight){var o=t.connections.self.toJSON();o.from=index,o.to=index,o.gater=null!=t.connections.self.gater?this.nodes.indexOf(t.connections.self.gater):null,n.connections.push(o)}}for(e in this.connections){var e=this.connections[e],o=e.toJSON();o.from=this.nodes.indexOf(e.from),o.to=this.nodes.indexOf(e.to),o.gater=null!=e.gater?this.nodes.indexOf(e.gater):null,n.connections.push(o)}return n},set:function(n){for(var t in this.nodes)this.nodes[t].bias=n.bias||this.nodes[t].bias,this.nodes[t].squash=n.squash||this.nodes[t].squash}},t.fromJSON=function(n){var o=new t(n.input,n.output);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=n.nodes.length-1-n.output;ic)var f=n.nodes.length;else var f=o.nodes.length;for(var d=0;d=f-n.output)for(;null==l||"output"!=l.type;)l=Math.random()>=.5?n.nodes[d]:o.nodes[d];else for(;null==l||d=.5?n.nodes[d]:o.nodes[d];r.nodes.push(l)}else d=f-n.output?r.nodes.push(n.nodes[n.nodes.length-(d-(f-n.output-1))]):r.nodes.push(n.nodes[d]):d=f-n.output?r.nodes.push(o.nodes[o.nodes.length-(d-(f-n.output-1))]):r.nodes.push(o.nodes[d]));for(l in r.nodes)r.nodes[l]=e.fromJSON(r.nodes[l].toJSON());var p={},v={},g=n.connections.concat(n.selfconns);for(m in g){var m=g[m],O={weight:m.weight,from:n.nodes.indexOf(m.from),to:n.nodes.indexOf(m.to),gater:n.nodes.indexOf(m.gater)};if(O.to==n.nodes.length-1){if(!(O.from=.5?M[m][0]:M[m][1];T.push(m)}if(a==c){for(m in p)T.push(p[m]);for(m in v)T.push(v[m])}else if(a>c)for(m in p)T.push(p[m]);else for(m in v)T.push(v[m]);for(m in T){var A=T[m];if(A.to0?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)}};n&&(n.exports=t)}).call(t,o(0)(n))},function(n,t,o){(function(n){var t=o(6),e=o(1),i=o(2),s=o(5),r={Construct:function(n){var o=new t(0,0),e=[];for(item in n)if(n[item]instanceof s)for(var r in n[item].nodes)e.push(n[item].nodes[r]);else n[item]instanceof i&&e.push(n[item]);for(var r in e)e[r].connections.out.length+e[r].connections.gated.length==0?(e[r].type="output",o.output++):e[r].connections.in.length||(e[r].type="input",o.input++);if(0==o.input||0==o.output)throw new Error("Given nodes have no clear input/output node!");for(var r in e){for(var a in e[r].connections.out)o.connections.push(e[r].connections.out[a]);for(var a in e[r].connections.gated)o.gates.push(e[r].connections.gated[a]);0!=e[r].connections.self.weight&&o.selfconns.push(e[r].connections.self)}return o.nodes=e,o},Perceptron:function(){var n=Array.prototype.slice.call(arguments);if(n.length<3)throw new Error("not enough layers (minimum 3) !!");var t=[];t.push(new s(n[0]));for(var o=1;o=.5?1:0;return n},o}};n&&(n.exports=r)}).call(t,o(0)(n))},function(n,t,o){(function(n){function t(n,t,o,s){this.input=n,this.output=t,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.FITNESS_PROPORTIONATE],this.crossover=s.crossover||[i.Crossover.SINGLE_POINT,i.Crossover.TWO_POINT,i.Crossover.UNIFORM,i.Crossover.AVERAGE],this.mutation=s.mutation||[i.Mutation.ADD_CONN,i.Mutation.SUB_CONN,i.Mutation.ADD_NODE,i.Mutation.SUB_NODE,i.Mutation.MOD_BIAS,i.Mutation.MOD_WEIGHT,i.Mutation.MOD_ACTIVATION],this.generation=0,this.createPool(s.network||new e(this.input,this.output))}n&&(n.exports=t);var e=(o(2),o(6)),i=o(1),s=(i.Activation,i.Mutation,i.Selection);i.Crossover;t.prototype={createPool:function(n){this.population=[];for(var t=0;t-1?e[r]+=i.weight*i.from.activation:(o.push(s),e.push(s.connections.self.gater==this?s.old:0),e[e.length-1]+=i.weight*i.from.activation)}for(var t in this.connections.in){t=this.connections.in[t],t.elegibility=this.connections.self.gain*this.connections.self.weight*t.elegibility+t.from.activation*t.gain;for(var a=0;a-1?t.xtrace.values[r]=s.connections.self.gain*s.connections.self.weight*t.xtrace.values[r]+this.derivative*t.elegibility*c:(t.xtrace.nodes.push(s),t.xtrace.values.push(this.derivative*t.elegibility*c))}}for(var t in this.connections.gated)this.connections.gated[t].gain=this.activation;return this.activation},propagate:function(n,t){var o=0;if("output"==this.type)this.error.responsibility=this.error.projected=t-this.activation;else{for(var e in this.connections.out){var e=this.connections.out[e],i=e.to;o+=i.error.responsibility*e.weight*e.gain}this.error.projected=this.derivative*o,o=0;for(var s in this.connections.gated){s=this.connections.gated[s];var i=s.to,r=i.connections.self.gater==this?i.old:0;r+=s.weight*s.from.activation,o+=i.error.responsibility*r}this.error.gated=this.derivative*o,this.error.responsibility=this.error.projected+this.error.gated}n=n||.1;for(var e in this.connections.in){for(var e=this.connections.in[e],a=this.error.projected*e.elegibility,c=0;c=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 in this.connections.in)n=this.connections.in[n],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(n){if(void 0===n)throw new Error("No mutate method given!");if(!n.name in i.Mutation)throw new Error("This method does not exist!");switch(n){case c.MOD_ACTIVATION:for(var t=Math.floor(Math.random()*c.MOD_ACTIVATION.allowed.length);c.MOD_ACTIVATION.allowed[t]==this.squash;)t=Math.floor(Math.random()*c.MOD_ACTIVATION.allowed.length);this.squash=c.MOD_ACTIVATION.allowed[t];break;case c.MOD_BIAS:var o=Math.random()*(c.MOD_BIAS.config.max-c.MOD_BIAS.config.min)+c.MOD_BIAS.config.min;this.bias+=o}},isProjectingTo:function(n){for(conn in this.connections.out)if(conn=this.connections.out[conn],conn.to==n)return!0;return!1},isProjectedBy:function(n){for(conn in this.connections.in)if(conn=this.connections.in[conn],conn.from==n)return!0;return!1},toJSON:function(){return{bias:this.bias,type:this.type,squash:this.squash.name}}},t.fromJSON=function(n){var o=new t;o.bias=n.bias,o.type=n.type;for(squash in a)if(a[squash].name==n.squash){o.squash=a[squash];break}return o}}).call(t,o(0)(n))},function(n,t,o){(function(n){var t={warnings:!0};n&&(n.exports=t)}).call(t,o(0)(n))},function(n,t,o){(function(n){function t(n,t,o){this.from=n,this.to=t,this.gain=1,this.weight=void 0===o?.2*Math.random()-.1:o,this.gater=null,this.elegibility=0,this.xtrace={nodes:[],values:[]}}n&&(n.exports=t),t.prototype={toJSON:function(){return{weight:this.weight}}},t.innovationID=function(n,t){return.5*(n+t)*(n+t+1)+t}}).call(t,o(0)(n))},function(n,t,o){(function(n){function t(n){this.nodes=[],this.connections={in:[],out:[],self:[]};for(var t=0;t=0;o--)void 0===t?this.nodes[o].propagate(n):this.nodes[o].propagate(n,t[o])},connect:function(n,o){var r=[];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 a=0;a=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 d=Math.floor(Math.random()*c.length),f=c[d];this.gate(l,c[d]),c.splice(d,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 a.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(a.MOD_ACTIVATION);var c=Math.min(i,this.nodes.length-this.output);this.nodes.splice(c,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 a.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),s=this.nodes[f];this.remove(s);break;case a.ADD_CONN:for(var l=[],d=0;d1&&A.to.connections.in.length>1&&this.nodes.indexOf(A.to)>this.nodes.indexOf(A.from)&&N.push(A);if(0==N.length){r.warnings&&console.warn("No connections to remove!");break}var M=N[Math.floor(Math.random()*N.length)];this.disconnect(M.from,M.to);break;case a.MOD_WEIGHT:var t=this.connections[Math.floor(Math.random()*this.connections.length)],T=Math.random()*(a.MOD_WEIGHT.config.max-a.MOD_WEIGHT.config.min)+a.MOD_WEIGHT.config.min;t.weight+=T;break;case a.MOD_BIAS:var f=Math.floor(Math.random()*(this.nodes.length-this.input)+this.input),s=this.nodes[f];s.mutate(a.MOD_BIAS);break;case a.MOD_ACTIVATION:var f=Math.floor(Math.random()*(this.nodes.length-this.input)+this.input),s=this.nodes[f];s.mutate(a.MOD_ACTIVATION);break;case a.ADD_SELF_CONN:for(var N=[],d=this.input;d1&&A.to.connections.in.length>1&&this.nodes.indexOf(A.from)>this.nodes.indexOf(A.to)&&N.push(A);if(0==N.length){r.warnings&&console.warn("No connections to remove!");break}var M=N[Math.floor(Math.random()*N.length)];this.disconnect(M.from,M.to);break;case a.SWAP_NODES:var f=Math.floor(Math.random()*(this.nodes.length-this.input)+this.input),p=this.nodes[f],f=Math.floor(Math.random()*(this.nodes.length-this.input)+this.input),g=this.nodes[f],E=p.bias,x=p.squash;p.bias=g.bias,p.squash=g.squash,g.bias=E,g.squash=x}},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||.005,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;if(f)var d=t.crossValidate.testSize,p=t.crossValidate.testError,v=Math.ceil((1-d)*n.length),g=n.slice(0,v),m=n.slice(v);var O=c,w=0;Array.isArray(c)&&(w=Math.floor(u/c.length));for(var N=0,M=1;M>i&&(0==u||N0){O=c[Math.floor(u/w)]||O}if(f?(this._trainSet(g,O,a),l&&this.clear(),M+=this.test(m,a).error,l&&this.clear()):(M+=this._trainSet(n,O,a),l&&this.clear(),M/=n.length),h)for(var T,A,_=n.length;_;T=Math.floor(Math.random()*_),A=n[--_],n[_]=n[T],n[T]=A);e&&N%e==0&&console.log("iterations",N,"error",M,"rate",O)}return{error:M,iterations:N,time:Date.now()-o}},_trainSet:function(n,t,o){var e=0;for(var i in n){var s=n[i].input,r=n[i].output,a=this.activate(s);this.propagate(t,r),e+=o(r,a)}return e},test:function(n,t){t=t||s.Cost.MSE;var o,e,i,r=0,a=Date.now();for(var c in n)o=n[c].input,i=n[c].output,e=this.activate(o),r+=t(i,e);return r/=n.length,{error:r,time:Date.now()-a}},graph:function(n,t){var o=0,e=0,i={nodes:[],links:[],constraints:[{type:"alignment",axis:"x",offsets:[]},{type:"alignment",axis:"y",offsets:[]}]};for(a in this.nodes){var s=this.nodes[a];"input"==s.type?(1==this.input?i.constraints[0].offsets.push({node:a,offset:0}):i.constraints[0].offsets.push({node:a,offset:.8*n/(this.input-1)*o++}),i.constraints[1].offsets.push({node:a,offset:0})):"output"==s.type&&(1==this.output?i.constraints[0].offsets.push({node:a,offset:0}):i.constraints[0].offsets.push({node:a,offset:.8*n/(this.output-1)*e++}),i.constraints[1].offsets.push({node:a,offset:-.8*t})),i.nodes.push({id:a,name:"hidden"==s.type?s.squash.name:s.type.toUpperCase(),activation:s.activation})}var r=this.connections.concat(this.selfconns);for(connection in r)if(connection=r[connection],null==connection.gater)i.links.push({source:this.nodes.indexOf(connection.from),target:this.nodes.indexOf(connection.to),weight:connection.weight});else{var a=i.nodes.length;i.nodes.push({id:a,activation:connection.gater.activation,name:"GATE"}),i.links.push({source:this.nodes.indexOf(connection.from),target:a,weight:.5*connection.weight}),i.links.push({source:a,target:this.nodes.indexOf(connection.to),weight:.5*connection.weight}),i.links.push({source:this.nodes.indexOf(connection.gater),target:a,weight:connection.gater.activation,gate:!0})}return i},toJSON:function(){var n={nodes:[],connections:[],input:this.input,output:this.output};for(index in this.nodes){var t=this.nodes[index],o=t.toJSON();if(o.index=index,n.nodes.push(o),0!=t.connections.self.weight){var o=t.connections.self.toJSON();o.from=index,o.to=index,o.gater=null!=t.connections.self.gater?this.nodes.indexOf(t.connections.self.gater):null,n.connections.push(o)}}for(e in this.connections){var e=this.connections[e],o=e.toJSON();o.from=this.nodes.indexOf(e.from),o.to=this.nodes.indexOf(e.to),o.gater=null!=e.gater?this.nodes.indexOf(e.gater):null,n.connections.push(o)}return n},set:function(n){for(var t in this.nodes)this.nodes[t].bias=n.bias||this.nodes[t].bias,this.nodes[t].squash=n.squash||this.nodes[t].squash}},t.fromJSON=function(n){var o=new t(n.input,n.output);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=n.nodes.length-1-n.output;ic)var f=n.nodes.length;else var f=o.nodes.length;for(var l=0;l=f-n.output)for(;null==d||"output"!=d.type;)d=Math.random()>=.5?n.nodes[l]:o.nodes[l];else for(;null==d||l=.5?n.nodes[l]:o.nodes[l];r.nodes.push(d)}else l=f-n.output?r.nodes.push(n.nodes[n.nodes.length-(l-(f-n.output-1))]):r.nodes.push(n.nodes[l]):l=f-n.output?r.nodes.push(o.nodes[o.nodes.length-(l-(f-n.output-1))]):r.nodes.push(o.nodes[l]));for(d in r.nodes)r.nodes[d]=e.fromJSON(r.nodes[d].toJSON());var p={},v={},g=n.connections.concat(n.selfconns);for(m in g){var m=g[m],O={weight:m.weight,from:n.nodes.indexOf(m.from),to:n.nodes.indexOf(m.to),gater:n.nodes.indexOf(m.gater)};if(O.to==n.nodes.length-1){if(!(O.from=.5?M[m][0]:M[m][1];T.push(m)}if(a==c){for(m in p)T.push(p[m]);for(m in v)T.push(v[m])}else if(a>c)for(m in p)T.push(p[m]);else for(m in v)T.push(v[m]);for(m in T){var A=T[m];if(A.to0?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)}};n&&(n.exports=t)}).call(t,o(0)(n))},function(n,t,o){(function(n){var t=o(6),e=o(1),i=o(2),s=o(5),r={Construct:function(n){var o=new t(0,0),e=[];for(item in n)if(n[item]instanceof s)for(var r in n[item].nodes)e.push(n[item].nodes[r]);else n[item]instanceof i&&e.push(n[item]);for(var a=[],c=[],h=e.length-1;h>=0;h--)e[h].connections.out.length+e[h].connections.gated.length==0?(e[h].type="output",o.output++,c.push(e[h]),e.splice(h,1)):e[h].connections.in.length||(e[h].type="input",o.input++,a.push(e[h]),e.splice(h,1));if(e=a.concat(e).concat(c),0==o.input||0==o.output)throw new Error("Given nodes have no clear input/output node!");for(var r in e){for(var u in e[r].connections.out)o.connections.push(e[r].connections.out[u]);for(var u in e[r].connections.gated)o.gates.push(e[r].connections.gated[u]);0!=e[r].connections.self.weight&&o.selfconns.push(e[r].connections.self)}return o.nodes=e,o},Perceptron:function(){var n=Array.prototype.slice.call(arguments);if(n.length<3)throw new Error("not enough layers (minimum 3) !!");var t=[];t.push(new s(n[0]));for(var o=1;o=.5?1:0;return n},o}};n&&(n.exports=r)}).call(t,o(0)(n))},function(n,t,o){(function(n){function t(n,t,o,s){this.input=n,this.output=t,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.FITNESS_PROPORTIONATE],this.crossover=s.crossover||[i.Crossover.SINGLE_POINT,i.Crossover.TWO_POINT,i.Crossover.UNIFORM,i.Crossover.AVERAGE],this.mutation=s.mutation||[i.Mutation.ADD_CONN,i.Mutation.SUB_CONN,i.Mutation.ADD_NODE,i.Mutation.SUB_NODE,i.Mutation.MOD_BIAS,i.Mutation.MOD_WEIGHT,i.Mutation.MOD_ACTIVATION],this.generation=0,this.createPool(s.network||new e(this.input,this.output))}n&&(n.exports=t);var e=(o(2),o(6)),i=o(1),s=(i.Activation,i.Mutation,i.Selection);i.Crossover;t.prototype={createPool:function(n){this.population=[];for(var t=0;t= 0; i--){ + if(nodes[i].connections.out.length + nodes[i].connections.gated.length == 0){ + nodes[i].type = 'output'; network.output++; - } else if(!nodes[node].connections.in.length){ - nodes[node].type = 'input'; + outputs.push(nodes[i]); + nodes.splice(i, 1); + } else if(!nodes[i].connections.in.length){ + nodes[i].type = 'input'; network.input++; + inputs.push(nodes[i]); + nodes.splice(i, 1); } } + // Input nodes are always first, output nodes are always last + nodes = inputs.concat(nodes).concat(outputs); + if(network.input == 0 || network.output == 0){ throw new Error('Given nodes have no clear input/output node!'); }