-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsr_main.py
298 lines (221 loc) · 8.48 KB
/
sr_main.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
import json
import mazes
import sys, os
import argparse
import numpy as np
from math import sqrt
import matplotlib.pyplot as plt
from itertools import product
from collections import OrderedDict
twobytwo = ['####',
'#P #',
'# G#',
'####']
tenbyten = ['############',
'#P #',
'# #',
'# #',
'# #',
'# #',
'# #',
'# #',
'# #',
'# #',
'# G#',
'############']
def flat2xy(f, ncol):
f = f + 1 # f indexes from 0, gridworld indexes from 1
y = f % ncol
x = (f-y)/ncol + 1
return x, y
def xy2flat(x, y, ncol):
f = (x-1)*ncol+y
f = f - 1 # f indexes from 0, gridworld indexes from 1
return f
# Take a random action
def random_policy(rnd):
action = rnd.randint(0, 4, 1)[0]
return action
def parse_obs(obs, nrow, ncol):
state_mtx = np.array(obs.layers['P'], dtype=np.float)
state_mtx = state_mtx[1:nrow+1, 1:ncol+1].flatten()
return state_mtx.argmax()
def setup_maze(maze_type, start_row, start_col):
if maze_type == 'twobytwo':
maze_init = mazes.make_maze(twobytwo, 'twobytwo_R1')
maze_update = mazes.make_maze(twobytwo, 'twobytwo_R2')
elif maze_type == 'tenbyten':
maze_init = mazes.make_maze(tenbyten, 'tenbyten_R1')
maze_update = mazes.make_maze(tenbyten, 'tenbyten_R2')
# Place engines in play mode
maze_init.its_showtime()
maze_update.its_showtime()
# Move agent to starting position
maze_init._sprites_and_drapes['P']._teleport((start_row, start_col))
maze_update._sprites_and_drapes['P']._teleport((start_row, start_col))
return maze_init, maze_update
def compute_transition_mtx(config):
maze_type = config['maze_type']
nrow = config['maze_params']['row']
ncol = config['maze_params']['col']
start_row = config['maze_params']['start_row']
start_col = config['maze_params']['start_col']
state_len = nrow*ncol
Trans_mtx = np.zeros((state_len, state_len))
maze, _ = setup_maze(maze_type, start_row, start_col)
for x, y in product(range(1, nrow+1), range(1, ncol+1)):
S = xy2flat(x, y, ncol)
for a in range(4):
maze, _ = setup_maze(maze_type, start_row, start_col)
maze._sprites_and_drapes['P']._teleport((x, y))
if (x, y) == (nrow, ncol) or maze._game_over:
S_prime = xy2flat(nrow, ncol, ncol)
else:
# Apply action A to current maze, get reward R, and new state S'
obs, R, _ = maze.play(a)
S_prime = parse_obs(obs, nrow, ncol)
Trans_mtx[S, S_prime] += 0.25
return Trans_mtx
def compute_reward_vector(config, ):
maze_type = config['maze_type']
nrow = config['maze_params']['row']
ncol = config['maze_params']['col']
start_row = config['maze_params']['start_row']
start_col = config['maze_params']['start_col']
state_len = nrow*ncol
reward_vec_r1 = np.zeros((state_len, 1))
reward_vec_r2 = np.zeros((state_len, 1))
maze_r1, maze_r2 = setup_maze(maze_type, start_row, start_col)
for x, y in product(range(1, nrow+1), range(1, ncol+1)):
S = xy2flat(x, y, ncol)
for a in range(4):
maze_r1, _ = setup_maze(maze_type, start_row, start_col)
maze_r1._sprites_and_drapes['P']._teleport((x, y))
if (x, y) == (nrow, ncol) or maze_r1._game_over:
R = 0.0
else:
# Apply action A to current maze, get reward R, and new state S'
obs, R, _ = maze_r1.play(a)
reward_vec_r1[S] += 0.25 * R
maze_r1, maze_r2 = setup_maze(maze_type, start_row, start_col)
for x, y in product(range(1, nrow+1), range(1, ncol+1)):
S = xy2flat(x, y, ncol)
for a in range(4):
_, maze_r2 = setup_maze(maze_type, start_row, start_col)
maze_r2._sprites_and_drapes['P']._teleport((x, y))
if (x, y) == (nrow, ncol) or maze_r2._game_over:
R = 0.0
else:
# Apply action A to current maze, get reward R, and new state S'
obs, R, _ = maze_r2.play(a)
reward_vec_r2[S] += 0.25 * R
return reward_vec_r1, reward_vec_r2
# Indicator Function
def indicator(s, j):
if s == j:
return 1
else:
return 0
# Track2 Q1
def run_experiment(config):
# Initializations
switch_reward = config['switch_reward']
maze_type = config['maze_type']
terminal_step = config['terminal_step']
switch_step = config['switch_reward_at_step']
episode_len = config['episode_length']
nrow = config['maze_params']['row']
ncol = config['maze_params']['col']
start_row = config['maze_params']['start_row']
start_col = config['maze_params']['start_col']
alpha = config['learning_alg_params']['alpha']
gamma = config['learning_alg_params']['gamma']
state_len = nrow*ncol
# Don't need all these guys
rnd = np.random.RandomState(24)
S = xy2flat(start_row, start_col, ncol)
S_prime = S
# Initialize mazes (r_1, r_2) and agent at starting position
maze_init, maze_update = setup_maze(maze_type, start_row, start_col)
curr_maze = maze_init
step = 0
episode = 0
episode_step = 0
result = OrderedDict()
Phi_pi = np.zeros((state_len, state_len))
V_pi = np.zeros((state_len,1))
result['config'] = config
while step < terminal_step:
# Reset episode:
if episode_step >= episode_len:
S = xy2flat(start_row, start_col, ncol)
S_prime = S
maze_init, maze_update = setup_maze(maze_type, start_row, start_col)
curr_maze = maze_init # only doing this to reset the agent to the starting position, the next if statement will actually correct the map if need be
episode_step = 0
episode += 1
# The maze evolves after switch_step:
if switch_reward:
if step <= switch_step:
curr_maze = maze_init
else:
old_row, old_col = \
curr_maze._sprites_and_drapes['P']._virtual_row, \
curr_maze._sprites_and_drapes['P']._virtual_col
maze_update._sprites_and_drapes['P']._teleport((old_row, old_col))
curr_maze = maze_update
# Select Action A
A = random_policy(rnd)
if curr_maze._game_over:
A = random_policy(rnd)
R = 0.
S_prime = S
else:
# Apply action A to current maze, get reward R, and new state S'
obs, R, _ = curr_maze.play(A)
S_prime = parse_obs(obs, nrow, ncol)
for j in range(state_len):
# Successor Representation Update
Phi_pi[S, j] = Phi_pi[S, j] + alpha*(indicator(S, j)
+ gamma*Phi_pi[S_prime, j]
- Phi_pi[S, j])
# Value Update
V_pi[S] = V_pi[S] + alpha*(R + gamma*V_pi[S_prime] - V_pi[S])
experience = {'S': S,
'A': A,
'R': R,
'S_prime': S_prime
}
result[step] = {'Phi_pi': Phi_pi.copy(),
'V_pi': V_pi.copy(),
'experience': experience
}
# Update to new state
S = S_prime
step += 1
episode_step += 1
return result
def main(argv):
parser = argparse.ArgumentParser()
parser.add_argument('config_file', type=str, help="Configuration file path. e.g. blocking.config")
args = parser.parse_args()
# Reading config file
config_file_path = args.config_file
if not os.path.exists(config_file_path):
raise argparse.ArgumentTypeError('Not a valid config file')
with open(config_file_path, 'r') as config_fd:
config = json.load(config_fd)
result = run_learning_sr(config)
# cum_reward = 0
# cum_reward_lst = []
# for i in range(len(result)):
# cum_reward += result[i]['experience']['R']
# cum_reward_lst.append(cum_reward)
#
# print("Cumulative reward: " + str(cum_reward_lst[-1]))
# plt.plot(cum_reward_lst)
# plt.ylabel('Cumulative Rewards')
# plt.xlabel('Step number')
# plt.show()
if __name__ == '__main__':
main(sys.argv)