/*! * 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.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-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*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=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 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=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=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=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=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=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-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=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=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;p1&&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;p1&&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||Tv&&(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 null!=m&&(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=0;e--)o.nodes.splice(e,1);for(var e=t.nodes.length-t.output;eh)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 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=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)),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;cthis.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;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