forked from JakobHeller/NatureInspiredComputing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller.cpp
349 lines (296 loc) · 9.02 KB
/
Controller.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <fstream>
#include <sstream>
#include <algorithm>
#include <iostream>
#include "Controller.h"
CController::CController(CKheperaUtility * pUtil, CRbfSettings* pSettings) : CThreadableBase(pUtil)
{
m_pSettings = pSettings;
//RebuildNetwork();
int steps = 0;
if (steps>0)
{
for (int i = 0; i < pow(steps, (int)Direction_Back + 1); i++)
{
CSensorData center;
int mod;
int div;
div = i;
for (int d = (int)Direction_FrontLeft; d <= (int)Direction_Back + 1; d++)
{
mod = div%steps;
div = div / steps;
center[(EDirection)(d - 1)] = mod * 1000 / (steps - 1);
}
m_NetworkNodes.AddNode(center, CSpeed(m_pUtil->GetUniformRandom(0, 20), m_pUtil->GetUniformRandom(-PI / 2, PI / 2)));
}
}
else
{
for (int f = (int)Direction_Left; f < (int)Direction_Back + 1; f++)
{
CSensorData center;
for (int d = (int)Direction_Left; d < (int)Direction_Back + 1; d++)
{
center[(EDirection)d] = 0;
}
center[(EDirection)f] = 1024;
m_NetworkNodes.AddNode(center, CSpeed(m_pUtil->GetUniformRandom(0, 20), m_pUtil->GetUniformRandom(-PI / 2, PI / 2)));
}
CNode::Sigma = 2 * 2;
//CreateTrainingData();
for (int i = 0; i < 100 * m_TrainingData.size(); i++)
{
int ind = i%m_TrainingData.size();
//Adapt(m_TrainingData[ind]);
}
}
}
void CController::LoadNodesFromFile(std::string path)
{
CNeuralNetwork newNetwork;
std::fstream file;
std::string line;
file.open(path);
if (file.is_open())
{
while (getline(file, line))
{
std::stringstream ss(line);
CSensorData sensors;
int sval;
for (int d = 0; d <= (int)Direction_Back; d++)
{
ss >> sval;
sensors[(EDirection)d] = SValue(sval);
}
CSpeed speed;
double left;
double right;
ss >> left >> right;
speed.SetComponents(left, right);
newNetwork.AddNode(sensors, speed);
}
m_NetworkNodes = newNetwork;
}
else std::cout << "Unable to open file";
file.close();
}
void CController::SaveNodesToFile(std::string path)
{
std::ofstream file;
file.open(path, std::ios_base::trunc);
for (auto it = m_NetworkNodes.begin(); it != m_NetworkNodes.end(); it++)
{
if (file.is_open())
{
for (int d = 0; d <= (int)Direction_Back; d++)
{
file << it->Center()[(EDirection)d].sensor << " ";
}
file << it->Weight().Left() << " " << it->Weight().Right() << std::endl;
}
else std::cout << "Unable to open file";
}
file.close();
}
void CController::DoCycle()
{
// evaluate current sensor data
CSensorData sensorData = m_pUtil->GetSensorData();
#ifdef SIM_ONLY
sensorData = CSensorData(Int8({ {
(int)round(m_pUtil->GetUniformRandom(0, 1024)),
(int)round(m_pUtil->GetUniformRandom(0, 1024)),
(int)round(m_pUtil->GetUniformRandom(0, 1024)),
(int)round(m_pUtil->GetUniformRandom(0, 1024)),
(int)round(m_pUtil->GetUniformRandom(0, 1024)),
(int)round(m_pUtil->GetUniformRandom(0, 1024))
} }));
#endif
SIOSet result = Evaluate(sensorData);
result.speed.Limit();
m_pUtil->AddNetworkResult(result);
// get value system's correction
SIOSet ideal = m_pUtil->GetLastCorrectedResult();
Adapt(ideal);
//if (m_NetworkNodes.Count() != m_pSettings->MaxNodes()) RebuildNetwork();
}
void CController::Adapt(SIOSet ideal)
{
auto current = m_NetworkNodes.Evaluate(ideal.sensors);
m_NetworkNodes.Adapt(ideal);
// output info
std::ofstream file;
file.open("History.txt", std::ios_base::app);
bool babble = true;
if (babble)
{
file << "ValueSystem's results:" << std::endl;
ideal.sensors.Dump(file);
file << " ==> Angle: " << (double)((int)(10 * ideal.speed.Angle())) / 10.0 << " Speed: " << round(ideal.speed.Velocity());
file << std::endl;
}
if (babble)
{
CSpeed output = current.speed;
output.Limit();
file << "Controller's results:" << std::endl;
ideal.sensors.Dump(file);
file << " ==> Angle: " << (double)((int)(10 * output.Angle())) / 10.0 << " Speed: " << round(output.Velocity());
file << std::endl;
}
file.close();
}
void CController::RebuildNetwork()
{
CNeuralNetwork newNetwork;
CNeuralNetwork oldNetwork = m_NetworkNodes;
int nodeCount = m_pSettings->MaxNodes();
for (int i = 0; i < nodeCount; i++)
{
Int8 raw = { {
(int)round(m_pUtil->GetUniformRandom(0,1024)),
(int)round(m_pUtil->GetUniformRandom(0,1024)),
(int)round(m_pUtil->GetUniformRandom(0,1024)),
(int)round(m_pUtil->GetUniformRandom(0,1024)),
(int)round(m_pUtil->GetUniformRandom(0,1024)),
(int)round(m_pUtil->GetUniformRandom(0,1024)),
(int)round(m_pUtil->GetUniformRandom(0,1024)),
(int)round(m_pUtil->GetUniformRandom(0,1024))
} };
CSensorData center = CSensorData(raw);
CSpeed weight = oldNetwork.Evaluate(center).speed;
newNetwork.AddNode(center, weight);
}
m_NetworkNodes = newNetwork;
}
SIOSet CController::Evaluate(CSensorData sensors)
{
return m_NetworkNodes.Evaluate(sensors);
}
void CController::ListNodes()
{
std::cout << "These are my nodes:" << std::endl;
//for (auto it = m_NetworkNodes.begin(); it != m_NetworkNodes.end(); it++)
//{
// it->Dump();
// std::cout << std::endl;
//}
}
void CController::CreateTrainingData()
{
m_TrainingData.clear();
Int8 sensors;
SIOSet set;
int FAR_SENSOR_VAL = 0;
int DISTANCE_LIMIT = 1000;
// free field
sensors.data[0] = FAR_SENSOR_VAL;
sensors.data[1] = FAR_SENSOR_VAL;
sensors.data[2] = FAR_SENSOR_VAL;
sensors.data[3] = FAR_SENSOR_VAL;
sensors.data[4] = FAR_SENSOR_VAL;
sensors.data[5] = FAR_SENSOR_VAL;
sensors.data[6] = FAR_SENSOR_VAL;
sensors.data[7] = FAR_SENSOR_VAL;
set.sensors = CSensorData(sensors);
set.speed.SetComponents(20, 20);
m_TrainingData.push_back(set);
// corridor
//sensors.data[0] = DISTANCE_LIMIT;
//sensors.data[1] = 0.5*DISTANCE_LIMIT;
//sensors.data[2] = FAR_SENSOR_VAL;
//sensors.data[3] = FAR_SENSOR_VAL;
//sensors.data[4] = 0.5*DISTANCE_LIMIT;
//sensors.data[5] = DISTANCE_LIMIT;
//sensors.data[6] = FAR_SENSOR_VAL;
//sensors.data[7] = FAR_SENSOR_VAL;
//set.sensors = CSensorData(sensors);
//set.speed.SetComponents(20, 20);
//m_TrainingData.push_back(set);
// back to dead end
//sensors.data[0] = DISTANCE_LIMIT;
//sensors.data[1] = FAR_SENSOR_VAL;
//sensors.data[2] = FAR_SENSOR_VAL;
//sensors.data[3] = FAR_SENSOR_VAL;
//sensors.data[4] = FAR_SENSOR_VAL;
//sensors.data[5] = DISTANCE_LIMIT;
//sensors.data[6] = DISTANCE_LIMIT;
//sensors.data[7] = DISTANCE_LIMIT;
//set.sensors = CSensorData(sensors);
//set.speed.SetComponents(20, 20);
//m_TrainingData.push_back(set);
// soft left corner
//sensors.data[0] = DISTANCE_LIMIT;
//sensors.data[1] = DISTANCE_LIMIT;
//sensors.data[2] = FAR_SENSOR_VAL;
//sensors.data[3] = FAR_SENSOR_VAL;
//sensors.data[4] = FAR_SENSOR_VAL;
//sensors.data[5] = FAR_SENSOR_VAL;
//sensors.data[6] = FAR_SENSOR_VAL;
//sensors.data[7] = FAR_SENSOR_VAL;
//set.sensors = CSensorData(sensors);
//set.speed.SetComponents(20, 10);
//m_TrainingData.push_back(set);
// left corner
sensors.data[0] = DISTANCE_LIMIT;
sensors.data[1] = DISTANCE_LIMIT;
sensors.data[2] = DISTANCE_LIMIT;
sensors.data[3] = FAR_SENSOR_VAL;
sensors.data[4] = FAR_SENSOR_VAL;
sensors.data[5] = FAR_SENSOR_VAL;
sensors.data[6] = FAR_SENSOR_VAL;
sensors.data[7] = FAR_SENSOR_VAL;
set.sensors = CSensorData(sensors);
set.speed.SetComponents(0, -10);
m_TrainingData.push_back(set);
// frontal left
//sensors.data[0] = FAR_SENSOR_VAL;
//sensors.data[1] = FAR_SENSOR_VAL;
//sensors.data[2] = DISTANCE_LIMIT;
//sensors.data[3] = DISTANCE_LIMIT;
//sensors.data[4] = FAR_SENSOR_VAL;
//sensors.data[5] = FAR_SENSOR_VAL;
//sensors.data[6] = FAR_SENSOR_VAL;
//sensors.data[7] = FAR_SENSOR_VAL;
//set.sensors = CSensorData(sensors);
//set.speed.SetComponents(0, -10);
//m_TrainingData.push_back(set);
// frontal right
//sensors.data[0] = FAR_SENSOR_VAL;
//sensors.data[1] = FAR_SENSOR_VAL;
//sensors.data[2] = FAR_SENSOR_VAL;
//sensors.data[3] = DISTANCE_LIMIT;
//sensors.data[4] = DISTANCE_LIMIT;
//sensors.data[5] = FAR_SENSOR_VAL;
//sensors.data[6] = FAR_SENSOR_VAL;
//sensors.data[7] = FAR_SENSOR_VAL;
//set.sensors = CSensorData(sensors);
//set.speed.SetComponents(-10, 0);
//m_TrainingData.push_back(set);
// soft right corner
//sensors.data[0] = FAR_SENSOR_VAL;
//sensors.data[1] = FAR_SENSOR_VAL;
//sensors.data[2] = FAR_SENSOR_VAL;
//sensors.data[3] = FAR_SENSOR_VAL;
//sensors.data[4] = DISTANCE_LIMIT;
//sensors.data[5] = DISTANCE_LIMIT;
//sensors.data[6] = FAR_SENSOR_VAL;
//sensors.data[7] = FAR_SENSOR_VAL;
//set.sensors = CSensorData(sensors);
//set.speed.SetComponents(10, 20);
//m_TrainingData.push_back(set);
// right corner
sensors.data[0] = FAR_SENSOR_VAL;
sensors.data[1] = FAR_SENSOR_VAL;
sensors.data[2] = FAR_SENSOR_VAL;
sensors.data[3] = DISTANCE_LIMIT;
sensors.data[4] = DISTANCE_LIMIT;
sensors.data[5] = DISTANCE_LIMIT;
sensors.data[6] = FAR_SENSOR_VAL;
sensors.data[7] = FAR_SENSOR_VAL;
set.sensors = CSensorData(sensors);
set.speed.SetComponents(-10, 0);
m_TrainingData.push_back(set);
}