-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdna.js
78 lines (73 loc) · 2.33 KB
/
dna.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
//jscs:disable
class DNA {
constructor() {
this.gene = [];
this.fitness = 0.01;
this.length = 7*100;
for (var i = 0; i < this.length; i++) {
//order: [R, G, B, alpha, x, y, radius]
// for RGB Values
if (i%7 == 0 || i%7 == 1 || i%7 == 2 ) {
this.gene[i] = Math.floor(Math.random() * (256));
} else if (i%7 == 3) {
// alpha value
this.gene[i] = Math.random();
} else if (i%7 == 4){
// x value: 0-300
this.gene[i] = Math.floor(Math.random() * (300))+1;
} else if (i%7 == 5){
// y value: 300-600
this.gene[i] = Math.floor(Math.random() * (300))+300;
} else{
// radius value: 0-150
this.gene[i] = Math.floor(Math.random() * (150));
}
}
}
calcFitness(width, height) {
loadPixels();
var d = pixelDensity();
for (var i = 0; i < width*height*d*d*2; i+=4) {
//Get delta per color
var deltaRed = pixels[i] - pixels[i+width*height*d*d*2];
var deltaGreen = pixels[i+1] - pixels[i+1+width*height*d*d*2];
var deltaBlue = pixels[i+2] - pixels[i+2+width*height*d*d*2];
// measure the distance between the colors in 3D space
var pixelFitness = deltaRed * deltaRed +
deltaGreen * deltaGreen +
deltaBlue * deltaBlue;
//add the pixel fitness to the total fitness ( lower is better )
this.fitness += pixelFitness;
}
this.fitness = 1/Math.log(this.fitness);
}
crossover(partner) {
var child = this;
for (var i = 0; i < this.length/2; i++) {
child.gene[i] = partner.gene[i];
}
return child;
}
mutate(mutation_rate){
for (var i = 0; i < this.gene.length; i++){
if (Math.random() < mutation_rate){
// for RGB Values
if (i%7 == 0 || i%7 == 1 || i%7 == 2 ) {
this.gene[i] = Math.floor(Math.random() * (256));
} else if (i%7 == 3) {
// alpha value
this.gene[i] = Math.random();
} else if (i%7 == 4){
// x values: 0 - 300
this.gene[i] = Math.floor(Math.random() * (300))+1;
} else if (i%7 == 5){
// y value: 300-600
this.gene[i] = Math.floor(Math.random() * (300))+300;
} else{
// radius
this.gene[i] = Math.floor(Math.random() * (150));
}
}
}
}
}