-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundle.js
1 lines (1 loc) · 7.73 KB
/
bundle.js
1
(()=>{"use strict";var t={46:(t,e,n)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.ActualBoard=void 0;const r=n(514),o=n(963);class s{constructor(t,e){this.name=t,this.coords=e}right(){return s.values[(s.reverseValues[this.toString()]+1)%s.values.length]}left(){return s.values[(s.reverseValues[this.toString()]+s.values.length-1)%s.values.length]}toString(){return this.name}}s.UP=new s("UP",{x:0,y:-1}),s.RIGHT=new s("RIGHT",{x:1,y:0}),s.DOWN=new s("DOWN",{x:0,y:1}),s.LEFT=new s("LEFT",{x:-1,y:0}),s.values=[s.UP,s.RIGHT,s.DOWN,s.LEFT],s.reverseValues=Object.fromEntries(s.values.map(((t,e)=>[t,e])));class i{constructor(){this.name="IU"}read(t){return t.direction===s.UP?1:0}}class a{constructor(){this.name="TB"}read(t){return 0===t.y||t.y===t.gameProps.maxY||0===t.x||t.x===t.gameProps.maxX?1:0}}class u{constructor(){this.name="AL"}read(t){return 1}}class c{constructor(){this.name="TP"}read(t){return t.gameProps.turn/t.gameProps.maxTurn}}class l{constructor(){this.name="RN"}read(t){return Math.random()}}function h(t,e,n){return t<e?e:t>n?n:t}class m extends r.BaseActor{constructor(){super("FD")}_innerWrite(t,e){if(t>0){const t=e.direction.coords,{maxX:n,maxY:r}=e.gameProps;e.x=h(e.x+2*t.x,0,n),e.y=h(e.y+2*t.y,0,r)}}}class d extends r.BaseActor{constructor(){super("BW")}_innerWrite(t,e){if(t>0){const t=e.direction.coords,{maxX:n,maxY:r}=e.gameProps;e.x=h(e.x-t.x,0,n),e.y=h(e.y-t.y,0,r)}}}class p extends r.BaseActor{constructor(){super("RT")}_innerWrite(t,e){t>0&&(e.direction=e.direction.right())}}class g extends r.BaseActor{constructor(){super("LT")}_innerWrite(t,e){t>0&&(e.direction=e.direction.left())}}e.ActualBoard=class{constructor(t,e,n,h){this.condition=h,this._survivorsLastGen=0,this._generationNumber=0,this.gameProps={maxX:t.x-1,maxY:t.y-1,turn:0,maxTurn:n},this.board=new r.Board(e,(()=>{const e=(0,o.randomChoice)(s.values);return{x:(0,o.getRandomInt)(0,t.x),y:(0,o.getRandomInt)(0,t.y),gameProps:this.gameProps,direction:e}}),[new i,new a,new l,new c,new u],[new g,new p,new m,new d],4,10,.01)}doTurn(){this.gameProps.turn>this.gameProps.maxTurn&&(this.gameProps.turn=0,this.board.kill(this.condition),this._survivorsLastGen=this.population.length,this.board.repopulate(),this._generationNumber++),this.board.tick(),this.gameProps.turn++}get generationNumber(){return this._generationNumber}get population(){return this.board.population}get survivorsLastGen(){return this._survivorsLastGen}dumpGeneration(){return{generationNumber:this.generationNumber,generation:this.board.dumpPopulation()}}}},514:(t,e,n)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.Board=e.Entity=e.NeuralLink=e.BaseActor=e.Neuron=void 0;const r=n(963),o=n(593);class s{constructor(t){this.neuronNumber=t,this.input=0,this.name="N"+t}read(){return Math.tanh(this.input)}write(t){this.input+=t}clear(){this.input=0}}e.Neuron=s,e.BaseActor=class{constructor(t){this.name=t}write(t,e){this._innerWrite(Math.tanh(t),e)}};class i{constructor(t,e,n){this.sensor=t,this.action=e,this.linkStrength=n}act(t){this.action.write(this.sensor.read(t)+this.linkStrength,t)}get shortRepr(){return`${this.sensor.name}-${this.action.name}`}static fromScratch(t,e,n){return new i(t,e,n)}static random(t,e){return i.fromScratch((0,r.randomChoice)(t),(0,r.randomChoice)(e),(0,r.getRandomFloat)(-4,4))}static withRandomStrength(t,e){return this.fromScratch(t,e,(0,r.getRandomFloat)(-4,4))}}e.NeuralLink=i;class a{constructor(t,e,n){this.properties=t,this.neurons=e,this.links=n}static createParentLinkMapping(t){const e={};return t.flatMap((t=>t.links)).forEach((t=>{var n;const r=t.shortRepr;(null!==(n=e[r])&&void 0!==n?n:e[r]=[]).push(t)})),e}static fromParents(t,e,n,u,c,l,h){const m=(0,o.generateArray)(n,(t=>new s(t))),d=[...m,...e],p=[...m,...u],g=a.createParentLinkMapping(l),y=this.calculateLinksNumber(h,l,c),f=(0,o.generateArray)(y,(t=>{if((0,o.isEmpty)(g))return i.random(d,p);const e=(0,o.popKey)(g,(0,r.randomChoice)(Object.keys(g)));return(0,r.randomBoolean)(h)?i.withRandomStrength(e[0].sensor,e[0].action):(0,r.randomChoice)(e)}));return new a(t,m,f)}static calculateLinksNumber(t,e,n){const o=Math.min(...e.map((t=>t.links.length))),s=Math.max(...e.map((t=>t.links.length)));return(0,r.randomBoolean)(t)?(0,r.randomBoolean)()?Math.max(o-1,0):Math.min(s+1,n):(0,r.getRandomIntInclusive)(o,s)}static random(t,e,n,s,u){const c=this.generateNeurons(n),l=[...c,...e],h=[...c,...s],m=(0,o.generateArray)((0,r.getRandomIntInclusive)(0,u/2),(t=>i.random(l,h)));return new a(t,c,m)}static generateNeurons(t){return(0,o.generateArray)(t,(t=>new s(t)))}run(){this.links.forEach((t=>t.act(this.properties))),this.neurons.forEach((t=>t.clear()))}}e.Entity=a,e.Board=class{constructor(t,e,n,r,s,i,a){this.populationSize=t,this.generateProps=e,this.receptors=n,this.actions=r,this.neuronNumber=s,this.maxLinks=i,this.mutationRate=a,this._population=(0,o.generateArray)(t,(t=>this.generateRandomEntity()))}tick(){this._population.forEach((t=>t.run()))}kill(t){this._population=this._population.filter((e=>t(e.properties)))}repopulate(){const t=[];for(let e=0;e<this.populationSize;e+=1)t.push(a.fromParents(this.generateProps(),this.receptors,this.neuronNumber,this.actions,this.maxLinks,[this._population[e%this._population.length],this._population[(e+1)%this._population.length]],this.mutationRate));this._population=t}generateRandomEntity(){return a.random(this.generateProps(),this.receptors,this.neuronNumber,this.actions,this.maxLinks)}get population(){return this._population}dumpPopulation(){return this._population.map((({links:t})=>t.map((({sensor:t,action:e,linkStrength:n})=>({sensor:t.name,action:e.name,linkStrength:n})))))}}},963:(t,e)=>{function n(t,e){return t=Math.ceil(t),e=Math.floor(e),Math.floor(Math.random()*(e-t))+t}Object.defineProperty(e,"__esModule",{value:!0}),e.randomBoolean=e.getRandomFloat=e.randomChoice=e.getRandomIntInclusive=e.getRandomInt=void 0,e.getRandomInt=n,e.getRandomIntInclusive=function(t,e){return n(t,e+1)},e.randomChoice=function(t){return t[n(0,t.length)]},e.getRandomFloat=function(t,e){return Math.random()*(e-t)+t},e.randomBoolean=function(t){return Math.random()<(null!=t?t:.5)}},593:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.generateArray=e.isEmpty=e.popKey=void 0,e.popKey=function(t,e){if(!Object.keys(t).includes(e))return;const n=t[e];return delete t[e],n},e.isEmpty=function(t){return 0===Object.keys(t).length},e.generateArray=function(t,e){const n=[];for(let r=0;r<t;r++)n.push(e(r));return n}}},e={};function n(r){var o=e[r];if(void 0!==o)return o.exports;var s=e[r]={exports:{}};return t[r](s,s.exports,n),s.exports}(()=>{var t;const e=n(46),r=Object.freeze({HEIGHT:200,WIDTH:200}),o=new e.ActualBoard({y:r.HEIGHT,x:r.WIDTH},1e3,60,(({gameProps:{maxX:t},x:e})=>e>t/2)),s=function(){const t=document.getElementById("cnvs");return t.height=r.HEIGHT,t.width=r.WIDTH,t.getContext("2d")}(),i=document.getElementById("runChkbx"),a=document.getElementById("ffChkbx"),u=document.getElementById("generation"),c=document.getElementById("survs");function l(t){return setInterval((()=>{i.checked&&(s.fillStyle="rgb(171,133,133)",s.fillRect(0,0,r.WIDTH/2,r.HEIGHT),s.fillStyle="rgb(206,175,175)",s.fillRect(r.WIDTH/2,0,r.WIDTH/2,r.HEIGHT),s.fillStyle="rgb(0, 0, 0)",o.population.forEach((t=>{s.fillRect(t.properties.x,t.properties.y,1,1)})),u.innerText=String(o.generationNumber),c.innerText=String(o.survivorsLastGen),o.doTurn())}),t)}document.getElementById("totalPop").innerText=String(1e3);let h=l(100);a.addEventListener("change",(t=>{clearInterval(h),h=t.target.checked?l(1):l(100)})),null===(t=document.getElementById("dump"))||void 0===t||t.addEventListener("click",(function(){const t=window.document.createElement("a");t.href=URL.createObjectURL(new Blob([JSON.stringify(o.dumpGeneration(),void 0,"\t")],{type:"application/json"})),t.download="dump.json",document.body.appendChild(t),t.click(),document.body.removeChild(t)}))})()})();