-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption.py
431 lines (289 loc) · 14.5 KB
/
option.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
from os import stat_result
import numpy as np
import random
from utils import matrix_to_list
class Option():
def __init__(self, option, I = None, beta = None, pi = None):
self.name = option
# Set I (initiation set), beta (termination set), pi (policy)
if I is not None:
self.I = I
self.beta = beta
self.pi = pi
else:
self._setIBetaPi()
self.initiation_as_list = matrix_to_list(self.I)
self.termination_as_list = matrix_to_list(self.beta)
def __copy__(self):
return type(self)(self.name,self.I,self.beta,self.pi)
def pickAction(self, state):
action_number = self.pi[state]
if action_number == 1:
action = "left"
elif action_number == 2:
action = "up"
elif action_number == 3:
action = "right"
elif action_number == 4:
action = "down"
else:
action = "still"
# Return action number, used for intra-option model learning
return action, action_number
def execute_policy_probabilistic(self,S):
pos = np.where(S == 1)
action = self.pi[pos][0]
if not action == 0:
i = random.randint(0,len(self.termination_as_list) -1)
pos = ([self.termination_as_list[i][0]], [self.termination_as_list[i][1]])
final_state = np.zeros((8, 8))
final_state[pos] = 1
return final_state
def get_beta_state_idx(self):
list_term_index = []
for term_state in self.list_termination_states():
# reshape state to a 1D array
term_state_n = np.reshape(term_state, (64, ))
#get the index of 1 in state
term_index = np.where(term_state_n == 1)[0][0]
list_term_index.append(term_index)
return list_term_index
def get_I_state_idx(self):
list_term_index = []
for term_state in self.list_initiation_states():
# reshape state to a 1D array
term_state_n = np.reshape(term_state, (64, ))
#get the index of 1 in state
term_index = np.where(term_state_n == 1)[0][0]
list_term_index.append(term_index)
return list_term_index
def execute_policy(self, S): #starting at position S, returns the state obtained after executing option policy
# NOTE: we don't want to actually execute policies while planning, this is just a cheap patch to avoid partitioning the options in main.py by hand
pos = np.where(S == 1)
# print("pos: ", pos)
# print(self.beta)
#print(self.name)
while self.beta[pos] == 0:
action = self.pi[pos][0]
if action == 1:
x = pos[0][0]
y = pos[1][0] - 1
elif action == 2:
x = pos[0][0] - 1
y = pos[1][0]
elif action == 3:
x = pos[0][0]
y = pos[1][0] + 1
elif action == 4:
x = pos[0][0] + 1
y = pos[1][0]
elif action == 0:
print("action not defined here")
pos = ([x], [y])
final_state = np.zeros((len(self.beta), len(self.beta[0])))
final_state[pos] = 1
return final_state
def list_initiation_states(self): #Split a set of states into a list of stat_result
states = []
for i in range(8):
for j in range(8):
if self.I[i][j] == 1:
#print("here")
arr = np.zeros((8, 8))
arr[i][j] = 1
states.append(arr)
return states
def list_termination_states(self): #Split a set of states into a list of stat_result
states = []
for i in range(8):
for j in range(8):
if self.beta[i][j] == 1:
arr = np.zeros((8, 8))
arr[i][j] = 1
states.append(arr)
return states
def _setIBetaPi(self):
self.I = np.zeros((8, 8))
self.beta = np.zeros((8, 8))
self.pi = np.zeros((8, 8))
if self.name == "room_2->room_1":
self.I[0:4, 4:8] = 1 # executable from room 2
self.beta[0:4, 0:4] = 1 # terminates at room 1
self.pi[0:4, 4:8] = 1 # go left when in room 2
elif self.name == "room_3->room_1":
self.I[4:8, 0:4] = 1 # executable from room 3
self.beta[0:4, 0:4] = 1 # terminates at room 1
self.pi[4:8, 0:4] = 2 # go up when in room 3
elif self.name == "room_1_quad_2->room_1_quad_1":
self.I[0:2, 2:4] = 1 # include room 1 quad 2
self.beta[0:2, 0:2] = 1 # terminates at room 1 quad 1
self.pi[0:2, 2:4] = 1 # go left when in room 1 quad 2
elif self.name == "room_1_quad_3->room_1_quad_1":
self.I[2:4, 0:2] = 1 # include room 1 quad 3
self.beta[0:2, 0:2] = 1 # terminates at room 1 quad 1
self.pi[2:4, 0:2] = 2 # go up when in room 1 quad 3
elif self.name == "room_1_quad_1->room_1_quad_2":
self.I[0:2, 0:2] = 1 # include room 1 quad 1
self.beta[0:2, 2:4] = 1 # terminates at room 1 quad 2
self.pi[0:2, 0:2] = 3 # go right when in room 1 quad 1
elif self.name == "room_1_quad_4->room_1_quad_2":
self.I[2:4, 2:4] = 1 # include room 1 quad 4
self.beta[0:2, 2:4] = 1 # terminates at room 1 quad 2
self.pi[2:4, 2:4] = 2 # go up when in room 1 quad 4
elif self.name == "room_1_quad_1->room_1_quad_3":
self.I[0:2, 0:2] = 1 # include room 1 quad 1
self.beta[2:4, 0:2] = 1 # terminates at room 1 quad 3
self.pi[0:2, 0:2] = 4 # go down when in room 1 quad 1
elif self.name == "room_1_quad_4->room_1_quad_3":
self.I[2:4, 2:4] = 1 # include room 1 quad 4
self.beta[2:4, 0:2] = 1 # terminates at room 1 quad 3
self.pi[2:4, 2:4] = 1 # go left when in room 1 quad 4
elif self.name == "room_1_quad_2->room_1_quad_4":
self.I[0:2, 2:4] = 1 # include room 1 quad 2
self.beta[2:4, 2:4] = 1 # terminates at room 1 quad 4
self.pi[0:2, 2:4] = 4 # go down when in room 1 quad 2
elif self.name == "room_1_quad_3->room_1_quad_4":
self.I[2:4, 0:2] = 1 # include room 1 quad 2
self.beta[2:4, 2:4] = 1 # terminates at room 1 quad 4
self.pi[2:4, 0:2] = 3 # go right when in room 1 quad 3
elif self.name == "room_1->room_2":
self.I[0:4, 0:4] = 1 # executable from room 1
self.beta[0:4, 4:8] = 1 # terminates at room 2
self.pi[0:4, 0:4] = 3 # go right when in room 1
elif self.name == "room_4->room_2":
self.I[4:8, 4:8] = 1 # executable from room 4
self.beta[0:4, 4:8] = 1 # terminates at room 2
self.pi[4:8, 4:8] = 2 # go up when in room 4
elif self.name == "room_2_quad_2->room_2_quad_1":
self.I[0:2, 6:8] = 1 # executable from room 2 quad 2
self.beta[0:2, 4:6] = 1 # terminates at room 2 quad 1
self.pi[0:2, 6:8] = 1 # go left when in room 2 quad 2
elif self.name == "room_2_quad_3->room_2_quad_1":
self.I[2:4, 4:6] = 1 # executable from room 2 quad 3
self.beta[0:2, 4:6] = 1 # terminates at room 2 quad 1
self.pi[2:4, 4:6] = 2 # go up when in room 2 quad 3
elif self.name == "room_2_quad_1->room_2_quad_2":
self.I[0:2, 4:6] = 1 # executable from room 2 quad 1
self.beta[0:2, 6:8] = 1 # terminates at room 2 quad 2
self.pi[0:2, 4:6] = 3 # go right when in room 2 quad 1
elif self.name == "room_2_quad_4->room_2_quad_2":
self.I[2:4, 6:8] = 1 # executable from room 2 quad 4
self.beta[0:2, 6:8] = 1 # terminates at room 2 quad 2
self.pi[2:4, 6:8] = 2 # go up when in room 2 quad 4
elif self.name == "room_2_quad_1->room_2_quad_3":
self.I[0:2, 4:6] = 1 # executable from room 2 quad 1
self.beta[2:4, 4:6] = 1 # terminates at room 2 quad 3
self.pi[0:2, 4:6] = 4 # go down when in room 2 quad 1
elif self.name == "room_2_quad_4->room_2_quad_3":
self.I[2:4, 6:8] = 1 # executable from room 2 quad 4
self.beta[2:4, 4:6] = 1 # terminates at room 2 quad 3
self.pi[2:4, 6:8] = 1 # go left when in room 2 quad 4
elif self.name == "room_2_quad_2->room_2_quad_4":
self.I[0:2, 6:8] = 1 # executable from room 2 quad 2
self.beta[2:4, 6:8] = 1 # terminates at room 2 quad 4
self.pi[0:2, 6:8] = 4 # go down when in room 2 quad 2
elif self.name == "room_2_quad_3->room_2_quad_4":
self.I[2:4, 4:6] = 1 # executable from room 2 quad 3
self.beta[2:4, 6:8] = 1 # terminates at room 2 quad 4
self.pi[2:4, 4:6] = 3 # go right when in room 2 quad 3
elif self.name == "room_1->room_3":
self.I[0:4, 0:4] = 1 # executable from room 1
self.beta[4:8, 0:4] = 1 # terminates at room 3
self.pi[0:4, 0:4] = 4 # go down when in room 1
elif self.name == "room_2->room_3":
self.I[0:4, 4:8] = 1 # executable from room 2
self.beta[4:8, 0:4] = 1 # terminates at room 3
self.pi[0:4, 4:8] = 4 # go down when in room 2
elif self.name == "room_3_quad_2->room_3_quad_1":
self.I[4:6, 2:4] = 1 # executable from room 3 quad 2
self.beta[4:6, 0:2] = 1 # terminates at room 3 quad 1
self.pi[4:6, 2:4] = 1 # go left when in room 3 quad 2
elif self.name == "room_3_quad_3->room_3_quad_1":
self.I[6:8, 0:2] = 2 # executable from room 3 quad 3
self.beta[4:6, 0:2] = 1 # terminates at room 3 quad 1
self.pi[6:8, 0:2] = 2 # go up when in room 3 quad 3
elif self.name == "room_3_quad_1->room_3_quad_2":
self.I[4:6, 0:2] = 1 # executable from room 3 quad 1
self.beta[4:6, 2:4] = 1 # terminates at room 3 quad 2
self.pi[4:6, 0:2] = 3 # go right when in room 3 quad 1
elif self.name == "room_3_quad_4->room_3_quad_2":
self.I[6:8, 2:4] = 1 # executable from room 3 quad 4
self.beta[4:6, 2:4] = 1 # terminates at room 3 quad 2
self.pi[6:8, 2:4] = 2 # go up when in room 3 quad 4
elif self.name == "room_3_quad_1->room_3_quad_3":
self.I[4:6, 0:2] = 1 # executable from room 3 quad 1
self.beta[6:8, 0:2] = 1 # terminates at room 3 quad 3
self.pi[4:6, 0:2] = 4 # go down when in room 3 quad 1
elif self.name == "room_3_quad_4->room_3_quad_3":
self.I[6:8, 2:4] = 1 # executable from room 3 quad 4
self.beta[6:8, 0:2] = 1 # terminates at room 3 quad 3
self.pi[6:8, 2:4] = 1 # go left when in room 3 quad 4
elif self.name == "room_3_quad_2->room_3_quad_4":
self.I[4:6, 2:4] = 1 # executable from room 3 quad 2
self.beta[6:8, 2:4] = 1 # terminates at room 3 quad 4
self.pi[4:6, 2:4] = 4 # go down when in room 3 quad 2
elif self.name == "room_3_quad_3->room_3_quad_4":
self.I[6:8, 0:2] = 1 # executable from room 3 quad 3
self.beta[6:8, 2:4] = 1 # terminates at room 3 quad 4
self.pi[6:8, 0:2] = 3 # go right when in room 3 quad 3
elif self.name == "room_2->room_4":
self.I[0:4, 4:8] = 1 # executable from room 2
self.beta[4:8, 4:8] = 1 # terminates at room 4
self.pi[0:4, 4:8] = 4 # go down when in room 2
elif self.name == "room_3->room_4":
self.I[4:8, 0:4] = 1 # executable from room 3
self.beta[4:8, 4:8] = 1 # terminates at room 4
self.pi[4:8, 0:4] = 3 # go right when in room 3
elif self.name == "room_4_quad_2->room_4_quad_1":
self.I[4:6, 6:8] = 1 # executable from room 4 quad 2
self.beta[4:6, 4:6] = 1 # terminates at room 4 quad 1
self.pi[4:6, 6:8] = 1 # go left when in room 4 quad 2
elif self.name == "room_4_quad_3->room_4_quad_1":
self.I[6:8, 4:6] = 1 # executable from room 4 quad 3
self.beta[4:6, 4:6] = 1 # terminates at room 4 quad 1
self.pi[6:8, 4:6] = 2 # go up when in room 4 quad 3
elif self.name == "room_4_quad_1->room_4_quad_2":
self.I[4:6, 4:6] = 1 # executable from room 4 quad 1
self.beta[4:6, 6:8] = 1 # terminates at room 4 quad 2
self.pi[4:6, 4:6] = 3 # go left when in room 4 quad 1
elif self.name == "room_4_quad_4->room_4_quad_2":
self.I[6:8, 6:8] = 1 # executable from room 4 quad 4
self.beta[4:6, 6:8] = 1 # terminates at room 4 quad 2
self.pi[6:8, 6:8] = 2 # go up when in room 4 quad 4
elif self.name == "room_4_quad_1->room_4_quad_3":
self.I[4:6, 4:6] = 1 # executable from room 4 quad 1
self.beta[6:8, 4:6] = 1 # terminates at room 4 quad 3
self.pi[4:6, 4:6] = 4 # go down when in room 4 quad 1
elif self.name == "room_4_quad_4->room_4_quad_3":
self.I[6:8, 6:8] = 1 # executable from room 4 quad 4
self.beta[6:8, 4:6] = 1 # terminates at room 4 quad 3
self.pi[6:8, 6:8] = 1 # go left when in room 4 quad 4
elif self.name == "room_4_quad_2->room_4_quad_4":
self.I[4:6, 6:8] = 1 # executable from room 4 quad 2
self.beta[6:8, 6:8] = 1 # terminates at room 4 quad 4
self.pi[4:6, 6:8] = 4 # go down when in room 4 quad 2
elif self.name == "room_4_quad_3->room_4_quad_4":
self.I[6:8, 4:6] = 1 # executable from room 4 quad 3
self.beta[6:8, 6:8] = 1 # terminates at room 4 quad 4
self.pi[6:8, 4:6] = 3 # go right when in room 4 quad 3
elif self.name == "room_4->room_3":
self.I[4:8, 4:8] = 1 # executable from room 4
self.beta[4:8, 0:4] = 1 # terminates at room 3
self.pi[4:8, 4:8] = 1 # go left when in room 4
else:
print("Cannot build " + self.name)
exit(-1)
def __str__(self):
return self.name
if __name__ == "__main__":
option = Option("room_1->room_2")
print (option.name)
print("Initiation Set")
print(option.I)
print("Termination Set")
print(option.beta)
print("Pi")
print(option.pi)
arr = np.zeros((8, 8))
arr[1, 0] = 1
print(option.execute_policy(arr))