-
Notifications
You must be signed in to change notification settings - Fork 51
/
optimizationbehaviors.cc
365 lines (315 loc) · 9.93 KB
/
optimizationbehaviors.cc
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include "optimizationbehaviors.h"
#include <fstream>
/*
*
*
* Fixed Kick optimization agent
*
*
*/
OptimizationBehaviorFixedKick::OptimizationBehaviorFixedKick(const std::string teamName,
int uNum, const map<string, string>& namedParams_, const string& rsg_,
const string& outputFile_) :
NaoBehavior(teamName, uNum, namedParams_, rsg_), outputFile(outputFile_),
kick(0), INIT_WAIT_TIME(3.0)
{
initKick();
}
void OptimizationBehaviorFixedKick::beam(double& beamX, double& beamY,
double& beamAngle) {
beamX = atof(namedParams.find("kick_xoffset")->second.c_str());
beamY = atof(namedParams.find("kick_yoffset")->second.c_str());
beamAngle = atof(namedParams.find("kick_angle")->second.c_str());
}
SkillType OptimizationBehaviorFixedKick::selectSkill() {
double time = worldModel->getTime();
if (timeStart < 0) {
initKick();
return SKILL_STAND;
}
// Wait a bit before attempting kick
if (time-timeStart <= INIT_WAIT_TIME) {
return SKILL_STAND;
}
if (!hasKicked) {
hasKicked = true;
return SKILL_KICK_LEFT_LEG; // The kick skill that we're optimizing
}
return SKILL_STAND;
}
void OptimizationBehaviorFixedKick::updateFitness() {
static double totalFitness = 0.0;
if (kick == 10) {
writeFitnessToOutputFile(totalFitness/(double(kick)));
return;
}
double time = worldModel->getTime();
VecPosition meTruth = worldModel->getMyPositionGroundTruth();
meTruth.setZ(0);
if (time-timeStart <= INIT_WAIT_TIME) {
return;
}
static bool failedLastBeamCheck = false;
if(!beamChecked) {
cout << "Checking whether beam was successful\n";
beamChecked = true;
LOG_STR("Checking whether beam was successful");
meTruth.setZ(0);
double beamX, beamY, beamAngle;
beam(beamX, beamY, beamAngle);
VecPosition meDesired = VecPosition(beamX, beamY, 0);
double distance = meTruth.getDistanceTo(meDesired);
double angle = worldModel->getMyAngDegGroundTruth();
VecPosition ballPos = worldModel->getBallGroundTruth();
double ballDistance = ballPos.getMagnitude();
// Check that we're close to our expected position and angle
// and also that the ball is close to it's exepected position
if(distance > .1 || ballDistance > .1 || abs(angle - beamAngle) > 3) {
cout << distance << "\t" << ballDistance << "\n";
LOG_STR("Problem with the beam!");
LOG(distance);
LOG(meTruth);
LOG(meDesired);
if (failedLastBeamCheck) {
kick++;
totalFitness -= 100;
failedLastBeamCheck = false;
} else {
failedLastBeamCheck = true;
}
initKick();
return;
}
failedLastBeamCheck = false;
string msg = "(playMode KickOff_Left)";
setMonMessage(msg);
}
if (!hasKicked && worldModel->getPlayMode() == PM_PLAY_ON) {
ranIntoBall = true;
}
if (!hasKicked) {
return;
}
VecPosition ballTruth = worldModel->getBallGroundTruth();
if (ballTruth.getX() < -.25) {
backwards = true;
}
if (worldModel->isFallen()) {
fallen = true;
}
if (worldModel->isFallen()) {
totalFitness += -1;
kick++;
initKick();
return;
}
if (time - (timeStart + INIT_WAIT_TIME) > 15 && !isBallMoving(this->worldModel)) {
double angleOffset = abs(VecPosition(0, 0, 0).getAngleBetweenPoints(VecPosition(20, 0, 0), ballTruth));
double distance = ballTruth.getX();
double fitness = distance;
if (backwards || distance <= 0.1 || ranIntoBall) {
fitness = -100;
if (backwards) {
cout << "Detected backward kick" << endl;
} else if (ranIntoBall) {
cout << "Detected ranIntoBall" << endl;
} else {
cout << "Detected insufficient distance" << endl;
}
}
cout << "Traveled distance = " << distance << endl;
cout << "Fitness = " << fitness << endl;
cout << "Final position = " << ballTruth.getX() << ", " << ballTruth.getY() << endl;
totalFitness += fitness;
kick++;
initKick();
return;
}
}
void OptimizationBehaviorFixedKick::initKick() {
hasKicked = false;
beamChecked = false;
backwards = false;
ranIntoBall = false;
timeStart = worldModel->getTime();
initialized = false;
initBeamed = false;
fallen = false;
resetSkills();
// Beam agent and ball
double beamX, beamY, beamAngle;
beam(beamX, beamY, beamAngle);
VecPosition beamPos = VecPosition(beamX, beamY, 0);
string msg = "(playMode BeforeKickOff)";
setMonMessage(msg);
}
void OptimizationBehaviorFixedKick::writeFitnessToOutputFile(double fitness) {
static bool written = false;
if (!written) {
LOG(fitness);
LOG(kick);
fstream file;
file.open(outputFile.c_str(), ios::out);
file << fitness << endl;
file.close();
written = true;
//string msg = "(killsim)";
//setMonMessage(msg);
}
}
/* Checks if the ball is currently moving */
bool isBallMoving(const WorldModel *worldModel) {
static VecPosition lastBall = worldModel->getBallGroundTruth();
static double lastTime = worldModel->getTime();
double thisTime = worldModel->getTime();
VecPosition thisBall = worldModel->getBallGroundTruth();
thisBall.setZ(0);
lastBall.setZ(0);
if(thisBall.getDistanceTo(lastBall) > 0.01)
{
// the ball moved!
lastBall = thisBall;
lastTime = thisTime;
return true;
}
if(thisTime - lastTime < 0.5)
{
// not sure yet if the ball has settled
return true;
}
else
{
return false;
}
}
void writeToOutputFile(const string &filename, const string &output) {
// static bool written = false;
// assert(!written);
// LOG(output);
fstream file;
file.open(filename.c_str(), ios::out);
file << output;
file.close();
// written = true;
}
/*
*
*
* WALK FORWARD OPTIMIZATION AGENT
*
*
*
*/
OptimizationBehaviorWalkForward::
OptimizationBehaviorWalkForward( const std::string teamName,
int uNum,
const map<string, string>& namedParams_,
const string& rsg_,
const string& outputFile_)
: NaoBehavior( teamName,
uNum,
namedParams_,
rsg_ ),
outputFile( outputFile_ ) {
INIT_WAIT = 3;
run = 0;
totalWalkDist = 0;
// Use ground truth localization for behavior
worldModel->setUseGroundTruthDataForLocalization(true);
init();
}
void OptimizationBehaviorWalkForward::init() {
startTime = worldModel->getTime();
initialized = false;
initBeamed = false;
beamChecked = false;
string msg = "(playMode BeforeKickOff)";
setMonMessage(msg);
}
void OptimizationBehaviorWalkForward::
beam( double& beamX, double& beamY, double& beamAngle ) {
beamX = -HALF_FIELD_X+3;
beamY = 0;
beamAngle = 0;
}
bool OptimizationBehaviorWalkForward::checkBeam() {
LOG_STR("Checking whether beam was successful");
VecPosition meTruth = worldModel->getMyPositionGroundTruth();
meTruth.setZ(0);
double beamX, beamY, beamAngle;
beam(beamX, beamY, beamAngle);
VecPosition meDesired = VecPosition(beamX, beamY, 0);
double distance = meTruth.getDistanceTo(meDesired);
double angleOffset = abs(worldModel->getMyAngDegGroundTruth()-beamAngle);
if(distance > 0.05 || angleOffset > 5) {
LOG_STR("Problem with the beam!");
LOG(distance);
LOG(meTruth);
return false;
}
beamChecked = true;
return true;
}
SkillType OptimizationBehaviorWalkForward::
selectSkill() {
double currentTime = worldModel->getTime();
if (currentTime-startTime < INIT_WAIT || startTime < 0) {
return SKILL_STAND;
}
return goToTarget(VecPosition(HALF_FIELD_X, 0, 0));
}
void OptimizationBehaviorWalkForward::
updateFitness() {
static bool written = false;
if (run == 10) {
if (!written) {
double fitness = totalWalkDist/(double)run;
fstream file;
file.open(outputFile.c_str(), ios::out );
file << fitness << endl;
file.close();
written = true;
}
return;
}
if (startTime < 0) {
init();
return;
}
double currentTime = worldModel->getTime();
if (currentTime-startTime < INIT_WAIT) {
return;
}
if (!beamChecked) {
static bool failedLastBeamCheck = false;
if (!checkBeam()) {
// Beam failed so reinitialize everything
if (failedLastBeamCheck) {
// Probably something bad happened if we failed the beam twice in
// a row (perhaps the agent can't stand) so give a bad score and
// move on
totalWalkDist -= 100;
run++;
}
failedLastBeamCheck = true;
init();
return;
} else {
failedLastBeamCheck = false;
// Set playmode to PlayOn to start run and move ball out of the way
string msg = "(playMode PlayOn) (ball (pos 0 -9 0) (vel 0 0 0))";
setMonMessage(msg);
}
}
if (currentTime-startTime >= 10.0+INIT_WAIT) {
VecPosition me = worldModel->getMyPositionGroundTruth();
double beamX, beamY, beamAngle;
beam(beamX, beamY, beamAngle);
VecPosition start = VecPosition(beamX, beamY, 0);
double walkdist = (me-start).getX();
cout << "Run " << run << " distance walked: " << walkdist << endl;
totalWalkDist += walkdist;
run++;
init();
}
}