-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrain.pde
More file actions
executable file
·46 lines (39 loc) · 1.52 KB
/
Brain.pde
File metadata and controls
executable file
·46 lines (39 loc) · 1.52 KB
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
class Brain {
// series of vectors which get the dot to the goal (hopefully)
PVector[] directions;
int step = 0;
Brain(int size) {
directions = new PVector[size];
randomize();
}
//--------------------------------------------------------------------------------------------------------------------------------
//sets all the vectors in directions to a random vector with length 1
void randomize() {
for (int i = 0; i< directions.length; i++) {
float randomAngle = random(2*PI);
directions[i] = PVector.fromAngle(randomAngle);
}
}
//-------------------------------------------------------------------------------------------------------------------------------------
//returns a perfect copy of this brain object
Brain clone() {
Brain clone = new Brain(directions.length);
for (int i = 0; i < directions.length; i++) {
clone.directions[i] = directions[i].copy();
}
return clone;
}
//----------------------------------------------------------------------------------------------------------------------------------------
//mutates the brain by setting some of the directions to random vectors
void mutate() {
float mutationRate = 0.02;//chance that any vector in directions gets changed
for (int i =0; i< directions.length; i++) {
float rand = random(1);
if (rand < mutationRate) {
//set this direction as a random direction
float randomAngle = random(2*PI);
directions[i] = PVector.fromAngle(randomAngle);
}
}
}
}