forked from llSourcell/q_learning_demo
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathWorld.py
More file actions
194 lines (158 loc) · 6.18 KB
/
World.py
File metadata and controls
194 lines (158 loc) · 6.18 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
__author__ = 'philippe'
from Tkinter import *
import numpy as np
from copy import copy, deepcopy
master = Tk()
triangle_size = 0.1
cell_score_min = -0.2
cell_score_max = 0.2
Width = 20
(x, y) = (25, 25)
actions = ["up", "down", "left", "right"]
red_blocks = 3
green_blocks = 2
board = Canvas(master, width=x*Width, height=y*Width)
score = 1
restart = False
walk_reward = -0.04
# Perform cellular automata to generate a random grid layout
iter_max = 7
map_grid = []
for i in range(x):
map_row = []
for j in range(y):
map_row.append(0)
map_grid.append(map_row)
for i in range(1,x-1):
for j in range(1,y-1):
map_grid[i][j] = np.random.choice([0,1], p=[0.38, 0.62])
for iter_t in range(iter_max):
new_map_grid = deepcopy(map_grid)
for i in range(1,x-1):
for j in range(1,y-1):
neighbour_score = 0
for i1 in range(-1,2):
for j1 in range(-1,2):
if (i1 != 0 or j1 != 0) and map_grid[i+i1][j+j1] == 1:
neighbour_score += 1
# print neighbour_score
if neighbour_score > 4:
new_map_grid[i][j] = 1
else:
new_map_grid[i][j] = 0
map_grid = deepcopy(new_map_grid)
walls = []
for i in range(0,x):
for j in range(0,y):
if map_grid[i][j] == 1:
walls.append((i,j))
# Randomly initilaize player where there is no wall
player = (np.random.randint(x), np.random.randint(y))
while map_grid[player[0]][player[1]] == 1:
player = (np.random.randint(x), np.random.randint(y))
orig_player = deepcopy(player)
specials = []
for i in range(red_blocks):
gen_pos = (np.random.randint(x), np.random.randint(y))
while map_grid[gen_pos[0]][gen_pos[1]] == 1:
gen_pos = (np.random.randint(x), np.random.randint(y))
specials.append((gen_pos[0], gen_pos[1], "red", -1))
for i in range(green_blocks):
gen_pos = (np.random.randint(x), np.random.randint(y))
while map_grid[gen_pos[0]][gen_pos[1]] == 1:
gen_pos = (np.random.randint(x), np.random.randint(y))
specials.append((gen_pos[0], gen_pos[1], "green", 1))
cell_scores = {}
def create_triangle(i, j, action):
if action == actions[0]:
return board.create_polygon((i+0.5-triangle_size)*Width, (j+triangle_size)*Width,
(i+0.5+triangle_size)*Width, (j+triangle_size)*Width,
(i+0.5)*Width, j*Width,
fill="white", width=1)
elif action == actions[1]:
return board.create_polygon((i+0.5-triangle_size)*Width, (j+1-triangle_size)*Width,
(i+0.5+triangle_size)*Width, (j+1-triangle_size)*Width,
(i+0.5)*Width, (j+1)*Width,
fill="white", width=1)
elif action == actions[2]:
return board.create_polygon((i+triangle_size)*Width, (j+0.5-triangle_size)*Width,
(i+triangle_size)*Width, (j+0.5+triangle_size)*Width,
i*Width, (j+0.5)*Width,
fill="white", width=1)
elif action == actions[3]:
return board.create_polygon((i+1-triangle_size)*Width, (j+0.5-triangle_size)*Width,
(i+1-triangle_size)*Width, (j+0.5+triangle_size)*Width,
(i+1)*Width, (j+0.5)*Width,
fill="white", width=1)
def render_grid():
global specials, walls, Width, x, y, player
for i in range(x):
for j in range(y):
board.create_rectangle(i*Width, j*Width, (i+1)*Width, (j+1)*Width, fill="white", width=1)
temp = {}
for action in actions:
temp[action] = create_triangle(i, j, action)
cell_scores[(i,j)] = temp
for (i, j, c, w) in specials:
board.create_rectangle(i*Width, j*Width, (i+1)*Width, (j+1)*Width, fill=c, width=1)
for (i, j) in walls:
board.create_rectangle(i*Width, j*Width, (i+1)*Width, (j+1)*Width, fill="black", width=1)
render_grid()
def set_cell_score(state, action, val):
global cell_score_min, cell_score_max
triangle = cell_scores[state][action]
green_dec = int(min(255, max(0, (val - cell_score_min) * 255.0 / (cell_score_max - cell_score_min))))
green = hex(green_dec)[2:]
red = hex(255-green_dec)[2:]
if len(red) == 1:
red += "0"
if len(green) == 1:
green += "0"
color = "#" + red + green + "00"
board.itemconfigure(triangle, fill=color)
def try_move(dx, dy):
global player, x, y, score, walk_reward, me, restart
if restart == True:
restart_game()
new_x = player[0] + dx
new_y = player[1] + dy
score += walk_reward
if (new_x >= 0) and (new_x < x) and (new_y >= 0) and (new_y < y) and not ((new_x, new_y) in walls):
board.coords(me, new_x*Width+Width*2/10, new_y*Width+Width*2/10, new_x*Width+Width*8/10, new_y*Width+Width*8/10)
player = (new_x, new_y)
for (i, j, c, w) in specials:
if new_x == i and new_y == j:
score -= walk_reward
score += w
if score > 0:
print "Success! score: ", score
else:
print "Fail! score: ", score
restart = True
return
#print "score: ", score
def call_up(event):
try_move(0, -1)
def call_down(event):
try_move(0, 1)
def call_left(event):
try_move(-1, 0)
def call_right(event):
try_move(1, 0)
def restart_game():
global player, score, me, restart
player = deepcopy(orig_player)
score = 1
restart = False
board.coords(me, player[0]*Width+Width*2/10, player[1]*Width+Width*2/10, player[0]*Width+Width*8/10, player[1]*Width+Width*8/10)
def has_restarted():
return restart
master.bind("<Up>", call_up)
master.bind("<Down>", call_down)
master.bind("<Right>", call_right)
master.bind("<Left>", call_left)
me = board.create_rectangle(player[0]*Width+Width*2/10, player[1]*Width+Width*2/10,
player[0]*Width+Width*8/10, player[1]*Width+Width*8/10, fill="orange", width=1, tag="me")
board.grid(row=0, column=0)
def start_game():
master.mainloop()