-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenome.pde
More file actions
119 lines (106 loc) · 3.25 KB
/
Copy pathGenome.pde
File metadata and controls
119 lines (106 loc) · 3.25 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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
class Genome
{
Node [] inputNodes = new Node [5];
Node [] layer1 = new Node [8];
Node [] outputNodes = new Node [4];
void setupNodes(){
for(int i = 0; i < inputNodes.length; i++){
Node test = new Node();
inputNodes[i] = test;
}
for(int i = 0; i < layer1.length; i++){
Node test = new Node();
test.bias(random(10.0) - 5);
for(int j = 0; j < inputNodes.length; j++){
Connection dummy = new Connection();
dummy.weight = random(10.0) - 5;
dummy.fromNode = inputNodes[j];
dummy.toNode = test;
test.connections.add(dummy);
}
layer1[i] = test;
}
for(int i = 0; i < outputNodes.length; i++){
Node test = new Node();
test.bias(random(10.0) - 5);
for(int j = 0; j < layer1.length; j++){
Connection dummy = new Connection();
dummy.weight = random(10.0) - 5;
dummy.fromNode = layer1[j];
dummy.toNode = test;
test.connections.add(dummy);
}
outputNodes[i] = test;
}
}
void setGenes(float px, float py, float bx, float by, float cooldown){
inputNodes[0].value = (px - 800) / 100;
inputNodes[1].value = (py - 500) / 100;
inputNodes[2].value = (bx - 800) / 100;
inputNodes[3].value = (by - 500) / 100;
inputNodes[4].value = (cooldown - 50) / 10;
for(int i = 0; i < inputNodes.length; i++){
inputNodes[i].value = inputNodes[i].sigmoid(inputNodes[i].value);
}
for(int i = 0; i < layer1.length; i++){
layer1[i].initialize();
}
for(int i = 0; i < outputNodes.length; i++){
outputNodes[i].initialize();
}
}
void mutate()
{
for(int i = 0; i < layer1.length; i++){
if(random(100) > 80){
layer1[i].mutateBias();}
for(int j= 0; j < layer1[i].connections.size(); j ++){
if(random(100) > 80){
layer1[i].connections.get(j).mutateWeight();
}
}
}
for(int i = 0; i < outputNodes.length; i++){
if(random(100) > 80){
outputNodes[i].mutateBias();}
for(int j= 0; j < outputNodes[i].connections.size(); j ++){
if(random(100) > 80){
outputNodes[i].connections.get(j).mutateWeight();
}
}
}
}
void mergeGene(Player p1, Player p2)
{
for(int i = 0; i < layer1.length; i++){
if(random(2) > 1)
{
layer1[i].bias = p1.genes.layer1[i].bias;
}
else
{layer1[i].bias = p2.genes.layer1[i].bias;}
for(int j= 0; j < layer1[i].connections.size(); j ++){
if(random(2) > 1){
layer1[i].connections.set(j, p1.genes.layer1[i].connections.get(j));
}
else
{layer1[i].connections.set(j, p2.genes.layer1[i].connections.get(j)); }
}
}
for(int i = 0; i < outputNodes.length; i++){
if(random(2) > 1)
{
outputNodes[i].bias = p1.genes.outputNodes[i].bias;
}
else
{outputNodes[i].bias = p2.genes.outputNodes[i].bias;}
for(int j= 0; j < outputNodes[i].connections.size(); j ++){
if(random(2) > 1){
outputNodes[i].connections.set(j, p1.genes.outputNodes[i].connections.get(j));
}
else
{outputNodes[i].connections.set(j, p1.genes.outputNodes[i].connections.get(j)); }
}
}
}
}