-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDNA.js
More file actions
35 lines (31 loc) · 825 Bytes
/
DNA.js
File metadata and controls
35 lines (31 loc) · 825 Bytes
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
// José Bezerra - 21/07/2018
// josebezerraneto@outlook.com
function DNA() {
this.genes = [];
for (var i = 0; i < lifespan; i++) {
this.genes[i] = p5.Vector.random2D();
this.genes[i].setMag(0.2);
}
}
DNA.prototype.crossover = function (partner) {
var child = new DNA();
var mid = random(this.genes.length);
for (var i = 0; i < this.genes.length; i++) {
if (i > mid) {
child.genes[i] = this.genes[i];
}
else {
child.genes[i] = partner.genes[i];
}
}
child.mutation();
return child;
}
DNA.prototype.mutation = function () {
for (var i = 0; i < this.genes.length; i++) {
if (random(1) < mutRate) {
this.genes[i] = p5.Vector.random2D();
this.genes[i].setMag(0.2);
}
}
}