forked from JakobHeller/NatureInspiredComputing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValueSystem.cpp
217 lines (178 loc) · 5.52 KB
/
ValueSystem.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
#include "ValueSystem.h"
#include <fstream>
CValueSystem::CValueSystem(CKheperaUtility * pUtil) : CThreadableBase(pUtil)
{
m_Repetitions = 0;
}
void CValueSystem::DoCycle()
{
// evaluate current results
std::vector<SIOSet> controllerResults = m_pUtil->GetNetworkResults();
if (controllerResults.size() < 2) return;
SIOSet correction = Correct(controllerResults);
m_pUtil->SetCorrectedResult(correction);
}
SIOSet CValueSystem::Correct(std::vector<SIOSet> history)
{
std::vector<std::pair<CSpeed, double>> speedFitness;
SIOSet correction = history.back();
// if repetitions are required, just output the last speed
if (m_Repetitions > 0)
{
m_Repetitions--;
correction.speed = m_RepeatSpeed;
speedFitness.push_back(FitSpeed(correction.sensors, m_RepeatSpeed));
}
// insert controller's results
speedFitness.push_back(FitSpeed(correction.sensors, correction.speed));
// generate alternatives
CSpeed alt;
CSpeed altBackwards;
// backwards
altBackwards = correction.speed;
altBackwards.SetVelocity(-altBackwards.Velocity());
speedFitness.push_back(FitSpeed(correction.sensors, altBackwards));
// more left
alt = correction.speed;
alt.IncreaseAngle(PI/4);
speedFitness.push_back(FitSpeed(correction.sensors, alt));
altBackwards = alt;
altBackwards.SetVelocity(-altBackwards.Velocity());
speedFitness.push_back(FitSpeed(correction.sensors, altBackwards));
// more right
alt = correction.speed;
alt.IncreaseAngle(-PI/4);
speedFitness.push_back(FitSpeed(correction.sensors, alt));
altBackwards = alt;
altBackwards.SetVelocity(-altBackwards.Velocity());
speedFitness.push_back(FitSpeed(correction.sensors, altBackwards));
// more speed
alt = correction.speed;
alt *= 1 + m_pUtil->GetUniformRandom();
speedFitness.push_back(FitSpeed(correction.sensors, alt));
altBackwards = alt;
altBackwards.SetVelocity(-altBackwards.Velocity());
speedFitness.push_back(FitSpeed(correction.sensors, altBackwards));
// less speed
alt = correction.speed;
alt *= m_pUtil->GetUniformRandom();
speedFitness.push_back(FitSpeed(correction.sensors, alt));
altBackwards = alt;
altBackwards.SetVelocity(-altBackwards.Velocity());
speedFitness.push_back(FitSpeed(correction.sensors, altBackwards));
// choose best solution
std::pair<CSpeed, double> best = speedFitness.front();
for (auto it = speedFitness.begin(); it != speedFitness.end(); it++)
{
if (it->second < best.second) best = *it;
}
correction.speed = best.first;
m_RepeatSpeed = correction.speed;
return correction;
}
double CValueSystem::Fitness(CSensorData position, CSpeed speed)
{
double fit = 0;
int steps = 3;
int current = steps;
CSensorData older = position;
while (current > 0)
{
CSensorData future = PredictChange(older, speed);
fit += SensorFitness(future);
older = future;
current--;
}
fit /= steps;
fit += 5*SpeedFitness(speed);
// output info
std::ofstream file;
file.open("Fitness.txt", std::ios_base::app);
bool babble = true;
if (babble)
{
position.Dump(file);
file << " ==> Angle: " << (double)((int)(100 * speed.Angle())) / 100.0 << " Speed: " << (double)((int)(100 * speed.Velocity())) / 100.0;
file << " => Fitness: " << fit;
file << std::endl;
}
return fit;
}
double CValueSystem::SpeedFitness(CSpeed speed)
{
double V = exp(-abs(speed.Velocity()));
double A = abs(speed.Angle());
double backPenalty = -speed.Velocity() / abs(speed.Velocity());
return 2*V + 1.2*A + backPenalty;
}
double CValueSystem::SensorFitness(CSensorData sensors)
{
double fit = 0;
double penalty = 10 * 1024;
if (sensors.Collision())
{
if (sensors[Direction_Back].proximity == Proximity_Collision)
fit += penalty / 2;
else
fit += penalty;
}
else if (sensors.CloseToCollision())
{
fit += penalty / 2;
}
fit += 1 * sensors[Direction_Left].sensor;
fit += 1.2 * sensors[Direction_FrontLeft].sensor;
fit += 1.3 * sensors[Direction_Front].sensor;
fit += 1.2 * sensors[Direction_FrontRight].sensor;
fit += 1 * sensors[Direction_Right].sensor;
fit += 5 * sensors[Direction_Back].sensor;
return fit;
}
SValue PredictValue(SValue start, double dirSpeed)
{
// assumption:
// approaching occurs on an exponential curve
double speedFactor = exp(dirSpeed / 100);
int newVal = (int)round(start.sensor*speedFactor);
if (newVal == start.sensor)
{
if (dirSpeed < 0) newVal--;
if (dirSpeed > 0) newVal++;
}
return SValue(newVal);
}
CSensorData CValueSystem::PredictChange(CSensorData start, CSpeed speed)
{
CSensorData next;
// straight component
double straight = (speed.Left() + speed.Right()) / 2;
// increases front and side front, decrease back
EDirection dir;
dir = Direction_Left;
next[dir] = PredictValue(start[dir], speed.Left());
dir = Direction_FrontLeft;
next[dir] = PredictValue(start[dir], (speed.Left() + straight) / 2);
dir = Direction_Front;
next[dir] = PredictValue(start[dir], straight);
dir = Direction_FrontRight;
next[dir] = PredictValue(start[dir], (speed.Right() + straight) / 2);
dir = Direction_Right;
next[dir] = PredictValue(start[dir], speed.Right());
dir = Direction_Back;
next[dir] = PredictValue(start[dir], - straight);
return next;
}
std::pair<CSpeed, double> CValueSystem::FitSpeed(CSensorData start, CSpeed speed)
{
double fit;
try
{
fit = Fitness(start, speed);
}
catch(...)
{
m_Repetitions = 3;
return std::make_pair(CSpeed(0,0), 100000);
}
return std::make_pair(speed, fit);
}