1 line
6.9 KiB
JavaScript
1 line
6.9 KiB
JavaScript
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("predictor",[],t):"object"==typeof exports?exports.predictor=t():e.predictor=t()}(window,function(){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=0)}([function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r,o=n(1),i=(r=o)&&r.__esModule?r:{default:r};t.default=i.default},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),o=a(n(2)),i=a(n(3)),u=a(n(5));function a(e){return e&&e.__esModule?e:{default:e}}var s={base:2,supervisor_epsilon:.01,daemons:[{human:2,robot:2,epsilon:.01},{human:3,robot:3,epsilon:.01},{human:4,robot:4,epsilon:.01},{human:5,robot:5,epsilon:.01},{human:6,robot:6,epsilon:.01}]},c=function(){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:s;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.base=t.base,this.score=0,this.journal=new i.default;var n=this._createDaemons(t.daemons);this.supervisor=new u.default(n,t.supervisor_epsilon)}return r(e,[{key:"pass",value:function(e){var t=parseInt(e,10);if(t<0||t>=this.base)throw new Error("Passed value must be in [0, "+this.base+")");var n=this.supervisor.predict(this.journal);return this.score+=n===t?-1:1,this.supervisor.adjust(this.journal,t),this.journal.makeMove(t,n),n}},{key:"_createDaemons",value:function(e){var t=this;return e.map(function(e){return new o.default(t.base,e.human,e.robot,e.epsilon||.01)})}},{key:"stepCount",value:function(){return this.journal.length}}]),e}();t.default=c},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}();function o(e){if(Array.isArray(e)){for(var t=0,n=Array(e.length);t<e.length;t++)n[t]=e[t];return n}return Array.from(e)}var i=.01;function u(e){return e.join(":")}var a=function(){function e(t,n,r){var o=arguments.length>3&&void 0!==arguments[3]?arguments[3]:i;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.weights={},this.base=t,this.humanCount=n,this.robotCount=r,this.epsilon=o}return r(e,[{key:"predict",value:function(e){for(var t=this._getStepSlice(e),n=[],r=0;r<this.base;++r)n[r]=this._getWeight([].concat(o(t),[r]));var i=Math.max.apply(Math,n);return n.indexOf(i)}},{key:"adjust",value:function(e,t){var n=this._getStepSlice(e),r=this._getAdjustmentWeight(e.length);this._adjustWeight([].concat(o(n),[t]),r)}},{key:"_getStepSlice",value:function(e){return e.getLastMovements(this.humanCount,this.robotCount)}},{key:"_getAdjustmentWeight",value:function(e){return Math.pow(1+this.epsilon,e)}},{key:"_getWeight",value:function(e){var t=u(e),n=this.weights[t];return void 0===n?0:n}},{key:"_setWeight",value:function(e,t){var n=u(e);this.weights[n]=t}},{key:"_adjustWeight",value:function(e,t){var n=this._getWeight(e)+t;this._setWeight(e,n)}},{key:"power",get:function(){return this.humanCount+this.robotCount}}]),e}();t.default=a},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r,o=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),i=n(4),u=(r=i)&&r.__esModule?r:{default:r};var a=function(){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[];!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.moves=[],this.moves=t}return o(e,[{key:"makeMove",value:function(e,t){this.moves.push(new u.default(e,t))}},{key:"getLastMovements",value:function(e,t){var n=this.moves.map(function(e){return e.human}),r=this.moves.map(function(e){return e.robot});return[].concat(r.slice(-t),n.slice(-e))}},{key:"length",get:function(){return this.moves.length}}]),e}();t.default=a},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});t.default=function e(t,n){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.human=t,this.robot=n}},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}();var o=.01,i=function(){function e(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:o;if(function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.daemons=[],!t||0===t.length)throw Error("Empty daemon list");this.daemons=t.map(function(e){return{daemon:e,rate:0}}),this.epsilon=n}return r(e,[{key:"predict",value:function(e){var t=this._createPredictions(e);return this._sortPredictions(t)[0].value}},{key:"adjust",value:function(e,t){var n=this._createPredictions(e),r=!0,o=!1,i=void 0;try{for(var u,a=n[Symbol.iterator]();!(r=(u=a.next()).done);r=!0){var s=u.value;s.value===t&&(s.daemon.rate+=this._getAdjustmentWeight(e.length)),s.daemon.daemon.adjust(e,t)}}catch(e){o=!0,i=e}finally{try{!r&&a.return&&a.return()}finally{if(o)throw i}}}},{key:"_createPredictions",value:function(e){return this.daemons.map(function(t){return{daemon:t,power:t.daemon.power,rate:t.rate,value:t.daemon.predict(e)}})}},{key:"_sortPredictions",value:function(e){return e.sort(function(e,t){var n=t.rate-e.rate;return Math.abs(n)>1e-6?n:e.power-t.power})}},{key:"_getAdjustmentWeight",value:function(e){return Math.pow(1+this.epsilon,e)}}]),e}();t.default=i}])}); |