diff --git a/demo/dist/metacar.min.js b/demo/dist/metacar.min.js index c8f1cf2..d4e7f7c 100644 --- a/demo/dist/metacar.min.js +++ b/demo/dist/metacar.min.js @@ -1 +1 @@ -window.metacar=function(t){var e={};function i(o){if(e[o])return e[o].exports;var n=e[o]={i:o,l:!1,exports:{}};return t[o].call(n.exports,n,n.exports,i),n.l=!0,n.exports}return i.m=t,i.c=e,i.d=function(t,e,o){i.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:o})},i.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},i.t=function(t,e){if(1&e&&(t=i(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var o=Object.create(null);if(i.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var n in t)i.d(o,n,function(e){return t[e]}.bind(null,n));return o},i.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return i.d(e,"a",e),e},i.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},i.p="",i(i.s=1)}([function(t,e){t.exports=window.PIXI},function(t,e,i){"use strict";i.r(e);var o=i(0),n=(o.Application,o.loader),a=(o.loader.resources,o.Sprite),r=o.Graphics,s=o.Container,h="https://metacar-project.com/",c=h+"public/textures/textures.json",l=h+"public/images/",d=60,p={DEFAULT:"car.png",AGENT:"car_agent.png"},u={ROAD:-1,DEFAULT:0,CAR:1,AGENT:1},v={ROADS:{"↕":{image:"road_u.png",orientation:1.5},"↔":{image:"road_r.png",orientation:0},"↱":{image:"road_rotate_ld.png",orientation:1.25},"↰":{image:"road_rotate_ul.png",orientation:.75},"↲":{image:"road_rotate_rl.png",orientation:.25},"↳":{image:"road_rotate_dl.png",orientation:1.75},"↟":{image:"road_up.png",orientation:1.5},"↠":{image:"road_rp.png",orientation:0},"↤":{image:"road_r2.png",orientation:0},"↦":{image:"road_l2.png",orientation:1},"↥":{image:"road_d2.png",orientation:.5},"↧":{image:"road_u2.png",orientation:1.5}},house:{image:"house.png"},house2:{image:"house2.png"},house3:{image:"house3.png"},bench:{image:"bench.png"},tree:{image:"tree.png"}},y={level:{fullCity:{cars:[{mx:2,my:5,line:0,auto:!0},{mx:3,my:4,line:0,auto:!0},{mx:3,my:4,line:1,auto:!0},{mx:6,my:3,line:0,auto:!0},{mx:6,my:3,line:1,auto:!0},{mx:8,my:2,line:0,auto:!0},{mx:8,my:2,line:1,auto:!0},{mx:2,my:7,line:0,auto:!0}],house:[{x:9,y:409},{x:146,y:162},{x:439,y:39}],house2:[{x:425,y:192},{x:431,y:354}],house3:[{x:426,y:281},{x:51,y:157},{x:553,y:34}],bench:[{x:259,y:206},{x:8,y:205}],tree:[{x:1,y:303},{x:54,y:357},{x:215,y:325},{x:230,y:400},{x:282,y:368},{x:182,y:433},{x:285,y:309},{x:576,y:233},{x:581,y:300},{x:519,y:388},{x:582,y:391},{x:256,y:69},{x:151,y:43},{x:77,y:77},{x:26,y:24},{x:189,y:98}],map:[[0,0,0,0,0,0,"↕",0,0,0,0],[0,0,0,0,0,0,"↟",0,0,0,0],[0,0,0,0,0,0,"↦","↔","↔","↠","↔"],[0,0,0,0,0,0,"↕",0,0,0,0],["↔","↔","↧","↔","↠","↔","↤",0,0,0,0],[0,0,"↕",0,0,0,"↕",0,0,0,0],[0,0,"↟",0,0,0,"↟",0,0,0,0],[0,0,"↕",0,0,0,"↕",0,0,0,0]]},level1:{cars:[{mx:5,my:3,line:0,auto:!0},{mx:5,my:3,line:1,auto:!0},{mx:5,my:1,line:0,auto:!0}],tree:[{x:120,y:50},{x:86.5,y:85},{x:94.5,y:135},{x:30.5,y:128},{x:72.5,y:190},{x:23.5,y:232},{x:110.5,y:242},{x:43.5,y:33},{x:18.5,y:178},{x:305.5,y:125},{x:360.5,y:122}],asset:[{x:252.5,y:142},{x:86.5,y:85},{x:94.5,y:135},{x:30.5,y:128},{x:252.5,y:26},{x:195.5,y:0},{x:307.5,y:1},{x:72.5,y:190},{x:23.5,y:232},{x:110.5,y:242},{x:43.5,y:33},{x:35.5,y:185},{x:18.5,y:178},{x:289.5,y:126},{x:337.5,y:126},{x:305.5,y:125},{x:360.5,y:122},{x:492.5,y:56},{x:503.5,y:136},{x:504.5,y:58}],bench:[{x:252.5,y:142},{x:252.5,y:26}],road:[{x:240,y:60},{x:240,y:60},{x:480,y:0},{x:480,y:60},{x:480,y:120},{x:480,y:180},{x:480,y:240},{x:300,y:180},{x:300,y:180},{x:360,y:180},{x:420,y:180},{x:420,y:120},{x:420,y:60},{x:360,y:60},{x:300,y:60},{x:420,y:120}],house3:[{x:503.5,y:136}],house:[{x:504.5,y:58}],map:[[0,0,0,0,0,0,0,0,0,0],[0,0,0,"↱","↠","↔","↔","↰",0,0],[0,0,0,"↕",0,0,0,"↟",0,0],[0,0,0,"↳","↔","↔","↔","↲",0,0],[0,0,0,0,0,0,0,0,0,0]],agent:{mx:4,my:3,line:0,motion:{type:"BasicMotionEngine",options:{rotationStep:.5,actions:["UP","LEFT","RIGHT"]}}}},level0:{cars:[],house:[{x:263,y:108},{x:112,y:258}],house3:[{x:262,y:16}],bench:[{x:135,y:34}],tree:[{x:124,y:134},{x:7,y:11},{x:69,y:10},{x:19,y:238},{x:5,y:133},{x:260,y:211}],map:[[0,0,0,0,0],[0,"↱","↔","↰",0],[0,"↕",0,"↕",0],[0,"↳","↠","↲",0],[0,0,0,0,0]],agent:{mx:2,my:1,line:1,motion:{type:"BasicMotionEngine",options:{rotationStep:.5,actions:["UP","LEFT","RIGHT","DOWN","WAIT"]}}}},level2:{cars:[{mx:7,my:7,line:0},{mx:9,my:6,line:0},{mx:8,my:6,line:1},{mx:10,my:6,line:1}],house:[{x:68,y:317}],house2:[{x:624,y:84},{x:-15,y:144}],house3:[{x:618,y:231},{x:13,y:441}],bench:[{x:318,y:216},{x:422,y:36}],tree:[{x:151,y:144},{x:229,y:175},{x:326,y:132},{x:413,y:167},{x:487,y:157},{x:445,y:123},{x:511,y:429},{x:584,y:427},{x:80,y:391},{x:219,y:409},{x:207,y:326},{x:156,y:426},{x:162,y:13}],map:[[0,0,0,0,0,0,0,0,0,0,0],[0,"↱","↔","↔","↔","↔","↔","↔","↔","↰",0],[0,"↕",0,0,0,0,0,0,0,"↕",0],[0,"↕",0,0,0,0,0,0,0,"↕",0],[0,"↳","↔","↔","↔","↧","↔","↔","↔","↲",0],[0,0,0,0,0,"↕",0,0,0,0,0],[0,0,0,0,0,"↕",0,"↱","↔","↔","↔"],[0,0,0,0,0,"↕",0,"↕",0,0,0]],agent:{mx:2,my:4,line:0,motion:{type:"BasicMotionEngine",options:{rotationStep:.5,actions:["UP","LEFT","RIGHT","DOWN","WAIT"]}}}}}};function m(t,e){var i=new XMLHttpRequest;i.overrideMimeType("application/json"),i.open("GET",t,!0),i.onreadystatechange=function(){4==i.readyState&&200==i.status&&e(JSON.parse(i.responseText))},i.send(null)}function f(t,e){var i=t.split("//");i=i[1].split("/"),e(y[i[0]][i[1]])}function g(t,e){({"embedded:":f,"http:":m,"https:":m})[t.split("//")[0]](t,e)}function x(t,e){var i=document.createElement("a");document.body.appendChild(i);var o=new Blob([t],{type:"application/octet-binary"}),n=window.URL.createObjectURL(o);i.href=n,i.download=e,i.click(),window.URL.revokeObjectURL(n),i.href=""}function _(t){var e={};return e.code=t,e.isDown=!1,e.isUp=!0,e.press=void 0,e.release=void 0,e.downHandler=function(t){t.keyCode===e.code&&(e.isUp&&e.press&&e.press(),e.isDown=!0,e.isUp=!1),t.preventDefault()},e.upHandler=function(t){t.keyCode===e.code&&(e.isDown&&e.release&&e.release(),e.isDown=!1,e.isUp=!0),t.preventDefault()},window.addEventListener("keydown",e.downHandler.bind(e),!1),window.addEventListener("keyup",e.upHandler.bind(e),!1),e}var I,C=0,w=function(){function t(t,e,i,o){void 0===o&&(o={});var n=this;this.level=t,o.image=o.image||p.DEFAULT,o.lidar=o.lidar||!1,o.lidarInfo=o.lidarInfo||{pts:5,width:4,height:5,pos:0},this.core=new a(i[o.image]),this.info=e,this.core.scale.x=.8,this.core.scale.y=.8,this.core.anchor.x=.5,this.core.anchor.y=.5,this.core.road=void 0,this.core.carId=C,C+=1,this.core.agent=!1,this.core.obstacle=!0,this.core.mapId=u.CAR,o.lidar&&(this.createLidar(o.lidarInfo),this.core.lidar=this.lidar),o.motionEngine?(this.motion=o.motionEngine,this.motion.setUp(this.core,this.lidar),this.core.rotationStep=.5):this.core.rotationStep=.5,this.core.mx=this.info.mx,this.core.my=this.info.my,this.core.x=this.info.mx*d,this.core.y=this.info.my*d,this.core.checkAndsetNewRoad=function(t){return n.checkAndsetNewRoad(t)};var r=this.level.getRoad(this.core.my,this.core.mx);this.core.line=0,r&&r.setCarPosition(this.core,this.info.line)}return t.prototype.checkAndsetNewRoad=function(t){var e=this.core.road;t||(t=this.level.getRoad(this.core.my,this.core.mx)),t!=e&&(e&&e.cars.splice(e.cars.indexOf(this.core.carId),1),this.core.road=t,this.core.haveTurned=!1,this.core.optionalTurn=!1,this.turnedRandom=void 0,t&&t.cars.push(this.core.carId))},t.prototype.reset=function(){this.core.mx=this.info.mx,this.core.my=this.info.my;var t=this.level.getRoad(this.core.my,this.core.mx);t&&t.setCarPosition(this.core,this.info.line,!0)},t.prototype.getState=function(t){if(void 0===t&&(t=!1),t){var e=[];return this.motion.state.map(function(t){e=e.concat(t)}),e}return this.motion.state.map(function(t){return t.slice()})},t.prototype.step=function(t,e){if(void 0===e&&(e=null),null==e)var i=this.motion.step(t),o=i.agentCollisions,n=i.onRoad;else{var a=this.motion.actionStep(t,e);o=a.agentCollisions,n=a.onRoad}return{agentCollisions:o,onRoad:n}},t.prototype.createLidar=function(t){t.pts=t.pts||5,t.width=t.width||4,t.height=t.height||5,t.pos=t.pos||0,this.lidar=new s,this.lidar.lidarPts=[],this.lidar.collisionPts=[];var e=new r;e.pt=!1,e.alpha=.3,e.beginFill(5329233),e.drawRect(0,0,this.core.width*t.width,this.core.height*t.height),e.endFill();var i=new r;i.pt=!1,i.alpha=0,i.beginFill(5308416),i.drawRect(this.core.width-5,e.height/2,3,3),i.endFill();var o=new r;o.pt=!1,o.alpha=0,o.beginFill(5308416),o.drawRect(-1,e.height/2-2,3,3),o.endFill();var n=new r;n.pt=!1,n.alpha=0,n.beginFill(5308416),n.drawRect(this.core.width/2,e.height/2-this.core.height/2+5,3,3),n.endFill();var a=new r;a.pt=!1,a.alpha=0,a.beginFill(5308416),a.drawRect(this.core.width/2,e.height/2+this.core.height/2-8,3,3),a.endFill(),o.x-=t.pos*this.core.width,i.x-=t.pos*this.core.width,a.x-=t.pos*this.core.width,n.x-=t.pos*this.core.width;for(var h=e.width/t.pts,c=e.height/t.pts,l=t.pts-1;l>=0;l--)for(var d=0;do.x&&i.xo.y&&i.y=4||!t)&&(r=!0),{agentCollisions:o,onRoad:r}},t.prototype.setState=function(t){for(var e=0,i=0;ithis.state[o][n]||t[a].mapId==u.ROAD&&this.state[o][n]==u.DEFAULT)&&(this.state[o][n]=t[a].mapId)}e+=1}},t}(),b=(I=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var i in e)e.hasOwnProperty(i)&&(t[i]=e[i])},function(t,e){function i(){this.constructor=t}I(t,e),t.prototype=null===e?Object.create(e):(i.prototype=e.prototype,new i)}),R=function(t){function e(e,i){var o=t.call(this,e)||this;return o.rotationStep=i.rotationStep||.5,o.actions=i.actions||["UP","LEFT","RIGHT","DOWN","WAIT"],o}return b(e,t),e.prototype.setUp=function(t,e){this.car=t,this.lidar=e,this.state=[];for(var i=0;i0&&(this.car.v=0,this.car.vy=0),{agentCollisions:i,onRoad:o}},e}(M),E=function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var i in e)e.hasOwnProperty(i)&&(t[i]=e[i])};return function(e,i){function o(){this.constructor=e}t(e,i),e.prototype=null===i?Object.create(i):(o.prototype=i.prototype,new o)}}(),L=function(t){function e(e){var i=t.call(this,e)||this;return i.map=i.level.getMap(),i.mapSizeY=i.map.length,i.mapSizeX=i.map[0].length,i.rotationStep=.5,i.rotationToNextCase={},i.rotationToNextCase[0]={mx:1,my:0},i.rotationToNextCase[.5]={mx:0,my:1},i.rotationToNextCase[1]={mx:-1,my:0},i.rotationToNextCase[1.5]={mx:0,my:-1},i.rotationToNextCase[-.5]={mx:0,my:-1},i.rotationToNextCase[-1]={mx:-1,my:0},i.rotationToNextCase[-1.5]={mx:0,my:1},i}return E(e,t),e.prototype.setUp=function(t,e){this.car=t,this.lidar=e,this.state=[];for(var i=0;i=this.mapSizeY||t<0||t>=this.mapSizeX||0==this.map[e][t])return!1;if(i&&this.level.getRoad(e,t).cars.length>0)return!1;return!0},e.prototype.autoRotation=function(){var t=d+d/3,e=d-d/3.5,i=d;if(this.car.next_road){var o=Math.abs((this.car.next_road.outroad.y+d/2-this.car.y)*this.car.next_road.outroad_np.my),n=Math.abs((this.car.next_road.outroad.x+d/2-this.car.x)*this.car.next_road.outroad_np.mx);if(0==this.car.next_road.rotation_type&&othis.car.next_road.outroad.x+d/2?(this.car.x=this.car.next_road.outroad.x+d-d/2-d/4,this.car.y=this.car.next_road.outroad.y):0!=n&&this.car.ythis.car.next_road.outroad.y+d/2&&(this.car.y=this.car.next_road.outroad.y+d-d/2-d/4,this.car.x=this.car.next_road.outroad.x),void(this.car.next_road=void 0)}else{var a=this.car.rotation/Math.PI,r=(this.car.rotation/Math.PI+.5)%2,s=(this.car.rotation/Math.PI-.5)%2,h=this.rotationToNextCase[a],c=this.rotationToNextCase[r],l=this.rotationToNextCase[s],p=!1,u=this.car.mx+h.mx,v=this.car.my+h.my;if(this.isRoad(u,v)?this.car.haveTurned||this.car.turnedRandom?!this.car.haveTurned&&this.car.turnedRandom<.5&&this.isRoad(this.car.mx+c.mx,this.car.my+c.my)?(p=!0,this.car.optionalTurn=!0):!this.car.haveTurned&&this.car.turnedRandom>=.5&&this.isRoad(this.car.mx+l.mx,this.car.my+l.my)&&(p=!0,this.car.optionalTurn=!0):this.car.turnedRandom=Math.random():(p=!0,this.car.optionalTurn=!1),p)for(var y=[(a+.5)%2,(a-.5)%2,null],m=0;m=1&&n&&n.core.line==i&&(i=0==i?1:0),e.line=i,e.x=t.x,e.y=t.y,e.x+=d/2,e.y+=d/2;var a=1*d/4;t.orientation=Math.floor(t.orientation/e.rotationStep)*e.rotationStep;var r=-t.orientation*Math.PI,s=0*Math.cos(r)-a*Math.sin(r),h=Math.cos(r)*a+0*Math.sin(r),c=0==i?1:-1,l=0==i?0:Math.PI;e.x+=c*Math.round(s),e.y+=c*Math.round(h),e.mx=Math.floor(e.x/d),e.my=Math.floor(e.y/d),e.rotation=r+l,e.checkAndsetNewRoad(t)},t.prototype.createAsset=function(t,e,i,o){var n=new a(i[t]);n.obstacle=!1,n.type=o,n.mapId=0,n.x=e.x,n.y=e.y,this.level.addChild(n),this.assets.push(n)},t.prototype.createMap=function(t,e,i,o){if(void 0===o&&(o=!0),o)for(var n=0;n0?o=-1:e||(o=-1),o},e.prototype.getLastReward=function(){return this.lastReward},e.prototype.step=function(t,e){void 0===e&&(e=null);for(var i=0;i0&&0==this.car.a?this.car.v=Math.max(0,this.car.v-.01):this.car.v<0&&0==this.car.a&&(this.car.v=Math.min(0,this.car.v+.01)),this.car.a>0&&this.car.v>=0?this.car.v=Math.min(1.5,this.car.v+.01*this.car.a):this.car.a>0&&this.car.v<0&&(this.car.v=Math.min(1.5,this.car.v+.03*this.car.a)),this.car.a<0&&this.car.v<=0?this.car.v=Math.max(-1.5,this.car.v+.01*this.car.a):this.car.a<0&&this.car.v>0&&(this.car.v=Math.max(-1.5,this.car.v+.03*this.car.a)),0==this.car.yaw_rate)this.car.x+=this.car.v*Math.cos(this.car.rotation)*t,this.car.y+=this.car.v*Math.sin(this.car.rotation)*t;else{var e=.01*this.car.yaw_rate*(this.car.v+.001)*Math.PI;this.car.x+=this.car.v/e*(Math.sin(this.car.rotation+e*t)-Math.sin(this.car.rotation)),this.car.y+=this.car.v/e*(Math.cos(this.car.rotation)-Math.cos(this.car.rotation+e*t)),this.car.rotation+=e*t}this.car.mx=Math.floor(this.car.x/d),this.car.my=Math.floor(this.car.y/d),this.car.checkAndsetNewRoad(),this.lidar.x=this.car.x,this.lidar.y=this.car.y,this.lidar.rotation=this.car.rotation;var i=this.detectInteractions(!0,!0),o=i.agentCollisions,n=i.onRoad;return o.length>0&&(this.car.v=0,this.car.vy=0),{agentCollisions:o,onRoad:n}},e}(M),j={env:k,editor:U,level:{fullCity:"embedded://level/fullCity",level1:"embedded://level/level1",level2:"embedded://level/level2",level0:"embedded://level/level0"},motion:{BasicMotion:R,ControlMotion:B}};e.default=j}]).default; \ No newline at end of file +window.metacar=function(t){var e={};function i(o){if(e[o])return e[o].exports;var n=e[o]={i:o,l:!1,exports:{}};return t[o].call(n.exports,n,n.exports,i),n.l=!0,n.exports}return i.m=t,i.c=e,i.d=function(t,e,o){i.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:o})},i.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},i.t=function(t,e){if(1&e&&(t=i(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var o=Object.create(null);if(i.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var n in t)i.d(o,n,function(e){return t[e]}.bind(null,n));return o},i.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return i.d(e,"a",e),e},i.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},i.p="",i(i.s=1)}([function(t,e){t.exports=window.PIXI},function(t,e,i){"use strict";i.r(e);var o=i(0),n=(o.Application,o.loader),a=(o.loader.resources,o.Sprite),r=o.Graphics,s=o.Container,h="https://metacar-project.com/",c=h+"public/textures/textures.json",l=h+"public/images/",d=60,p={DEFAULT:"car.png",AGENT:"car_agent.png"},y={ROAD:-1,DEFAULT:0,CAR:1,AGENT:1},u={ROADS:{"↕":{image:"road_u.png",orientation:1.5},"↔":{image:"road_r.png",orientation:0},"↱":{image:"road_rotate_ld.png",orientation:1.25},"↰":{image:"road_rotate_ul.png",orientation:.75},"↲":{image:"road_rotate_rl.png",orientation:.25},"↳":{image:"road_rotate_dl.png",orientation:1.75},"↟":{image:"road_up.png",orientation:1.5},"↠":{image:"road_rp.png",orientation:0},"↤":{image:"road_r2.png",orientation:0},"↦":{image:"road_l2.png",orientation:1},"↥":{image:"road_d2.png",orientation:.5},"↧":{image:"road_u2.png",orientation:1.5}},house:{image:"house.png"},house2:{image:"house2.png"},house3:{image:"house3.png"},bench:{image:"bench.png"},tree:{image:"tree.png"}},x={level:{fullCity:{cars:[{mx:2,my:5,line:0,auto:!0},{mx:3,my:4,line:0,auto:!0},{mx:3,my:4,line:1,auto:!0},{mx:6,my:3,line:0,auto:!0},{mx:6,my:3,line:1,auto:!0},{mx:8,my:2,line:0,auto:!0},{mx:8,my:2,line:1,auto:!0},{mx:2,my:7,line:0,auto:!0}],house:[{x:9,y:409},{x:146,y:162},{x:439,y:39}],house2:[{x:425,y:192},{x:431,y:354}],house3:[{x:426,y:281},{x:51,y:157},{x:553,y:34}],bench:[{x:259,y:206},{x:8,y:205}],tree:[{x:1,y:303},{x:54,y:357},{x:215,y:325},{x:230,y:400},{x:282,y:368},{x:182,y:433},{x:285,y:309},{x:576,y:233},{x:581,y:300},{x:519,y:388},{x:582,y:391},{x:256,y:69},{x:151,y:43},{x:77,y:77},{x:26,y:24},{x:189,y:98}],map:[[0,0,0,0,0,0,"↕",0,0,0,0],[0,0,0,0,0,0,"↟",0,0,0,0],[0,0,0,0,0,0,"↦","↔","↔","↠","↔"],[0,0,0,0,0,0,"↕",0,0,0,0],["↔","↔","↧","↔","↠","↔","↤",0,0,0,0],[0,0,"↕",0,0,0,"↕",0,0,0,0],[0,0,"↟",0,0,0,"↟",0,0,0,0],[0,0,"↕",0,0,0,"↕",0,0,0,0]]},level1:{cars:[{mx:5,my:3,line:0,auto:!0},{mx:5,my:3,line:1,auto:!0},{mx:5,my:1,line:0,auto:!0}],tree:[{x:120,y:50},{x:86.5,y:85},{x:94.5,y:135},{x:30.5,y:128},{x:72.5,y:190},{x:23.5,y:232},{x:110.5,y:242},{x:43.5,y:33},{x:18.5,y:178},{x:305.5,y:125},{x:360.5,y:122}],asset:[{x:252.5,y:142},{x:86.5,y:85},{x:94.5,y:135},{x:30.5,y:128},{x:252.5,y:26},{x:195.5,y:0},{x:307.5,y:1},{x:72.5,y:190},{x:23.5,y:232},{x:110.5,y:242},{x:43.5,y:33},{x:35.5,y:185},{x:18.5,y:178},{x:289.5,y:126},{x:337.5,y:126},{x:305.5,y:125},{x:360.5,y:122},{x:492.5,y:56},{x:503.5,y:136},{x:504.5,y:58}],bench:[{x:252.5,y:142},{x:252.5,y:26}],road:[{x:240,y:60},{x:240,y:60},{x:480,y:0},{x:480,y:60},{x:480,y:120},{x:480,y:180},{x:480,y:240},{x:300,y:180},{x:300,y:180},{x:360,y:180},{x:420,y:180},{x:420,y:120},{x:420,y:60},{x:360,y:60},{x:300,y:60},{x:420,y:120}],house3:[{x:503.5,y:136}],house:[{x:504.5,y:58}],map:[[0,0,0,0,0,0,0,0,0,0],[0,0,0,"↱","↠","↔","↔","↰",0,0],[0,0,0,"↕",0,0,0,"↟",0,0],[0,0,0,"↳","↔","↔","↔","↲",0,0],[0,0,0,0,0,0,0,0,0,0]],agent:{mx:4,my:3,line:0,motion:{type:"BasicMotionEngine",options:{rotationStep:.5,actions:["UP","LEFT","RIGHT"]}}}},level0:{cars:[],house:[{x:263,y:108},{x:112,y:258}],house3:[{x:262,y:16}],bench:[{x:135,y:34}],tree:[{x:124,y:134},{x:7,y:11},{x:69,y:10},{x:19,y:238},{x:5,y:133},{x:260,y:211}],map:[[0,0,0,0,0],[0,"↱","↔","↰",0],[0,"↕",0,"↕",0],[0,"↳","↠","↲",0],[0,0,0,0,0]],agent:{mx:2,my:1,line:1,motion:{type:"BasicMotionEngine",options:{rotationStep:.5,actions:["UP","LEFT","RIGHT","DOWN","WAIT"]}}}},level2:{cars:[],road:[{x:300,y:360},{x:300,y:360},{x:360,y:360},{x:420,y:360},{x:240,y:360},{x:180,y:360},{x:120,y:360},{x:480,y:360},{x:480,y:360},{x:120,y:360},{x:60,y:360},{x:300,y:300},{x:300,y:240},{x:480,y:360},{x:480,y:360},{x:540,y:360},{x:300,y:180},{x:360,y:180},{x:240,y:180},{x:240,y:180},{x:180,y:180},{x:420,y:180},{x:420,y:120},{x:180,y:120},{x:300,y:60},{x:420,y:60},{x:180,y:60},{x:240,y:60},{x:360,y:60},{x:480,y:60},{x:120,y:60},{x:540,y:60},{x:540,y:120},{x:540,y:180},{x:540,y:240},{x:540,y:300},{x:60,y:60},{x:60,y:120},{x:60,y:180},{x:60,y:240},{x:60,y:300},{x:60,y:240},{x:360,y:180},{x:480,y:360}],asset:[{x:162,y:264},{x:407,y:264},{x:259,y:126},{x:306,y:33},{x:491,y:148},{x:391,y:431},{x:608,y:35},{x:484,y:224},{x:248,y:297},{x:376,y:336},{x:78,y:429},{x:1,y:205},{x:3,y:106},{x:-46,y:290},{x:622,y:154},{x:617,y:280}],house:[{x:162,y:264},{x:622,y:154}],house3:[{x:407,y:264},{x:-46,y:290}],tree:[{x:259,y:126},{x:491,y:148},{x:391,y:431},{x:608,y:35},{x:484,y:224},{x:248,y:297},{x:78,y:429},{x:1,y:205},{x:3,y:106},{x:617,y:280}],bench:[{x:306,y:33},{x:376,y:336}],map:[[0,0,0,0,0,0,0,0,0,0,0],[0,"↱","↔","↧","↔","↔","↔","↧","↔","↰",0],[0,"↕",0,"↕",0,0,0,"↕",0,"↕",0],[0,"↕",0,"↳","↔","↧","↠","↲",0,"↕",0],[0,"↟",0,0,0,"↕",0,0,0,"↕",0],[0,"↕",0,0,0,"↕",0,0,0,"↕",0],[0,"↳","↔","↔","↔","↥","↔","↔","↠","↲",0],[0,0,0,0,0,0,0,0,0,0,0]],agent:{mx:4,my:3,line:0,motion:{type:"BasicMotionEngine",options:{rotationStep:.5,actions:["UP","LEFT","RIGHT","DOWN","WAIT"]}}}},level3:{cars:[{mx:1,my:3,line:0},{mx:1,my:8,line:0},{mx:4,my:8,line:0},{mx:7,my:7,line:1},{mx:9,my:6,line:0},{mx:11,my:4,line:1},{mx:9,my:3,line:1},{mx:7,my:2,line:1},{mx:5,my:1,line:1},{mx:2,my:1,line:1},{mx:6,my:8,line:0},{mx:1,my:6,line:0},{mx:5,my:5,line:1},{mx:4,my:3,line:1},{mx:3,my:4,line:0},{mx:4,my:6,line:0}],road:[{x:120,y:60},{x:180,y:60},{x:240,y:60},{x:60,y:60},{x:60,y:120},{x:60,y:180},{x:60,y:240},{x:60,y:300},{x:60,y:360},{x:60,y:420},{x:60,y:480},{x:120,y:480},{x:180,y:480},{x:240,y:480},{x:300,y:60},{x:300,y:120},{x:300,y:180},{x:300,y:480},{x:300,y:420},{x:300,y:360},{x:300,y:180},{x:360,y:180},{x:420,y:180},{x:420,y:120},{x:420,y:60},{x:240,y:240},{x:300,y:60},{x:360,y:60},{x:240,y:180},{x:300,y:180},{x:300,y:240},{x:300,y:300},{x:240,y:300},{x:180,y:180},{x:180,y:240},{x:180,y:300},{x:360,y:60},{x:420,y:60},{x:420,y:60},{x:420,y:120},{x:420,y:180},{x:420,y:180},{x:480,y:180},{x:540,y:180},{x:660,y:180},{x:600,y:180},{x:660,y:240},{x:660,y:300},{x:660,y:360},{x:600,y:360},{x:540,y:360},{x:480,y:360},{x:420,y:360},{x:300,y:480},{x:360,y:480},{x:360,y:420},{x:360,y:360},{x:420,y:360},{x:420,y:420},{x:420,y:480},{x:360,y:480},{x:180,y:360},{x:240,y:360},{x:300,y:360},{x:300,y:300},{x:300,y:240},{x:180,y:180},{x:240,y:180},{x:300,y:180},{x:180,y:240},{x:180,y:300},{x:240,y:180},{x:180,y:60},{x:60,y:300},{x:300,y:480},{x:240,y:480},{x:600,y:180},{x:540,y:180},{x:660,y:300},{x:480,y:360}],asset:[{x:515,y:262},{x:378,y:254},{x:510,y:89},{x:626,y:86},{x:518,y:445},{x:631,y:436},{x:711,y:434},{x:243,y:295},{x:243,y:246},{x:253,y:153},{x:16,y:521},{x:129,y:214},{x:121,y:228},{x:238,y:425},{x:456,y:273},{x:584,y:531},{x:376,y:558},{x:63,y:539},{x:91,y:559},{x:4,y:515},{x:-3,y:315},{x:-3,y:183},{x:368,y:188},{x:500,y:18},{x:610,y:16},{x:685,y:13},{x:273,y:0},{x:738,y:359},{x:742,y:181},{x:752,y:12}],house3:[{x:515,y:262},{x:626,y:86}],house2:[{x:378,y:254},{x:631,y:436},{x:711,y:434},{x:752,y:12}],house:[{x:510,y:89},{x:518,y:445},{x:742,y:181}],tree:[{x:243,y:295},{x:243,y:246},{x:121,y:228},{x:238,y:425},{x:456,y:273},{x:584,y:531},{x:376,y:558},{x:91,y:559},{x:4,y:515},{x:-3,y:315},{x:-3,y:183},{x:368,y:188},{x:500,y:18},{x:610,y:16},{x:685,y:13},{x:273,y:0},{x:738,y:359}],bench:[{x:253,y:153}],map:[[0,0,0,0,0,0,0,0,0,0,0,0,0],[0,"↱","↔","↠","↔","↔","↔","↰",0,0,0,0,0],[0,"↕",0,0,0,0,0,"↕",0,0,0,0,0],[0,"↕",0,"↱","↠","↰",0,"↳","↔","↠","↔","↰",0],[0,"↕",0,"↕",0,"↕",0,0,0,0,0,"↕",0],[0,"↟",0,"↕",0,"↕",0,0,0,0,0,"↟",0],[0,"↕",0,"↳","↔","↲",0,"↱","↠","↔","↔","↲",0],[0,"↕",0,0,0,0,0,"↕",0,0,0,0,0],[0,"↳","↔","↔","↔","↠","↔","↲",0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0]],agent:{mx:3,my:8,line:0,motion:{type:"BasicMotionEngine",options:{rotationStep:.5,actions:["UP","LEFT","RIGHT","DOWN","WAIT"]}}}}}};function v(t,e){var i=new XMLHttpRequest;i.overrideMimeType("application/json"),i.open("GET",t,!0),i.onreadystatechange=function(){4==i.readyState&&200==i.status&&e(JSON.parse(i.responseText))},i.send(null)}function m(t,e){var i=t.split("//");i=i[1].split("/"),e(x[i[0]][i[1]])}function f(t,e){({"embedded:":m,"http:":v,"https:":v})[t.split("//")[0]](t,e)}function g(t,e){var i=document.createElement("a");document.body.appendChild(i);var o=new Blob([t],{type:"application/octet-binary"}),n=window.URL.createObjectURL(o);i.href=n,i.download=e,i.click(),window.URL.revokeObjectURL(n),i.href=""}function _(t){var e={};return e.code=t,e.isDown=!1,e.isUp=!0,e.press=void 0,e.release=void 0,e.downHandler=function(t){t.keyCode===e.code&&(e.isUp&&e.press&&e.press(),e.isDown=!0,e.isUp=!1),t.preventDefault()},e.upHandler=function(t){t.keyCode===e.code&&(e.isDown&&e.release&&e.release(),e.isDown=!1,e.isUp=!0),t.preventDefault()},window.addEventListener("keydown",e.downHandler.bind(e),!1),window.addEventListener("keyup",e.upHandler.bind(e),!1),e}var I,C=0,w=function(){function t(t,e,i,o){void 0===o&&(o={});var n=this;this.level=t,o.image=o.image||p.DEFAULT,o.lidar=o.lidar||!1,o.lidarInfo=o.lidarInfo||{pts:5,width:4,height:5,pos:0},this.core=new a(i[o.image]),this.info=e,this.core.scale.x=.8,this.core.scale.y=.8,this.core.anchor.x=.5,this.core.anchor.y=.5,this.core.road=void 0,this.core.carId=C,C+=1,this.core.agent=!1,this.core.obstacle=!0,this.core.mapId=y.CAR,o.lidar&&(this.createLidar(o.lidarInfo),this.core.lidar=this.lidar),o.motionEngine?(this.motion=o.motionEngine,this.motion.setUp(this.core,this.lidar),this.core.rotationStep=.5):this.core.rotationStep=.5,this.core.mx=this.info.mx,this.core.my=this.info.my,this.core.x=this.info.mx*d,this.core.y=this.info.my*d,this.core.checkAndsetNewRoad=function(t){return n.checkAndsetNewRoad(t)};var r=this.level.getRoad(this.core.my,this.core.mx);this.core.line=0,r&&r.setCarPosition(this.core,this.info.line)}return t.prototype.checkAndsetNewRoad=function(t){var e=this.core.road;t||(t=this.level.getRoad(this.core.my,this.core.mx)),t!=e&&(e&&e.cars.splice(e.cars.indexOf(this.core.carId),1),this.core.road=t,this.core.haveTurned=!1,this.core.optionalTurn=!1,this.core.turnedRandom=void 0,this.core.next_road=!1,t&&t.cars.push(this.core.carId))},t.prototype.reset=function(){this.core.mx=this.info.mx,this.core.my=this.info.my;var t=this.level.getRoad(this.core.my,this.core.mx);t&&t.setCarPosition(this.core,this.info.line,!0)},t.prototype.getState=function(){var t={},e=[];return this.motion.state.map(function(t){e=e.concat(t)}),e.push(this.core.v),t.linear=e,t.lidar=this.motion.state.map(function(t){return t.slice()}),t.v=this.core.v,t.a=this.core.last_a,t.steering=this.core.last_yaw_rate,t},t.prototype.step=function(t,e){if(void 0===e&&(e=null),null==e)var i=this.motion.step(t),o=i.agentCollisions,n=i.onRoad;else{var a=this.motion.actionStep(t,e);o=a.agentCollisions,n=a.onRoad}return{agentCollisions:o,onRoad:n}},t.prototype.createLidar=function(t){t.pts=t.pts||5,t.width=t.width||4,t.height=t.height||5,t.pos=t.pos||0,this.lidar=new s,this.lidar.lidarPts=[],this.lidar.collisionPts=[];var e=new r;e.pt=!1,e.alpha=.3,e.beginFill(5329233),e.drawRect(0,0,this.core.width*t.width,this.core.height*t.height),e.endFill();var i=new r;i.pt=!1,i.alpha=0,i.beginFill(5308416),i.drawRect(this.core.width-5,e.height/2,3,3),i.endFill();var o=new r;o.pt=!1,o.alpha=0,o.beginFill(5308416),o.drawRect(-1,e.height/2-2,3,3),o.endFill();var n=new r;n.pt=!1,n.alpha=0,n.beginFill(5308416),n.drawRect(this.core.width/2,e.height/2-this.core.height/2+5,3,3),n.endFill();var a=new r;a.pt=!1,a.alpha=0,a.beginFill(5308416),a.drawRect(this.core.width/2,e.height/2+this.core.height/2-8,3,3),a.endFill(),o.x-=t.pos*this.core.width,i.x-=t.pos*this.core.width,a.x-=t.pos*this.core.width,n.x-=t.pos*this.core.width;for(var h=e.width/t.pts,c=e.height/t.pts,l=t.pts-1;l>=0;l--)for(var d=0;do.x&&i.xo.y&&i.y=4||!t)&&(r=!0),{agentCollisions:o,onRoad:r}},t.prototype.setState=function(t){for(var e=0,i=0;ithis.state[o][n]||t[a].mapId==y.ROAD&&this.state[o][n]==y.DEFAULT)&&(this.state[o][n]=t[a].mapId)}e+=1}},t}(),b=(I=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var i in e)e.hasOwnProperty(i)&&(t[i]=e[i])},function(t,e){function i(){this.constructor=t}I(t,e),t.prototype=null===e?Object.create(e):(i.prototype=e.prototype,new i)}),R=function(t){function e(e,i){var o=t.call(this,e)||this;return o.maxSpeed=1,o.rotationStep=i.rotationStep||.5,o.actions=i.actions||["UP","LEFT","RIGHT","DOWN","WAIT"],o}return b(e,t),e.prototype.setUp=function(t,e){this.car=t,this.lidar=e,this.state=[];for(var i=0;i0&&(this.car.v=0,this.car.vy=0),{agentCollisions:i,onRoad:o}},e}(M),E=function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var i in e)e.hasOwnProperty(i)&&(t[i]=e[i])};return function(e,i){function o(){this.constructor=e}t(e,i),e.prototype=null===i?Object.create(i):(o.prototype=i.prototype,new o)}}(),S=function(t){function e(e){var i=t.call(this,e)||this;return i.map=i.level.getMap(),i.mapSizeY=i.map.length,i.mapSizeX=i.map[0].length,i.rotationStep=.5,i.rotationToNextCase={},i.rotationToNextCase[0]={mx:1,my:0},i.rotationToNextCase[.5]={mx:0,my:1},i.rotationToNextCase[1]={mx:-1,my:0},i.rotationToNextCase[1.5]={mx:0,my:-1},i.rotationToNextCase[-.5]={mx:0,my:-1},i.rotationToNextCase[-1]={mx:-1,my:0},i.rotationToNextCase[-1.5]={mx:0,my:1},i}return E(e,t),e.prototype.setUp=function(t,e){this.car=t,this.lidar=e,this.state=[];for(var i=0;i=this.mapSizeY||t<0||t>=this.mapSizeX||0==this.map[e][t])return!1;if(i&&this.level.getRoad(e,t).cars.length>0)return!1;return!0},e.prototype.autoRotation=function(){var t=d+d/3,e=d-d/3.5,i=d;if(this.car.next_road){var o=Math.abs((this.car.next_road.outroad.y+d/2-this.car.y)*this.car.next_road.outroad_np.my),n=Math.abs((this.car.next_road.outroad.x+d/2-this.car.x)*this.car.next_road.outroad_np.mx);if(0==this.car.next_road.rotation_type&&othis.car.next_road.outroad.x+d/2?(this.car.x=this.car.next_road.outroad.x+d-d/2-d/4,this.car.y=this.car.next_road.outroad.y):0!=n&&this.car.ythis.car.next_road.outroad.y+d/2&&(this.car.y=this.car.next_road.outroad.y+d-d/2-d/4,this.car.x=this.car.next_road.outroad.x),void(this.car.next_road=void 0)}else{var a=this.car.rotation/Math.PI,r=(this.car.rotation/Math.PI+.5)%2,s=(this.car.rotation/Math.PI-.5)%2,h=this.rotationToNextCase[a],c=this.rotationToNextCase[r],l=this.rotationToNextCase[s],p=!1,y=this.car.mx+h.mx,u=this.car.my+h.my;if(this.isRoad(y,u)?this.car.haveTurned||this.car.turnedRandom?!this.car.haveTurned&&this.car.turnedRandom<.5&&this.isRoad(this.car.mx+c.mx,this.car.my+c.my)?(p=!0,this.car.optionalTurn=!0):!this.car.haveTurned&&this.car.turnedRandom>=.5&&this.isRoad(this.car.mx+l.mx,this.car.my+l.my)&&(p=!0,this.car.optionalTurn=!0):this.car.turnedRandom=Math.random():(p=!0,this.car.optionalTurn=!1),p)for(var x=[(a+.5)%2,(a-.5)%2,null],v=0;v=1&&n&&n.core.line==i&&(i=0==i?1:0),e.line=i,e.a=0,e.v=0,e.x=t.x,e.y=t.y,e.x+=d/2,e.y+=d/2;var a=1*d/4;t.orientation=Math.floor(t.orientation/e.rotationStep)*e.rotationStep;var r=-t.orientation*Math.PI,s=0*Math.cos(r)-a*Math.sin(r),h=Math.cos(r)*a+0*Math.sin(r),c=0==i?1:-1,l=0==i?0:Math.PI;e.x+=c*Math.round(s),e.y+=c*Math.round(h),e.mx=Math.floor(e.x/d),e.my=Math.floor(e.y/d),e.rotation=r+l,e.checkAndsetNewRoad(t)},t.prototype.createAsset=function(t,e,i,o){var n=new a(i[t]);n.obstacle=!1,n.type=o,n.mapId=0,n.x=e.x,n.y=e.y,this.level.addChild(n),this.assets.push(n)},t.prototype.createMap=function(t,e,i,o){if(void 0===o&&(o=!0),o)for(var n=0;n0?o=-1:e||(o=-1),o},e.prototype.getLastReward=function(){return this.lastReward},e.prototype.step=function(t,e,i){if(void 0===e&&(e=null),void 0===i&&(i=!0),!i||this.steping){for(var o=0;o0&&0==this.car.a?this.car.v=Math.max(0,this.car.v-.01):this.car.v<0&&0==this.car.a&&(this.car.v=Math.min(0,this.car.v+.01)),this.car.a>0&&this.car.v>=0?this.car.v=Math.min(this.maxSpeed,this.car.v+.005*this.car.a):this.car.a>0&&this.car.v<0&&(this.car.v=Math.min(this.maxSpeed,this.car.v+.03*this.car.a)),this.car.a<0&&this.car.v<=0?this.car.v=Math.max(-this.maxSpeed,this.car.v+.005*this.car.a):this.car.a<0&&this.car.v>0&&(this.car.v=Math.max(-this.maxSpeed,this.car.v+.03*this.car.a)),0==this.car.yaw_rate)this.car.x+=this.car.v*Math.cos(this.car.rotation)*t,this.car.y+=this.car.v*Math.sin(this.car.rotation)*t;else{var e=.01*this.car.yaw_rate*(this.car.v+.001)*Math.PI;this.car.x+=this.car.v/e*(Math.sin(this.car.rotation+e*t)-Math.sin(this.car.rotation)),this.car.y+=this.car.v/e*(Math.cos(this.car.rotation)-Math.cos(this.car.rotation+e*t)),this.car.rotation+=e*t}this.car.mx=Math.floor(this.car.x/d),this.car.my=Math.floor(this.car.y/d),this.car.checkAndsetNewRoad(),this.lidar.x=this.car.x,this.lidar.y=this.car.y,this.lidar.rotation=this.car.rotation;var i=this.detectInteractions(!0,!0),o=i.agentCollisions,n=i.onRoad;return o.length>0&&(this.car.v=0,this.car.vy=0),{agentCollisions:o,onRoad:n}},e}(M),j={env:k,editor:U,level:{fullCity:"embedded://level/fullCity",level1:"embedded://level/level1",level2:"embedded://level/level2",level3:"embedded://level/level3",level0:"embedded://level/level0"},motion:{BasicMotion:R,ControlMotion:B}};e.default=j}]).default; \ No newline at end of file diff --git a/demo/webapp/ddpg-traffic.html b/demo/webapp/ddpg-traffic.html index ff6108b..f522764 100644 --- a/demo/webapp/ddpg-traffic.html +++ b/demo/webapp/ddpg-traffic.html @@ -32,7 +32,6 @@
  • Deep Deterministic Policy Gradients (DDPG): paper
  • Parameter Space Noise for Exploration: paper
  • -
  • Prioritized Experience Replay: paper

You can use the arrow keys to control the car by yourself.

@@ -58,6 +57,7 @@ + diff --git a/demo/webapp/ddpg.html b/demo/webapp/ddpg.html index 1fa1e0a..da58b53 100644 --- a/demo/webapp/ddpg.html +++ b/demo/webapp/ddpg.html @@ -32,7 +32,6 @@
  • Deep Deterministic Policy Gradients (DDPG): paper
  • Parameter Space Noise for Exploration: paper
  • -
  • Prioritized Experience Replay: paper

You can use the arrow keys to control the car by yourself.

@@ -57,6 +56,7 @@ + diff --git a/demo/webapp/index.html b/demo/webapp/index.html index 09c30eb..8a3f655 100644 --- a/demo/webapp/index.html +++ b/demo/webapp/index.html @@ -71,7 +71,6 @@
  • Deep Deterministic Policy Gradients (DDPG): paper
  • Parameter Space Noise for Exploration: paper
  • -
  • Prioritized Experience Replay: paper

@@ -90,7 +89,6 @@

  • Deep Deterministic Policy Gradients (DDPG): paper
  • Parameter Space Noise for Exploration: paper
  • -
  • Prioritized Experience Replay: paper

The control is based on two continuous values for the throttle and steering angle of the car. diff --git a/demo/webapp/public/js/ddpg-traffic/index.js b/demo/webapp/public/js/ddpg-traffic/index.js index 141181d..db93f4d 100644 --- a/demo/webapp/public/js/ddpg-traffic/index.js +++ b/demo/webapp/public/js/ddpg-traffic/index.js @@ -63,8 +63,7 @@ env.load().then(() => { }); env.addEvent("load", () => { - //agent.restore("ddpg-traffic", "model-ddpg-traffic") - agent.restore("four", "model-ddpg-traffic-epoch-120"); + agent.restore("ddpg-traffic", "model-ddpg-traffic-epoch-120"); }); }); diff --git a/demo/webapp/public/js/ddpg/ddpg.js b/demo/webapp/public/js/ddpg/ddpg.js index e8175c4..1f79b99 100644 --- a/demo/webapp/public/js/ddpg/ddpg.js +++ b/demo/webapp/public/js/ddpg/ddpg.js @@ -179,6 +179,7 @@ class DDPG { return erros.mean(); }, true, this.criticWeights); + // For experience Replay this.memory.appendBackWithCost(batch, costs); const loss = criticLoss.buffer().values[0]; diff --git a/demo/webapp/public/js/ddpg/ddpg_agent.js b/demo/webapp/public/js/ddpg/ddpg_agent.js index bee45ee..95f922f 100644 --- a/demo/webapp/public/js/ddpg/ddpg_agent.js +++ b/demo/webapp/public/js/ddpg/ddpg_agent.js @@ -25,7 +25,7 @@ class DDPGAgent { "nbEpochs": config.nbEpochs || 200, "nbEpochsCycle": config.nbEpochsCycle || 10, "nbTrainSteps": config.nbTrainSteps || 110, - "tau": config.tau || 0.01, + "tau": config.tau || 0.008, "initialStddev": config.initialStddev || 0.1, "desiredActionStddev": config.desiredActionStddev || 0.1, "adoptionCoefficient": config.adoptionCoefficient || 1.01, @@ -48,7 +48,7 @@ class DDPGAgent { // Buffer replay // The baseline use 1e6 but this size should be enough for this problem - this.memory = new Memory(this.config.memorySize); + this.memory = new PrioritizedMemory(this.config.memorySize); // Actor and Critic are from js/DDPG/models.js this.actor = new Actor(this.config); this.critic = new Critic(this.config); @@ -75,8 +75,8 @@ class DDPGAgent { /* Restore the weights of the network */ - const critic = await tf.loadModel('http://localhost:3000/public/models/'+folder+'/critic-'+name+'.json'); - const actor = await tf.loadModel("http://localhost:3000/public/models/"+folder+"/actor-"+name+".json"); + const critic = await tf.loadModel('https://metacar-project.com/public/models/'+folder+'/critic-'+name+'.json'); + const actor = await tf.loadModel("https://metacar-project.com/public/models/"+folder+"/actor-"+name+".json"); this.ddpg.critic = copyFromSave(critic, Critic, this.config, this.ddpg.obsInput, this.ddpg.actionInput); this.ddpg.actor = copyFromSave(actor, Actor, this.config, this.ddpg.obsInput, this.ddpg.actionInput); diff --git a/demo/webapp/public/js/ddpg/index.js b/demo/webapp/public/js/ddpg/index.js index 984feae..bbe6fd3 100644 --- a/demo/webapp/public/js/ddpg/index.js +++ b/demo/webapp/public/js/ddpg/index.js @@ -7,10 +7,7 @@ env.setAgentLidar({pts: 5, width: 3, height: 7, pos: -0.5}) // js/DDPG/ddpg.js var agent = new DDPGAgent(env, { - stateSize: 26, - resetEpisode: true, - saveDuringTraining: true, - saveInterval: 10, + stateSize: 26 }); initMetricsContainer("statContainer", ["Reward", "ActorLoss", "CriticLoss", "EpisodeDuration", "NoiseDistance"]); diff --git a/demo/webapp/public/js/ddpg/memory.js b/demo/webapp/public/js/ddpg/memory.js index b4921fb..fc8db0a 100644 --- a/demo/webapp/public/js/ddpg/memory.js +++ b/demo/webapp/public/js/ddpg/memory.js @@ -6,8 +6,24 @@ class Memory { */ constructor(maxlen){ this.maxlen = maxlen; - this.buffer = []; - this.priorBuffer = []; + this.length = 0; + this.start = 0; + + this.obs0List = Array.apply(null, Array(maxlen)).map(Number.prototype.valueOf, 0); + this.obs1List = Array.apply(null, Array(maxlen)).map(Number.prototype.valueOf, 0); + this.rewardsList = Array.apply(null, Array(maxlen)).map(Number.prototype.valueOf, 0); + this.actionsList = Array.apply(null, Array(maxlen)).map(Number.prototype.valueOf, 0); + this.terminals1List = Array.apply(null, Array(maxlen)).map(Number.prototype.valueOf, 0); + } + + /** + * @param idx (number) + */ + getItem(idx){ + if (idx < 0 || idx >= this.length){ + console.error("Memory.getItem: idx not in range."); + } + return this.data[(this.start + idx) % this.maxlen] } /** @@ -16,92 +32,7 @@ class Memory { * @return batch [] */ getBatch(batchSize){ - const batch = { - 'obs0': [], - 'obs1': [], - 'rewards': [], - 'actions': [], - 'terminals': [], - }; - - if (batchSize > this.priorBuffer.length){ - console.warn("The size of the replay buffer is < to the batchSize. Return empty batch."); - return batch; - } - - for (let b=0; b < batchSize/2; b++){ - let id = Math.floor(Math.random() * this.priorBuffer.length); - batch.obs0.push(this.priorBuffer[id].obs0); - batch.obs1.push(this.priorBuffer[id].obs1); - batch.rewards.push(this.priorBuffer[id].reward); - batch.actions.push(this.priorBuffer[id].action); - batch.terminals.push(this.priorBuffer[id].terminal); - } - return batch - } - - _bufferBatch(batchSize){ - const batch = { - 'obs0': [], - 'obs1': [], - 'rewards': [], - 'actions': [], - 'terminals': [], - }; - - for (let b=0; b < batchSize/2; b++){ - let nElem = this.buffer.pop(); - batch.obs0.push(nElem.obs0); - batch.obs1.push(nElem.obs1); - batch.rewards.push(nElem.reward); - batch.actions.push(nElem.action); - batch.terminals.push(nElem.terminal); - } - - for (let b=0; b < batchSize/2; b++){ - let id = Math.floor(Math.random() * this.buffer.length); - batch.obs0.push(this.buffer[id].obs0); - batch.obs1.push(this.buffer[id].obs1); - batch.rewards.push(this.buffer[id].reward); - batch.actions.push(this.buffer[id].action); - batch.terminals.push(this.buffer[id].terminal); - this.buffer.splice(id, 1); - } - - return batch - } - - _addRandomBufferBatch(batchSize, batch){ - for (let b=0; b < batchSize; b++){ - let id = Math.floor(Math.random() * this.buffer.length); - batch.obs0.push(this.buffer[id].obs0); - batch.obs1.push(this.buffer[id].obs1); - batch.rewards.push(this.buffer[id].reward); - batch.actions.push(this.buffer[id].action); - batch.terminals.push(this.buffer[id].terminal); - this.buffer.splice(id, 1); - } - return batch - } - - /** - * Sample a batch - * @param batchSize (number) - * @return batch [] - */ - popBatch(batchSize){ - let originalBatchSize = batchSize; - let priorBufferBatchSize; - let bufferBatchSize; - if (batchSize % 2 != 0){ - console.warn("Batch size should be a even.") - } - if (this.priorBuffer.length < batchSize/2){ - //console.log("get full batch from buffer"); - const batch = this._bufferBatch(batchSize); - console.assert(batch.obs0.length == batchSize); - return batch; - } + const arrLength = this.length; const batch = { 'obs0': [], 'obs1': [], @@ -110,80 +41,19 @@ class Memory { 'terminals': [], }; if (batchSize > this.length){ - console.warn("The size of the replay buffer is < to the batchSize. Return empty batch."); return batch; } - - if (this.buffer.length > 0){ - //console.log("Get half of prior and other from buffer."); - batchSize = batchSize / 2; - } - else{ - //console.log("Get all from priorBuffer"); - } - for (let b=0; b < batchSize; b++){ - let id = Math.floor(Math.random() * this.priorBuffer.length); - batch.obs0.push(this.priorBuffer[id].obs0); - batch.obs1.push(this.priorBuffer[id].obs1); - batch.rewards.push(this.priorBuffer[id].reward); - batch.actions.push(this.priorBuffer[id].action); - batch.terminals.push(this.priorBuffer[id].terminal); - this.priorBuffer.splice(id, 1); + let id = Math.floor(Math.random() * arrLength); + batch.obs0.push(this.obs0List[id]); + batch.obs1.push(this.obs1List[id]); + batch.rewards.push(this.rewardsList[id]); + batch.actions.push(this.actionsList[id]); + batch.terminals.push(this.terminals1List[id]); } - - if (this.buffer.length > 0){ - this._addRandomBufferBatch(batchSize, batch); - } - console.assert(batch.obs0.length == originalBatchSize); return batch } - _insert(element, array) { - if (array.length == 0 || element.cost < array[0].cost || array[0].cost == null){ - array.unshift(element); - return array; - } - array.splice(this._locationOf(element, array) + 1, 0, element); - return array; - } - - _locationOf(element, array, start, end) { - start = start || 0; - end = end || array.length; - - var pivot = parseInt(start + (end - start) / 2, 10); - - if (end-start <= 1 || array[pivot] === element) return pivot; - - if (array[pivot].cost != null && array[pivot].cost < element.cost) { - return this._locationOf(element, array, pivot, end); - } else { - return this._locationOf(element, array, start, pivot); - } - } - - /** - * @param batch (Object) from getBatch() - * @param cost (number) Cost associated with each row of the batch - */ - appendBackWithCost(batch, costs){ - for (let b=0; b < batch.obs0.length; b++){ - if (this.buffer.length == this.maxlen){ - this.buffer.shift(); - } - this._insert({ - obs0: batch.obs0[b], - action: batch.actions[b], - reward: batch.rewards[b], - obs1: batch.obs1[b], - terminal: batch.terminals[b], - cost: costs[b] - }, this.buffer); - } - console.assert(this.buffer.length <= this.maxlen); - } - /** * @param obs0 [] * @param action (number) @@ -191,50 +61,23 @@ class Memory { * @param obs1 [] * @param terminal1 (boolean) */ - append(obs0, action, reward, obs1, terminal){ - if (this.priorBuffer.length == this.maxlen){ - this.priorBuffer.shift(); + append(obs0, action, reward, obs1, terminal1){ + if (this.length < this.maxlen){ + this.length += 1; } - this.priorBuffer.push({ - obs0: obs0, - action: action, - reward: reward, - obs1: obs1, - terminal: terminal, - cost: null - }); - console.assert(this.priorBuffer.length <= this.maxlen); + else if (this.length == this.maxlen) { + //this.obs0List[(this.start + this.length - 1) % this.maxlen].dispose(); + //this.obs1List[(this.start + this.length - 1) % this.maxlen].dispose(); + //this.actionsList[(this.start + this.length - 1) % this.maxlen].dispose(); + this.start = (this.start + 1) % this.maxlen; + } + else { + console.error("Memory.append: This should never be printed"); + } + this.obs0List[(this.start + this.length - 1) % this.maxlen] = obs0; + this.obs1List[(this.start + this.length - 1) % this.maxlen] = obs1; + this.rewardsList[(this.start + this.length - 1) % this.maxlen] = reward; + this.actionsList[(this.start + this.length - 1) % this.maxlen] = action; + this.terminals1List[(this.start + this.length - 1) % this.maxlen] = terminal1; } -} -/* -var mem = new Memory(20000); -Math.seedrandom(0); -console.assert(mem.length == 0); - -var array = []; -for (let i=1; i < 40000; i++){ - mem.append("obs0-"+i, "action-"+i, "reward-"+i, "obs1-"+i, "terminal-"+i); -} - -console.assert(mem.length == 20000); -console.assert(mem.list[0].obs0 == "obs0-20000"); -console.assert(mem.list[19999].obs0 == "obs0-39999"); - -let batch = mem.getBatch(32); - -console.assert(batch.obs0.length == 32); -console.assert(mem.length == 20000 - 32); - -let costs = []; -for (i=31; i >= 0; i--){ - costs.push(i); -} -mem.appendBackWithCost(batch, costs); - -console.log(mem.list); - -/* -for (let i=1; i < 64; i++){ - mem.append("obs0-"+i, "action-"+i, "reward-"+i, "obs1-"+i, "terminal-"+i); -} -*/ \ No newline at end of file +} \ No newline at end of file diff --git a/demo/webapp/public/js/ddpg/prioritized_memory.js b/demo/webapp/public/js/ddpg/prioritized_memory.js new file mode 100644 index 0000000..5a7f74b --- /dev/null +++ b/demo/webapp/public/js/ddpg/prioritized_memory.js @@ -0,0 +1,240 @@ + +class PrioritizedMemory { + + /** + * @param maxlen (number) Buffer limit + */ + constructor(maxlen){ + this.maxlen = maxlen; + this.buffer = []; + this.priorBuffer = []; + } + + /** + * Sample a batch + * @param batchSize (number) + * @return batch [] + */ + getBatch(batchSize){ + const batch = { + 'obs0': [], + 'obs1': [], + 'rewards': [], + 'actions': [], + 'terminals': [], + }; + + if (batchSize > this.priorBuffer.length){ + console.warn("The size of the replay buffer is < to the batchSize. Return empty batch."); + return batch; + } + + for (let b=0; b < batchSize/2; b++){ + let id = Math.floor(Math.random() * this.priorBuffer.length); + batch.obs0.push(this.priorBuffer[id].obs0); + batch.obs1.push(this.priorBuffer[id].obs1); + batch.rewards.push(this.priorBuffer[id].reward); + batch.actions.push(this.priorBuffer[id].action); + batch.terminals.push(this.priorBuffer[id].terminal); + } + return batch + } + + _bufferBatch(batchSize){ + const batch = { + 'obs0': [], + 'obs1': [], + 'rewards': [], + 'actions': [], + 'terminals': [], + }; + + for (let b=0; b < batchSize/2; b++){ + let nElem = this.buffer.pop(); + batch.obs0.push(nElem.obs0); + batch.obs1.push(nElem.obs1); + batch.rewards.push(nElem.reward); + batch.actions.push(nElem.action); + batch.terminals.push(nElem.terminal); + } + + for (let b=0; b < batchSize/2; b++){ + let id = Math.floor(Math.random() * this.buffer.length); + batch.obs0.push(this.buffer[id].obs0); + batch.obs1.push(this.buffer[id].obs1); + batch.rewards.push(this.buffer[id].reward); + batch.actions.push(this.buffer[id].action); + batch.terminals.push(this.buffer[id].terminal); + this.buffer.splice(id, 1); + } + + return batch + } + + _addRandomBufferBatch(batchSize, batch){ + for (let b=0; b < batchSize; b++){ + let id = Math.floor(Math.random() * this.buffer.length); + batch.obs0.push(this.buffer[id].obs0); + batch.obs1.push(this.buffer[id].obs1); + batch.rewards.push(this.buffer[id].reward); + batch.actions.push(this.buffer[id].action); + batch.terminals.push(this.buffer[id].terminal); + this.buffer.splice(id, 1); + } + return batch + } + + /** + * Sample a batch + * @param batchSize (number) + * @return batch [] + */ + popBatch(batchSize){ + let originalBatchSize = batchSize; + let priorBufferBatchSize; + let bufferBatchSize; + if (batchSize % 2 != 0){ + console.warn("Batch size should be a even.") + } + if (this.priorBuffer.length < batchSize/2){ + //console.log("get full batch from buffer"); + const batch = this._bufferBatch(batchSize); + console.assert(batch.obs0.length == batchSize); + return batch; + } + const batch = { + 'obs0': [], + 'obs1': [], + 'rewards': [], + 'actions': [], + 'terminals': [], + }; + if (batchSize > this.length){ + console.warn("The size of the replay buffer is < to the batchSize. Return empty batch."); + return batch; + } + + if (this.buffer.length > 0){ + //console.log("Get half of prior and other from buffer."); + batchSize = batchSize / 2; + } + else{ + //console.log("Get all from priorBuffer"); + } + + for (let b=0; b < batchSize; b++){ + let id = Math.floor(Math.random() * this.priorBuffer.length); + batch.obs0.push(this.priorBuffer[id].obs0); + batch.obs1.push(this.priorBuffer[id].obs1); + batch.rewards.push(this.priorBuffer[id].reward); + batch.actions.push(this.priorBuffer[id].action); + batch.terminals.push(this.priorBuffer[id].terminal); + this.priorBuffer.splice(id, 1); + } + + if (this.buffer.length > 0){ + this._addRandomBufferBatch(batchSize, batch); + } + console.assert(batch.obs0.length == originalBatchSize); + return batch + } + + _insert(element, array) { + if (array.length == 0 || element.cost < array[0].cost || array[0].cost == null){ + array.unshift(element); + return array; + } + array.splice(this._locationOf(element, array) + 1, 0, element); + return array; + } + + _locationOf(element, array, start, end) { + start = start || 0; + end = end || array.length; + + var pivot = parseInt(start + (end - start) / 2, 10); + + if (end-start <= 1 || array[pivot] === element) return pivot; + + if (array[pivot].cost != null && array[pivot].cost < element.cost) { + return this._locationOf(element, array, pivot, end); + } else { + return this._locationOf(element, array, start, pivot); + } + } + + /** + * @param batch (Object) from getBatch() + * @param cost (number) Cost associated with each row of the batch + */ + appendBackWithCost(batch, costs){ + for (let b=0; b < batch.obs0.length; b++){ + if (this.buffer.length == this.maxlen){ + this.buffer.shift(); + } + this._insert({ + obs0: batch.obs0[b], + action: batch.actions[b], + reward: batch.rewards[b], + obs1: batch.obs1[b], + terminal: batch.terminals[b], + cost: costs[b] + }, this.buffer); + } + console.assert(this.buffer.length <= this.maxlen); + } + + /** + * @param obs0 [] + * @param action (number) + * @param reward (number) + * @param obs1 [] + * @param terminal1 (boolean) + */ + append(obs0, action, reward, obs1, terminal){ + if (this.priorBuffer.length == this.maxlen){ + this.priorBuffer.shift(); + } + this.priorBuffer.push({ + obs0: obs0, + action: action, + reward: reward, + obs1: obs1, + terminal: terminal, + cost: null + }); + console.assert(this.priorBuffer.length <= this.maxlen); + } +} +/* +var mem = new Memory(20000); +Math.seedrandom(0); +console.assert(mem.length == 0); + +var array = []; +for (let i=1; i < 40000; i++){ + mem.append("obs0-"+i, "action-"+i, "reward-"+i, "obs1-"+i, "terminal-"+i); +} + +console.assert(mem.length == 20000); +console.assert(mem.list[0].obs0 == "obs0-20000"); +console.assert(mem.list[19999].obs0 == "obs0-39999"); + +let batch = mem.getBatch(32); + +console.assert(batch.obs0.length == 32); +console.assert(mem.length == 20000 - 32); + +let costs = []; +for (i=31; i >= 0; i--){ + costs.push(i); +} +mem.appendBackWithCost(batch, costs); + +console.log(mem.list); + +/* +for (let i=1; i < 64; i++){ + mem.append("obs0-"+i, "action-"+i, "reward-"+i, "obs1-"+i, "terminal-"+i); +} +*/ \ No newline at end of file diff --git a/demo/webapp/public/models/ddpg-traffic/actor-model-ddpg-traffic-epoch-120.json b/demo/webapp/public/models/ddpg-traffic/actor-model-ddpg-traffic-epoch-120.json new file mode 100644 index 0000000..728c09d --- /dev/null +++ b/demo/webapp/public/models/ddpg-traffic/actor-model-ddpg-traffic-epoch-120.json @@ -0,0 +1 @@ +{"modelTopology":{"class_name":"Model","config":{"name":"model7","layers":[{"name":"input1","class_name":"InputLayer","config":{"batch_input_shape":[null,50],"dtype":"float32","sparse":false,"name":"input1"},"inbound_nodes":[]},{"name":"dense_Dense22","class_name":"Dense","config":{"units":128,"activation":"relu","use_bias":true,"kernel_initializer":{"class_name":"VarianceScaling","config":{"scale":1,"mode":"fan_avg","distribution":"uniform","seed":0}},"bias_initializer":{"class_name":"Zeros","config":{}},"kernel_regularizer":null,"bias_regularizer":null,"activity_regularizer":null,"kernel_constraint":null,"bias_constraint":null,"name":"dense_Dense22","trainable":true},"inbound_nodes":[[["input1",0,0,{}]]]},{"name":"dense_Dense23","class_name":"Dense","config":{"units":64,"activation":"relu","use_bias":true,"kernel_initializer":{"class_name":"VarianceScaling","config":{"scale":1,"mode":"fan_avg","distribution":"uniform","seed":0}},"bias_initializer":{"class_name":"Zeros","config":{}},"kernel_regularizer":null,"bias_regularizer":null,"activity_regularizer":null,"kernel_constraint":null,"bias_constraint":null,"name":"dense_Dense23","trainable":true},"inbound_nodes":[[["dense_Dense22",0,0,{}]]]},{"name":"dense_Dense24","class_name":"Dense","config":{"units":2,"activation":"tanh","use_bias":true,"kernel_initializer":{"class_name":"RandomUniform","config":{"minval":0.003,"maxval":0.003,"seed":0}},"bias_initializer":{"class_name":"Zeros","config":{}},"kernel_regularizer":null,"bias_regularizer":null,"activity_regularizer":null,"kernel_constraint":null,"bias_constraint":null,"name":"dense_Dense24","trainable":true},"inbound_nodes":[[["dense_Dense23",0,0,{}]]]}],"input_layers":[["input1",0,0]],"output_layers":[["dense_Dense24",0,0]]},"keras_version":"tfjs-layers 0.6.6","backend":"tensor_flow.js"},"weightsManifest":[{"paths":["./actor-model-ddpg-traffic-epoch-120.weights.bin"],"weights":[{"name":"dense_Dense22/kernel","shape":[50,128],"dtype":"float32"},{"name":"dense_Dense22/bias","shape":[128],"dtype":"float32"},{"name":"dense_Dense23/kernel","shape":[128,64],"dtype":"float32"},{"name":"dense_Dense23/bias","shape":[64],"dtype":"float32"},{"name":"dense_Dense24/kernel","shape":[64,2],"dtype":"float32"},{"name":"dense_Dense24/bias","shape":[2],"dtype":"float32"}]}]} \ No newline at end of file diff --git a/demo/webapp/public/models/ddpg-traffic/actor-model-ddpg-traffic-epoch-120.weights.bin b/demo/webapp/public/models/ddpg-traffic/actor-model-ddpg-traffic-epoch-120.weights.bin new file mode 100644 index 0000000..224b9a3 Binary files /dev/null and b/demo/webapp/public/models/ddpg-traffic/actor-model-ddpg-traffic-epoch-120.weights.bin differ diff --git a/demo/webapp/public/models/ddpg-traffic/actor-model-ddpg-traffic.json b/demo/webapp/public/models/ddpg-traffic/actor-model-ddpg-traffic.json deleted file mode 100644 index e44d5d4..0000000 --- a/demo/webapp/public/models/ddpg-traffic/actor-model-ddpg-traffic.json +++ /dev/null @@ -1 +0,0 @@ -{"modelTopology":{"class_name":"Model","config":{"name":"model1","layers":[{"name":"input1","class_name":"InputLayer","config":{"batch_input_shape":[null,50],"dtype":"float32","sparse":false,"name":"input1"},"inbound_nodes":[]},{"name":"dense_Dense1","class_name":"Dense","config":{"units":128,"activation":"relu","use_bias":true,"kernel_initializer":{"class_name":"VarianceScaling","config":{"scale":1,"mode":"fan_avg","distribution":"uniform","seed":0}},"bias_initializer":{"class_name":"Zeros","config":{}},"kernel_regularizer":null,"bias_regularizer":null,"activity_regularizer":null,"kernel_constraint":null,"bias_constraint":null,"name":"dense_Dense1","trainable":true},"inbound_nodes":[[["input1",0,0,{}]]]},{"name":"dense_Dense2","class_name":"Dense","config":{"units":64,"activation":"relu","use_bias":true,"kernel_initializer":{"class_name":"VarianceScaling","config":{"scale":1,"mode":"fan_avg","distribution":"uniform","seed":0}},"bias_initializer":{"class_name":"Zeros","config":{}},"kernel_regularizer":null,"bias_regularizer":null,"activity_regularizer":null,"kernel_constraint":null,"bias_constraint":null,"name":"dense_Dense2","trainable":true},"inbound_nodes":[[["dense_Dense1",0,0,{}]]]},{"name":"dense_Dense3","class_name":"Dense","config":{"units":2,"activation":"tanh","use_bias":true,"kernel_initializer":{"class_name":"RandomUniform","config":{"minval":0.003,"maxval":0.003,"seed":0}},"bias_initializer":{"class_name":"Zeros","config":{}},"kernel_regularizer":null,"bias_regularizer":null,"activity_regularizer":null,"kernel_constraint":null,"bias_constraint":null,"name":"dense_Dense3","trainable":true},"inbound_nodes":[[["dense_Dense2",0,0,{}]]]}],"input_layers":[["input1",0,0]],"output_layers":[["dense_Dense3",0,0]]},"keras_version":"tfjs-layers 0.6.6","backend":"tensor_flow.js"},"weightsManifest":[{"paths":["./actor-model-ddpg-traffic.weights.bin"],"weights":[{"name":"dense_Dense1/kernel","shape":[50,128],"dtype":"float32"},{"name":"dense_Dense1/bias","shape":[128],"dtype":"float32"},{"name":"dense_Dense2/kernel","shape":[128,64],"dtype":"float32"},{"name":"dense_Dense2/bias","shape":[64],"dtype":"float32"},{"name":"dense_Dense3/kernel","shape":[64,2],"dtype":"float32"},{"name":"dense_Dense3/bias","shape":[2],"dtype":"float32"}]}]} \ No newline at end of file diff --git a/demo/webapp/public/models/ddpg-traffic/actor-model-ddpg-traffic.weights.bin b/demo/webapp/public/models/ddpg-traffic/actor-model-ddpg-traffic.weights.bin deleted file mode 100644 index 70a6974..0000000 Binary files a/demo/webapp/public/models/ddpg-traffic/actor-model-ddpg-traffic.weights.bin and /dev/null differ diff --git a/demo/webapp/public/models/ddpg-traffic/critic-model-ddpg-traffic-epoch-120.json b/demo/webapp/public/models/ddpg-traffic/critic-model-ddpg-traffic-epoch-120.json new file mode 100644 index 0000000..2f54e16 --- /dev/null +++ b/demo/webapp/public/models/ddpg-traffic/critic-model-ddpg-traffic-epoch-120.json @@ -0,0 +1 @@ +{"modelTopology":{"class_name":"Model","config":{"name":"model6","layers":[{"name":"input2","class_name":"InputLayer","config":{"batch_input_shape":[null,2],"dtype":"float32","sparse":false,"name":"input2"},"inbound_nodes":[]},{"name":"input1","class_name":"InputLayer","config":{"batch_input_shape":[null,50],"dtype":"float32","sparse":false,"name":"input1"},"inbound_nodes":[]},{"name":"dense_Dense19","class_name":"Dense","config":{"units":128,"activation":"linear","use_bias":true,"kernel_initializer":{"class_name":"VarianceScaling","config":{"scale":1,"mode":"fan_avg","distribution":"uniform","seed":0}},"bias_initializer":{"class_name":"Zeros","config":{}},"kernel_regularizer":null,"bias_regularizer":null,"activity_regularizer":null,"kernel_constraint":null,"bias_constraint":null,"name":"dense_Dense19","trainable":true},"inbound_nodes":[[["input2",0,0,{}]]]},{"name":"dense_Dense18","class_name":"Dense","config":{"units":128,"activation":"linear","use_bias":true,"kernel_initializer":{"class_name":"VarianceScaling","config":{"scale":1,"mode":"fan_avg","distribution":"uniform","seed":0}},"bias_initializer":{"class_name":"Zeros","config":{}},"kernel_regularizer":null,"bias_regularizer":null,"activity_regularizer":null,"kernel_constraint":null,"bias_constraint":null,"name":"dense_Dense18","trainable":true},"inbound_nodes":[[["input1",0,0,{}]]]},{"name":"add_Add3","class_name":"Add","config":{"name":"add_Add3","trainable":true},"inbound_nodes":[[["dense_Dense19",0,0,{}],["dense_Dense18",0,0,{}]]]},{"name":"dense_Dense20","class_name":"Dense","config":{"units":64,"activation":"relu","use_bias":true,"kernel_initializer":{"class_name":"VarianceScaling","config":{"scale":1,"mode":"fan_avg","distribution":"uniform","seed":0}},"bias_initializer":{"class_name":"Zeros","config":{}},"kernel_regularizer":null,"bias_regularizer":null,"activity_regularizer":null,"kernel_constraint":null,"bias_constraint":null,"name":"dense_Dense20","trainable":true},"inbound_nodes":[[["add_Add3",0,0,{}]]]},{"name":"dense_Dense21","class_name":"Dense","config":{"units":1,"activation":"linear","use_bias":true,"kernel_initializer":{"class_name":"RandomUniform","config":{"minval":0.003,"maxval":0.003,"seed":0}},"bias_initializer":{"class_name":"Zeros","config":{}},"kernel_regularizer":null,"bias_regularizer":null,"activity_regularizer":null,"kernel_constraint":null,"bias_constraint":null,"name":"dense_Dense21","trainable":true},"inbound_nodes":[[["dense_Dense20",0,0,{}]]]}],"input_layers":[["input1",0,0],["input2",0,0]],"output_layers":[["dense_Dense21",0,0]]},"keras_version":"tfjs-layers 0.6.6","backend":"tensor_flow.js"},"weightsManifest":[{"paths":["./critic-model-ddpg-traffic-epoch-120.weights.bin"],"weights":[{"name":"dense_Dense19/kernel","shape":[2,128],"dtype":"float32"},{"name":"dense_Dense19/bias","shape":[128],"dtype":"float32"},{"name":"dense_Dense18/kernel","shape":[50,128],"dtype":"float32"},{"name":"dense_Dense18/bias","shape":[128],"dtype":"float32"},{"name":"dense_Dense20/kernel","shape":[128,64],"dtype":"float32"},{"name":"dense_Dense20/bias","shape":[64],"dtype":"float32"},{"name":"dense_Dense21/kernel","shape":[64,1],"dtype":"float32"},{"name":"dense_Dense21/bias","shape":[1],"dtype":"float32"}]}]} \ No newline at end of file diff --git a/demo/webapp/public/models/ddpg-traffic/critic-model-ddpg-traffic-epoch-120.weights.bin b/demo/webapp/public/models/ddpg-traffic/critic-model-ddpg-traffic-epoch-120.weights.bin new file mode 100644 index 0000000..461b225 Binary files /dev/null and b/demo/webapp/public/models/ddpg-traffic/critic-model-ddpg-traffic-epoch-120.weights.bin differ diff --git a/demo/webapp/public/models/ddpg-traffic/critic-model-ddpg-traffic.json b/demo/webapp/public/models/ddpg-traffic/critic-model-ddpg-traffic.json deleted file mode 100644 index c649d80..0000000 --- a/demo/webapp/public/models/ddpg-traffic/critic-model-ddpg-traffic.json +++ /dev/null @@ -1 +0,0 @@ -{"modelTopology":{"class_name":"Model","config":{"name":"model2","layers":[{"name":"input2","class_name":"InputLayer","config":{"batch_input_shape":[null,2],"dtype":"float32","sparse":false,"name":"input2"},"inbound_nodes":[]},{"name":"input1","class_name":"InputLayer","config":{"batch_input_shape":[null,50],"dtype":"float32","sparse":false,"name":"input1"},"inbound_nodes":[]},{"name":"dense_Dense5","class_name":"Dense","config":{"units":128,"activation":"linear","use_bias":true,"kernel_initializer":{"class_name":"VarianceScaling","config":{"scale":1,"mode":"fan_avg","distribution":"uniform","seed":0}},"bias_initializer":{"class_name":"Zeros","config":{}},"kernel_regularizer":null,"bias_regularizer":null,"activity_regularizer":null,"kernel_constraint":null,"bias_constraint":null,"name":"dense_Dense5","trainable":true},"inbound_nodes":[[["input2",0,0,{}]]]},{"name":"dense_Dense4","class_name":"Dense","config":{"units":128,"activation":"linear","use_bias":true,"kernel_initializer":{"class_name":"VarianceScaling","config":{"scale":1,"mode":"fan_avg","distribution":"uniform","seed":0}},"bias_initializer":{"class_name":"Zeros","config":{}},"kernel_regularizer":null,"bias_regularizer":null,"activity_regularizer":null,"kernel_constraint":null,"bias_constraint":null,"name":"dense_Dense4","trainable":true},"inbound_nodes":[[["input1",0,0,{}]]]},{"name":"add_Add1","class_name":"Add","config":{"name":"add_Add1","trainable":true},"inbound_nodes":[[["dense_Dense5",0,0,{}],["dense_Dense4",0,0,{}]]]},{"name":"dense_Dense6","class_name":"Dense","config":{"units":64,"activation":"relu","use_bias":true,"kernel_initializer":{"class_name":"VarianceScaling","config":{"scale":1,"mode":"fan_avg","distribution":"uniform","seed":0}},"bias_initializer":{"class_name":"Zeros","config":{}},"kernel_regularizer":null,"bias_regularizer":null,"activity_regularizer":null,"kernel_constraint":null,"bias_constraint":null,"name":"dense_Dense6","trainable":true},"inbound_nodes":[[["add_Add1",0,0,{}]]]},{"name":"dense_Dense7","class_name":"Dense","config":{"units":1,"activation":"linear","use_bias":true,"kernel_initializer":{"class_name":"RandomUniform","config":{"minval":0.003,"maxval":0.003,"seed":0}},"bias_initializer":{"class_name":"Zeros","config":{}},"kernel_regularizer":null,"bias_regularizer":null,"activity_regularizer":null,"kernel_constraint":null,"bias_constraint":null,"name":"dense_Dense7","trainable":true},"inbound_nodes":[[["dense_Dense6",0,0,{}]]]}],"input_layers":[["input1",0,0],["input2",0,0]],"output_layers":[["dense_Dense7",0,0]]},"keras_version":"tfjs-layers 0.6.6","backend":"tensor_flow.js"},"weightsManifest":[{"paths":["./critic-model-ddpg-traffic.weights.bin"],"weights":[{"name":"dense_Dense5/kernel","shape":[2,128],"dtype":"float32"},{"name":"dense_Dense5/bias","shape":[128],"dtype":"float32"},{"name":"dense_Dense4/kernel","shape":[50,128],"dtype":"float32"},{"name":"dense_Dense4/bias","shape":[128],"dtype":"float32"},{"name":"dense_Dense6/kernel","shape":[128,64],"dtype":"float32"},{"name":"dense_Dense6/bias","shape":[64],"dtype":"float32"},{"name":"dense_Dense7/kernel","shape":[64,1],"dtype":"float32"},{"name":"dense_Dense7/bias","shape":[1],"dtype":"float32"}]}]} \ No newline at end of file diff --git a/demo/webapp/public/models/ddpg-traffic/critic-model-ddpg-traffic.weights.bin b/demo/webapp/public/models/ddpg-traffic/critic-model-ddpg-traffic.weights.bin deleted file mode 100644 index 876b5fd..0000000 Binary files a/demo/webapp/public/models/ddpg-traffic/critic-model-ddpg-traffic.weights.bin and /dev/null differ diff --git a/dist/metacar.min.js b/dist/metacar.min.js index c8f1cf2..d4e7f7c 100644 --- a/dist/metacar.min.js +++ b/dist/metacar.min.js @@ -1 +1 @@ -window.metacar=function(t){var e={};function i(o){if(e[o])return e[o].exports;var n=e[o]={i:o,l:!1,exports:{}};return t[o].call(n.exports,n,n.exports,i),n.l=!0,n.exports}return i.m=t,i.c=e,i.d=function(t,e,o){i.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:o})},i.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},i.t=function(t,e){if(1&e&&(t=i(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var o=Object.create(null);if(i.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var n in t)i.d(o,n,function(e){return t[e]}.bind(null,n));return o},i.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return i.d(e,"a",e),e},i.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},i.p="",i(i.s=1)}([function(t,e){t.exports=window.PIXI},function(t,e,i){"use strict";i.r(e);var o=i(0),n=(o.Application,o.loader),a=(o.loader.resources,o.Sprite),r=o.Graphics,s=o.Container,h="https://metacar-project.com/",c=h+"public/textures/textures.json",l=h+"public/images/",d=60,p={DEFAULT:"car.png",AGENT:"car_agent.png"},u={ROAD:-1,DEFAULT:0,CAR:1,AGENT:1},v={ROADS:{"↕":{image:"road_u.png",orientation:1.5},"↔":{image:"road_r.png",orientation:0},"↱":{image:"road_rotate_ld.png",orientation:1.25},"↰":{image:"road_rotate_ul.png",orientation:.75},"↲":{image:"road_rotate_rl.png",orientation:.25},"↳":{image:"road_rotate_dl.png",orientation:1.75},"↟":{image:"road_up.png",orientation:1.5},"↠":{image:"road_rp.png",orientation:0},"↤":{image:"road_r2.png",orientation:0},"↦":{image:"road_l2.png",orientation:1},"↥":{image:"road_d2.png",orientation:.5},"↧":{image:"road_u2.png",orientation:1.5}},house:{image:"house.png"},house2:{image:"house2.png"},house3:{image:"house3.png"},bench:{image:"bench.png"},tree:{image:"tree.png"}},y={level:{fullCity:{cars:[{mx:2,my:5,line:0,auto:!0},{mx:3,my:4,line:0,auto:!0},{mx:3,my:4,line:1,auto:!0},{mx:6,my:3,line:0,auto:!0},{mx:6,my:3,line:1,auto:!0},{mx:8,my:2,line:0,auto:!0},{mx:8,my:2,line:1,auto:!0},{mx:2,my:7,line:0,auto:!0}],house:[{x:9,y:409},{x:146,y:162},{x:439,y:39}],house2:[{x:425,y:192},{x:431,y:354}],house3:[{x:426,y:281},{x:51,y:157},{x:553,y:34}],bench:[{x:259,y:206},{x:8,y:205}],tree:[{x:1,y:303},{x:54,y:357},{x:215,y:325},{x:230,y:400},{x:282,y:368},{x:182,y:433},{x:285,y:309},{x:576,y:233},{x:581,y:300},{x:519,y:388},{x:582,y:391},{x:256,y:69},{x:151,y:43},{x:77,y:77},{x:26,y:24},{x:189,y:98}],map:[[0,0,0,0,0,0,"↕",0,0,0,0],[0,0,0,0,0,0,"↟",0,0,0,0],[0,0,0,0,0,0,"↦","↔","↔","↠","↔"],[0,0,0,0,0,0,"↕",0,0,0,0],["↔","↔","↧","↔","↠","↔","↤",0,0,0,0],[0,0,"↕",0,0,0,"↕",0,0,0,0],[0,0,"↟",0,0,0,"↟",0,0,0,0],[0,0,"↕",0,0,0,"↕",0,0,0,0]]},level1:{cars:[{mx:5,my:3,line:0,auto:!0},{mx:5,my:3,line:1,auto:!0},{mx:5,my:1,line:0,auto:!0}],tree:[{x:120,y:50},{x:86.5,y:85},{x:94.5,y:135},{x:30.5,y:128},{x:72.5,y:190},{x:23.5,y:232},{x:110.5,y:242},{x:43.5,y:33},{x:18.5,y:178},{x:305.5,y:125},{x:360.5,y:122}],asset:[{x:252.5,y:142},{x:86.5,y:85},{x:94.5,y:135},{x:30.5,y:128},{x:252.5,y:26},{x:195.5,y:0},{x:307.5,y:1},{x:72.5,y:190},{x:23.5,y:232},{x:110.5,y:242},{x:43.5,y:33},{x:35.5,y:185},{x:18.5,y:178},{x:289.5,y:126},{x:337.5,y:126},{x:305.5,y:125},{x:360.5,y:122},{x:492.5,y:56},{x:503.5,y:136},{x:504.5,y:58}],bench:[{x:252.5,y:142},{x:252.5,y:26}],road:[{x:240,y:60},{x:240,y:60},{x:480,y:0},{x:480,y:60},{x:480,y:120},{x:480,y:180},{x:480,y:240},{x:300,y:180},{x:300,y:180},{x:360,y:180},{x:420,y:180},{x:420,y:120},{x:420,y:60},{x:360,y:60},{x:300,y:60},{x:420,y:120}],house3:[{x:503.5,y:136}],house:[{x:504.5,y:58}],map:[[0,0,0,0,0,0,0,0,0,0],[0,0,0,"↱","↠","↔","↔","↰",0,0],[0,0,0,"↕",0,0,0,"↟",0,0],[0,0,0,"↳","↔","↔","↔","↲",0,0],[0,0,0,0,0,0,0,0,0,0]],agent:{mx:4,my:3,line:0,motion:{type:"BasicMotionEngine",options:{rotationStep:.5,actions:["UP","LEFT","RIGHT"]}}}},level0:{cars:[],house:[{x:263,y:108},{x:112,y:258}],house3:[{x:262,y:16}],bench:[{x:135,y:34}],tree:[{x:124,y:134},{x:7,y:11},{x:69,y:10},{x:19,y:238},{x:5,y:133},{x:260,y:211}],map:[[0,0,0,0,0],[0,"↱","↔","↰",0],[0,"↕",0,"↕",0],[0,"↳","↠","↲",0],[0,0,0,0,0]],agent:{mx:2,my:1,line:1,motion:{type:"BasicMotionEngine",options:{rotationStep:.5,actions:["UP","LEFT","RIGHT","DOWN","WAIT"]}}}},level2:{cars:[{mx:7,my:7,line:0},{mx:9,my:6,line:0},{mx:8,my:6,line:1},{mx:10,my:6,line:1}],house:[{x:68,y:317}],house2:[{x:624,y:84},{x:-15,y:144}],house3:[{x:618,y:231},{x:13,y:441}],bench:[{x:318,y:216},{x:422,y:36}],tree:[{x:151,y:144},{x:229,y:175},{x:326,y:132},{x:413,y:167},{x:487,y:157},{x:445,y:123},{x:511,y:429},{x:584,y:427},{x:80,y:391},{x:219,y:409},{x:207,y:326},{x:156,y:426},{x:162,y:13}],map:[[0,0,0,0,0,0,0,0,0,0,0],[0,"↱","↔","↔","↔","↔","↔","↔","↔","↰",0],[0,"↕",0,0,0,0,0,0,0,"↕",0],[0,"↕",0,0,0,0,0,0,0,"↕",0],[0,"↳","↔","↔","↔","↧","↔","↔","↔","↲",0],[0,0,0,0,0,"↕",0,0,0,0,0],[0,0,0,0,0,"↕",0,"↱","↔","↔","↔"],[0,0,0,0,0,"↕",0,"↕",0,0,0]],agent:{mx:2,my:4,line:0,motion:{type:"BasicMotionEngine",options:{rotationStep:.5,actions:["UP","LEFT","RIGHT","DOWN","WAIT"]}}}}}};function m(t,e){var i=new XMLHttpRequest;i.overrideMimeType("application/json"),i.open("GET",t,!0),i.onreadystatechange=function(){4==i.readyState&&200==i.status&&e(JSON.parse(i.responseText))},i.send(null)}function f(t,e){var i=t.split("//");i=i[1].split("/"),e(y[i[0]][i[1]])}function g(t,e){({"embedded:":f,"http:":m,"https:":m})[t.split("//")[0]](t,e)}function x(t,e){var i=document.createElement("a");document.body.appendChild(i);var o=new Blob([t],{type:"application/octet-binary"}),n=window.URL.createObjectURL(o);i.href=n,i.download=e,i.click(),window.URL.revokeObjectURL(n),i.href=""}function _(t){var e={};return e.code=t,e.isDown=!1,e.isUp=!0,e.press=void 0,e.release=void 0,e.downHandler=function(t){t.keyCode===e.code&&(e.isUp&&e.press&&e.press(),e.isDown=!0,e.isUp=!1),t.preventDefault()},e.upHandler=function(t){t.keyCode===e.code&&(e.isDown&&e.release&&e.release(),e.isDown=!1,e.isUp=!0),t.preventDefault()},window.addEventListener("keydown",e.downHandler.bind(e),!1),window.addEventListener("keyup",e.upHandler.bind(e),!1),e}var I,C=0,w=function(){function t(t,e,i,o){void 0===o&&(o={});var n=this;this.level=t,o.image=o.image||p.DEFAULT,o.lidar=o.lidar||!1,o.lidarInfo=o.lidarInfo||{pts:5,width:4,height:5,pos:0},this.core=new a(i[o.image]),this.info=e,this.core.scale.x=.8,this.core.scale.y=.8,this.core.anchor.x=.5,this.core.anchor.y=.5,this.core.road=void 0,this.core.carId=C,C+=1,this.core.agent=!1,this.core.obstacle=!0,this.core.mapId=u.CAR,o.lidar&&(this.createLidar(o.lidarInfo),this.core.lidar=this.lidar),o.motionEngine?(this.motion=o.motionEngine,this.motion.setUp(this.core,this.lidar),this.core.rotationStep=.5):this.core.rotationStep=.5,this.core.mx=this.info.mx,this.core.my=this.info.my,this.core.x=this.info.mx*d,this.core.y=this.info.my*d,this.core.checkAndsetNewRoad=function(t){return n.checkAndsetNewRoad(t)};var r=this.level.getRoad(this.core.my,this.core.mx);this.core.line=0,r&&r.setCarPosition(this.core,this.info.line)}return t.prototype.checkAndsetNewRoad=function(t){var e=this.core.road;t||(t=this.level.getRoad(this.core.my,this.core.mx)),t!=e&&(e&&e.cars.splice(e.cars.indexOf(this.core.carId),1),this.core.road=t,this.core.haveTurned=!1,this.core.optionalTurn=!1,this.turnedRandom=void 0,t&&t.cars.push(this.core.carId))},t.prototype.reset=function(){this.core.mx=this.info.mx,this.core.my=this.info.my;var t=this.level.getRoad(this.core.my,this.core.mx);t&&t.setCarPosition(this.core,this.info.line,!0)},t.prototype.getState=function(t){if(void 0===t&&(t=!1),t){var e=[];return this.motion.state.map(function(t){e=e.concat(t)}),e}return this.motion.state.map(function(t){return t.slice()})},t.prototype.step=function(t,e){if(void 0===e&&(e=null),null==e)var i=this.motion.step(t),o=i.agentCollisions,n=i.onRoad;else{var a=this.motion.actionStep(t,e);o=a.agentCollisions,n=a.onRoad}return{agentCollisions:o,onRoad:n}},t.prototype.createLidar=function(t){t.pts=t.pts||5,t.width=t.width||4,t.height=t.height||5,t.pos=t.pos||0,this.lidar=new s,this.lidar.lidarPts=[],this.lidar.collisionPts=[];var e=new r;e.pt=!1,e.alpha=.3,e.beginFill(5329233),e.drawRect(0,0,this.core.width*t.width,this.core.height*t.height),e.endFill();var i=new r;i.pt=!1,i.alpha=0,i.beginFill(5308416),i.drawRect(this.core.width-5,e.height/2,3,3),i.endFill();var o=new r;o.pt=!1,o.alpha=0,o.beginFill(5308416),o.drawRect(-1,e.height/2-2,3,3),o.endFill();var n=new r;n.pt=!1,n.alpha=0,n.beginFill(5308416),n.drawRect(this.core.width/2,e.height/2-this.core.height/2+5,3,3),n.endFill();var a=new r;a.pt=!1,a.alpha=0,a.beginFill(5308416),a.drawRect(this.core.width/2,e.height/2+this.core.height/2-8,3,3),a.endFill(),o.x-=t.pos*this.core.width,i.x-=t.pos*this.core.width,a.x-=t.pos*this.core.width,n.x-=t.pos*this.core.width;for(var h=e.width/t.pts,c=e.height/t.pts,l=t.pts-1;l>=0;l--)for(var d=0;do.x&&i.xo.y&&i.y=4||!t)&&(r=!0),{agentCollisions:o,onRoad:r}},t.prototype.setState=function(t){for(var e=0,i=0;ithis.state[o][n]||t[a].mapId==u.ROAD&&this.state[o][n]==u.DEFAULT)&&(this.state[o][n]=t[a].mapId)}e+=1}},t}(),b=(I=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var i in e)e.hasOwnProperty(i)&&(t[i]=e[i])},function(t,e){function i(){this.constructor=t}I(t,e),t.prototype=null===e?Object.create(e):(i.prototype=e.prototype,new i)}),R=function(t){function e(e,i){var o=t.call(this,e)||this;return o.rotationStep=i.rotationStep||.5,o.actions=i.actions||["UP","LEFT","RIGHT","DOWN","WAIT"],o}return b(e,t),e.prototype.setUp=function(t,e){this.car=t,this.lidar=e,this.state=[];for(var i=0;i0&&(this.car.v=0,this.car.vy=0),{agentCollisions:i,onRoad:o}},e}(M),E=function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var i in e)e.hasOwnProperty(i)&&(t[i]=e[i])};return function(e,i){function o(){this.constructor=e}t(e,i),e.prototype=null===i?Object.create(i):(o.prototype=i.prototype,new o)}}(),L=function(t){function e(e){var i=t.call(this,e)||this;return i.map=i.level.getMap(),i.mapSizeY=i.map.length,i.mapSizeX=i.map[0].length,i.rotationStep=.5,i.rotationToNextCase={},i.rotationToNextCase[0]={mx:1,my:0},i.rotationToNextCase[.5]={mx:0,my:1},i.rotationToNextCase[1]={mx:-1,my:0},i.rotationToNextCase[1.5]={mx:0,my:-1},i.rotationToNextCase[-.5]={mx:0,my:-1},i.rotationToNextCase[-1]={mx:-1,my:0},i.rotationToNextCase[-1.5]={mx:0,my:1},i}return E(e,t),e.prototype.setUp=function(t,e){this.car=t,this.lidar=e,this.state=[];for(var i=0;i=this.mapSizeY||t<0||t>=this.mapSizeX||0==this.map[e][t])return!1;if(i&&this.level.getRoad(e,t).cars.length>0)return!1;return!0},e.prototype.autoRotation=function(){var t=d+d/3,e=d-d/3.5,i=d;if(this.car.next_road){var o=Math.abs((this.car.next_road.outroad.y+d/2-this.car.y)*this.car.next_road.outroad_np.my),n=Math.abs((this.car.next_road.outroad.x+d/2-this.car.x)*this.car.next_road.outroad_np.mx);if(0==this.car.next_road.rotation_type&&othis.car.next_road.outroad.x+d/2?(this.car.x=this.car.next_road.outroad.x+d-d/2-d/4,this.car.y=this.car.next_road.outroad.y):0!=n&&this.car.ythis.car.next_road.outroad.y+d/2&&(this.car.y=this.car.next_road.outroad.y+d-d/2-d/4,this.car.x=this.car.next_road.outroad.x),void(this.car.next_road=void 0)}else{var a=this.car.rotation/Math.PI,r=(this.car.rotation/Math.PI+.5)%2,s=(this.car.rotation/Math.PI-.5)%2,h=this.rotationToNextCase[a],c=this.rotationToNextCase[r],l=this.rotationToNextCase[s],p=!1,u=this.car.mx+h.mx,v=this.car.my+h.my;if(this.isRoad(u,v)?this.car.haveTurned||this.car.turnedRandom?!this.car.haveTurned&&this.car.turnedRandom<.5&&this.isRoad(this.car.mx+c.mx,this.car.my+c.my)?(p=!0,this.car.optionalTurn=!0):!this.car.haveTurned&&this.car.turnedRandom>=.5&&this.isRoad(this.car.mx+l.mx,this.car.my+l.my)&&(p=!0,this.car.optionalTurn=!0):this.car.turnedRandom=Math.random():(p=!0,this.car.optionalTurn=!1),p)for(var y=[(a+.5)%2,(a-.5)%2,null],m=0;m=1&&n&&n.core.line==i&&(i=0==i?1:0),e.line=i,e.x=t.x,e.y=t.y,e.x+=d/2,e.y+=d/2;var a=1*d/4;t.orientation=Math.floor(t.orientation/e.rotationStep)*e.rotationStep;var r=-t.orientation*Math.PI,s=0*Math.cos(r)-a*Math.sin(r),h=Math.cos(r)*a+0*Math.sin(r),c=0==i?1:-1,l=0==i?0:Math.PI;e.x+=c*Math.round(s),e.y+=c*Math.round(h),e.mx=Math.floor(e.x/d),e.my=Math.floor(e.y/d),e.rotation=r+l,e.checkAndsetNewRoad(t)},t.prototype.createAsset=function(t,e,i,o){var n=new a(i[t]);n.obstacle=!1,n.type=o,n.mapId=0,n.x=e.x,n.y=e.y,this.level.addChild(n),this.assets.push(n)},t.prototype.createMap=function(t,e,i,o){if(void 0===o&&(o=!0),o)for(var n=0;n0?o=-1:e||(o=-1),o},e.prototype.getLastReward=function(){return this.lastReward},e.prototype.step=function(t,e){void 0===e&&(e=null);for(var i=0;i0&&0==this.car.a?this.car.v=Math.max(0,this.car.v-.01):this.car.v<0&&0==this.car.a&&(this.car.v=Math.min(0,this.car.v+.01)),this.car.a>0&&this.car.v>=0?this.car.v=Math.min(1.5,this.car.v+.01*this.car.a):this.car.a>0&&this.car.v<0&&(this.car.v=Math.min(1.5,this.car.v+.03*this.car.a)),this.car.a<0&&this.car.v<=0?this.car.v=Math.max(-1.5,this.car.v+.01*this.car.a):this.car.a<0&&this.car.v>0&&(this.car.v=Math.max(-1.5,this.car.v+.03*this.car.a)),0==this.car.yaw_rate)this.car.x+=this.car.v*Math.cos(this.car.rotation)*t,this.car.y+=this.car.v*Math.sin(this.car.rotation)*t;else{var e=.01*this.car.yaw_rate*(this.car.v+.001)*Math.PI;this.car.x+=this.car.v/e*(Math.sin(this.car.rotation+e*t)-Math.sin(this.car.rotation)),this.car.y+=this.car.v/e*(Math.cos(this.car.rotation)-Math.cos(this.car.rotation+e*t)),this.car.rotation+=e*t}this.car.mx=Math.floor(this.car.x/d),this.car.my=Math.floor(this.car.y/d),this.car.checkAndsetNewRoad(),this.lidar.x=this.car.x,this.lidar.y=this.car.y,this.lidar.rotation=this.car.rotation;var i=this.detectInteractions(!0,!0),o=i.agentCollisions,n=i.onRoad;return o.length>0&&(this.car.v=0,this.car.vy=0),{agentCollisions:o,onRoad:n}},e}(M),j={env:k,editor:U,level:{fullCity:"embedded://level/fullCity",level1:"embedded://level/level1",level2:"embedded://level/level2",level0:"embedded://level/level0"},motion:{BasicMotion:R,ControlMotion:B}};e.default=j}]).default; \ No newline at end of file +window.metacar=function(t){var e={};function i(o){if(e[o])return e[o].exports;var n=e[o]={i:o,l:!1,exports:{}};return t[o].call(n.exports,n,n.exports,i),n.l=!0,n.exports}return i.m=t,i.c=e,i.d=function(t,e,o){i.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:o})},i.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},i.t=function(t,e){if(1&e&&(t=i(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var o=Object.create(null);if(i.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var n in t)i.d(o,n,function(e){return t[e]}.bind(null,n));return o},i.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return i.d(e,"a",e),e},i.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},i.p="",i(i.s=1)}([function(t,e){t.exports=window.PIXI},function(t,e,i){"use strict";i.r(e);var o=i(0),n=(o.Application,o.loader),a=(o.loader.resources,o.Sprite),r=o.Graphics,s=o.Container,h="https://metacar-project.com/",c=h+"public/textures/textures.json",l=h+"public/images/",d=60,p={DEFAULT:"car.png",AGENT:"car_agent.png"},y={ROAD:-1,DEFAULT:0,CAR:1,AGENT:1},u={ROADS:{"↕":{image:"road_u.png",orientation:1.5},"↔":{image:"road_r.png",orientation:0},"↱":{image:"road_rotate_ld.png",orientation:1.25},"↰":{image:"road_rotate_ul.png",orientation:.75},"↲":{image:"road_rotate_rl.png",orientation:.25},"↳":{image:"road_rotate_dl.png",orientation:1.75},"↟":{image:"road_up.png",orientation:1.5},"↠":{image:"road_rp.png",orientation:0},"↤":{image:"road_r2.png",orientation:0},"↦":{image:"road_l2.png",orientation:1},"↥":{image:"road_d2.png",orientation:.5},"↧":{image:"road_u2.png",orientation:1.5}},house:{image:"house.png"},house2:{image:"house2.png"},house3:{image:"house3.png"},bench:{image:"bench.png"},tree:{image:"tree.png"}},x={level:{fullCity:{cars:[{mx:2,my:5,line:0,auto:!0},{mx:3,my:4,line:0,auto:!0},{mx:3,my:4,line:1,auto:!0},{mx:6,my:3,line:0,auto:!0},{mx:6,my:3,line:1,auto:!0},{mx:8,my:2,line:0,auto:!0},{mx:8,my:2,line:1,auto:!0},{mx:2,my:7,line:0,auto:!0}],house:[{x:9,y:409},{x:146,y:162},{x:439,y:39}],house2:[{x:425,y:192},{x:431,y:354}],house3:[{x:426,y:281},{x:51,y:157},{x:553,y:34}],bench:[{x:259,y:206},{x:8,y:205}],tree:[{x:1,y:303},{x:54,y:357},{x:215,y:325},{x:230,y:400},{x:282,y:368},{x:182,y:433},{x:285,y:309},{x:576,y:233},{x:581,y:300},{x:519,y:388},{x:582,y:391},{x:256,y:69},{x:151,y:43},{x:77,y:77},{x:26,y:24},{x:189,y:98}],map:[[0,0,0,0,0,0,"↕",0,0,0,0],[0,0,0,0,0,0,"↟",0,0,0,0],[0,0,0,0,0,0,"↦","↔","↔","↠","↔"],[0,0,0,0,0,0,"↕",0,0,0,0],["↔","↔","↧","↔","↠","↔","↤",0,0,0,0],[0,0,"↕",0,0,0,"↕",0,0,0,0],[0,0,"↟",0,0,0,"↟",0,0,0,0],[0,0,"↕",0,0,0,"↕",0,0,0,0]]},level1:{cars:[{mx:5,my:3,line:0,auto:!0},{mx:5,my:3,line:1,auto:!0},{mx:5,my:1,line:0,auto:!0}],tree:[{x:120,y:50},{x:86.5,y:85},{x:94.5,y:135},{x:30.5,y:128},{x:72.5,y:190},{x:23.5,y:232},{x:110.5,y:242},{x:43.5,y:33},{x:18.5,y:178},{x:305.5,y:125},{x:360.5,y:122}],asset:[{x:252.5,y:142},{x:86.5,y:85},{x:94.5,y:135},{x:30.5,y:128},{x:252.5,y:26},{x:195.5,y:0},{x:307.5,y:1},{x:72.5,y:190},{x:23.5,y:232},{x:110.5,y:242},{x:43.5,y:33},{x:35.5,y:185},{x:18.5,y:178},{x:289.5,y:126},{x:337.5,y:126},{x:305.5,y:125},{x:360.5,y:122},{x:492.5,y:56},{x:503.5,y:136},{x:504.5,y:58}],bench:[{x:252.5,y:142},{x:252.5,y:26}],road:[{x:240,y:60},{x:240,y:60},{x:480,y:0},{x:480,y:60},{x:480,y:120},{x:480,y:180},{x:480,y:240},{x:300,y:180},{x:300,y:180},{x:360,y:180},{x:420,y:180},{x:420,y:120},{x:420,y:60},{x:360,y:60},{x:300,y:60},{x:420,y:120}],house3:[{x:503.5,y:136}],house:[{x:504.5,y:58}],map:[[0,0,0,0,0,0,0,0,0,0],[0,0,0,"↱","↠","↔","↔","↰",0,0],[0,0,0,"↕",0,0,0,"↟",0,0],[0,0,0,"↳","↔","↔","↔","↲",0,0],[0,0,0,0,0,0,0,0,0,0]],agent:{mx:4,my:3,line:0,motion:{type:"BasicMotionEngine",options:{rotationStep:.5,actions:["UP","LEFT","RIGHT"]}}}},level0:{cars:[],house:[{x:263,y:108},{x:112,y:258}],house3:[{x:262,y:16}],bench:[{x:135,y:34}],tree:[{x:124,y:134},{x:7,y:11},{x:69,y:10},{x:19,y:238},{x:5,y:133},{x:260,y:211}],map:[[0,0,0,0,0],[0,"↱","↔","↰",0],[0,"↕",0,"↕",0],[0,"↳","↠","↲",0],[0,0,0,0,0]],agent:{mx:2,my:1,line:1,motion:{type:"BasicMotionEngine",options:{rotationStep:.5,actions:["UP","LEFT","RIGHT","DOWN","WAIT"]}}}},level2:{cars:[],road:[{x:300,y:360},{x:300,y:360},{x:360,y:360},{x:420,y:360},{x:240,y:360},{x:180,y:360},{x:120,y:360},{x:480,y:360},{x:480,y:360},{x:120,y:360},{x:60,y:360},{x:300,y:300},{x:300,y:240},{x:480,y:360},{x:480,y:360},{x:540,y:360},{x:300,y:180},{x:360,y:180},{x:240,y:180},{x:240,y:180},{x:180,y:180},{x:420,y:180},{x:420,y:120},{x:180,y:120},{x:300,y:60},{x:420,y:60},{x:180,y:60},{x:240,y:60},{x:360,y:60},{x:480,y:60},{x:120,y:60},{x:540,y:60},{x:540,y:120},{x:540,y:180},{x:540,y:240},{x:540,y:300},{x:60,y:60},{x:60,y:120},{x:60,y:180},{x:60,y:240},{x:60,y:300},{x:60,y:240},{x:360,y:180},{x:480,y:360}],asset:[{x:162,y:264},{x:407,y:264},{x:259,y:126},{x:306,y:33},{x:491,y:148},{x:391,y:431},{x:608,y:35},{x:484,y:224},{x:248,y:297},{x:376,y:336},{x:78,y:429},{x:1,y:205},{x:3,y:106},{x:-46,y:290},{x:622,y:154},{x:617,y:280}],house:[{x:162,y:264},{x:622,y:154}],house3:[{x:407,y:264},{x:-46,y:290}],tree:[{x:259,y:126},{x:491,y:148},{x:391,y:431},{x:608,y:35},{x:484,y:224},{x:248,y:297},{x:78,y:429},{x:1,y:205},{x:3,y:106},{x:617,y:280}],bench:[{x:306,y:33},{x:376,y:336}],map:[[0,0,0,0,0,0,0,0,0,0,0],[0,"↱","↔","↧","↔","↔","↔","↧","↔","↰",0],[0,"↕",0,"↕",0,0,0,"↕",0,"↕",0],[0,"↕",0,"↳","↔","↧","↠","↲",0,"↕",0],[0,"↟",0,0,0,"↕",0,0,0,"↕",0],[0,"↕",0,0,0,"↕",0,0,0,"↕",0],[0,"↳","↔","↔","↔","↥","↔","↔","↠","↲",0],[0,0,0,0,0,0,0,0,0,0,0]],agent:{mx:4,my:3,line:0,motion:{type:"BasicMotionEngine",options:{rotationStep:.5,actions:["UP","LEFT","RIGHT","DOWN","WAIT"]}}}},level3:{cars:[{mx:1,my:3,line:0},{mx:1,my:8,line:0},{mx:4,my:8,line:0},{mx:7,my:7,line:1},{mx:9,my:6,line:0},{mx:11,my:4,line:1},{mx:9,my:3,line:1},{mx:7,my:2,line:1},{mx:5,my:1,line:1},{mx:2,my:1,line:1},{mx:6,my:8,line:0},{mx:1,my:6,line:0},{mx:5,my:5,line:1},{mx:4,my:3,line:1},{mx:3,my:4,line:0},{mx:4,my:6,line:0}],road:[{x:120,y:60},{x:180,y:60},{x:240,y:60},{x:60,y:60},{x:60,y:120},{x:60,y:180},{x:60,y:240},{x:60,y:300},{x:60,y:360},{x:60,y:420},{x:60,y:480},{x:120,y:480},{x:180,y:480},{x:240,y:480},{x:300,y:60},{x:300,y:120},{x:300,y:180},{x:300,y:480},{x:300,y:420},{x:300,y:360},{x:300,y:180},{x:360,y:180},{x:420,y:180},{x:420,y:120},{x:420,y:60},{x:240,y:240},{x:300,y:60},{x:360,y:60},{x:240,y:180},{x:300,y:180},{x:300,y:240},{x:300,y:300},{x:240,y:300},{x:180,y:180},{x:180,y:240},{x:180,y:300},{x:360,y:60},{x:420,y:60},{x:420,y:60},{x:420,y:120},{x:420,y:180},{x:420,y:180},{x:480,y:180},{x:540,y:180},{x:660,y:180},{x:600,y:180},{x:660,y:240},{x:660,y:300},{x:660,y:360},{x:600,y:360},{x:540,y:360},{x:480,y:360},{x:420,y:360},{x:300,y:480},{x:360,y:480},{x:360,y:420},{x:360,y:360},{x:420,y:360},{x:420,y:420},{x:420,y:480},{x:360,y:480},{x:180,y:360},{x:240,y:360},{x:300,y:360},{x:300,y:300},{x:300,y:240},{x:180,y:180},{x:240,y:180},{x:300,y:180},{x:180,y:240},{x:180,y:300},{x:240,y:180},{x:180,y:60},{x:60,y:300},{x:300,y:480},{x:240,y:480},{x:600,y:180},{x:540,y:180},{x:660,y:300},{x:480,y:360}],asset:[{x:515,y:262},{x:378,y:254},{x:510,y:89},{x:626,y:86},{x:518,y:445},{x:631,y:436},{x:711,y:434},{x:243,y:295},{x:243,y:246},{x:253,y:153},{x:16,y:521},{x:129,y:214},{x:121,y:228},{x:238,y:425},{x:456,y:273},{x:584,y:531},{x:376,y:558},{x:63,y:539},{x:91,y:559},{x:4,y:515},{x:-3,y:315},{x:-3,y:183},{x:368,y:188},{x:500,y:18},{x:610,y:16},{x:685,y:13},{x:273,y:0},{x:738,y:359},{x:742,y:181},{x:752,y:12}],house3:[{x:515,y:262},{x:626,y:86}],house2:[{x:378,y:254},{x:631,y:436},{x:711,y:434},{x:752,y:12}],house:[{x:510,y:89},{x:518,y:445},{x:742,y:181}],tree:[{x:243,y:295},{x:243,y:246},{x:121,y:228},{x:238,y:425},{x:456,y:273},{x:584,y:531},{x:376,y:558},{x:91,y:559},{x:4,y:515},{x:-3,y:315},{x:-3,y:183},{x:368,y:188},{x:500,y:18},{x:610,y:16},{x:685,y:13},{x:273,y:0},{x:738,y:359}],bench:[{x:253,y:153}],map:[[0,0,0,0,0,0,0,0,0,0,0,0,0],[0,"↱","↔","↠","↔","↔","↔","↰",0,0,0,0,0],[0,"↕",0,0,0,0,0,"↕",0,0,0,0,0],[0,"↕",0,"↱","↠","↰",0,"↳","↔","↠","↔","↰",0],[0,"↕",0,"↕",0,"↕",0,0,0,0,0,"↕",0],[0,"↟",0,"↕",0,"↕",0,0,0,0,0,"↟",0],[0,"↕",0,"↳","↔","↲",0,"↱","↠","↔","↔","↲",0],[0,"↕",0,0,0,0,0,"↕",0,0,0,0,0],[0,"↳","↔","↔","↔","↠","↔","↲",0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0]],agent:{mx:3,my:8,line:0,motion:{type:"BasicMotionEngine",options:{rotationStep:.5,actions:["UP","LEFT","RIGHT","DOWN","WAIT"]}}}}}};function v(t,e){var i=new XMLHttpRequest;i.overrideMimeType("application/json"),i.open("GET",t,!0),i.onreadystatechange=function(){4==i.readyState&&200==i.status&&e(JSON.parse(i.responseText))},i.send(null)}function m(t,e){var i=t.split("//");i=i[1].split("/"),e(x[i[0]][i[1]])}function f(t,e){({"embedded:":m,"http:":v,"https:":v})[t.split("//")[0]](t,e)}function g(t,e){var i=document.createElement("a");document.body.appendChild(i);var o=new Blob([t],{type:"application/octet-binary"}),n=window.URL.createObjectURL(o);i.href=n,i.download=e,i.click(),window.URL.revokeObjectURL(n),i.href=""}function _(t){var e={};return e.code=t,e.isDown=!1,e.isUp=!0,e.press=void 0,e.release=void 0,e.downHandler=function(t){t.keyCode===e.code&&(e.isUp&&e.press&&e.press(),e.isDown=!0,e.isUp=!1),t.preventDefault()},e.upHandler=function(t){t.keyCode===e.code&&(e.isDown&&e.release&&e.release(),e.isDown=!1,e.isUp=!0),t.preventDefault()},window.addEventListener("keydown",e.downHandler.bind(e),!1),window.addEventListener("keyup",e.upHandler.bind(e),!1),e}var I,C=0,w=function(){function t(t,e,i,o){void 0===o&&(o={});var n=this;this.level=t,o.image=o.image||p.DEFAULT,o.lidar=o.lidar||!1,o.lidarInfo=o.lidarInfo||{pts:5,width:4,height:5,pos:0},this.core=new a(i[o.image]),this.info=e,this.core.scale.x=.8,this.core.scale.y=.8,this.core.anchor.x=.5,this.core.anchor.y=.5,this.core.road=void 0,this.core.carId=C,C+=1,this.core.agent=!1,this.core.obstacle=!0,this.core.mapId=y.CAR,o.lidar&&(this.createLidar(o.lidarInfo),this.core.lidar=this.lidar),o.motionEngine?(this.motion=o.motionEngine,this.motion.setUp(this.core,this.lidar),this.core.rotationStep=.5):this.core.rotationStep=.5,this.core.mx=this.info.mx,this.core.my=this.info.my,this.core.x=this.info.mx*d,this.core.y=this.info.my*d,this.core.checkAndsetNewRoad=function(t){return n.checkAndsetNewRoad(t)};var r=this.level.getRoad(this.core.my,this.core.mx);this.core.line=0,r&&r.setCarPosition(this.core,this.info.line)}return t.prototype.checkAndsetNewRoad=function(t){var e=this.core.road;t||(t=this.level.getRoad(this.core.my,this.core.mx)),t!=e&&(e&&e.cars.splice(e.cars.indexOf(this.core.carId),1),this.core.road=t,this.core.haveTurned=!1,this.core.optionalTurn=!1,this.core.turnedRandom=void 0,this.core.next_road=!1,t&&t.cars.push(this.core.carId))},t.prototype.reset=function(){this.core.mx=this.info.mx,this.core.my=this.info.my;var t=this.level.getRoad(this.core.my,this.core.mx);t&&t.setCarPosition(this.core,this.info.line,!0)},t.prototype.getState=function(){var t={},e=[];return this.motion.state.map(function(t){e=e.concat(t)}),e.push(this.core.v),t.linear=e,t.lidar=this.motion.state.map(function(t){return t.slice()}),t.v=this.core.v,t.a=this.core.last_a,t.steering=this.core.last_yaw_rate,t},t.prototype.step=function(t,e){if(void 0===e&&(e=null),null==e)var i=this.motion.step(t),o=i.agentCollisions,n=i.onRoad;else{var a=this.motion.actionStep(t,e);o=a.agentCollisions,n=a.onRoad}return{agentCollisions:o,onRoad:n}},t.prototype.createLidar=function(t){t.pts=t.pts||5,t.width=t.width||4,t.height=t.height||5,t.pos=t.pos||0,this.lidar=new s,this.lidar.lidarPts=[],this.lidar.collisionPts=[];var e=new r;e.pt=!1,e.alpha=.3,e.beginFill(5329233),e.drawRect(0,0,this.core.width*t.width,this.core.height*t.height),e.endFill();var i=new r;i.pt=!1,i.alpha=0,i.beginFill(5308416),i.drawRect(this.core.width-5,e.height/2,3,3),i.endFill();var o=new r;o.pt=!1,o.alpha=0,o.beginFill(5308416),o.drawRect(-1,e.height/2-2,3,3),o.endFill();var n=new r;n.pt=!1,n.alpha=0,n.beginFill(5308416),n.drawRect(this.core.width/2,e.height/2-this.core.height/2+5,3,3),n.endFill();var a=new r;a.pt=!1,a.alpha=0,a.beginFill(5308416),a.drawRect(this.core.width/2,e.height/2+this.core.height/2-8,3,3),a.endFill(),o.x-=t.pos*this.core.width,i.x-=t.pos*this.core.width,a.x-=t.pos*this.core.width,n.x-=t.pos*this.core.width;for(var h=e.width/t.pts,c=e.height/t.pts,l=t.pts-1;l>=0;l--)for(var d=0;do.x&&i.xo.y&&i.y=4||!t)&&(r=!0),{agentCollisions:o,onRoad:r}},t.prototype.setState=function(t){for(var e=0,i=0;ithis.state[o][n]||t[a].mapId==y.ROAD&&this.state[o][n]==y.DEFAULT)&&(this.state[o][n]=t[a].mapId)}e+=1}},t}(),b=(I=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var i in e)e.hasOwnProperty(i)&&(t[i]=e[i])},function(t,e){function i(){this.constructor=t}I(t,e),t.prototype=null===e?Object.create(e):(i.prototype=e.prototype,new i)}),R=function(t){function e(e,i){var o=t.call(this,e)||this;return o.maxSpeed=1,o.rotationStep=i.rotationStep||.5,o.actions=i.actions||["UP","LEFT","RIGHT","DOWN","WAIT"],o}return b(e,t),e.prototype.setUp=function(t,e){this.car=t,this.lidar=e,this.state=[];for(var i=0;i0&&(this.car.v=0,this.car.vy=0),{agentCollisions:i,onRoad:o}},e}(M),E=function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var i in e)e.hasOwnProperty(i)&&(t[i]=e[i])};return function(e,i){function o(){this.constructor=e}t(e,i),e.prototype=null===i?Object.create(i):(o.prototype=i.prototype,new o)}}(),S=function(t){function e(e){var i=t.call(this,e)||this;return i.map=i.level.getMap(),i.mapSizeY=i.map.length,i.mapSizeX=i.map[0].length,i.rotationStep=.5,i.rotationToNextCase={},i.rotationToNextCase[0]={mx:1,my:0},i.rotationToNextCase[.5]={mx:0,my:1},i.rotationToNextCase[1]={mx:-1,my:0},i.rotationToNextCase[1.5]={mx:0,my:-1},i.rotationToNextCase[-.5]={mx:0,my:-1},i.rotationToNextCase[-1]={mx:-1,my:0},i.rotationToNextCase[-1.5]={mx:0,my:1},i}return E(e,t),e.prototype.setUp=function(t,e){this.car=t,this.lidar=e,this.state=[];for(var i=0;i=this.mapSizeY||t<0||t>=this.mapSizeX||0==this.map[e][t])return!1;if(i&&this.level.getRoad(e,t).cars.length>0)return!1;return!0},e.prototype.autoRotation=function(){var t=d+d/3,e=d-d/3.5,i=d;if(this.car.next_road){var o=Math.abs((this.car.next_road.outroad.y+d/2-this.car.y)*this.car.next_road.outroad_np.my),n=Math.abs((this.car.next_road.outroad.x+d/2-this.car.x)*this.car.next_road.outroad_np.mx);if(0==this.car.next_road.rotation_type&&othis.car.next_road.outroad.x+d/2?(this.car.x=this.car.next_road.outroad.x+d-d/2-d/4,this.car.y=this.car.next_road.outroad.y):0!=n&&this.car.ythis.car.next_road.outroad.y+d/2&&(this.car.y=this.car.next_road.outroad.y+d-d/2-d/4,this.car.x=this.car.next_road.outroad.x),void(this.car.next_road=void 0)}else{var a=this.car.rotation/Math.PI,r=(this.car.rotation/Math.PI+.5)%2,s=(this.car.rotation/Math.PI-.5)%2,h=this.rotationToNextCase[a],c=this.rotationToNextCase[r],l=this.rotationToNextCase[s],p=!1,y=this.car.mx+h.mx,u=this.car.my+h.my;if(this.isRoad(y,u)?this.car.haveTurned||this.car.turnedRandom?!this.car.haveTurned&&this.car.turnedRandom<.5&&this.isRoad(this.car.mx+c.mx,this.car.my+c.my)?(p=!0,this.car.optionalTurn=!0):!this.car.haveTurned&&this.car.turnedRandom>=.5&&this.isRoad(this.car.mx+l.mx,this.car.my+l.my)&&(p=!0,this.car.optionalTurn=!0):this.car.turnedRandom=Math.random():(p=!0,this.car.optionalTurn=!1),p)for(var x=[(a+.5)%2,(a-.5)%2,null],v=0;v=1&&n&&n.core.line==i&&(i=0==i?1:0),e.line=i,e.a=0,e.v=0,e.x=t.x,e.y=t.y,e.x+=d/2,e.y+=d/2;var a=1*d/4;t.orientation=Math.floor(t.orientation/e.rotationStep)*e.rotationStep;var r=-t.orientation*Math.PI,s=0*Math.cos(r)-a*Math.sin(r),h=Math.cos(r)*a+0*Math.sin(r),c=0==i?1:-1,l=0==i?0:Math.PI;e.x+=c*Math.round(s),e.y+=c*Math.round(h),e.mx=Math.floor(e.x/d),e.my=Math.floor(e.y/d),e.rotation=r+l,e.checkAndsetNewRoad(t)},t.prototype.createAsset=function(t,e,i,o){var n=new a(i[t]);n.obstacle=!1,n.type=o,n.mapId=0,n.x=e.x,n.y=e.y,this.level.addChild(n),this.assets.push(n)},t.prototype.createMap=function(t,e,i,o){if(void 0===o&&(o=!0),o)for(var n=0;n0?o=-1:e||(o=-1),o},e.prototype.getLastReward=function(){return this.lastReward},e.prototype.step=function(t,e,i){if(void 0===e&&(e=null),void 0===i&&(i=!0),!i||this.steping){for(var o=0;o0&&0==this.car.a?this.car.v=Math.max(0,this.car.v-.01):this.car.v<0&&0==this.car.a&&(this.car.v=Math.min(0,this.car.v+.01)),this.car.a>0&&this.car.v>=0?this.car.v=Math.min(this.maxSpeed,this.car.v+.005*this.car.a):this.car.a>0&&this.car.v<0&&(this.car.v=Math.min(this.maxSpeed,this.car.v+.03*this.car.a)),this.car.a<0&&this.car.v<=0?this.car.v=Math.max(-this.maxSpeed,this.car.v+.005*this.car.a):this.car.a<0&&this.car.v>0&&(this.car.v=Math.max(-this.maxSpeed,this.car.v+.03*this.car.a)),0==this.car.yaw_rate)this.car.x+=this.car.v*Math.cos(this.car.rotation)*t,this.car.y+=this.car.v*Math.sin(this.car.rotation)*t;else{var e=.01*this.car.yaw_rate*(this.car.v+.001)*Math.PI;this.car.x+=this.car.v/e*(Math.sin(this.car.rotation+e*t)-Math.sin(this.car.rotation)),this.car.y+=this.car.v/e*(Math.cos(this.car.rotation)-Math.cos(this.car.rotation+e*t)),this.car.rotation+=e*t}this.car.mx=Math.floor(this.car.x/d),this.car.my=Math.floor(this.car.y/d),this.car.checkAndsetNewRoad(),this.lidar.x=this.car.x,this.lidar.y=this.car.y,this.lidar.rotation=this.car.rotation;var i=this.detectInteractions(!0,!0),o=i.agentCollisions,n=i.onRoad;return o.length>0&&(this.car.v=0,this.car.vy=0),{agentCollisions:o,onRoad:n}},e}(M),j={env:k,editor:U,level:{fullCity:"embedded://level/fullCity",level1:"embedded://level/level1",level2:"embedded://level/level2",level3:"embedded://level/level3",level0:"embedded://level/level0"},motion:{BasicMotion:R,ControlMotion:B}};e.default=j}]).default; \ No newline at end of file