-
Notifications
You must be signed in to change notification settings - Fork 0
/
Neuron.js
72 lines (61 loc) · 1.58 KB
/
Neuron.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
class Neuron{
constructor(){
this.outputConnections = [];
this.inputConnections = [];
this.bias = getRandom(-1, 1);
this.input = false;
this.source = [];
}
setInput(source, snake){
this.input = true;
this.source = source;
this.snake = snake;
}
setBias(bias){
this.bias = bias;
}
getBias(){
return this.bias;
}
getWeight(){
var weights = [];
for (var i = 0; i < this.outputConnections.length; i++){
weights[i] = this.outputConnections[i].getWeight();
}
this.weights = weights;
return this.weights;
}
setDraw(x, y){
this.drawX = x;
this.drawY = y;
}
setLine(x, y){
this.lineX = x;
this.lineY = y;
}
addOutputConnection(connection){
this.outputConnections.push(connection);
}
addInputConnection(connection){
this.inputConnections.push(connection);
}
getData(){
if (this.input == true){
return this.snake.getData(this.source);
} else {
var total = 0;
for (var i = 0; i < this.inputConnections.length; i++){
var conn = this.inputConnections[i];
var data = conn.getData();
total += data;
}
var answer = sigmoid(total);
return answer;
}
}
setWeights(weights){
for (var i = 0; i < this.outputConnections.length; i++){
this.outputConnections[i].setWeight(weights[i]);
}
}
}