-
Notifications
You must be signed in to change notification settings - Fork 0
/
Node.pde
74 lines (70 loc) · 2.99 KB
/
Node.pde
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
class Node {
int number;
float inputSum = 0;//current sum i.e. before activation
float outputValue = 0; //after activation function is applied
ArrayList<connectionGene> outputConnections = new ArrayList<connectionGene>();
int layer = 0;
PVector drawPos = new PVector();
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//constructor
Node(int no) {
number = no;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//the node sends its output to the inputs of the nodes its connected to
void engage() {
if (layer!=0) {//no sigmoid for the inputs and bias
outputValue = sigmoid(inputSum);
}
for (int i = 0; i< outputConnections.size(); i++) {//for each connection
if (outputConnections.get(i).enabled) {//dont do shit if not enabled
outputConnections.get(i).toNode.inputSum += outputConnections.get(i).weight * outputValue;//add the weighted output to the sum of the inputs of whatever node this node is connected to
}
}
}
//----------------------------------------------------------------------------------------------------------------------------------------
//not used
float stepFunction(float x) {
if (x < 0) {
return 0;
} else {
return 1;
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//sigmoid activation function
float sigmoid(float x) {
float y = 1 / (1 + pow((float)Math.E, -4.9*x));
return y;
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------
//returns whether this node connected to the parameter node
//used when adding a new connection
boolean isConnectedTo(Node node) {
if (node.layer == layer) {//nodes in the same layer cannot be connected
return false;
}
//you get it
if (node.layer < layer) {
for (int i = 0; i < node.outputConnections.size(); i++) {
if (node.outputConnections.get(i).toNode == this) {
return true;
}
}
} else {
for (int i = 0; i < outputConnections.size(); i++) {
if (outputConnections.get(i).toNode == node) {
return true;
}
}
}
return false;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//returns a copy of this node
Node clone() {
Node clone = new Node(number);
clone.layer = layer;
return clone;
}
}