-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironment.cpp
639 lines (575 loc) · 19.9 KB
/
environment.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
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
#include "environment.hpp"
#include <cassert>
#include <stack>
#include <string>
#include <bitset>
#include <climits>
#include "util.hpp"
class next;
class next;
CoinFlip::CoinFlip(options_t &options) {
// Determine the probability of the coin landing on heads
p = 1.0;
if (options.count("coin-flip-p") > 0) {
strExtract(options["coin-flip-p"], p);
}
assert(0.0 <= p);
assert(p <= 1.0);
// Set up the initial observation
m_observation = rand01() < p ? 1 : 0;
m_reward = 0;
}
// Observes 1 (heads) with probability p and 0 (tails) with probability 1 - p.
// Observations are independent of the agent's actions. Gives a reward of 1 if
// the agent correctly predicts the next observation and 0 otherwise.
void CoinFlip::performAction(action_t action) {
m_observation = rand01() < p ? 1 : 0;
m_reward = action == m_observation ? 1 : 0;
}
/*
* the maze is coded as a sequence of percepts of the free cells
* in the config file.
* This is a depth first sequence.
* Each node would be represented by a structure which has 4
* pointers to represent the 4 possible next nodes from there.
* Pointer is NULL if there is a wall in any particular direction.
* pointer index: 0-up, 1-right, 2-down, 3-left
*/
CheeseMaze::CheeseMaze(options_t &options) {
int mouse_pos = 1;
int cheese_pos = 1;
//setup the environment according to the config file
if (options.count("maze-structure") > 0) {
strExtract(options["maze-structure"], m_maze_conf);
}
if (options.count("mouse-pos") > 0) {
strExtract(options["mouse-pos"], mouse_pos);
}
if (options.count("cheese-pos") > 0) {
strExtract(options["cheese-pos"], cheese_pos);
}
//Create the nodes of the maze.
int i = 0;
int count = 1;
char num[3];
int j = 0;
unsigned int per;
node_t *curr_node;
//stack of pointers of a pointer to a node.
/*Basic depth first search algorithm involves pushing into a stack the
* neighbouring nodes. Since we do not know the graph beforehand,
* we can push the addresses of the pointers in the structures which need
* to have values assigned, which we can find out based on the value of
* percept.
*/
std::stack<node_t*> nodes;
do {
if (m_maze_conf[i] == ',' || m_maze_conf[i] == '\0') {
num[j] = '\0';
std::istringstream(num) >> per;
node_t *new_node = new node_t;
new_node->percept = per;
if (count == mouse_pos)
m_mouse_start = new_node;
if (count == cheese_pos)
m_cheese_node = new_node;
for (int k = 0; k < 4; k++)
new_node->next[k] = NULL;
//if stack is empty, meaning all edges will have to be connected
if (nodes.empty()) {
if ((new_node->percept & 8) == 0)
nodes.push(new_node);
if ((new_node->percept & 4) == 0)
nodes.push(new_node);
if ((new_node->percept & 2) == 0)
nodes.push(new_node);
if ((new_node->percept & 1) == 0)
nodes.push(new_node);
} else {
curr_node = nodes.top();
nodes.pop();
//connecting node is below the latest node
if (((curr_node->percept & 8) == 0)
&& (curr_node->next[0] == NULL)) {
new_node->next[2] = curr_node;
curr_node->next[0] = new_node;
}
//connecting node is on the left of the latest node
else if (((curr_node->percept & 4) == 0)
&& (curr_node->next[1] == NULL)) {
new_node->next[3] = curr_node;
curr_node->next[1] = new_node;
}
//connecting node is above the latest node
else if (((curr_node->percept & 2) == 0)
&& (curr_node->next[2] == NULL)) {
new_node->next[0] = curr_node;
curr_node->next[2] = new_node;
} else if (((curr_node->percept & 1) == 0)
&& (curr_node->next[3] == NULL)) {
new_node->next[1] = curr_node;
curr_node->next[3] = new_node;
}
//push the node into the stack for every free node which is still unassigned.
if (((new_node->percept & 8) == 0)
&& (new_node->next[0] == NULL))
nodes.push(new_node);
if (((new_node->percept & 4) == 0)
&& (new_node->next[1] == NULL))
nodes.push(new_node);
if (((new_node->percept & 2) == 0)
&& (new_node->next[2] == NULL))
nodes.push(new_node);
if (((new_node->percept & 1) == 0)
&& (new_node->next[3] == NULL))
nodes.push(new_node);
}
j = 0;
count++;
} else {
num[j] = m_maze_conf[i];
j++;
}
} while (m_maze_conf[i++] != '\0');
m_current_node = m_mouse_start;
//setting initial observation
m_observation = m_current_node->percept;
m_reward = 10;
}
void CheeseMaze::performAction(action_t action) {
//action takes agent into wall
if (m_current_node->next[action] == NULL) {
m_reward = 0;
return;
}
//action takes agent into free cell
else
m_current_node = m_current_node->next[action];
//set percept for agent
m_observation = m_current_node->percept;
m_reward = m_current_node == m_cheese_node ? 20 : 9;
}
void CheeseMaze::envReset() {
m_current_node = m_mouse_start;
m_reward = 10;
m_observation = m_current_node->percept;
}
bool CheeseMaze::isFinished() const {
return m_current_node == m_cheese_node ? 1 : 0;
}
//Creating the environment
ExtTiger::ExtTiger(options_t &options) {
m_p = 1.0;
//determine the probability of listening correctly
if (options.count("listen-p") > 0) {
strExtract(options["listen-p"], m_p);
}
assert(0.0 <= m_p);
assert(m_p <= 1.0);
m_standing = 0; //player is sitting
m_tiger = rand01() < 0.5 ? 0 : 1; //tiger behind left door with 0.5 probability.
//initial observation
m_observation = 0;
m_reward = 100;
}
//the observation will be correct with probability of p.
void ExtTiger::performAction(action_t action) {
switch (action) {
/*
* Agent tries to stand
* STATE REWARD
* sitting -1
* standing -10
*/
case 0:
m_reward = m_standing ? -10 : -1;
m_standing = 1;
break;
/*
* Agent ties to open left door
* STATE REWARD
* sitting -10
* standing(tiger behind left door) -100
* standing(tiger behind right door) 30
*/
case 2:
if (m_standing) {
m_reward = m_tiger ? -100 : 30;
} else {
m_reward = -10;
}
break;
/*
* Agent tries to open right door
* STATE REWARD
* sitting -10
* Standing(tiger behind left door) 30
* Standing(tiger behind right door) -100
*/
case 3:
if (m_standing) {
m_reward = m_tiger ? 30 : -100;
} else {
m_reward = -10;
}
break;
/*
* Agent tries to listen
* STATE REWARD
* sitting -1
* standing -10
*/
case 1:
if (!m_standing) {
if (m_tiger == 1)
m_observation = rand01() < m_p ? 1 : 2;
else
m_observation = rand01() < m_p ? 2 : 1;
m_reward = -1;
} else {
m_reward = -10;
}
}
m_reward = (int) m_reward + 100;
}
//check if the environment is finished
bool ExtTiger::isFinished() const {
if (m_reward == 130 || m_reward == 0)
return 1;
else
return 0;
}
//reset the enviroment
void ExtTiger::envReset() {
m_standing = 0; //player is sitting
m_tiger = rand01() < 0.5 ? 0 : 1; //tiger behind left door with 0.5 probability.
//initial observation
m_observation = 0;
m_reward = 100;
}
/*Tic Tac Toe environment.*/
TicTacToe::TicTacToe(options_t &options) {
for (int i = 0; i < 9; i++)
m_board[i] = 0;
m_freeCells = 9;
m_finished = 0;
//return the initial percept,
m_observation = calBoardVal();
m_reward = 3;
}
void TicTacToe::performAction(action_t action) {
if (m_board[action] != 0 && m_freeCells != 0) //illegal move
{
m_reward = 0;
return; //Obverstaion will not change so there is no need to re-calculate
} else {
m_board[action] = 2;
m_freeCells--;
if (check_winner() == 2) //agent won the game
{
m_reward = 5;
m_observation = calBoardVal();
m_finished = 1;
return;
} else if (m_freeCells == 0) //game is a draw
{
m_reward = 4;
m_observation = calBoardVal();
m_finished = 1;
return;
} else {
env_move();
if (check_winner() == 1) //agent lost the game
{
m_reward = 1;
m_observation = calBoardVal();
m_finished = 1;
return;
} else if (m_freeCells != 0) //game has not yet ended
{
m_reward = 3;
m_observation = calBoardVal();
return;
} else {
m_reward = 4;
m_observation = calBoardVal();
m_finished = 1;
return;
}
}
}
}
//reset the environment
void TicTacToe::envReset() {
for (int i = 0; i < 9; i++)
m_board[i] = 0;
m_freeCells = 9;
m_finished = 0;
//return the initial percept,
m_observation = calBoardVal();
m_reward = 3;
}
bool TicTacToe::isFinished() const {
return m_finished;
}
/*
Biased Rock Paper Scissors environment*
Move:
0 : Rock
1 : Paper
2 : Scissors
*/
BRockPaperScissors::BRockPaperScissors(options_t &options) {
m_move = (int) (rand01() * 3);
//return the initial precept
m_observation = m_move;
m_reward = 1;
}
void BRockPaperScissors::performAction(action_t action) {
m_move = m_reward == 0 ? m_move : (int) (rand01() * 3);
if (action != m_move) {
switch (m_move) {
case 0:
m_reward = action == 1 ? 2 : 0;
break;
case 1:
m_reward = action == 2 ? 2 : 0;
break;
case 2:
m_reward = action == 0 ? 2 : 0;
}
} else {
m_reward = 1;
}
m_observation = m_move;
}
/* Pacman environment
*/
Pacman::Pacman(options_t &options) {
if (options.count("power-pill-time") > 0) {
strExtract(options["power-pill-time"], m_power_pill_time);
}
if (options.count("ghost-follow-time") > 0) {
strExtract(options["ghost-follow-time"], m_ghost_follow_time);
}
//encoding the structure of the maze
for (int i = 0; i < 21; i++)
for (int j = 0; j < 19; j++)
m_maze[i][j].isFreeCell = maze1[i][j];
std::bitset<4> per;
for (int i = 0; i < 21; i++)
for (int j = 0; j < 19; j++) {
if (m_maze[i][j].isFreeCell) {
!m_maze[i - 1][j].isFreeCell ? per.set(3, 1) : per.set(3, 0);
!m_maze[i][j + 1].isFreeCell ? per.set(2, 1) : per.set(2, 0);
!m_maze[i + 1][j].isFreeCell ? per.set(1, 1) : per.set(1, 0);
!m_maze[i][j - 1].isFreeCell ? per.set(0, 1) : per.set(0, 0);
m_maze[i][j].wall = per.to_ulong() & INT_MAX;
if (rand01() < 0.5)
m_maze[i][j].contents = 1;
else
m_maze[i][j].contents = 0;
} else {
m_maze[i][j].wall = 15;
m_maze[i][j].contents = -1;
}
}
m_maze[9][0].wall = 10;
m_maze[9][18].wall = 10;
if (rand01() < 0.5)
m_maze[9][0].contents = 1;
else
m_maze[9][0].contents = 0;
if (rand01() < 0.5)
m_maze[9][18].contents = 1;
else
m_maze[9][18].contents = 0;
m_maze[1][3].contents = 2;
m_maze[17][3].contents = 2;
m_maze[1][15].contents = 2;
m_maze[17][15].contents = 2;
//initialising the ghosts
for (int i = 0; i < 4; i++) {
ghost[i].x = 8 + (int) (i / 2);
ghost[i].y = 9 + (int) (i % 2);
ghost[i].state = 1;
m_ghost_timer[i] = -1 * m_ghost_follow_time;
assert(m_maze[ghost[i].x][ghost[i].y].isFreeCell);
}
//initialising pacman
m_pacman.x = 9;
m_pacman.y = 0;
m_pacman.state = 0;
m_maze[13][9].contents = 0;
m_power_pill_counter = 0;
m_finished = false;
m_observation = ((m_maze[m_pacman.x][m_pacman.y].wall & 15) << 12)
| ((seeGhost() & 15) << 8) | ((smellFood() & 7) << 5)
| ((seeFood() & 15) << 1) | m_pacman.state;
m_reward = 60;
}
void Pacman::performAction(action_t action) {
m_reward = 60;
int pac_x_shift = (2 - (int) action) % 2;
int pac_y_shift = ((int) action - 1) % 2;
assert(action == 0 ? pac_x_shift == 0 : true);
assert(pac_x_shift == 0 || pac_x_shift == -1 || pac_x_shift == 1);
assert(pac_y_shift == 0 || pac_y_shift == -1 || pac_y_shift == 1);
assert(pac_x_shift == 0 ? pac_y_shift != 0 : pac_y_shift == 0);
//adding condition for loop back in row 9
if (m_pacman.x == 9 && m_pacman.y == 0 && pac_y_shift == -1) {
pac_y_shift = 18;
} else if (m_pacman.x == 9 && m_pacman.y == 18 && pac_y_shift == 1) {
pac_y_shift = -18;
}
//power pill effect fades when counter reaches 0
if (m_power_pill_counter-- > 1)
m_pacman.state = 0;
if (m_maze[m_pacman.x + pac_x_shift][m_pacman.y + pac_y_shift].isFreeCell) {
m_pacman.x = m_pacman.x + pac_x_shift;
m_pacman.y = m_pacman.y + pac_y_shift;
assert(m_maze[m_pacman.x][m_pacman.y].isFreeCell);
if (!isCaught()) {
if (m_maze[m_pacman.x][m_pacman.y].contents == 1) //pacman moved to a cell with food
{
m_maze[m_pacman.x][m_pacman.y].contents = 0;
m_reward += 10;
} else if (m_maze[m_pacman.x][m_pacman.y].contents == 2) //POWER PILL!!!
{
m_pacman.state = 1;
m_power_pill_counter = m_power_pill_time;
} else
//empty cell
m_reward += -1;
} else //pacman ran into the ghost
{
m_reward += -50;
m_finished = true;
m_observation = ((m_maze[m_pacman.x][m_pacman.y].wall & 15) << 12)
| ((seeGhost() & 15) << 8) | ((smellFood() & 7) << 5)
| ((seeFood() & 15) << 1) | m_pacman.state;
return;
}
} else
//pacman ran into the wall
m_reward += -10;
for (int i = 0; i < 4; i++) {
pos curr_ghost = ghost[i];
if (ghost[i].state) {
if (manhattan_dist(ghost[i], m_pacman) < 5) {
if (m_ghost_timer[i]-- > 0
|| m_ghost_timer[i] <= -1 * m_ghost_follow_time)
//ghost follows the pacman for the follow time set in the configuration file and then takes random moves for the same time
{
if (m_ghost_timer[i] < 0) {
m_ghost_timer[i] = m_ghost_follow_time;
}
manMove(i);
}
} else {
int movableCells[4][2];
int count = 0;
for (int j = 3; j >= 0; j--) //check each direction for empty cell
{
int xshift = (2 - j) % 2;
assert(j == 0 ? xshift == 0 : true);
assert(xshift == 0 || xshift == -1 || xshift == 1);
int yshift = (j - 1) % 2;
assert(yshift == 0 || yshift == -1 || yshift == 1);
assert(xshift == 0 ? yshift != 0 : yshift == 0);
//adding condition for loop back in row 9
if (ghost[i].x == 9 && ghost[i].y == 0 && yshift == -1) {
yshift = 18;
} else if (ghost[i].x == 9 && ghost[i].y == 18
&& yshift == 1) {
yshift = -18;
}
if (m_maze[ghost[i].x + xshift][ghost[i].y + yshift].isFreeCell) {
//check for collisions with other ghosts
bool ghost_collision = false;
for (int k = 0; k < 4; k++) {
if (ghost[k].x == (ghost[i].x + xshift)
&& ghost[k].y == (ghost[i].y + yshift)) {
ghost_collision = true;
}
}
if (!ghost_collision) {
movableCells[count][0] = ghost[i].x + xshift;
movableCells[count++][1] = ghost[i].y + yshift;
}
}
}
if (count != 0) {
int move = (int) (rand01() * count);
ghost[i].x = movableCells[move][0];
ghost[i].y = movableCells[move][1];
}
}
assert(ghost[i].x >= 0 && ghost[i].x < 21);
assert(ghost[i].y >= 0 && ghost[i].y < 19);
//Confirm ghost is moving into a free cell
assert(m_maze[ghost[i].x][ghost[i].y].isFreeCell);
//confirm ghost is not moving into the same cell as another ghost or the pacman.
for (int i_1 = 0; i_1 < 4; i_1++) {
for (int i_2 = 0; i_2 < 4; i_2++) {
assert(
(ghost[i_1].x != ghost[i_2].x)
|| (ghost[i_1].y != ghost[i_2].y)
|| (i_1 == i_2));
}
}
} else {
eatenGhostMove(i);
}
}
if (isCaught()) {
m_reward += -50;
m_finished = true;
}
m_observation = ((m_maze[m_pacman.x][m_pacman.y].wall & 15) << 12)
| ((seeGhost() & 15) << 8) | ((smellFood() & 7) << 5)
| ((seeFood() & 15) << 1) | m_pacman.state;
//check if all food is eaten
for (int x = 0; x < 21; x++)
for (int y = 0; y < 19; y++)
if (m_maze[x][y].contents == 1)
return;
//agent wins
m_finished = true;
m_reward += 100;
}
bool Pacman::isFinished(void) const {
return m_finished;
}
void Pacman::envReset(void) {
for (int i = 0; i < 21; i++)
for (int j = 0; j < 19; j++) {
if (m_maze[i][j].isFreeCell) {
if (rand01() < 0.5)
m_maze[i][j].contents = 1;
else
m_maze[i][j].contents = 0;
}
}
m_maze[1][3].contents = 2;
m_maze[17][3].contents = 2;
m_maze[1][15].contents = 2;
m_maze[17][15].contents = 2;
//initialising the ghosts
for (int i = 0; i < 4; i++) {
ghost[i].x = 8 + (int) (i / 2);
ghost[i].y = 9 + (int) (i % 2);
ghost[i].state = 1;
m_ghost_timer[i] = 0;
}
//initialising pacman
m_pacman.x = 13;
m_pacman.y = 9;
m_pacman.state = 0;
m_maze[13][9].contents = 0;
m_finished = false;
m_observation = ((m_maze[m_pacman.x][m_pacman.y].wall & 15) << 12)
| ((seeGhost() & 15) << 8) | ((smellFood() & 7) << 5)
| ((seeFood() & 15) << 1) | m_pacman.state;
m_reward = 60;
}