-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathga.js
131 lines (116 loc) · 2.99 KB
/
ga.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const password = "letmeseeyouguessthispassword"
const length = password.length;
const elite = 50;
const random = 50;
const numberOfChildren = 5;
const totalPopulation= 500;
const mutation = 5;
const numberOfGenerations = 100;
function fitness(individual) {
if(individual.length != password.length){
return "Incompatible"
}
var score = 0;
for(i = 0; i< individual.length; i++){
if(individual[i] == password[i]){
score++;
}
}
return (score/individual.length) * 100
}
function generateIndividuals(){
var alphabets = "qwertyuioplkjhgfdsazxcvbnm";
var word = '';
for(i = 0; i< length; i++){
word += alphabets[Math.floor(Math.random() * 26)]
}
return word;
}
function generatePopulation(size){
let population = [];
for(p = 0; p < size; p++){
population.push(generateIndividuals());
}
console.log("Initial Population " , population)
return population;
}
function elitePopulation(population){
let elitePopulationHash = {}
population.map(function(individual){
elitePopulationHash[individual] = fitness(individual)
return elitePopulationHash;
})
//console.log('Elite HASH: ', elitePopulationHash);
elitePop = Object.keys(elitePopulationHash).sort(function(a,b){return elitePopulationHash[b]-elitePopulationHash[a]})
//console.log("Best in this Generation: ", elitePop[0]);
return elitePop
}
function computeBreeders(elitePopulation){
let nextGen = []
for(j = elite + random; j> 0; j--){
var index = Math.floor(Math.random() * elitePopulation.length * 0.55);
nextGen.push(elitePopulation[index]);
}
//console.log('NEXTGEN ', nextGen)
return nextGen;
}
function crossingOver(m , n){
var x = "";
var y = "";
for(k=0; k< m.length; k++) {
if(Math.random() < 0.60){
x += n[k]
y += m[k]
}else{
x += m[k]
y += n[k]
}
}
return [x, y];
}
function createChildren(breeders){
let nextGenPopulation = [];
for(z = 0; z< breeders.length; z++){
for(a = 0; a< numberOfChildren; a++){
nextGenPopulation.push(...crossingOver(breeders[z], breeders[Math.floor(Math.random()*breeders.length)]));
}
}
return nextGenPopulation;
}
function mutate(population){
var newPop = [];
population.map((individual) => {
if(Math.random() < mutation/100 ){
newPop.push(mutateIndividual(individual));
}else{
newPop.push(individual);
}
})
return newPop;
}
function mutateIndividual(individual){
var mutInd = ""
let alphabets = "abcdefghijklmnopqrstuvwxyz";
for(h=0; h< individual.length; h++){
if(Math.random() > 0.2){
mutInd += alphabets[Math.floor(Math.random() * 26)]
}else{
mutInd += individual[h]
}
}
return mutInd;
}
function nextGen(currentGen){
elitePop = elitePopulation(currentGen);
breeders = computeBreeders(elitePop);
nextGeneration = createChildren(breeders);
return mutate(nextGeneration);
}
var current = generatePopulation(totalPopulation)
for(s = 0; s< numberOfGenerations; s++){
newGeneration = nextGen(current);
current = newGeneration;
if(s == numberOfGenerations - 1){
console.log("GA ran for ", s , " generations: ", elitePopulation(current)[0])
}
}