Skip to content

Commit

Permalink
move ga
Browse files Browse the repository at this point in the history
  • Loading branch information
U1F30C committed Nov 26, 2020
1 parent 5344992 commit 7584a5f
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions ga.js → src/ai/ga.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const { maxBy, random, times, sample, sampleSize } = require('lodash');

class Individual {
export class Individual {
constructor(genome) {
this.fitness = 0;
this.genome = genome;
}
}

class GeneticAlgorithm {
constructor(populationSize, problem, mutationRate) {
export class GeneticAlgorithm {
constructor(populationSize, problem, mutationRate = 0.001) {
this.mutationRate = mutationRate;
this.problem = problem;
this.populate(populationSize, problem);
Expand Down Expand Up @@ -58,19 +58,19 @@ class GeneticAlgorithm {
}
}

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);
// 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);

0 comments on commit 7584a5f

Please sign in to comment.