forked from praseedm/Reinforcement_Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathma_Gui.py
189 lines (151 loc) · 5.49 KB
/
ma_Gui.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
import time
import random
from Tkinter import *
master = Tk()
Width = 80
(x, y) = (7, 7)
actions = ["up", "down", "left", "right"]
board = Canvas(master, width=x*Width, height=y*Width)
player = (0, y-1)
ma_player = (0, y-1)
score = 1
ma_score = 1
restart = False
ma_restart = False
start_time = 0.0
walk_reward = -0.1
wall_count = 5
walli = 5
walls_gui = []
dynamic_walls = {0:[(1, 1), (2, 2), (3,0), (4,2), (6,3),(3,4), (4,5)],
4:[(0, 4), (0, 5), (2,6), (4,5), (3,4),(1,3), (2,3), (6,2), (5,1), (4,1), (3,1)],
2:[(4, 0), (6,2), (3,3),(3,1), (3,2), (2,6), (1,5), (1,4), (1,3)],
5:[(4, 0), (5,5), (1,2), (6,2), (5,4), (4,3),(3,1), (3,2), (2,6), (1,5), (1,4), (1,3)],
3:[(0, 2), (0, 3), (1, 3), (2,3), (3,1), (4,1), (1,5), (2,5), (5,1), (6,2), (3,3)],
1:[(1, 1), (2, 2), (5,0), (4,2), (5,3),(3,3), (6,3)]}
walls = dynamic_walls[walli]
specials = [(6, 0, "red", 30)]
#Toggle agents visibility
show_ma = True
show_solver = True
#counts
tile = 0;
total_move = 0;
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 (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:
w=board.create_rectangle(i*Width, j*Width, (i+1)*Width, (j+1)*Width, fill="black", width=1)
walls_gui.append(w)
render_grid()
def env_change():
global walls, walls_gui, walli,start_time,restart,total_move
for gui in walls_gui :
board.delete(gui)
#walli = random.randint(0,wall_count)
if(walli == wall_count):
walli = 0
else :
walli += 1
walls = dynamic_walls[walli]
print "/n CHANGE MAZE ",walli
start_time = time.time()
for (i, j) in walls:
w=board.create_rectangle(i*Width, j*Width, (i+1)*Width, (j+1)*Width, fill="black", width=1)
walls_gui.append(w)
total_move = 0
restart = True
ma_restart =True
def try_move(dx, dy):
global player, x, y, score, walk_reward, agent, restart, tile,total_move
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):
if(show_solver):
board.coords(agent, 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)
tile += 1
total_move += 1
for (i, j, c, w) in specials:
if new_x == i and new_y == j:
score -= walk_reward
score += w
if score > 0:
print "Goal! : ", score , " move : ", tile, " tot_mov :",total_move,(" time : %s sec--" % (time.time() - start_time))
# else:
# print "Goal! : ", score
restart = True
return
#print "score: ", score
def ma_try_move(dx, dy):
global ma_player, x, y, ma_score, walk_reward, ma_agent, ma_restart
if ma_restart == True:
ma_restart_game()
new_x = ma_player[0] + dx
new_y = ma_player[1] + dy
ma_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):
if(show_ma):
board.coords(ma_agent, new_x*Width+Width*2/10, new_y*Width+Width*2/10, new_x*Width+Width*8/10, new_y*Width+Width*8/10)
ma_player = (new_x, new_y)
for (i, j, c, w) in specials:
if new_x == i and new_y == j:
ma_score -= walk_reward
ma_score += w
'''if ma_score > 0:
print "2nd agent Goal! : ", ma_score'''
# else:
# print "Goal! : ", score
ma_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 ma_restart_game():
global ma_player, ma_score, ma_agent, ma_restart
ma_player = (0, y-1)
ma_score = 1
ma_restart = False
if(show_ma):
board.coords(ma_agent, ma_player[0]*Width+Width*2/10, ma_player[1]*Width+Width*2/10, ma_player[0]*Width+Width*8/10, ma_player[1]*Width+Width*8/10)
def restart_game():
global player, score, agent, restart, tile
player = (0, y-1)
score = 1
restart = False
tile = 0
if(show_solver):
board.coords(agent, 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
def ma_has_restarted():
return ma_restart
master.bind("<Up>", call_up)
master.bind("<Down>", call_down)
master.bind("<Right>", call_right)
master.bind("<Left>", call_left)
if(show_solver) :
agent = 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="yellow", width=1, tag="agent")
if(show_ma):
ma_agent = 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="blue", width=1, tag="agent")
board.grid(row=0, column=0)
def start_game():
global start_time
start_time = time.time()
master.mainloop()