diff --git a/draft b/draft new file mode 100644 index 0000000..9c2278d --- /dev/null +++ b/draft @@ -0,0 +1,14 @@ +const genesO = 'hellaworld'; +const genes: number[] = []; + +for (let i = 0; i < genesO.length; i += 1) { + genes.push(genesO.charCodeAt(i) - 97); +} + +for (let i = 0; i < genes.length; i += 1) { + const charCode = answer.charCodeAt(i) - 97; + const geneCharCode = Math.floor(genes[i]); + sum += Math.abs(charCode - geneCharCode); +} + +console.log(sum); \ No newline at end of file diff --git a/src/core/Population.ts b/src/core/Population.ts index a7c39d4..1b822a1 100644 --- a/src/core/Population.ts +++ b/src/core/Population.ts @@ -35,7 +35,7 @@ export class Population { } selectBestChromosomes() { - const pivot = Math.floor(this.size / 3); + const pivot = Math.floor(this.chromosomes.length / 3); this.chromosomes.splice(2 * pivot); this.sumFitness = 0; @@ -45,20 +45,27 @@ export class Population { } crossoverChromosomes() { + let newChromosomes = []; for (let i = this.chromosomes.length; i < this.size; i += 1) { - const chromosomeA = this.getRandomChromosome(); - const chromosomeB = this.getRandomChromosome(); + let chromosomeA = null; + let chromosomeB = null; + + do { + chromosomeA = this.getRandomChromosome(); + chromosomeB = this.getRandomChromosome(); + } while (!chromosomeA || !chromosomeB); if (chromosomeA && chromosomeB) { const pivot = Math.floor(Math.random() * chromosomeA.getLength()); const genesA = chromosomeA.getGenes().slice(0, pivot); const genesB = chromosomeB.getGenes().slice(pivot); const newChromosome = Chromosome.fromDNA([...genesA, ...genesB]); - this.chromosomes.push(newChromosome); + newChromosomes.push(newChromosome); } else { - console.error('Should not happen'); + // console.error('Should not happen'); } } + this.chromosomes = [...this.chromosomes, ...newChromosomes]; } mutateChromosones() { @@ -85,7 +92,7 @@ export class Population { run(times: number = 1) { for (let i = 0; i < times; i += 1) { this.process(); - console.log(`Generation ${i}: ${this.chromosomes[0].getFitness()}`); + console.log(`Generation ${i}: ${this.chromosomes[0].getFitness()} (remaining: ${this.chromosomes.length})`); } let finalString = ''; this.chromosomes[0].getGenes().map((gene: Gene) => { diff --git a/src/example.ts b/src/example.ts index 6ce5e85..e1039d9 100644 --- a/src/example.ts +++ b/src/example.ts @@ -2,23 +2,23 @@ import { Population } from './core/Population'; import { Blueprint } from './core/Blueprint'; import { Gene } from './core/Gene'; -const answer = 'helloworld'; +const answer = 'helloworldhowareyoutoday'; const blueprint = new Blueprint(); blueprint.add(26, answer.length); -const population = new Population(10, blueprint); +const population = new Population(500, blueprint); population.setFitnessCalculation((genes: Gene[]) => { - let sum = 0; + let sum = 1; for (let i = 0; i < genes.length; i += 1) { const charCode = answer.charCodeAt(i) - 97; const geneCharCode = Math.floor(genes[i].get()); - sum += Math.abs(charCode - geneCharCode); + sum += charCode === geneCharCode ? 1 : 0; } - return 1 / sum; + return sum / genes.length; }); population.run(1000);