-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgent.cpp
More file actions
480 lines (336 loc) · 14.7 KB
/
Agent.cpp
File metadata and controls
480 lines (336 loc) · 14.7 KB
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#include <iostream>
#include "thresholds.h"
#include "Utils.h"
#include "LocF.h"
#include "Hn.h"
#include "StrategyFactory.h"
#include "sensors.h"
#include "StrategyTypes.h"
#include "WorkingMemory.h"
using namespace std;
// draw a i 50*50 grid centred on current location and plot the readings
// DO NOT WANT THIS IN THE REAL CODE - JUST FOR DEBUGGING
void print_belief_map(BMap* map) {
int grid[51][51];
// sprintf
printf("Printing belief map\n");
int x, y;
for(y=0;y<51;y++) {
for(x=0;x<51;x++) {
grid[x][y]=0;
}
}
int i;
for(i=0;i<map->_size;i++) {
Hn* point = map->GetPoint(i);
int rebasedPointX = 25 + point->x;
int rebasedPointY = 25 + point->y;
int isObstacle = point->is_obstacle;
if (rebasedPointX > 0 && rebasedPointX < 51 && rebasedPointY > 0 && rebasedPointY < 51) {
grid[rebasedPointX] [rebasedPointY] = isObstacle;
}
printf("%d,%d,%d\n",point->x, point->y, point->is_obstacle);
printf("%d,%d,%d\n",rebasedPointX, rebasedPointY, isObstacle);
}
for(y=51;y>=0;y--) {
printf("\n");
for(x=0;x<51;x++) {
if(x == (map->_curx +25) && y == (map->_cury+25)) {
printf(" ^");
}
else if(x == 25 && y == 25) {
printf(" 0");
} else if(grid[x][y]==0) {
printf(" -");
} else {
printf(" %d", grid[x][y]);
}
}
}
printf("\nFinished printing beleif map\n");
}
// just orchestrates the working memory
void agent_take_reading(funcSensor* sensors, WorkingMemory* wm, ActionTypes::Enum act) {
struct LocF* fact = new LocF();
fact->s0 = ((*sensors[S0])(0));
fact->s90 = ((*sensors[S90])(0));
fact->s180 = ((*sensors[S180])(0));
fact->s270 = ((*sensors[S270])(0));
fact->ori = ((*sensors[OR0])(0));
fact->precact = act;
printf("sensor reading %d\n", fact->s0);
// DEBUG - encapsulate
wm->AssertFact(fact);
}
void RunDiagnostic() {
cout << "Starting Agent\n";
std::cout << "---------------------------------------------\n";
std::cout << "Running Diagnostic\n";
std::cout << "---------------------------------------------\n";
std::cout << "charbits are " << BITS << "\n";
std::cout << "pos should be positive " << Utils::pos(-10) << "\n";
std::cout << "size of LocF " << sizeof(LocF) << "\n";
std::cout << "size of Hn " << sizeof(Hn) << "\n";
std::cout << "---------------------------------------------\n";
std::cout << "testing vectors\n";
std::cout << "---------------------------------------------\n";
std::cout << "vec test x is 1 " << Utils::lvfp(90,1)->x << "\n";
std::cout << "vec test y is 0 " << Utils::lvfp(90,1)->y << "\n";
std::cout << "vec test x is 2 " << Utils::lvfp(90,2)->x << "\n";
std::cout << "vec test y is 0 " << Utils::lvfp(90,2)->y << "\n";
std::cout << "vec test x is 0 " << Utils::lvfp(0,1)->x << "\n";
std::cout << "vec test y is 1 " << Utils::lvfp(0,1)->y << "\n";
std::cout << "vec test x is 0 " << Utils::lvfp(180,1)->x << "\n";
std::cout << "vec test y is -1 " << Utils::lvfp(180,1)->y << "\n";
std::cout << "vec test x is -1 " << Utils::lvfp(270,1)->x << "\n";
std::cout << "vec test y is 0 " << Utils::lvfp(270,1)->y << "\n";
Vct* dVct = Utils::lvfp(0,10);
std::cout << "this should be 0" << dVct->x << "\n";
std::cout << "this should be 10" << dVct->y << "\n";
BMap* beliefs = new BMap();
std::cout << "---------------------------------------------\n";
std::cout << "Test Add Beliefs\n";
std::cout << "---------------------------------------------\n";
beliefs->AddBelief(0,0,1.0,1);
std::cout << "Size of beliefs is (1) " << beliefs->_size << "\n";
beliefs->AddBelief(5,5,1.0,1);
std::cout << "Size of beliefs is (2) " << beliefs->_size << "\n";
beliefs->AddBelief(10,10,1.0,1);
std::cout << "Size of beliefs is (3) " << beliefs->_size << "\n";
beliefs->AddBelief(15,15,1.0,1);
std::cout << "Size of beliefs is (4) " << beliefs->_size << "\n";
// should display a diagonal
print_belief_map(beliefs);
delete beliefs;
beliefs = new BMap();
funcSensor sensors[NUMSNSRS];
std::cout << "Loading Sensor Array\n";
std::cout << "------------------------------------\n";
std::cout << "Test all in range orientation 0\n";
std::cout << "------------------------------------\n";
sensors[S0] = &read_within_range;
sensors[S90] = &read_within_range;
sensors[S180] = &read_within_range;
sensors[S270] = &read_within_range;
sensors[T0] = &read_within_range;
sensors[OR0] = &read_orientation_is_0;
StrategyFactory* sf = new StrategyFactory();
IStrategy* s = sf->makeStrategy(StrategyTypes::COLLISION_AVOIDANCE);
Action* action = s->getHighestYieldingAction(sensors, beliefs, 0, 0, 0, 0);
std::cout << "Agent should MOVE 1 m 10 " << action->c << " " << action->m << "\n";
delete action;
std::cout << "------------------------------------\n";
std::cout << "Test all will collide but 180 orientation 0\n";
std::cout << "------------------------------------\n";
sensors[S0] = &read_will_collide;
sensors[S90] = &read_will_collide;
sensors[S180] = &read_within_range;
sensors[S270] = &read_will_collide;
action = s->getHighestYieldingAction(sensors, beliefs, 0, 0, 0, 100);
std::cout << "Agent should TURN 0 m 180 " << action->c << " " << action->m << "\n";
delete action;
std::cout << "------------------------------------\n";
std::cout << "Test all will collide but 270 orientation 0\n";
std::cout << "------------------------------------\n";
sensors[S0] = &read_will_collide;
sensors[S90] = &read_will_collide;
sensors[S180] = &read_will_collide;
sensors[S270] = &read_within_range;
action = s->getHighestYieldingAction(sensors, beliefs, 0, 0, 0, 100);
std::cout << "Agent should TURN 0 m 270 " << action->c << " " << action->m << "\n";
delete action;
std::cout << "------------------------------------\n";
std::cout << "Test all will collide orientation 0\n";
std::cout << "------------------------------------\n";
sensors[S0] = &read_will_collide;
sensors[S90] = &read_will_collide;
sensors[S180] = &read_will_collide;
sensors[S270] = &read_will_collide;
action = s->getHighestYieldingAction(sensors, beliefs, 0, 0, 0, 0);
std::cout << "Agent should NOP 2 m 0 " << action->c << " " << action->m << "\n";
std::cout << "------------------------------------\n";
std::cout << "Test all will collide but 270 orientation 270 SHOULD MOVE \n";
std::cout << "------------------------------------\n";
sensors[S0] = &read_will_collide;
sensors[S90] = &read_will_collide;
sensors[S180] = &read_will_collide;
sensors[S270] = &read_within_range;
sensors[OR0] = &read_orientation_is_270;
action = s->getHighestYieldingAction(sensors, beliefs, 0, 0, 0, 0);
std::cout << "Agent should MOVE 1 m 10 " << action->c << " " << action->m << "\n";
delete action;
std::cout << "---------------------------------------------\n";
std::cout << "Test Working Memory\n";
std::cout << "---------------------------------------------\n";
std::cout << "Simulating moving toward obstacle bearing 0 degrees\n";
WorkingMemory* wm = new WorkingMemory();
sensors[S0] = &read_at_range_100;
sensors[S90] = &read_within_range;
sensors[S180] = &read_within_range;
sensors[S270] = &read_within_range;
sensors[T0] = &read_within_range;
sensors[OR0] = &read_orientation_is_0;
agent_take_reading(sensors, wm, ActionTypes::NOP); // start with NOPs
agent_take_reading(sensors, wm, ActionTypes::NOP); // start with NOPs
agent_take_reading(sensors, wm, ActionTypes::NOP); // start with NOPs
agent_take_reading(sensors, wm, ActionTypes::NOP); // start with NOPs
agent_take_reading(sensors, wm, ActionTypes::NOP); // start with NOPs
sensors[S0] = &read_at_range_90;
agent_take_reading(sensors, wm, ActionTypes::MOVE); // move
sensors[S0] = &read_at_range_80;
agent_take_reading(sensors, wm, ActionTypes::MOVE); // move
sensors[S0] = &read_at_range_70;
agent_take_reading(sensors, wm, ActionTypes::MOVE); // move
sensors[S0] = &read_at_range_70;
agent_take_reading(sensors, wm, ActionTypes::NOP); // stopped
agent_take_reading(sensors, wm, ActionTypes::NOP); // start with NOPs
agent_take_reading(sensors, wm, ActionTypes::NOP); // start with NOPs
std::cout << "generating belief map from working memory\n";
beliefs = wm->MaterializeWorld();
std::cout << "print should show a single point\n";
print_belief_map(beliefs);
std::cout << "print should show a single point\n";
// DEBUG - fix the leak
//delete wm;
delete beliefs;
std::cout << "Simulating moving toward obstacle bearing 0 degrees with objects on all sides\n";
wm = new WorkingMemory();
sensors[S0] = &read_at_range_40;
sensors[S90] = &read_at_range_15;
sensors[S180] = &read_beyond_range;
sensors[S270] = &read_at_range_15;
sensors[T0] = &read_at_range_15;
sensors[OR0] = &read_orientation_is_0;
agent_take_reading(sensors, wm, ActionTypes::NOP); // start with NOPs
agent_take_reading(sensors, wm, ActionTypes::NOP); // start with NOPs
agent_take_reading(sensors, wm, ActionTypes::NOP); // start with NOPs
agent_take_reading(sensors, wm, ActionTypes::NOP); // start with NOPs
agent_take_reading(sensors, wm, ActionTypes::NOP); // start with NOPs
sensors[S0] = &read_at_range_30;
agent_take_reading(sensors, wm, ActionTypes::MOVE); // move
sensors[S0] = &read_at_range_20;
agent_take_reading(sensors, wm, ActionTypes::MOVE); // move
sensors[S0] = &read_at_range_15;
agent_take_reading(sensors, wm, ActionTypes::MOVE); // move
std::cout << "generating belief map from working memory\n";
beliefs = wm->MaterializeWorld();
std::cout << "print should show a single point at bearing 0 with a tunnel on all sides\n";
print_belief_map(beliefs);
std::cout << "print should show a single point at bearing 0 with a tunnel on all sides\n";
// delete wm;
delete beliefs;
std::cout << "print should show a tunnel\n";
delete s;
std::cout << "---------------------------------------------\n";
std::cout << "Test Highest Yielding Strategy\n";
std::cout << "---------------------------------------------\n";
s = sf->makeStrategy(StrategyTypes::HIGHEST_YIELD);
sensors[S0] = &read_within_range;
sensors[S90] = &read_within_range;
sensors[S180] = &read_within_range;
sensors[S270] = &read_within_range;
action = s->getHighestYieldingAction(sensors, beliefs, 0, 0, 0, 0);
std::cout << "Agent should MOVE 1 m 10 " << action->c << " " << action->m << "\n";
delete action;
WorkingMemory* wm2;
wm2 = new WorkingMemory();
sensors[S0] = &read_at_range_40;
sensors[S90] = &read_at_range_15;
sensors[S180] = &read_beyond_range;
sensors[S270] = &read_at_range_15;
sensors[T0] = &read_at_range_15;
sensors[OR0] = &read_orientation_is_0;
agent_take_reading(sensors, wm2, ActionTypes::NOP); // start with NOPs
agent_take_reading(sensors, wm2, ActionTypes::NOP); // start with NOPs
agent_take_reading(sensors, wm2, ActionTypes::NOP); // start with NOPs
agent_take_reading(sensors, wm2, ActionTypes::NOP); // start with NOPs
agent_take_reading(sensors, wm2, ActionTypes::NOP); // start with NOPs
sensors[S0] = &read_at_range_30;
agent_take_reading(sensors, wm2, ActionTypes::MOVE); // move
sensors[S0] = &read_at_range_20;
agent_take_reading(sensors, wm2, ActionTypes::MOVE); // move
sensors[S0] = &read_at_range_15;
agent_take_reading(sensors, wm2, ActionTypes::MOVE); // move
std::cout << "generating belief map from working memory\n";
beliefs = wm2->MaterializeWorld();
std::cout << "print should show a single point at bearing 0 with a tunnel on all sides\n";
print_belief_map(beliefs);
std::cout << "print should show a single point at bearing 0 with a tunnel on all sides\n";
action = s->getHighestYieldingAction(sensors, beliefs, 0, 0, 0, 0);
std::cout << "Agent should MOVE 1 m 10 " << action->c << " " << action->m << "\n";
delete wm2;
delete beliefs;
delete s;
delete sf;
std::cout << "---------------------------------------------\n";
std::cout << "Test Highest Yielding Strategy for turn\n";
std::cout << "---------------------------------------------\n";
WorkingMemory* wm3;
wm3 = new WorkingMemory();
s = sf->makeStrategy(StrategyTypes::HIGHEST_YIELD);
sensors[S0] = &read_within_range;
sensors[S90] = &read_within_range;
sensors[S180] = &read_within_range;
sensors[S270] = &read_within_range;
sensors[T0] = &read_at_range_15;
sensors[OR0] = &read_orientation_is_0;
agent_take_reading(sensors, wm3, ActionTypes::NOP); // start with NOPs
agent_take_reading(sensors, wm3, ActionTypes::NOP); // start with NOPs
beliefs = wm3->MaterializeWorld();
action = s->getHighestYieldingAction(sensors, beliefs, 0, 0, 0, 100);
std::cout << "Agent should TURN 0 m 0 " << action->c << " " << action->m << "\n";
delete action;
delete wm3;
//delete beliefs;
std::cout << "Test 270 yield\n";
WorkingMemory* wm4;
wm4 = new WorkingMemory();
sensors[S0] = &read_at_range_20;
sensors[S90] = &read_at_range_20;
sensors[S180] = &read_at_range_20;
sensors[S270] = &read_beyond_range;
sensors[T0] = &read_at_range_15;
sensors[OR0] = &read_orientation_is_0;
agent_take_reading(sensors, wm4, ActionTypes::NOP); // start with NOPs
agent_take_reading(sensors, wm4, ActionTypes::NOP); // start with NOPs
agent_take_reading(sensors, wm4, ActionTypes::NOP); // start with NOPs
agent_take_reading(sensors, wm4, ActionTypes::NOP); // start with NOPs
agent_take_reading(sensors, wm4, ActionTypes::NOP); // start with NOPs
std::cout << "generating belief map from working memory\n";
beliefs = wm4->MaterializeWorld();
print_belief_map(beliefs);
std::cout << "print should show distant contact with an object on all sides but left\n";
action = s->getHighestYieldingAction(sensors, beliefs, 0, 0, 0, 100);
std::cout << "Agent should TURN 1 m 270 " << action->c << " " << action->m << "\n";
delete action;
//delete wm4;
delete beliefs;
std::cout << "Test 180 multipoint yield\n";
WorkingMemory* wm7;
wm7 = new WorkingMemory();
sensors[S0] = &read_at_range_20;
sensors[S90] = &read_at_range_20;
sensors[S180] = &read_beyond_range;
sensors[S270] = &read_at_range_15;
sensors[T0] = &read_at_range_15;
sensors[OR0] = &read_orientation_is_0;
agent_take_reading(sensors, wm7, ActionTypes::NOP); // start with NOPs
agent_take_reading(sensors, wm7, ActionTypes::NOP); // start with NOPs
agent_take_reading(sensors, wm7, ActionTypes::NOP); // start with NOPs
agent_take_reading(sensors, wm7, ActionTypes::NOP); // start with NOPs
agent_take_reading(sensors, wm7, ActionTypes::NOP); // start with NOPs
std::cout << "generating belief map from working memory\n";
beliefs = wm7->MaterializeWorld();
print_belief_map(beliefs);
std::cout << "print should show distant contact with an object on all sides but left\n";
action = s->getHighestYieldingAction(sensors, beliefs, 0, 0, 0, 100);
std::cout << "Agent should TURN 1 m 180 " << action->c << " " << action->m << "\n";
delete action;
//delete wm4;
delete beliefs;
delete s;
delete sf;
}
int main() {
RunDiagnostic();
}