-
Notifications
You must be signed in to change notification settings - Fork 0
/
JeneticS.min.js
1 lines (1 loc) · 3.87 KB
/
JeneticS.min.js
1
class JeneticS{constructor(t){this.culture=null,this.settings={mutationRate:.01,population:500,crossoverMethod:"all",elitism:.1,eliteMutationMultiplier:5};for(let n in t)this.settings[n]=t[n];this.load=function(t){"Culture"==typeof t&&(this.culture=t)},this.save=function(t){},this.innoculate=function(t,n=this.settings.population){return this.settings.population=n,this.culture=new Culture(t,n),this.culture},this.health=function(){let t=[];return this.culture.citizens.forEach(n=>{t.push(n.dna.join(""))}),t},this.run=function(){let t=this;return{generation:function(){t.culture.naturalSelection(),t.culture.normalize(),t.culture.crossover()[t.settings.crossoverMethod](),t.culture.mutate(t.settings.mutationRate,t.settings.elitism,t.settings.eliteMutationMultiplier)},continuous:function(){}}}}}class Culture{constructor(t,n){if(isNaN(n))throw"Please use a number";if("function"!=typeof t)throw"Expected function as first argument. Please define a function to create citizens.";this.population=n,this.best=null,this.fitnessRange={low:1/0,high:-1/0},this.citizens=[],this.census=[],this.nextGeneration=[];for(let e=0;e<n;e++)this.citizens.push(t(e)),this.census.push(e);this.genCensus=function(){return this.census.length!==this.citizens.length?Array.apply(null,Array(this.citizens.length)).map((t,n)=>n):this.census},this.naturalSelection=function(){this.best=null,this.fitnessRange.low=1/0,this.fitnessRange.high=-1/0,this.liveOneEpoch()},this.crossover=function(){let t=this;function n(){for(;;){let n,e=t.util.random();for(let i=0;i<t.citizens.length;i++){if(e<(n=t.citizen(i)).weighted)return n;e-=n.weighted}}}function e(){let t=n();return[n(),t]}function i(n,e=!1){if(n&&n.length){let i=t.util.floor(n.length/2);return e?n.slice(0,i):n.slice(i)}return[]}function s(n){let[s,o]=e();s=i(s.dna,!0),o=i(o.dna);let h=new Agent;h.dna=s.concat(o),t.nextGeneration.push(h)}function o(n){let[i,s]=e(),o=new Agent;o.dna=i.dna.filter((t,n)=>n%2==0).reduce(function(t,n,e){return t.concat(n,s.dna.filter((t,n)=>n%2==1)[e])},[]);let h=s.dna.length,u=i.dna.length;h<u?o.dna.concat(i.dna.slice(h)):o.dna.concat(s.dna.slice(u)),o.dna=o.dna.filter(function(t){return void 0!=t}),t.nextGeneration.push(o)}function h(n){t.util.randFloor(10)%2?s():o()}function u(n){for(t.nextGeneration=[];t.nextGeneration.length<t.population;)n(t.nextGeneration.length)}return{all:function(){u(h)},half:function(){u(s)},alternate:function(){u(o)}}},this.mutate=function(t,n,e=2){for(let n=1;n<this.nextGeneration.length;n++)this.nextGeneration[n].mutate(t);let i=this.util.floor(this.population*n);for(let n=1;n<i;n++){let n=this.best.copy();n.mutate(t*e),this.nextGeneration.push(n)}this.nextGeneration.push(this.best),this.citizens=this.nextGeneration,this.census=this.genCensus()},this.citizen=function(t){return this.citizens[this.census[t]]},this.shuffle=function(){for(var t,n,e=this.census.length;e;)n=Math.floor(Math.random()*len--),t=this.census[e],this.census[e]=this.census[n],this.census[n]=t},this.liveOneEpoch=function(){for(let t=0;t<this.citizens.length;t++){let n=this.citizen(t);n.live(),this.fitnessRange.low=n.score<this.fitnessRange.low?n.score:this.fitnessRange.low,n.score>this.fitnessRange.high&&(this.fitnessRange.high=n.score,this.best=n.copy())}},this.normalize=function(){let t=0;for(let n=0;n<this.citizens.length;n++){let e=this.citizen(n);e.score=this.util.map(e.score,2*this.fitnessRange.low/3,1.5*this.fitnessRange.high,0,100),t+=e.score}for(let n=0;n<this.citizens.length;n++){let e=this.citizen(n);e.weighted=e.score/t}},this.util={map:function(t,n,e,i,s){return(t-n)*(s-i)/(e-n)+i},random:Math.random,floor:Math.floor,randFloor:function(t){return this.floor(this.random()*t)}}}}class Agent{constructor(){this.type=null,this.score=1,this.weighted=1,this.config={},this.dna=[],this.copy=function(){let t=new Agent;for(var n in this.config)this.config.hasOwnProperty(n)&&(t.config[n]=this.config[n]);return t.type=this.type,t.dna=this.dna.slice(),t}}}