-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdna.js
More file actions
34 lines (31 loc) · 767 Bytes
/
dna.js
File metadata and controls
34 lines (31 loc) · 767 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
function DNA(genes) {
if (genes) {
this.genes = genes;
} else {
this.genes = [];
for (var i = 0; i < lifespan; i++) {
this.genes[i] = p5.Vector.random2D();
this.genes[i].setMag(maxforce);
}
}
this.crossover = function (partner) {
var newgen = [];
var idx = floor(this.genes.length / 2);
for (var i = 0; i < this.genes.length; i++) {
if (i > idx) {
newgen[i] = partner.genes[i];
} else {
newgen[i] = this.genes[i];
}
}
return new DNA(newgen);
};
this.mutation = function () {
for (var i = 0; i < this.genes.length; i++) {
if (random(i) < mutationRate) {
this.genes[i] = p5.Vector.random2D();
this.genes[i].setMag(maxforce);
}
}
};
}