-
Notifications
You must be signed in to change notification settings - Fork 0
/
Network.js
169 lines (153 loc) · 3.85 KB
/
Network.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
this class is the phenotype of an organism,
and gets constructed by the genome, its soul
purpose is to compute an output
*/
class Network {
constructor(inputs, outputs, all) {
this.inputs = inputs; // just input neurons
this.outputs = outputs; // just output neurons
this.all = all; // all neurons, HIDDEN, OUTPUT, BIAS, and INPUT
this.genotype = undefined; // used to match the genome to this network
this.connectionNum = undefined; // number of all connections
this.nodeNum = undefined; // number of all nodes
this.depth = -1; // number of nodes inputs have to travel through to get to the output nodes
this.initBias(); // initialize the bias
}
/*
initializes the bias node to be activated,
makes it so the user doesn't have to manualy
activate it
*/
initBias() {
this.inputs.forEach(node => {
if(node.placement === nodePlaces.BIAS) {
node.sensorLoad(1);
}
});
return this;
}
/*
this resets the network,
uses recursive function that
flushes backwards through the whole
network starting at the output nodes
*/
flush() {
this.outputs.forEach(node => node.flushBack());
return this;
}
/*
activates the network till
all outputs are active, simulates
one time step of activation, it uses
recursive functions from the node class
to go through the entire network cleanly
*/
activate() {
this.inputs.forEach(node => node.feedForward());
this.all.forEach(node => {
if(node.type !== nodeTypes.SENSOR && node.activeFlag === true) {
node.activate();
}
node.activesum = 0;
node.activeFlag = false;
node.visited = false;
});
return this;
}
/*
adds input neuron to the input list,
assuming the node is an input neuron
*/
addInput(node) {
this.inputs.push(node);
return this;
}
/*
adds output neuron to the output list
assuming the node is an input neuron
*/
addOutput(node) {
this.outputs.push(node);
return this;
}
/*
loads an array of values to all
the input nodes, each index of the
array goes to each input neuron, array has
to be the same length as inputs array -1 to
ignore the BIAS node
*/
loadSensors(inputArray) {
if (inputArray.length !== this.inputs.length - 1) {
console.log("netowrk receiving an invalid number inputs");
} else {
let index = 0;
this.inputs.forEach(node => {
if (node.placement === nodePlaces.BIAS) return;
node.sensorLoad(inputArray[index]);
index++;
});
return this;
}
}
/*
overrides the output neurons with given
array of inputs values to be overriden with
must be the same length as output array
*/
overrideOutputs(outputArray) {
if (outputArray.length !== this.outputs.length) {
console.log("netowrk receiving an invalid number override outputs");
} else {
for (let i = 0; i < this.outputs.length; i++) {
this.outputs[i].overrideOutput(outputArray[i]);
}
return this;
}
}
/*
count the amount of nodes in this network
then put the value to nodeNum
*/
countNodes() {
}
/*
counts the amount of connections in this network
then puts that value in to connectionNum
*/
countConnections() {
}
/*
checks to see the maximum number of nodes
a input has to go through to get to an output
node, used for clasification problems
*/
calcMaxDepth() {
let curDepth;
this.outputs.forEach(node => {
curDepth = Node.findDepth(node, 0);
if(curDepth > this.depth) this.depth = curDepth;
});
return this;
}
/*
recursivly checks if a potential connection is
recurrent or not.
*/
static checkRecur(inNode, outNode, count, thresh) {
count++;
if (count >= thresh) {
return false;
}
if (inNode === outNode) return true;
for (let i = 0; i < inNode.inConnections.length; i++) {
let connection = inNode.inConnections[i];
if (connection.isRecur === false) {
if (Network.checkRecur(connection.inNode, outNode, count, thresh)) return true;
}
}
return false;
}
}