-
Notifications
You must be signed in to change notification settings - Fork 0
/
population.js
42 lines (38 loc) · 1.41 KB
/
population.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const DNA = require('./dna');
class Population{
constructor(size, customers, products, totals, prices,toUse, mutationRate){
this.dnas = new Array(size);
this.mutationRate = mutationRate;
for(let i = 0; i < size; i++){
this.dnas[i] = new DNA(customers,products,totals,prices,toUse);
}
}
selection(){
let matingPool = [];
this.dnas.sort((a,b)=> {
return a.fitness() < b.fitness()
})
let fitnessOffset = this.dnas[this.dnas.length - 1].fitness();
for(let i = 0; i < this.dnas.length; i++){
let fitness = this.dnas[i].fitness();
fitness -= fitnessOffset;
let n = Math.pow(2, fitness/100)
// let n = Math.pow(fitness/100, 5)
// let n = Math.pow(2,Math.round(fitness/fitnessOffset))
for(let j = 0; j < n ; j++){
matingPool.push(this.dnas[i]);
}
}
if(matingPool.length === 0){
matingPool = this.dnas;
}
for(let i = 0; i < this.dnas.length; i++) {
let parentA = matingPool[Math.floor(Math.random() * matingPool.length)]
let parentB = matingPool[Math.floor(Math.random() * matingPool.length)]
let child = parentA.crossover(parentB);
child.mutate(this.mutationRate);
this.dnas[i] = child;
}
}
}
module.exports = Population;