-
Notifications
You must be signed in to change notification settings - Fork 0
/
play.py
305 lines (264 loc) · 14.7 KB
/
play.py
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
import sys
import time
import pygame
import pickle
from RushHour4.core import Map
from RushHour4.interact import FourAgentGame
from RushHour4.utils import *
blockSize = 100
ROWS, COLS = 8, 8
WINDOW_HEIGHT = blockSize * ROWS
WINDOW_WIDTH = blockSize * COLS
BLACK, RED = (0, 0, 0), (255, 0, 0)
def main(model_type='common'):
global screen, CLOCK
agent, action = None, None
pygame.init()
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
clock = pygame.time.Clock()
screen.fill((0, 0, 0))
path = pygame.image.load('Images/1_path.png').convert_alpha()
path_rect = path.get_rect()
wall = pygame.image.load('Images/wall.png').convert_alpha()
cop_1 = pygame.image.load('Images/1_cop_1.png').convert_alpha()
cop_1_rect = cop_1.get_rect()
cop_2 = pygame.image.load('Images/1_cop_2.png').convert_alpha()
cop_2_rect = cop_2.get_rect()
cop_3 = pygame.image.load('Images/1_cop_3.png').convert_alpha()
cop_3_rect = cop_2.get_rect()
thief = pygame.image.load('Images/1_thief.png').convert_alpha()
thief_rect = thief.get_rect()
objects_original = [wall, path, path_rect, thief, thief_rect, cop_1, cop_1_rect, cop_2, cop_2_rect, cop_3, cop_3_rect]
mymap = Map(ROWS, COLS)
game = FourAgentGame(mymap, blockSize)
game.initialize()
game.setup_agents({'1': game.random_state()})
game.setup_agents({'2': game.random_state()})
game.setup_agents({'3': game.random_state()})
game.setup_agents({'x': game.random_state()})
drawGrid(game.grid, objects_original, '1')
pygame.display.update()
action_done = False
action_direction = ['up', 'down', 'left', 'right']
match_count_human_common = 0
match_count_human_1 = 0
match_count_human_2 = 0
match_count_human_3 = 0
match_count_common_1 = 0
match_count_common_2 = 0
match_count_common_3 = 0
total_steps = 1
start_q_table_common = 'Models/qtable_common.pickle' # None or Filename
with open(start_q_table_common, 'rb') as f:
q_table_common = pickle.load(f)
start_q_table_1 = 'Models/qtable_1.pickle' # None or Filename
with open(start_q_table_1, 'rb') as f:
q_table_1 = pickle.load(f)
start_q_table_2 = 'Models/qtable_2.pickle' # None or Filename
with open(start_q_table_2, 'rb') as f:
q_table_2 = pickle.load(f)
start_q_table_3 = 'Models/qtable_3.pickle' # None or Filename
with open(start_q_table_3, 'rb') as f:
q_table_3 = pickle.load(f)
while True:
next = True
drawGrid(game.grid, objects_original, '1')
pygame.display.update()
for agent in ['1', '2', '3']:
next, action_done = True, False
cop1_pos = game.locate_agent('1')
cop2_pos = game.locate_agent('2')
cop3_pos = game.locate_agent('3')
thief_pos = game.locate_agent('x')
cop1_state, cop2_state, cop3_state = get_cop_states(cop1_pos, cop2_pos, cop3_pos, thief_pos)
while next:
if not action_done:
key = pygame.key.get_pressed()
if key[pygame.K_UP]: action = 'up'
elif key[pygame.K_DOWN]: action = 'down'
elif key[pygame.K_LEFT]: action = 'left'
elif key[pygame.K_RIGHT]: action = 'right'
if action in ['up', 'down', 'left', 'right']:
game.update({agent: action})
if agent == '1':
model_1_prediction = perform_action(cop1_state, q_table_1, 0.0)
if model_1_prediction<0 or model_1_prediction>4:
model_1_prediction = 0
if action == action_direction[model_1_prediction]:
match_count_human_1 += 1
model_common_prediction = perform_action(cop1_state, q_table_common, 0.0)
if model_common_prediction<0 or model_common_prediction>4:
model_common_prediction = 0
if action == action_direction[model_common_prediction]:
match_count_human_common += 1
if action_direction[model_1_prediction] == action_direction[model_common_prediction]:
match_count_common_1 += 1
elif agent == '2':
model_2_prediction = perform_action(cop2_state, q_table_2, 0.0)
if model_2_prediction<0 or model_2_prediction>4:
model_2_prediction = 0
if action == action_direction[model_2_prediction]:
match_count_human_2 += 1
model_common_prediction = perform_action(cop2_state, q_table_common, 0.0)
if model_common_prediction<0 or model_common_prediction>4:
model_common_prediction = 0
if action == action_direction[model_common_prediction]:
match_count_human_common += 1
if action_direction[model_2_prediction] == action_direction[model_common_prediction]:
match_count_common_2 += 1
elif agent == '3':
model_3_prediction = perform_action(cop3_state, q_table_3, 0.0)
if model_3_prediction<0 or model_3_prediction>4:
model_3_prediction = 0
if action == action_direction[model_3_prediction]:
match_count_human_3 += 1
model_common_prediction = perform_action(cop3_state, q_table_common, 0.0)
if model_common_prediction<0 or model_common_prediction>4:
model_common_prediction = 0
if action == action_direction[model_common_prediction]:
match_count_human_common += 1
if action_direction[model_3_prediction] == action_direction[model_common_prediction]:
match_count_common_3 += 1
drawGrid(game.grid, objects_original)
pygame.display.update()
action_done, action = True, None
if action_done and action == None:
key = pygame.key.get_pressed()
if key[pygame.K_SPACE]:
if agent == '1':
drawGrid(game.grid, objects_original, '2')
pygame.display.update()
elif agent == '2':
drawGrid(game.grid, objects_original, '3')
pygame.display.update()
elif agent == '3':
drawGrid(game.grid, objects_original, '1')
pygame.display.update()
next = False
pygame.event.pump()
time.sleep(1)
thief_pos = game.locate_agent('x')
if game._agent_location['x'] == 'end':
total_matches_human = match_count_human_1 + match_count_human_2 + match_count_human_3
print("Human Performance VS Models:")
print('Percentage Actions Matched : Q Table 1 :', match_count_human_1 * 100 / total_steps)
print('Percentage Actions Matched : Q Table 2 :', match_count_human_2 * 100 / total_steps)
print('Percentage Actions Matched : Q Table 3 :', match_count_human_3 * 100 / total_steps)
print()
print('Percentage Actions Matched with Independent Q Tables :', total_matches_human * 100 / (total_steps*3))
print('Percentage Actions Matched with Shared Q Tables :', match_count_human_common * 100 / (total_steps*3))
total_matches_common = match_count_common_1 + match_count_common_2 + match_count_common_3
print("\n\n\n")
print("Shared Q Table VS Independent Q tables:")
print('Percentage Actions Matched : Q Table 1 :', match_count_common_1 * 100 / total_steps)
print('Percentage Actions Matched : Q Table 2 :', match_count_common_2 * 100 / total_steps)
print('Percentage Actions Matched : Q Table 3 :', match_count_common_3 * 100 / total_steps)
print('Percentage Actions Matched with Independent Q Tables :', total_matches_common * 100 / (total_steps*3))
print('Total Number Of Steps To Catch :', total_steps)
pygame.quit()
sys.exit()
else:
thief_run_direction = game.thief_run()
total_steps += 1
if thief_run_direction in game.valid_actions(thief_pos, index=True):
game.update({'x': thief_run_direction})
drawGrid(game.grid, objects_original)
pygame.display.update()
pygame.event.pump()
for event in pygame.event.get():
if event.type == pygame.QUIT:
total_matches_human = match_count_human_1 + match_count_human_2 + match_count_human_3
print("Human Performance VS Models:")
print('Percentage Actions Matched : Q Table 1 :', match_count_human_1 * 100 / total_steps)
print('Percentage Actions Matched : Q Table 2 :', match_count_human_2 * 100 / total_steps)
print('Percentage Actions Matched : Q Table 3 :', match_count_human_3 * 100 / total_steps)
print()
print('Percentage Actions Matched with Independent Q Tables :', total_matches_human * 100 / (total_steps*3))
print('Percentage Actions Matched with Shared Q Tables :', match_count_human_common * 100 / (total_steps*3))
total_matches_common = match_count_common_1 + match_count_common_2 + match_count_common_3
print("\n\n\n")
print("Shared Q Table VS Independent Q tables:")
print('Percentage Actions Matched : Q Table 1 :', match_count_common_1 * 100 / total_steps)
print('Percentage Actions Matched : Q Table 2 :', match_count_common_2 * 100 / total_steps)
print('Percentage Actions Matched : Q Table 3 :', match_count_common_3 * 100 / total_steps)
print('Percentage Actions Matched with Independent Q Tables :', total_matches_common * 100 / (total_steps*3))
print('Total Number Of Steps To Catch :', total_steps)
pygame.quit()
sys.exit()
def drawGrid(grid, objects, agent=None):
wall, path, path_rect, thief, thief_rect, cop_1, cop_1_rect, cop_2, cop_2_rect, cop_3, cop_3_rect = objects
X, Y = 0, 0
for row in range(0, WINDOW_HEIGHT, blockSize):
Y = 0
for col in range(0, WINDOW_WIDTH, blockSize):
rect = pygame.Rect(col, row, blockSize, blockSize)
if agent == None:
if grid[X][Y] == '1':
pygame.draw.rect(screen, BLACK, pygame.Rect(col, row, blockSize, blockSize))
cop_1_rect.topleft = (col, row)
screen.blit(cop_1, cop_1_rect)
if grid[X][Y] == '2':
pygame.draw.rect(screen, BLACK, pygame.Rect(col, row, blockSize, blockSize))
cop_2_rect.topleft = (col, row)
screen.blit(cop_2, cop_2_rect)
if grid[X][Y] == '3':
pygame.draw.rect(screen, BLACK, pygame.Rect(col, row, blockSize, blockSize))
cop_3_rect.topleft = (col, row)
screen.blit(cop_3, cop_3_rect)
if agent == '1':
if grid[X][Y] == '1':
pygame.draw.rect(screen, RED, pygame.Rect(col, row, blockSize, blockSize))
cop_1_rect.topleft = (col, row)
screen.blit(cop_1, cop_1_rect)
if grid[X][Y] == '2':
pygame.draw.rect(screen, BLACK, pygame.Rect(col, row, blockSize, blockSize))
cop_2_rect.topleft = (col, row)
screen.blit(cop_2, cop_2_rect)
if grid[X][Y] == '3':
pygame.draw.rect(screen, BLACK, pygame.Rect(col, row, blockSize, blockSize))
cop_3_rect.topleft = (col, row)
screen.blit(cop_3, cop_3_rect)
elif agent == '2':
if grid[X][Y] == '1':
pygame.draw.rect(screen, BLACK, pygame.Rect(col, row, blockSize, blockSize))
cop_1_rect.topleft = (col, row)
screen.blit(cop_1, cop_1_rect)
if grid[X][Y] == '2':
pygame.draw.rect(screen, RED, pygame.Rect(col, row, blockSize, blockSize))
cop_2_rect.topleft = (col, row)
screen.blit(cop_2, cop_2_rect)
if grid[X][Y] == '3':
pygame.draw.rect(screen, BLACK, pygame.Rect(col, row, blockSize, blockSize))
cop_3_rect.topleft = (col, row)
screen.blit(cop_3, cop_3_rect)
elif agent == '3':
if grid[X][Y] == '1':
pygame.draw.rect(screen, BLACK, pygame.Rect(col, row, blockSize, blockSize))
cop_1_rect.topleft = (col, row)
screen.blit(cop_1, cop_1_rect)
if grid[X][Y] == '2':
pygame.draw.rect(screen, BLACK, pygame.Rect(col, row, blockSize, blockSize))
cop_2_rect.topleft = (col, row)
screen.blit(cop_2, cop_2_rect)
if grid[X][Y] == '3':
pygame.draw.rect(screen, RED, pygame.Rect(col, row, blockSize, blockSize))
cop_3_rect.topleft = (col, row)
screen.blit(cop_3, cop_3_rect)
if grid[X][Y] == '[]':
pygame.draw.rect(screen, BLACK, pygame.Rect(col, row, blockSize, blockSize), 3)
screen.blit(wall, (col, row))
if grid[X][Y] == 'x':
pygame.draw.rect(screen, BLACK, pygame.Rect(col, row, blockSize, blockSize), 3)
thief_rect.topleft = (col, row)
screen.blit(thief, thief_rect)
if grid[X][Y] == 'o':
pygame.draw.rect(screen, BLACK, pygame.Rect(col, row, blockSize, blockSize), 3)
path_rect.topleft = (col, row)
screen.blit(path, path_rect)
Y += 1
X += 1
if __name__ == '__main__':
model_type = 'common'
if len(sys.argv) == 2:
model_type = sys.argv[1]
main(model_type)