UNPKG

genome.js

Version:

Genetics algorithms done right

1 lines 7.67 kB
!function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var o=e();for(var s in o)("object"==typeof exports?exports:t)[s]=o[s]}}(window,function(){return function(o){var s={};function n(t){if(s[t])return s[t].exports;var e=s[t]={i:t,l:!1,exports:{}};return o[t].call(e.exports,e,e.exports,n),e.l=!0,e.exports}return n.m=o,n.c=s,n.d=function(t,e,o){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:o})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var o=Object.create(null);if(n.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var s in e)n.d(o,s,function(t){return e[t]}.bind(null,s));return o},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=3)}([function(t,e,o){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n=o(1),s=function(){function s(t){void 0===t&&(t=null),this.genes=[],this.constants=[],this.fitness=0,this.isKilled=!1,t&&this.initializeGenes(t)}return s.prototype.initializeGenes=function(t){var o=this;t.getProperties().map(function(t){var e=new n.Gene(t);o.genes.push(e)}),t.getConstants().map(function(t){var e=new n.Gene(t);o.constants.push(e)})},s.prototype.computeFitness=function(t){this.fitness=t(this.genes,this.constants)},s.fromDNA=function(t,e){var o=new s;return o.genes=[],o.constants=[],t.map(function(t){var e=s.copyGene(t);o.genes.push(e)}),e.map(function(t){var e=s.copyGene(t);o.constants.push(e)}),o},s.prototype.setDNA=function(t){var o=this;this.genes=[],t.map(function(t){var e=s.copyGene(t);o.genes.push(e)}),this.isKilled=!1},s.prototype.kill=function(){this.isKilled=!0},s.copyGene=function(t){return Object.assign(Object.create(Object.getPrototypeOf(t)),t)},s.prototype.mutate=function(){var t=Math.floor(Math.random()*this.genes.length);this.genes[t].mutate()},s.prototype.getFitness=function(){return this.fitness},s.prototype.getLength=function(){return this.genes.length},s.prototype.getGenes=function(){return this.genes},s.prototype.getConstants=function(){return this.constants},s}();e.Chromosome=s},function(t,e,o){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var s=function(){function t(t){this.factor=t,this.value=Math.random()}return t.prototype.get=function(){return this.value*this.factor},t.prototype.mutate=function(){this.value=Math.random()},t}();e.Gene=s},function(t,e,o){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var s,n,i=function(){function s(){}return s.dispatch=function(e,o){void 0===o&&(o={}),s.listeners.map(function(t){t.eventType===e&&t.callback(o)})},s.on=function(t,e){s.listeners.push({eventType:t,callback:e})},s.listeners=[],s}();e.GenomeEvent=i,(n=s||(s={})).GENOME_EVENT_GENERATION_BEGIN="GENOME_EVENT_GENERATION_BEGIN",n.GENOME_EVENT_POPULATION_CREATED="GENOME_EVENT_POPULATION_CREATED",n.GENOME_EVENT_GENERATION_END="GENOME_EVENT_GENERATION_END",n.GENOME_EVENT_GENERATION_FINISH="GENOME_EVENT_GENERATION_FINISH",e.GenomeEventType=s},function(t,e,o){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var s=o(4);e.Population=s.Population;var n=o(0);e.Chromosome=n.Chromosome;var i=o(1);e.Gene=i.Gene;var r=o(5);e.Blueprint=r.Blueprint;var u=o(2);e.GenomeEvent=u.GenomeEvent,e.GenomeEventType=u.GenomeEventType},function(t,e,o){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var s=o(0),n=o(2),i=function(){function t(t,e){this.index=0,this.size=t,this.blueprint=e,this.chromosomes=[],this.bestChromosome=new s.Chromosome,this.sumFitness=0,this.mutationRate=.01,this.stopAt=null,this.cutOff=.3,this.initializeChromosomes()}return t.prototype.initializeChromosomes=function(){for(var t=0;t<this.size;t+=1){var e=new s.Chromosome(this.blueprint);this.chromosomes.push(e)}n.GenomeEvent.dispatch(n.GenomeEventType.GENOME_EVENT_POPULATION_CREATED,this.chromosomes)},t.prototype.setFitnessCalculation=function(t){this.fitnessCalculation=t},t.prototype.setStopAt=function(t){this.stopAt=t},t.prototype.sortChromosomes=function(){this.chromosomes=this.chromosomes.sort(function(t,e){return t.getFitness()>e.getFitness()?-1:1})},t.prototype.selectBestChromosomes=function(){var e=this,t=Math.floor(this.chromosomes.length*this.cutOff),o=this.chromosomes.slice(this.chromosomes.length-t),s=this.chromosomes.slice(0,this.chromosomes.length-t+1);o.map(function(t){t.kill()}),this.sumFitness=0,s.map(function(t){e.sumFitness+=t.getFitness()})},t.prototype.crossoverChromosomes=function(){for(var t=this.chromosomes.filter(function(t){return t.isKilled}),e=this.chromosomes.filter(function(t){return!t.isKilled}),o=0;o<t.length;o+=1){for(var s=null,n=null;s=this.getRandomChromosome(e),n=this.getRandomChromosome(e),!s||!n;);if(s&&n){var i=Math.floor(Math.random()*s.getLength()),r=s.getGenes().slice(0,i),u=n.getGenes().slice(i);t[o].setDNA(r.concat(u))}}},t.prototype.setMutationRate=function(t){this.mutationRate=t},t.prototype.setCutOff=function(t){this.cutOff=t},t.prototype.mutateChromosomes=function(){var e=this;this.chromosomes.map(function(t){Math.random()<e.mutationRate&&t.mutate()})},t.prototype.getRandomChromosome=function(t){for(var e=Math.random()*this.sumFitness,o=0,s=0,n=t;s<n.length;s++){var i=n[s];if(e<(o+=i.getFitness()))return i}return null},t.prototype.shuffleChromosomes=function(){this.chromosomes=this.chromosomes.sort(function(t,e){return Math.random()-.5})},t.prototype.keepBestChromosome=function(){this.bestChromosome?this.bestChromosome.getFitness()<this.chromosomes[0].getFitness()&&(this.bestChromosome=this.copyChromosome(this.chromosomes[0])):this.bestChromosome=this.copyChromosome(this.chromosomes[0])},t.prototype.copyChromosome=function(t){return Object.assign(Object.create(Object.getPrototypeOf(t)),t)},t.prototype.run=function(t){if(void 0===t&&(t=1),!this.fitnessCalculation)throw new Error("You must specify a fitness calculation function using 'setFitnessCalculation'.");if(!this.blueprint)throw new Error("You must specify a blueprint to design your chromosomes.");for(var e=0;e<t&&(this.index+=1,this.process(),!(this.stopAt&&this.bestChromosome.getFitness()>=this.stopAt));e+=1);n.GenomeEvent.dispatch(n.GenomeEventType.GENOME_EVENT_GENERATION_FINISH,this.chromosomes)},t.prototype.getGenerationNumber=function(){return this.index},t.prototype.getBestChromosome=function(){return this.bestChromosome},t.prototype.process=function(){var e=this;n.GenomeEvent.dispatch(n.GenomeEventType.GENOME_EVENT_GENERATION_BEGIN,this.chromosomes),this.shuffleChromosomes(),this.chromosomes.map(function(t){t.computeFitness(e.fitnessCalculation)}),this.sortChromosomes(),this.selectBestChromosomes(),this.crossoverChromosomes(),this.mutateChromosomes(),this.keepBestChromosome(),n.GenomeEvent.dispatch(n.GenomeEventType.GENOME_EVENT_GENERATION_END,this.chromosomes)},t}();e.Population=i},function(t,e,o){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var s=function(){function t(){this.properties=[],this.constants=[]}return t.prototype.add=function(t,e){void 0===e&&(e=1);for(var o=0;o<e;o+=1)this.properties.push(t)},t.prototype.addConstant=function(t,e){void 0===e&&(e=1);for(var o=0;o<e;o+=1)this.constants.push(t)},t.prototype.getProperties=function(){return this.properties},t.prototype.getConstants=function(){return this.constants},t}();e.Blueprint=s}])});