forked from JakobHeller/NatureInspiredComputing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Node.cpp
209 lines (168 loc) · 4.05 KB
/
Node.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <math.h>
#include <iostream>
#include <algorithm>
#include "Node.h"
double CNode::Sigma = 1;
double CNode::LearningWeight = 0.3;
const double CNode::DecayRate = 0.9;
CNode::CNode()
{
m_Center = CSensorData();
m_Weight = CSpeed();
m_Activity = 0;
}
CNode::CNode(CSensorData c, CSpeed w) : CNode()
{
m_Center = c;
m_Weight = w;
m_Weight.Limit();
AddActivation();
}
double CNode::Activate(CSensorData input, CSpeed & output)
{
double activation;
Decay();
activation = Calculate(input, output);
AddActivation(activation);
return activation;
}
double CNode::Calculate(CSensorData input, CSpeed & output)
{
m_Weight.Limit();
double activation;
activation = BaseFunction(input);
output = m_Weight * activation;
return activation;
}
void CNode::Adapt(CSensorData input, CSpeed difference)
{
double activation;
CSpeed output;
activation = Calculate(input, output);
CSpeed change = difference * activation * LearningWeight;
m_Weight += change;
m_Weight.Limit();
}
void CNode::Adapt(CSensorData input, CSpeed old, CSpeed better, double totalActivity)
{
double activation;
CSpeed output;
activation = Calculate(input, output);
double strength = LearningWeight * activation;
if (activation > 0.5)
{
//std::cout << "Node activity = " << activation << ", Old L R = " << m_Weight.Left() << " " << m_Weight.Right() << ", ";
}
CSpeed change = (better - old) * LearningWeight;
m_Weight += change;
if (activation > 0.5)
{
//std::cout << "New L R = " << m_Weight.Left() << " " << m_Weight.Right() << std::endl;
}
m_Weight.Limit();
}
CSensorData CNode::Center()
{
return m_Center;
}
CSpeed CNode::Weight()
{
return m_Weight;
}
void CNode::Dump()
{
std::cout << "Node (";
m_Center.Dump();
std::cout << " ) : W = " << m_Weight.Velocity() << "(" << m_Weight.Angle() << ")" << "; Act = " << m_Activity;
}
bool CNode::CompareActivity(CNode a, CNode b)
{
return a.m_Activity > b.m_Activity;
}
double CNode::BaseFunction(CSensorData sensors)
{
double sqdist = 0;
for (auto sens = sensors.begin(); sens != sensors.end(); sens++)
{
double diff = sens->second.sensor - m_Center[sens->first].sensor;
sqdist += pow(diff/1024.0, 2);
}
return exp(-sqrt(sqdist) / Sigma);
}
void CNode::Decay()
{
m_Activity *= DecayRate;
}
void CNode::AddActivation(double act)
{
m_Activity += act;
}
void CNeuralNetwork::AddNode(CSensorData sensors, CSpeed speed)
{
CNode node(sensors, speed);
this->push_back(node);
std::cout << "Added new ";
node.Dump();
std::cout << std::endl;
}
void CNeuralNetwork::Forget(int maxCount)
{
std::sort(this->begin(), this->end(), CNode::CompareActivity);
int count = 0;
for (auto it = this->begin(); it != this->end(); it++)
{
count++;
if (count > maxCount)
{
std::cout << "Forgetting ";
it->Dump();
std::cout << std::endl;
}
}
this->resize(maxCount);
}
int CNeuralNetwork::Count()
{
return this->size();
}
SIOSet CNeuralNetwork::Evaluate(CSensorData sensors)
{
double activation = 0;
double maxAct = 0;
CSpeed speed;
for (int n = 0; n < this->size(); n++)
{
double act;
CSpeed out;
act = this->at(n).Activate(sensors, out);
speed += out;
activation += act;
maxAct = fmax(maxAct, act);
}
if (activation > 0) speed /= activation;
SIOSet result;
result.sensors = sensors;
result.speed = speed;
// if (activation>1.2) printf("Activation is too high (%f)\n", activation);
if (activation<0.8) printf("Activation is too low (%f)\n", activation);
return result;
}
void CNeuralNetwork::Adapt(SIOSet ideal)
{
double activation = 0;
CSpeed current;
for (int n = 0; n < this->size(); n++)
{
double act;
CSpeed out;
act = this->at(n).Calculate(ideal.sensors, out);
current += out;
activation += act;
}
if (activation > 0) current /= activation;
ideal.speed.Limit();
for (int n = 0; n < this->size(); n++)
{
this->at(n).Adapt(ideal.sensors, current, ideal.speed, activation);
}
}