-
Notifications
You must be signed in to change notification settings - Fork 0
/
gene.js
36 lines (29 loc) · 831 Bytes
/
gene.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
function Gene(depth) {
this.depth = depth;
this.angles = [];
for (let i = 0; i < depth; i++) {
for (let j = 0; j < pow(2, i); j++) {
this.angles.push({
r: random(0, TWO_PI),
l: random(0, TWO_PI)
});
}
}
}
Gene.prototype.mutate = function() {
const index = floor(random(this.angles.length))
const a = Object.assign({}, this.angles[index]);
if (random() > 0.5) {
a.r = random(0, TWO_PI);
} else {
a.l = random(0, TWO_PI);
}
this.angles[index] = a;
}
Gene.prototype.randomIndexPath = function() {
const i1 = floor(random(this.angles.length));
const i2 = i1 + floor(random(this.angles.length - i1))
const ip1 = { index: i1, path: floor(random(2)) > 0 ? 'r' : 'l' }
const ip2 = { index: i2, path: floor(random(2)) > 0 ? 'r' : 'l' }
return {ip1, ip2};
}