-
Notifications
You must be signed in to change notification settings - Fork 4
/
process_actions.py
281 lines (248 loc) · 11.5 KB
/
process_actions.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
import numpy as np
from copy import deepcopy
def filter_actions_IMDB(actions, num_diff_actions, max_path_length, num_m, num_d, num_a, mode):
'''Filter illegal action combination
mode 1: only make actions on Movie nodes in batch
mode 2: make actions on all nodes in batch
'''
if mode == 1:
added_actions = np.concatenate([np.zeros((1, num_m), dtype=int), actions])
elif mode == 2:
to_add = np.concatenate([np.zeros((1, num_m), dtype=int),
np.ones((1, num_d), dtype=int),
2 * np.ones((1, num_a), dtype=int)], axis=1)
added_actions = np.concatenate([to_add, actions])
# # case 1: stay at one node three steps
# for i in range(max_path_length-1):
# illegal_index = np.where((added_actions[i]==added_actions[i+1]) & (added_actions[i]==added_actions[i+2]))[0]
# added_actions[i+2, illegal_index] = 3 * np.ones((1, illegal_index.shape[0]), dtype=int)
# case 1: stay at one node two steps
for i in range(max_path_length):
illegal_index = np.where(added_actions[i] == added_actions[i+1])[0]
added_actions[i+1, illegal_index] = (num_diff_actions-1) * np.ones((1, illegal_index.shape[0]), dtype=int)
# # case 2: illegal paths in this dataset
# for i in range(max_path_length):
# # 0-0
# illegal_index = np.where((added_actions[i]==0) & (added_actions[i+1]==0))[0]
# added_actions[i+1, illegal_index] = 3 * np.ones((1, illegal_index.shape[0]), dtype=int)
# # 1-1
# illegal_index = np.where((added_actions[i]==1) & (added_actions[i+1]==1))[0]
# added_actions[i+1, illegal_index] = 3 * np.ones((1, illegal_index.shape[0]), dtype=int)
# case 1: stay at one node two steps
for i in range(max_path_length):
illegal_index = np.where(added_actions[i] == added_actions[i+1])[0]
added_actions[i+1, illegal_index] = (num_diff_actions-1) * np.ones((1, illegal_index.shape[0]), dtype=int)
# case 2: 1-2 / 2-1
for i in range(max_path_length):
illegal_index = np.where((added_actions[i] == 1) & (added_actions[i+1] == 2))[0]
added_actions[i+1, illegal_index] = (num_diff_actions-1) * np.ones((1, illegal_index.shape[0]), dtype=int)
for i in range(max_path_length):
illegal_index = np.where((added_actions[i] == 2) & (added_actions[i+1] == 1))[0]
added_actions[i+1, illegal_index] = (num_diff_actions-1) * np.ones((1, illegal_index.shape[0]), dtype=int)
# case 3: Agent picks stop action
for i in range(1, max_path_length+1):
stop_index = np.where(added_actions[i] == num_diff_actions-1)[0]
added_actions[i:, stop_index] = -1
added_actions = np.where(added_actions == -1, (num_diff_actions-1), added_actions)
return added_actions
def filter_actions_DBLP(actions, num_diff_actions, max_path_length, num_a, num_p, num_t, num_c, mode):
'''Filter illegal action combination
mode 1: only make actions on Movie nodes in batch
mode 2: make actions on all nodes in batch
'''
if mode == 1:
added_actions = np.concatenate([np.zeros((1, num_a), dtype=int), actions])
elif mode == 2:
to_add = np.concatenate([np.zeros((1, num_a), dtype=int), np.ones((1, num_p), dtype=int),
2 * np.ones((1, num_t), dtype=int), 3 * np.ones((1, num_c), dtype=int)], axis=1)
added_actions = np.concatenate([to_add, actions])
# case 1: stay at one node two steps
for i in range(max_path_length):
illegal_index = np.where(added_actions[i] == added_actions[i + 1])[0]
added_actions[i + 1, illegal_index] = (num_diff_actions - 1) * np.ones((1, illegal_index.shape[0]), dtype=int)
# case 3: Agent picks stop action
for i in range(1, max_path_length + 1):
stop_index = np.where(added_actions[i] == num_diff_actions - 1)[0]
added_actions[i:, stop_index] = -1
added_actions = np.where(added_actions == -1, (num_diff_actions - 1), added_actions)
return added_actions
def process_single_action_IMDB(action1, action2, col_id,
edge_index_md, edge_index_dm,
edge_index_ma, edge_index_am):
'''
m: 0; d: 1; a: 2;
stop: 3;
'''
# across levels
if action1 == 0 and action2 == 1:
return edge_index_md[:, np.where((edge_index_md[0]==col_id))[0]].T
elif action1 == 1 and action2 == 0:
return edge_index_dm[:, np.where((edge_index_dm[0]==col_id))[0]].T
elif action1 == 0 and action2 == 2:
return edge_index_ma[:, np.where((edge_index_ma[0]==col_id))[0]].T
elif action1 == 2 and action2 == 0:
return edge_index_am[:, np.where((edge_index_am[0]==col_id))[0]].T
# stop
elif action1 == 3 or action2 == 3:
return np.array([[col_id, -1]])
# recursion
def process_actions_IMDB(action_col, col_id, result, edge_index_md, edge_index_dm,
edge_index_ma, edge_index_am):
# stop condition
if len(action_col) < 2:
return
level_result = process_single_action_IMDB(
action_col[0], action_col[1], col_id,
edge_index_md, edge_index_dm, edge_index_ma, edge_index_am
)
for c in level_result:
next_level_result = deepcopy(c.tolist())
process_actions_IMDB(
action_col[1:], c[1], next_level_result,
edge_index_md, edge_index_dm, edge_index_ma, edge_index_am
)
result.append(next_level_result)
def process_single_action_DBLP(action1, action2, col_id, edge_index_ap,
edge_index_pa, edge_index_pt, edge_index_pc,
edge_index_tp, edge_index_cp):
'''
a: 0; p: 1; t: 2; c: 3
stop: 4;
'''
# across levels
if action1 == 0 and action2 == 1:
return edge_index_ap[:, np.where((edge_index_ap[0] == col_id))[0]].T
elif action1 == 1 and action2 == 0:
return edge_index_pa[:, np.where((edge_index_pa[0] == col_id))[0]].T
elif action1 == 1 and action2 == 2:
return edge_index_pt[:, np.where((edge_index_pt[0] == col_id))[0]].T
elif action1 == 1 and action2 == 3:
return edge_index_pc[:, np.where((edge_index_pc[0] == col_id))[0]].T
elif action1 == 2 and action2 == 1:
return edge_index_tp[:, np.where((edge_index_tp[0] == col_id))[0]].T
elif action1 == 3 and action2 == 1:
return edge_index_cp[:, np.where((edge_index_cp[0] == col_id))[0]].T
elif action1 == 0 and action2 == 2:
return np.array([[col_id, -1]])
elif action1 == 0 and action2 == 3:
return np.array([[col_id, -1]])
elif action1 == 2 and action2 == 0:
return np.array([[col_id, -1]])
elif action1 == 2 and action2 == 3:
return np.array([[col_id, -1]])
elif action1 == 3 and action2 == 0:
return np.array([[col_id, -1]])
elif action1 == 3 and action2 == 2:
return np.array([[col_id, -1]])
# stop
elif action1 == 4 or action2 == 4:
return np.array([[col_id, -1]])
# recursion
def process_actions_DBLP(action_col, col_id, result, edge_index_ap,
edge_index_pa, edge_index_pt, edge_index_pc,
edge_index_tp, edge_index_cp):
# stop condition
if len(action_col) < 2:
return
level_result = process_single_action_DBLP(action_col[0], action_col[1], col_id,
edge_index_ap,
edge_index_pa, edge_index_pt, edge_index_pc,
edge_index_tp, edge_index_cp)
for c in level_result:
next_level_result = deepcopy(c.tolist())
process_actions_DBLP(action_col[1:], c[1], next_level_result,
edge_index_ap,
edge_index_pa, edge_index_pt, edge_index_pc,
edge_index_tp, edge_index_cp)
result.append(next_level_result)
def flat_list_A(arr, output, level_id):
if type(arr[0]) == int:
if len(output[0]) == 0:
output[level_id] = [arr[:2]]
else:
output[level_id].append(arr[:2])
if len(arr) == 2:
return output
else:
level_id += 1
for item in arr[2:]:
flat_list_A(arr=item, output=output, level_id=level_id)
def generate_result_A(actions, result_final):
result_A = [list()] * (actions.shape[0]-1)
for k, v in result_final.items():
for idx, c in enumerate(v):
output = [[] for i in range(actions.shape[0]-1)]
flat_list_A(c, output, level_id=0)
if len(result_A[0]) == 0:
result_A = output.copy()
else:
for step_id, item in enumerate(output):
result_A[step_id].extend(item)
return result_A
def flat_list_B(arr, output, prefix):
take_prefix = True
for i in arr:
if type(i) == int:
prefix.append(i)
if type(i) == list:
if type(i[1]) == int:
sublist = prefix.copy()
take_prefix = False
sublist.append(i[1])
if len(i) > 2:
flat_list_B(i[2:], output, sublist)
else:
output.append(sublist)
if take_prefix:
output.append(prefix)
return output
def generate_result_B(result_final):
result_B = [list()] * len(result_final.keys())
for k, v in result_final.items():
line_output = list()
for c in v:
output = list()
flat_list_B(c, output, list())
line_output.extend(output)
result_B[k] = line_output
return result_B
def actions_to_agg_paths_IMDB(actions, mode, edge_index_md, edge_index_dm,
edge_index_ma, edge_index_am):
"""
mode: A: obtain agg path, 2*n
B: obatin find next arrival path, m*n
"""
# TODO: parallise
result_final = dict()
N = actions.shape[1]
for node_id in range(N):
res = list()
process_actions_IMDB(
actions[:, node_id].tolist(), node_id, res,
edge_index_md, edge_index_dm, edge_index_ma, edge_index_am
)
result_final[node_id] = res
if mode == 'A':
return generate_result_A(actions, result_final)
elif mode == 'B':
return generate_result_B(result_final)
elif mode == 'both':
return generate_result_A(actions, result_final), generate_result_B(result_final)
def actions_to_agg_paths_DBLP(actions, mode, edge_index_ap,
edge_index_pa, edge_index_pt, edge_index_pc,
edge_index_tp, edge_index_cp):
# TODO: parallise
result_final = dict()
for node_id in range(actions.shape[1]):
res = list()
process_actions_DBLP(actions[:, node_id].tolist(), node_id, res,
edge_index_ap,
edge_index_pa, edge_index_pt, edge_index_pc,
edge_index_tp, edge_index_cp)
result_final[node_id] = res
if mode == 'A':
return generate_result_A(actions, result_final)
elif mode == 'B':
return generate_result_B(result_final)
elif mode == 'both':
return generate_result_A(actions, result_final), generate_result_B(result_final)