-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfoodnetwork.cpp
68 lines (58 loc) · 1.88 KB
/
foodnetwork.cpp
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
#include<cmath>
class foodnetwork{
private:
double weights[num_weights];
public:
foodnetwork();
void set_weights(foodgenome);
void run_hidden(double[], double[]);
void run(double[], double[]);
void get_weights(ofstream outfile);
};
foodnetwork::foodnetwork(){ // genome g){
for(int i = 0; i < num_weights; i++){
weights[i] = 0;
}
}
void foodnetwork::set_weights(foodgenome g){
for(int i = 0; i < num_weights; i++){
weights[i] = g.getWeight(i); // g.getWeight(i); // randomGaussian();
}
}
void foodnetwork::get_weights(ofstream outfile) {
for(int i = 0; i < num_weights; i++) {
outfile << weights[i];
//No comma at the end
if (i == num_weights -1) {
outfile << ", ";
}
}
outfile << endl;
}
void foodnetwork::run_hidden(double inputs[], double outputs[]){
double hidden[hidden_size];
hidden[0] = 1; // bias
for(int h = 1; h < hidden_size; h++){
hidden[h] = 0;
for(int i = 0; i < input_size; i++){
hidden[h] += (inputs[i] * weights[(h*input_size) + i] );
}
hidden[h] = -1.0 + 2.0 * (1.0/(1.0+pow(2.7183,-1*hidden[h])));
}
for(int o = 0; o < output_size; o++){
outputs[o] = 0; //1.0 * weights[o*(input_size+1)]; // bias
for(int i = 0; i < hidden_size; i++){
outputs[o] += (hidden[i] * weights[(input_size*hidden_size) + (o*hidden_size) + i] );
}
outputs[o] = -1.0 + 2.0 * (1.0/(1.0+pow(2.7183,-1*outputs[o])));
}
}
void foodnetwork::run(double inputs[], double outputs[]){
for(int o = 0; o < output_size; o++){
outputs[o] = 0; //1.0 * weights[o*(input_size+1)]; // bias
for(int i = 0; i < input_size; i++){
outputs[o] += (inputs[i] * weights[(o*input_size) + i] );
}
outputs[o] = -1.0 + 2.0 * (1.0/(1.0+pow(2.7183,-1*outputs[o])));
}
}