Skip to content

Commit

Permalink
Finish crossover, add mutation and multiples processes
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Riva committed Mar 27, 2019
1 parent f6d09eb commit c5ba305
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
26 changes: 24 additions & 2 deletions src/core/Chromosome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import { Blueprint } from './Blueprint';
export class Chromosome {
private genes: Gene[];
private fitness: number;
constructor(blueprint: Blueprint) {
constructor(blueprint: Blueprint | null = null) {
this.genes = [];
this.fitness = 0;
this.initializeGenes(blueprint);

if (blueprint) {
this.initializeGenes(blueprint);
}
}

initializeGenes(blueprint: Blueprint) {
Expand All @@ -22,7 +25,26 @@ export class Chromosome {
this.fitness = fitnessCalculation(this.genes);
}

static fromDNA(genes: Gene[]) {
const chromosome = new Chromosome();
chromosome.genes = genes;
return chromosome;
}

mutate() {
const pivot = Math.floor(Math.random() * this.genes.length);
this.genes[pivot].mutate();
}

getFitness() {
return this.fitness;
}

getLength() {
return this.genes.length;
}

getGenes() {
return this.genes;
}
}
4 changes: 4 additions & 0 deletions src/core/Gene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ export class Gene {
get() {
return this.value * this.factor;
}

mutate() {
this.value = Math.random();
}
}
35 changes: 31 additions & 4 deletions src/core/Population.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Chromosome } from './Chromosome';
import { Blueprint } from './Blueprint';
import { Gene } from './Gene';

export class Population {
private size: number;
Expand Down Expand Up @@ -47,11 +48,28 @@ export class Population {
for (let i = this.chromosomes.length; i < this.size; i += 1) {
const chromosomeA = this.getRandomChromosome();
const chromosomeB = this.getRandomChromosome();
console.log('A', chromosomeA);
console.log('B', 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);
} else {
console.error('Should not happen');
}
}
}

mutateChromosones() {
const mutationRate = 0.01;
this.chromosomes.map((chromosome: Chromosome) => {
if (Math.random() < mutationRate) {
chromosome.mutate();
}
});
}

getRandomChromosome() {
const random = Math.random() * this.sumFitness;
let sumFitness = 0;
Expand All @@ -64,8 +82,16 @@ export class Population {
return null;
}

run() {
this.process();
run(times: number = 1) {
for (let i = 0; i < times; i += 1) {
this.process();
console.log(`Generation ${i}: ${this.chromosomes[0].getFitness()}`);
}
let finalString = '';
this.chromosomes[0].getGenes().map((gene: Gene) => {
finalString += String.fromCharCode(gene.get() + 97);
});
console.log(`Result: ${finalString}`);
}

process() {
Expand All @@ -76,5 +102,6 @@ export class Population {
this.sortChromosomes();
this.selectBestChromosomes();
this.crossoverChromosomes();
this.mutateChromosones();
}
}
4 changes: 2 additions & 2 deletions src/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ population.setFitnessCalculation((genes: Gene[]) => {
sum += Math.abs(charCode - geneCharCode);
}

return 1 / (sum * sum);
return 1 / sum;
});

population.run();
population.run(1000);

0 comments on commit c5ba305

Please sign in to comment.