-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhello.js
90 lines (69 loc) · 2.33 KB
/
hello.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
// https://github.com/subprotocol/genetic-js/blob/master/examples/string-solver.html
const Genetic = require('genetic-js');
const genetic = Genetic.create();
genetic.optimize = Genetic.Optimize.Maximize;
genetic.select1 = Genetic.Select1.Tournament2;
genetic.select2 = Genetic.Select2.Tournament2;
genetic.seed = function() {
const randomString = len => {
var text = "";
var charset = "abcdefghijklmnopqrstuvwxyz0123456789";
for (let i=0; i<len; i++) {
text += charset.charAt(Math.floor(Math.random() * charset.length));
}
return text;
};
return randomString(this.userData.solution.length);
}
genetic.mutate = function(entity) {
const replaceAt = (str, index, replacement) => {
return str.substr(0, index) + replacement + str.substr(index + replacement.length);
};
let index = Math.floor(Math.random() * entity.length);
const direction = Math.floor(Math.random() * 2);
return replaceAt(entity, index, String.fromCharCode(entity.charCodeAt(index) + (direction ? 1 : -1)));
}
genetic.crossover = function(parent1, parent2) {
let index = Math.floor(Math.random() * parent1.length);
const parent1Left = parent1.substr(0, index);
const parent1Right = parent1.substr(index);
const parent2Left = parent2.substr(0, index);
const parent2Right = parent2.substr(index);
// Crossover the left or right side.
let direction = Math.floor(Math.random() * 2);
let child1 = '';
let child2 = '';
if (direction === 0) {
child1 = parent1Left + parent2Right;
child2 = parent2Left + parent1Right;
}
else {
child1 = parent1Right + parent2Left;
child2 = parent2Right + parent1Left;
}
return [child1, child2];
}
genetic.fitness = function(entity) {
let score = 0;
for (let i=0; i<this.userData.solution.length; i++) {
score += 256 - Math.abs(entity.charCodeAt(i) - this.userData.solution.charCodeAt(i));
}
return score;
}
genetic.generation = function(pop, generation, stats) {
return pop[0].fitness < this.userData.solution.length * 256;
}
genetic.notification = function(pop, generation, stats, isDone) {
const value = pop[0].entity;
console.log(`Generation ${generation}`);
console.log(`Best fitness: ${value}`);
}
genetic.evolve({
iterations: 100000,
size: 100,
crossover: 0.3,
mutation: 0.3,
skip: 10 /* frequency for notifications */
}, {
solution: 'Hello World'
})