forked from Code-Bullet/Flappy-Bird-AI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Node.js
71 lines (66 loc) · 2.86 KB
/
Node.js
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
class Node {
constructor(no) {
this.number = no;
this.inputSum = 0; //current sum i.e. before activation
this.outputValue = 0; //after activation function is applied
this.outputConnections = []; //new ArrayList<connectionGene>();
this.layer = 0;
this.drawPos = createVector();
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//the node sends its output to the inputs of the nodes its connected to
engage() {
if(this.layer != 0) { //no sigmoid for the inputs and bias
this.outputValue = this.sigmoid(this.inputSum);
}
for(var i = 0; i < this.outputConnections.length; i++) { //for each connection
if(this.outputConnections[i].enabled) { //dont do shit if not enabled
this.outputConnections[i].toNode.inputSum += this.outputConnections[i].weight * this.outputValue; //add the weighted output to the sum of the inputs of whatever node this node is connected to
}
}
}
//----------------------------------------------------------------------------------------------------------------------------------------
//not used
stepFunction(x) {
if(x < 0) {
return 0;
} else {
return 1;
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//sigmoid activation function
sigmoid(x) {
return 1.0 / (1.0 + pow(Math.E, -4.9 * x)); //todo check pow
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------
//returns whether this node connected to the parameter node
//used when adding a new connection
isConnectedTo(node) {
if(node.layer == this.layer) { //nodes in the same this.layer cannot be connected
return false;
}
//you get it
if(node.layer < this.layer) {
for(var i = 0; i < node.outputConnections.length; i++) {
if(node.outputConnections[i].toNode == this) {
return true;
}
}
} else {
for(var i = 0; i < this.outputConnections.length; i++) {
if(this.outputConnections[i].toNode == node) {
return true;
}
}
}
return false;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//returns a copy of this node
clone() {
var clone = new Node(this.number);
clone.layer = this.layer;
return clone;
}
}