-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmdp_matrix.py
304 lines (246 loc) · 12.8 KB
/
mdp_matrix.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
import numpy as np
class MDP:
def __init__(self, T, S, R, A, act_list, terminal_states):
# State space
# Integer number of states
self.S = S
# Transition probabilities
# Form: np ndarray of shape (start_state, action, end_state)
self.T = np.array(T)
# Reward space
# Form: vector, rewards for each state
self.R = np.array(R)
# Action space
# integer, number of possible actions
self.A = A
# Possible actions in the MDP
self.actions = act_list
self.terminal_states = terminal_states
def is_terminal(self, s):
return s in self.terminal_states
class GridWorld(MDP):
def __init__(self, grid_size, reward_pos, terminal_states):
S = grid_size*grid_size
R = np.zeros((grid_size, grid_size))
# Each row of reward_pos is a tuple: x, y, reward
for row in reward_pos:
R[row[0], row[1]] = row[2]
R = R.flatten()
A = 4
act_list = ['S', 'E', 'N', 'W']
T = np.zeros((S, A, S))
for start_state in range(S):
state_i = start_state/grid_size
state_j = (start_state)%grid_size
# Actions indexed as: 0:S, 1:E, 2:N, 3:W
for act in range(A):
feas_grid = np.zeros((grid_size, grid_size))
if(act == 0 ):
if(state_i+1 < grid_size):
feas_grid[state_i+1, state_j] = 1
else:
feas_grid[state_i, state_j] = 1
elif(act == 1):
if(state_j+1 < grid_size):
feas_grid[state_i, state_j+1] = 1
else:
feas_grid[state_i, state_j] = 1
elif(act == 2):
if(state_i-1 >= 0):
feas_grid[state_i-1, state_j] = 1
else:
feas_grid[state_i, state_j] = 1
elif(act == 3):
if(state_j-1 >= 0):
feas_grid[state_i, state_j-1] = 1
else:
feas_grid[state_i, state_j] = 1
# Flatten the feasibility grid and assign to transition matrix
T[start_state, act, :] = feas_grid.flatten()
MDP.__init__(self, T, S, R, A, act_list, terminal_states)
class StochasticGridWorld(MDP):
def __init__(self, grid_size, reward_pos, terminal_states, p_success = 0.7):
S = grid_size*grid_size
R = np.zeros((grid_size, grid_size))
# Each row of reward_pos is a tuple: x, y, reward
for row in reward_pos:
R[row[0], row[1]] = row[2]
R = R.flatten()
A = 4
act_list = ['S', 'E', 'N', 'W']
T = np.zeros((S, A, S))
for start_state in range(S):
state_i = start_state/grid_size
state_j = (start_state)%grid_size
# Actions indexed as: 0:S, 1:E, 2:N, 3:W
for act in range(A):
feas_grid = np.zeros((grid_size, grid_size))
if(act == 0 ): # Going South
if state_i+1 < grid_size:
feas_grid[state_i+1, state_j] = p_success
else:
feas_grid[state_i, state_j] = p_success
if state_j+1 < grid_size:
feas_grid[state_i, state_j+1] = (1.0-p_success)/3.0
else:
feas_grid[state_i, state_j ] += (1.0-p_success)/3.0
if state_i-1 >= 0:
feas_grid[state_i-1, state_j] = (1.0-p_success)/3.0
else:
feas_grid[state_i, state_j ] += (1.0-p_success)/3.0
if state_j-1 >= 0:
feas_grid[state_i, state_j-1] = (1.0-p_success)/3.0
else:
feas_grid[state_i, state_j ] += (1.0-p_success)/3.0
elif(act == 1): # Going East
if state_j+1 < grid_size:
feas_grid[state_i, state_j+1] = p_success
else:
feas_grid[state_i, state_j] = p_success
if state_i+1 < grid_size:
feas_grid[state_i+1, state_j] = (1.0-p_success)/3.0
else:
feas_grid[state_i, state_j ] += (1.0-p_success)/3.0
if state_i-1 >= 0:
feas_grid[state_i-1, state_j] = (1.0-p_success)/3.0
else:
feas_grid[state_i, state_j ] += (1.0-p_success)/3.0
if state_j-1 >= 0:
feas_grid[state_i, state_j-1] = (1.0-p_success)/3.0
else:
feas_grid[state_i, state_j ] += (1.0-p_success)/3.0
elif(act == 2): # Going North
if state_i-1 >= 0:
feas_grid[state_i-1, state_j] = p_success
else:
feas_grid[state_i, state_j] = p_success
if state_j+1 < grid_size:
feas_grid[state_i, state_j+1] = (1.0-p_success)/3.0
else:
feas_grid[state_i, state_j ] += (1.0-p_success)/3.0
if state_i+1 < grid_size:
feas_grid[state_i+1, state_j] = (1.0-p_success)/3.0
else:
feas_grid[state_i, state_j ] += (1.0-p_success)/3.0
if state_j-1 >= 0:
feas_grid[state_i, state_j-1] = (1.0-p_success)/3.0
else:
feas_grid[state_i, state_j ] += (1.0-p_success)/3.0
elif(act == 3): # Going West
if state_j-1 >= 0:
feas_grid[state_i, state_j-1] = p_success
else:
feas_grid[state_i, state_j] = p_success
if state_j+1 < grid_size:
feas_grid[state_i, state_j+1] = (1.0-p_success)/3.0
else:
feas_grid[state_i, state_j ] += (1.0-p_success)/3.0
if state_i+1 < grid_size:
feas_grid[state_i+1, state_j] = (1.0-p_success)/3.0
else:
feas_grid[state_i, state_j ] += (1.0-p_success)/3.0
if state_i-1 >= 0:
feas_grid[state_i-1, state_j] = (1.0-p_success)/3.0
else:
feas_grid[state_i, state_j ] += (1.0-p_success)/3.0
# Flatten the feasibility grid and assign to transition matrix
T[start_state, act, :] = feas_grid.flatten()
MDP.__init__(self, T, S, R, A, act_list, terminal_states)
class WindyGridCliffMazeWorld(MDP):
def __init__(self, grid_size, reward_pos, terminal_states, traps, initial_state = [0,0], obstacles = [[i, j, 0] for i in range(10) for j in range(10)]
, p_success = 0.7):
S = grid_size*grid_size
R = np.zeros((grid_size, grid_size))
# Each row of reward_pos is a tuple: x, y, reward
for row in reward_pos:
R[row[0], row[1]] = row[2]
R = R.flatten()
for i in range(len(traps)):
if traps[i] == 1:
R[i] = -25
# Define actions
A = 4
act_list = ['S', 'E', 'N', 'W']
# Set start state value
self.initial_state = initial_state[0]*grid_size + initial_state[1]
T = np.zeros((S, A, S))
for start_state in range(S):
state_i = start_state/grid_size
state_j = (start_state)%grid_size
# Actions indexed as: 0:S, 1:E, 2:N, 3:W
for act in range(A):
feas_grid = np.zeros((grid_size, grid_size))
if traps[(state_i)*grid_size + state_j] == 1:
feas_grid[initial_state[0], initial_state[1]] = 1
else:
if(act == 0 ): # Going South
if state_i+1 < grid_size and obstacles[(state_i+1)*grid_size+state_j][2] != 1:
feas_grid[state_i+1, state_j] = p_success
else:
feas_grid[state_i, state_j] = p_success
if state_j+1 < grid_size and obstacles[(state_i)*grid_size+state_j+1][2] != 1:
feas_grid[state_i, state_j+1] = (1.0-p_success)/3.0
else:
feas_grid[state_i, state_j ] += (1.0-p_success)/3.0
if state_i-1 >= 0 and obstacles[(state_i-1)*grid_size+state_j][2] != 1:
feas_grid[state_i-1, state_j] = (1.0-p_success)/3.0
else:
feas_grid[state_i, state_j ] += (1.0-p_success)/3.0
if state_j-1 >= 0 and obstacles[(state_i)*grid_size+state_j-1][2] != 1:
feas_grid[state_i, state_j-1] = (1.0-p_success)/3.0
else:
feas_grid[state_i, state_j ] += (1.0-p_success)/3.0
elif(act == 1): # Going East
if state_j+1 < grid_size and obstacles[state_i*grid_size+state_j+1][2] != 1:
feas_grid[state_i, state_j+1] = p_success
else:
feas_grid[state_i, state_j] = p_success
if state_i+1 < grid_size and obstacles[(state_i+1)*grid_size+state_j][2] != 1:
feas_grid[state_i+1, state_j] = (1.0-p_success)/3.0
else:
feas_grid[state_i, state_j ] += (1.0-p_success)/3.0
if state_i-1 >= 0 and obstacles[(state_i-1)*grid_size+state_j][2] != 1:
feas_grid[state_i-1, state_j] = (1.0-p_success)/3.0
else:
feas_grid[state_i, state_j ] += (1.0-p_success)/3.0
if state_j-1 >= 0 and obstacles[(state_i)*grid_size+state_j-1][2] != 1:
feas_grid[state_i, state_j-1] = (1.0-p_success)/3.0
else:
feas_grid[state_i, state_j ] += (1.0-p_success)/3.0
elif(act == 2): # Going North
if state_i-1 >= 0 and obstacles[(state_i-1)*grid_size+state_j][2] != 1:
feas_grid[state_i-1, state_j] = p_success
else:
feas_grid[state_i, state_j] = p_success
if state_j+1 < grid_size and obstacles[(state_i)*grid_size+state_j+1][2] != 1:
feas_grid[state_i, state_j+1] = (1.0-p_success)/3.0
else:
feas_grid[state_i, state_j ] += (1.0-p_success)/3.0
if state_i+1 < grid_size and obstacles[(state_i+1)*grid_size+state_j][2] != 1:
feas_grid[state_i+1, state_j] = (1.0-p_success)/3.0
else:
feas_grid[state_i, state_j ] += (1.0-p_success)/3.0
if state_j-1 >= 0 and obstacles[(state_i)*grid_size+state_j-1][2] != 1:
feas_grid[state_i, state_j-1] = (1.0-p_success)/3.0
else:
feas_grid[state_i, state_j ] += (1.0-p_success)/3.0
elif(act == 3): # Going West
if state_j-1 >= 0 and obstacles[(state_i)*grid_size+state_j-1][2] != 1:
feas_grid[state_i, state_j-1] = p_success
else:
feas_grid[state_i, state_j] = p_success
if state_j+1 < grid_size and obstacles[(state_i)*grid_size+state_j+1][2] != 1:
feas_grid[state_i, state_j+1] = (1.0-p_success)/3.0
else:
feas_grid[state_i, state_j ] += (1.0-p_success)/3.0
if state_i+1 < grid_size and obstacles[(state_i+1)*grid_size+state_j][2] != 1:
feas_grid[state_i+1, state_j] = (1.0-p_success)/3.0
else:
feas_grid[state_i, state_j ] += (1.0-p_success)/3.0
if state_i-1 >= 0 and obstacles[(state_i-1)*grid_size+state_j][2] != 1:
feas_grid[state_i-1, state_j] = (1.0-p_success)/3.0
else:
feas_grid[state_i, state_j ] += (1.0-p_success)/3.0
# Flatten the feasibility grid and assign to transition matrix
T[start_state, act, :] = feas_grid.flatten()
MDP.__init__(self, T, S, R, A, act_list, terminal_states)