-
Notifications
You must be signed in to change notification settings - Fork 1
/
visual_run.py
172 lines (170 loc) · 6.1 KB
/
visual_run.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
from utilities import MrX,detective,observation,graph_utils
import numpy as np
import matplotlib.pyplot as plt
import game
from utilities import mcts
import pygame
#
reward_x,victory = [],0
for i in range(0,1):
# Start the run
G = game.game()
pygame.init()
win = pygame.display.set_mode((1000, 500))
pygame.display.set_caption("Example Run")
win.fill((0, 0, 0))
for i in range(20):
for j in range(10):
pygame.draw.circle(win, (255,255,255), (50*i+25, 50*j+25), 20, 5)
pygame.draw.circle(win, (255,0,0), ((G.X.position%20)*50+25, (G.X.position//20)*50+25), 15, 15)
for t in range(4):
pygame.draw.circle(win, (255,255,0), ((G.detectives[t].position%20)*50+25, (G.detectives[t].position//20)*50+25), 15, 15)
pygame.display.update()
type = ["detective","x"]
hide_move = [3,6,9]
multi_move = []
print("\n ################### START GAME ########################################")
print(G.M)
pygame.time.delay(1000)
while(not G.finish()):
win.fill((0, 0, 0))
move_no = G.move
print("List of actions ", G.X.list_actions(G.board,G.detectives))
print("\nMove No", move_no + 1, "Start -------------------------------------------------------------- \n")
if(move_no in hide_move):
(_,rew,_,_)=G.take_action(None,type[1],[4],0,"random")
print('\n')
elif(move_no in multi_move):
print("Multi Move")
(_,rew,_,_)=G.take_action(None,type[1],[3],0,"random")
print('\n')
else:
(_,rew,_,_)=G.take_action(None,type[1],[],0,"random")
print("\n")
for i in range(0,4):
print("Detective ", i, "taking action .. ")
if(G.end_flag):
continue
(_,rew,_,_)=G.take_action(None,type[0],[],i,"random")
print("\n")
G.update_fv()
print(G.f_x_action((2,24)).shape)
G.print_pos()
G.print_reward()
for i in range(20):
for j in range(10):
pygame.draw.circle(win, (255,255,255), (50*i+25, 50*j+25), 20, 5)
pygame.draw.circle(win, (255,0,0), ((G.X.position%20)*50+25, (G.X.position//20)*50+25), 15, 15)
for t in range(4):
pygame.draw.circle(win, (255,255,0), ((G.detectives[t].position%20)*50+25, (G.detectives[t].position//20)*50+25), 15, 15)
pygame.time.delay(1000)
pygame.display.update()
if(move_no >= 19):
victory+=1
print("\nMove No" ,move_no+1, "Over -------------------------------------------------------------- \n")
reward_x.append(G.X_reward)
for i in range(20):
for j in range(10):
pygame.draw.circle(win, (255,255,255), (50*i+25, 50*j+25), 20, 5)
pygame.draw.circle(win, (255,0,0), ((G.X.position%20)*50+25, (G.X.position//20)*50+25), 15, 15)
for t in range(4):
pygame.draw.circle(win, (255,255,0), ((G.detectives[t].position%20)*50+25, (G.detectives[t].position//20)*50+25), 15, 15)
pygame.display.update()
pygame.time.delay(1000)
win.fill((0, 0, 0))
font = pygame.font.Font('freesansbold.ttf', 64)
if G.move>=19:
text = font.render('X Won', True, (0,255,0),(0,0,128))
else:
text = font.render('Detectives Won', True, (0,255,0),(0,0,128))
textRect = text.get_rect()
textRect.center = (500, 250)
win.blit(text, textRect)
pygame.display.update()
pygame.time.delay(5000)
pygame.quit()
G = game.game()
pygame.init()
win = pygame.display.set_mode((1000, 500))
pygame.display.set_caption("Scotland Yard")
win.fill((0, 0, 0))
for i in range(20):
for j in range(10):
pygame.draw.circle(win, (255,255,255), (50*i+25, 50*j+25), 20, 5)
pygame.draw.circle(win, (255,0,0), ((G.X.position%20)*50+25, (G.X.position//20)*50+25), 15, 15)
for t in range(4):
pygame.draw.circle(win, (255,255,0), ((G.detectives[t].position%20)*50+25, (G.detectives[t].position//20)*50+25), 15, 15)
pygame.display.update()
type = ["detective","x"]
hide_move = [3,6,9]
multi_move = []
print("\n ################### START GAME ########################################")
print(G.M)
pygame.time.delay(1000)
while(not G.finish()):
win.fill((0, 0, 0))
move_no = G.move
print("List of actions ", G.X.list_actions(G.board,G.detectives))
print("\nMove No", move_no + 1, "Start -------------------------------------------------------------- \n")
if(move_no in hide_move):
(_,rew,_,_)=G.take_action(None,type[1],[4],0,"random")
print('\n')
elif(move_no in multi_move):
print("Multi Move")
(_,rew,_,_)=G.take_action(None,type[1],[3],0,"random")
print('\n')
else:
(_,rew,_,_)=G.take_action(None,type[1],[],0,"random")
print("\n")
for i in range(0,4):
print("Detective ", i, "taking action .. ")
if(G.end_flag):
continue
(_,rew,_,_)=G.take_action(None,type[0],[],i,"random")
print("\n")
G.update_fv()
print(G.f_x_action((2,24)).shape)
G.print_pos()
G.print_reward()
for i in range(20):
for j in range(10):
pygame.draw.circle(win, (255,255,255), (50*i+25, 50*j+25), 20, 5)
pygame.draw.circle(win, (255,0,0), ((G.X.position%20)*50+25, (G.X.position//20)*50+25), 15, 15)
for t in range(4):
pygame.draw.circle(win, (255,255,0), ((G.detectives[t].position%20)*50+25, (G.detectives[t].position//20)*50+25), 15, 15)
pygame.time.delay(1000)
pygame.display.update()
if(move_no >= 19):
victory+=1
print("\nMove No" ,move_no+1, "Over -------------------------------------------------------------- \n")
reward_x.append(G.X_reward)
for i in range(20):
for j in range(10):
pygame.draw.circle(win, (255,255,255), (50*i+25, 50*j+25), 20, 5)
pygame.draw.circle(win, (255,0,0), ((G.X.position%20)*50+25, (G.X.position//20)*50+25), 15, 15)
for t in range(4):
pygame.draw.circle(win, (255,255,0), ((G.detectives[t].position%20)*50+25, (G.detectives[t].position//20)*50+25), 15, 15)
pygame.display.update()
pygame.time.delay(1000)
win.fill((0, 0, 0))
font = pygame.font.Font('freesansbold.ttf', 64)
if G.move>=19:
text = font.render('X Won', True, (0,255,0),(0,0,128))
else:
text = font.render('Detectives Won', True, (0,255,0),(0,0,128))
textRect = text.get_rect()
textRect.center = (500, 250)
win.blit(text, textRect)
pygame.display.update()
pygame.time.delay(5000)
pygame.quit()
print(victory,np.mean(reward_x))
plt.plot(reward_x)
plt.xlabel("Run")
plt.ylabel("Reward for X")
# plt.title("Reward for X with Random Policies")
y_axis = [x for x in range(-40,40,5)]
x_axis = [x for x in range(1,21)]
plt.yticks(y_axis)
plt.xticks(x_axis)
plt.savefig("Result.png")