Skip to content

Commit

Permalink
Add variable mutationRate
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Riva committed Mar 28, 2019
1 parent d0e06e9 commit 9936efa
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/core/Population.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export class Population {
private blueprint: Blueprint;
private chromosomes: Chromosome[];
private sumFitness: number;
private mutationRate: number;

private fitnessCalculation: any;
private render: any;
Expand All @@ -16,6 +17,7 @@ export class Population {
this.blueprint = blueprint;
this.chromosomes = [];
this.sumFitness = 0;
this.mutationRate = 0.01;
this.initializeChromosomes();
}

Expand Down Expand Up @@ -74,10 +76,13 @@ export class Population {
this.chromosomes = [...this.chromosomes, ...newChromosomes];
}

setMutationRate(mutationRate: number) {
this.mutationRate = mutationRate;
}

mutateChromosones() {
const mutationRate = 0.01;
this.chromosomes.map((chromosome: Chromosome) => {
if (Math.random() < mutationRate) {
if (Math.random() < this.mutationRate) {
chromosome.mutate();
}
});
Expand Down
1 change: 1 addition & 0 deletions src/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const blueprint = new Blueprint();
blueprint.add(26, answer.length);

const population = new Population(200, blueprint);
population.setMutationRate(0.01);

population.setFitnessCalculation((genes: Gene[]) => {
let sum = 1; // Avoid to have 0 on fitness
Expand Down

0 comments on commit 9936efa

Please sign in to comment.