Skip to content

Commit

Permalink
implement ga
Browse files Browse the repository at this point in the history
  • Loading branch information
U1F30C committed Nov 25, 2020
1 parent e453e54 commit 8995b2e
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 11 deletions.
80 changes: 69 additions & 11 deletions ga.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,79 @@
const { maxBy, random, times, sample, sampleSize } = require('lodash');

class Individual {
constructor(dimentions, min, max) {
const range = max - min;
this.genome = Array.from(Array(dimentions)).map(
() => min + range * Math.random
);
constructor(genome) {
this.fitness = 0;
this.genome = genome;
}
}

class GeneticAlgorithm {
constructor(populationSize, dimentions) {
this.populate(populationSize, dimentions);
constructor(populationSize, problem, mutationRate) {
this.mutationRate = mutationRate;
this.problem = problem;
this.populate(populationSize, problem);
}
evolve() {}
populate(populationSize, dimentions) {
this.population = Array.from(Array(populationSize)).map(
() => new Individual(dimentions)
populate(populationSize, problem) {
const { dimentions, min, max } = problem;
this.population = times(
populationSize,
() => new Individual(times(dimentions, () => random(min, max, true))),
);
}
evolve() {
//calculate fitness before this
const currentMostFit = maxBy(this.population, i => i.fitness);
if (!this.mostFit || currentMostFit.fitness > this.mostFit.fitness)
this.mostFit = currentMostFit;
this.population = this.population.sort((a, b) => a.fitness - b.fitness);
let offspring = [];
while (offspring.length < this.population.length) {
const parents = sampleSize(this.population.slice(this.population.length / 2), 2);
const children = this.breed(parents);

offspring = offspring.concat(children);
}
this.mutate();
this.population = offspring;
}
breed(parents) {
const breedingPoint = random(1, this.problem.dimentions);
// const genome = [];
// for (let i = 0; i < this.problem.dimentions; i++) {
// genome.push(sample(parents).genome[i]);
// }
const genome1 = parents[0].genome
.slice(0, breedingPoint)
.concat(parents[0].genome.slice(breedingPoint));
const genome2 = parents[1].genome
.slice(0, breedingPoint)
.concat(parents[0].genome.slice(breedingPoint));
return [new Individual(genome1), new Individual(genome2)];
}
mutate() {
this.population.forEach(individual => {
individual.genome = individual.genome.map(gene => {
if (Math.random() < this.mutationRate) {
return random(this.problem.min, this.problem.max, true);
} else return gene;
});
});
}
}

function fitness(genome) {
// let sum = 0;
// genome.forEach(gene => (sum -= gene ** 2));
// return sum;
let z = genome.length * 10;
genome.forEach(gene => {
z = z + gene ** 2 - 10 * Math.cos(2 * Math.PI * gene);
});
return -z;
}
const ga = new GeneticAlgorithm(32, { min: -5.12, max: 5.12, dimentions: 8 }, 0.2);
for (let i = 0; i < 20000; i++) {
ga.population.forEach(individual => (individual.fitness = fitness(individual.genome)));
ga.evolve();
console.log(-ga.mostFit.fitness);
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"lint:staged": "lint-staged --relative"
},
"dependencies": {
"lodash": "^4.17.20",
"phaser": "^3.24.1"
},
"devDependencies": {
Expand Down

0 comments on commit 8995b2e

Please sign in to comment.