-
Notifications
You must be signed in to change notification settings - Fork 24
/
dsl_enum_program.py
284 lines (217 loc) · 7.15 KB
/
dsl_enum_program.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
MONSTER_LIST = ['Demon', 'HellKnight', 'Revenant']
ITEMS_IN_INTEREST = ['MyAmmo']
ACTION_LIST = ['MOVE_FORWARD', 'MOVE_BACKWARD', 'MOVE_LEFT', 'MOVE_RIGHT',
'TURN_LEFT', 'TURN_RIGHT', 'ATTACK',
'SELECT_WEAPON1', 'SELECT_WEAPON2', 'SELECT_WEAPON3',
'SELECT_WEAPON4', 'SELECT_WEAPON5']
DISTANCE_DICT = {
'doncare_dist': lambda d: True,
'far': lambda d: d > 400,
'mid': lambda d: d < 300,
'close': lambda d: d < 180,
'very_close': lambda d: d < 135}
HORIZONTAL_DICT = {
'doncare_horz': lambda l, r, x: True,
'center': lambda l, r, x: l < x and x < r,
'slight_left': lambda l, r, x: r < x and x <= r + 10,
'slight_right': lambda l, r, x: l > x and x >= l - 10,
'mid_left': lambda l, r, x: r < x and x <= r + 20,
'mid_right': lambda l, r, x: l > x and x >= l - 20,
'left': lambda l, r, x: r < x,
'right': lambda l, r, x: l > x}
CLEAR_DISTANCE_DICT = {
'far': lambda d: d > 400,
'mid_far': lambda d: 300 < d and d <= 400,
'mid': lambda d: 180 < d and d <= 300,
'close': lambda d: 135 < d and d <= 180,
'very_close': lambda d: d <= 135}
CLEAR_HORIZONTAL_DICT = {
'slight_left': lambda l, r, x: r < x and x <= r + 10,
'slight_right': lambda l, r, x: l > x and x >= l - 10,
'mid_left': lambda l, r, x: r + 10 < x and x <= r + 20,
'mid_right': lambda l, r, x: l - 10 > x and x >= l - 20,
'left': lambda l, r, x: r + 20 < x,
'right': lambda l, r, x: l - 20 > x}
merge_distance_vocab = list(set(DISTANCE_DICT.keys()).union(
set(CLEAR_DISTANCE_DICT.keys())))
merge_horizontal_vocab = list(set(HORIZONTAL_DICT.keys()).union(
set(CLEAR_HORIZONTAL_DICT.keys())))
def check_and_apply(queue, rule):
r = rule[0].split()
l = len(r)
if len(queue) >= l:
t = queue[-l:]
if list(zip(*t)[0]) == r:
new_t = rule[1](list(zip(*t)[1]))
del queue[-l:]
queue.extend(new_t)
return True
return False
rules = []
# world, n, s = fn(world, n)
# world: vizdoom_world
# n: num_call
# s: success
# c: condition [True, False]
MAX_WHILE = 1000
def r_prog(t):
stmt = t[3]
return [('prog', stmt(0, 0))]
rules.append(('DEF run m( stmt m)', r_prog))
def r_stmt(t):
stmt = t[0]
def fn(k, n):
return stmt(k, n)
return [('stmt', fn)]
rules.append(('while_stmt', r_stmt))
rules.append(('repeat_stmt', r_stmt))
rules.append(('stmt_stmt', r_stmt))
rules.append(('action', r_stmt))
rules.append(('if_stmt', r_stmt))
rules.append(('ifelse_stmt', r_stmt))
def r_stmt_stmt(t):
stmt1, stmt2 = t[0], t[1]
def fn(k, n):
return stmt1(k, n) + stmt2(k, n)
return [('stmt_stmt', fn)]
rules.append(('stmt stmt', r_stmt_stmt))
def r_if(t):
cond, stmt = t[2], t[5]
def fn(k, n):
return ['if'] + cond(k, n) + stmt(k, n)
return [('if_stmt', fn)]
rules.append(('IF c( cond c) i( stmt i)', r_if))
def r_ifelse(t):
cond, stmt1, stmt2 = t[2], t[5], t[9]
def fn(k, n):
stmt1_out = stmt1(k, n)
stmt2_out = stmt2(k, n)
if stmt1_out == stmt2_out:
return stmt1_out
cond_out = cond(k, n)
if cond_out[0] == 'not':
else_cond = ['if'] + cond_out[1:]
else:
else_cond = ['if', 'not'] + cond_out
return ['if'] + cond_out + stmt1(k, n) + else_cond + stmt2(k, n)
return [('ifelse_stmt', fn)]
rules.append(('IFELSE c( cond c) i( stmt i) ELSE e( stmt e)', r_ifelse))
def r_while(t):
cond, stmt = t[2], t[5]
def fn(k, n):
cond_out = cond(k, n)
stmt_out = stmt(k, n)
while_out = []
for _ in range(MAX_WHILE):
while_out.extend(['if'] + cond_out + stmt_out)
return while_out
return [('while_stmt', fn)]
rules.append(('WHILE c( cond c) w( stmt w)', r_while))
def r_repeat(t):
cste, stmt = t[1], t[3]
def fn(k, n):
repeat_out = []
for _ in range(cste()):
repeat_out.extend(stmt(k, n))
return repeat_out
return [('repeat_stmt', fn)]
rules.append(('REPEAT cste r( stmt r)', r_repeat))
def r_cond1(t):
cond = t[0]
def fn(k, n):
return cond(k, n)
return [('cond', fn)]
rules.append(('percept', r_cond1))
def r_cond2(t):
cond = t[2]
def fn(k, n):
cond_out = cond(k, n)
if cond_out[0] == 'not':
cond_out = cond_out[1:]
else:
cond_out = ['not'] + cond_out
return cond_out
return [('cond', fn)]
rules.append(('not c( cond c)', r_cond2))
def r_percept1(t):
actor, dist, horz = t[1], t[3], t[4]
def fn(world, n):
return ['exist_actor_in_distance_horizontal', actor(), dist(), horz()]
return [('percept', fn)]
rules.append(('EXIST actor IN distance horizontal', r_percept1))
def r_percept2(t):
actor = t[1]
def fn(world, n):
return ['in_target', actor()]
return [('percept', fn)]
rules.append(('INTARGET actor', r_percept2))
def r_percept3(t):
actor = t[1]
def fn(world, n):
return ['is_there', actor()]
return [('percept', fn)]
rules.append(('ISTHERE actor', r_percept3))
def r_actor1(t):
return [('actor', t[0])]
rules.append(('monster', r_actor1))
def create_r_monster(monster):
def r_monster(t):
return [('monster', lambda: monster)]
return r_monster
for monster in MONSTER_LIST:
rules.append((monster, create_r_monster(monster)))
def r_actor2(t):
return [('actor', t[0])]
rules.append(('items', r_actor2))
def create_r_item(item):
def r_item(t):
return [('items', lambda: item)]
return r_item
for item in ITEMS_IN_INTEREST:
rules.append((item, create_r_item(item)))
def create_r_distance(distance):
def r_distance(t):
return [('distance', lambda: distance)]
return r_distance
for distance in merge_distance_vocab:
rules.append((distance, create_r_distance(distance)))
def create_r_horizontal(horizontal):
def r_horizontal(t):
return [('horizontal', lambda: horizontal)]
return r_horizontal
for horizontal in merge_horizontal_vocab:
rules.append((horizontal, create_r_horizontal(horizontal)))
def create_r_slot(slot_number):
def r_slot(t):
return [('slot', lambda: slot_number)]
return r_slot
for slot_number in range(1, 7):
rules.append(('S={}'.format(slot_number), create_r_slot(slot_number)))
def create_r_action(action):
def r_action(t):
def fn(world, n):
return [action]
return [('action', fn)]
return r_action
for action in ACTION_LIST:
rules.append((action, create_r_action(action)))
def create_r_cste(number):
def r_cste(t):
return [('cste', lambda: number)]
return r_cste
for i in range(20):
rules.append(('R={}'.format(i), create_r_cste(i)))
def parse(program):
p_tokens = program.split()[::-1]
queue = []
applied = False
while len(p_tokens) > 0 or len(queue) != 1:
if applied: applied = False
else:
queue.append((p_tokens.pop(), None))
for rule in rules:
applied = check_and_apply(queue, rule)
if applied: break
if not applied and len(p_tokens) == 0: # error parsing
return None, False
return queue[0][1], True