-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc.js
103 lines (91 loc) · 2.41 KB
/
calc.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
function exp1 (x) {
x = 1.0 + x/256.0;
x *= x;
x *= x;
x *= x;
x *= x;
x *= x;
x *= x;
x *= x;
x *= x;
return x;
};
var Network = function(json){
if(typeof json === "string"){
this.network = JSON.parse(json);
}else{
this.network = json;
}
for(var i in this.network.Nodes){
this.network.Nodes[i]["id"] = parseInt(i)+1+"";
}
}
Network.prototype.activationFunctions = {
1: function(x){ // DirectActivation
return x;
},
2: function(x){ // SteependSigmoidActivation
return 1.0 / (1.0 + exp1(-4.9*x))
},
3: function(x){ // SigmoidActivation
return 1.0 / (1.0 + exp1(-x))
},
4: function(x){ // TanhActivation
return Math.tanh(0.9 * x);
},
5: function(x){ // InverseAbsActivation
return x / (1.0 + Math.abs(x));
},
};
Network.prototype.getNeuronWithInnovationId = function(innID){
for(var i in this.network.Nodes){
if(this.network.Nodes[i].Innovation == innID){
return this.network.Nodes[i];
}
}
}
Network.prototype.getActivation = function(node){
// console.log(node.Innovation,"in");
if(node.NeuronType == 1){
return 1; // bias' value
}
if(node.NeuronType == 2){
var index = parseInt(node.Innovation)-2; index += "";
// console.log(node.Innovation, "out", "bias/input");
return this.inputs[index];
}else{
node.activationValues = [];
// node.activationValues.length = 0;
for(var i in this.network.Conns){
var connection = this.network.Conns[i];
if(connection.Target == node.Innovation && !connection.used && connection.Enabled == true){
// connection.used = true;
var sourceId = "" + (parseInt(connection.Source)-1);
connection.result = connection.Weight * this.getActivation(this.getNeuronWithInnovationId(connection.Source));
node.activationValues.push(connection.result);
}
}
// TODO: get sum with underscorejs
var sum = 0;
for(var j in node.activationValues){
sum += node.activationValues[j];
}
// console.log(node.Innovation, "out");
node.activationResult = this.activationFunctions[node.ActivationType](sum);
return node.activationResult;
}
}
Network.prototype.getOutputNode = function(){
// TODO: Implement this with underscore.js
for(var i in this.network.Nodes){
var node = this.network.Nodes[i];
if(node.NeuronType == 4){
return node;
}
}
}
Network.prototype.response = function(inputArray){
this.inputs = inputArray;
var outNode = this.getOutputNode();
return this.getActivation(outNode);
}