Skip to content

Commit

Permalink
Fix fitness calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Riva committed Mar 27, 2019
1 parent c5ba305 commit a9df1f4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
14 changes: 14 additions & 0 deletions draft
Original file line number Diff line number Diff line change
@@ -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);
19 changes: 13 additions & 6 deletions src/core/Population.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand All @@ -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) => {
Expand Down
10 changes: 5 additions & 5 deletions src/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit a9df1f4

Please sign in to comment.