-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrain.pde
More file actions
36 lines (32 loc) · 821 Bytes
/
Brain.pde
File metadata and controls
36 lines (32 loc) · 821 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
36
class Brain {
PVector[] directions;
int step = 0;
float mutationRate;
Brain(int size, float mutationRate) {
this.mutationRate = mutationRate;
directions = new PVector[size];
randomize();
}
void randomize() {
for (int i = 0; i < directions.length; i++) {
float randomAngle = random(2*PI);
directions[i] = PVector.fromAngle(randomAngle);
}
}
Brain clone() {
Brain clone = new Brain(directions.length, mutationRate);
for (int i = 0; i < directions.length; i++) {
clone.directions[i] = directions[i].copy();
}
return clone;
}
void mutate() {
for (int i = 1; i < directions.length; i++) {
float rand = random(1);
if (rand < mutationRate) {
float randomAngle = random(2*PI);
directions[i] = PVector.fromAngle(randomAngle);
}
}
}
}