-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredicate.py
402 lines (332 loc) · 15.6 KB
/
predicate.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
from typing import List
from legent import Action, Observation
from legent.utils.math import vec_xz, distance
import numpy as np
class Predicate:
def __init__(self) -> None:
pass
def task_done(self, action: Action, obs: Observation, options, task_setting) -> int:
# 0 continue 1 success -1 failed
raise NotImplementedError
class PredicateChoose(Predicate):
def __init__(self, answer) -> None:
self.answer = answer
def task_done(self, action: Action, obs: Observation, options, task_setting) -> int:
if options[action.action_choice] == self.answer:
return 1, {}
elif options[action.action_choice].startswith("answer"):
return -1, {}
else:
return 0, {}
class PredicateAgentNear(Predicate):
def __init__(self, object_id) -> None:
self.object_id = object_id
def task_done(self, action: Action, obs: Observation, options, task_setting) -> int:
game_states = obs.game_states
d = distance(vec_xz(game_states["agent"]["position"]), vec_xz(game_states["instances"][self.object_id]["position"]))
if d < 1.5:
return 1, {"distance": d}
else:
return 0, {"distance": d}
class PredicateCloser(Predicate):
def __init__(self, object1, object2, obs: Observation) -> None:
self.object1 = object1
self.object2 = object2
instances = obs.game_states["instances"]
self.init_distance = distance(vec_xz(instances[object1]["position"]), vec_xz(instances[object2]["position"]))
def task_done(self, action: Action, obs: Observation, options, task_setting) -> int:
instances = obs.game_states["instances"]
d = distance(vec_xz(instances[self.object1]["position"]), vec_xz(instances[self.object2]["position"]))
if self.init_distance - d > 2:
return 1, {"distance_closer": self.init_distance - d}
else:
return 0, {"distance_closer": self.init_distance - d}
class PredicateFurther(Predicate):
def __init__(self, object1, object2, obs: Observation) -> None:
self.object1 = object1
self.object2 = object2
instances = obs.game_states["instances"]
self.init_distance = distance(vec_xz(instances[object1]["position"]), vec_xz(instances[object2]["position"]))
def task_done(self, action: Action, obs: Observation, options, task_setting) -> int:
instances = obs.game_states["instances"]
d = distance(vec_xz(instances[self.object1]["position"]), vec_xz(instances[self.object2]["position"]))
if d - self.init_distance > 1:
return 1, {"distance_closer": d - self.init_distance}
else:
return 0, {"distance_closer": d - self.init_distance}
class PredicateOn(Predicate):
def __init__(self, object1, object2) -> None:
self.object1 = object1
self.object2 = object2
def task_done(self, action: Action, obs: Observation, options, task_setting) -> int:
instances = obs.game_states["instances"]
d = distance(vec_xz(instances[self.object1]["position"]), vec_xz(instances[self.object2]["position"]))
if d < 0.3:
return 1, {}
else:
return 0, {}
class PredicateGrab(Predicate):
def __init__(self, object) -> None:
self.object = object
def task_done(self, action: Action, obs: Observation, options, task_setting) -> int:
done = obs.game_states["agent_grab_instance"] == self.object
if done:
return 1, {}
else:
return 0, {}
class PredicateGrabOnce(Predicate):
def __init__(self, object) -> None:
self.object = object
self.grabbed_once = False
def task_done(self, action: Action, obs: Observation, options, task_setting) -> int:
if self.grabbed_once:
return 1, {}
done = obs.game_states["agent_grab_instance"] == self.object
if done:
self.grabbed_once = True
return 1, {}
else:
return 0, {}
class PredicateSwap(Predicate):
def __init__(self, object1, object2, obs: Observation) -> None:
self.object1 = object1
self.object2 = object2
instances = obs.game_states["instances"]
self.init_pos1 = instances[object1]["position"]
self.init_pos2 = instances[object2]["position"]
def task_done(self, action: Action, obs: Observation, options, task_setting) -> int:
instances = obs.game_states["instances"]
pos1 = instances[self.object1]["position"]
pos2 = instances[self.object2]["position"]
if distance(vec_xz(self.init_pos1), vec_xz(pos2)) < 1 and distance(vec_xz(self.init_pos2), vec_xz(pos1)) < 1:
return 1, {}
else:
return 0, {}
class PredicateIn(Predicate):
def __init__(self, objects, on_object) -> None:
self.objects = objects
self.on_object = on_object
def task_done(self, action: Action, obs: Observation, options, task_setting) -> int:
instances = obs.game_states["instances"]
for object_id in self.objects:
d = distance(vec_xz(instances[object_id]["position"]), vec_xz(instances[self.on_object]["position"]))
if d > 0.3:
return 0, {}
return 1, {}
class PredicateNotOn(Predicate):
def __init__(self, objects, on_object) -> None:
self.objects = objects
self.on_object = on_object
def task_done(self, action: Action, obs: Observation, options, task_setting) -> int:
instances = obs.game_states["instances"]
for object_id in self.objects:
d = distance(vec_xz(instances[object_id]["position"]), vec_xz(instances[self.on_object]["position"]))
if d < 1:
return 0, {}
return 1, {}
# TODO: near和at基本一样,可以合并
class PredicateAgentAt(Predicate):
def __init__(self, object_id) -> None:
self.object_id = object_id
def task_done(self, action: Action, obs: Observation, options, task_setting) -> int:
game_states = obs.game_states
d = distance(vec_xz(game_states["agent"]["position"]), vec_xz(game_states["instances"][self.object_id]["position"]))
if d < 0.3:
return 1, {"distance": d}
else:
return 0, {"distance": d}
class PredicateAt(Predicate):
def __init__(self, object1, object2) -> None:
self.object1 = object1
self.object2 = object2
def task_done(self, action: Action, obs: Observation, options, task_setting) -> int:
instances = obs.game_states["instances"]
d = distance(vec_xz(instances[self.object1]["position"]), vec_xz(instances[self.object2]["position"]))
if d < 0.3:
return 1, {"distance": d}
else:
return 0, {"distance": d}
class PredicateNear(Predicate):
def __init__(self, object1, object2) -> None:
self.object1 = object1
self.object2 = object2
def task_done(self, action: Action, obs: Observation, options, task_setting) -> int:
instances = obs.game_states["instances"]
d = distance(vec_xz(instances[self.object1]["position"]), vec_xz(instances[self.object2]["position"]))
if d < 1.5:
return 1, {"distance": d}
else:
return 0, {"distance": d}
class PredicateNotNear(Predicate):
def __init__(self, object1, object2) -> None:
self.object1 = object1
self.object2 = object2
def task_done(self, action: Action, obs: Observation, options, task_setting) -> int:
instances = obs.game_states["instances"]
d = distance(vec_xz(instances[self.object1]["position"]), vec_xz(instances[self.object2]["position"]))
if d > 1.5:
return 1, {"distance": d}
else:
return 0, {"distance": d}
class PredicateNotAt(Predicate):
def __init__(self, object1, object2) -> None:
self.object1 = object1
self.object2 = object2
def task_done(self, action: Action, obs: Observation, options, task_setting) -> int:
instances = obs.game_states["instances"]
def vec_xyz(position):
return np.array([position["x"], position["y"], position["z"]])
d = np.linalg.norm(vec_xyz(instances[self.object1]["position"]) - vec_xyz(instances[self.object2]["position"]))
if d > 0.3:
return 1, {"distance": d}
else:
return 0, {"distance": d}
# TODO: agent pass 和 agent at一样,可以合并
class PredicateAgentPass(Predicate):
def __init__(self, object_id) -> None:
self.object_id = object_id
self.passed = False
def task_done(self, action: Action, obs: Observation, options, task_setting) -> int:
if self.passed:
return 1, {}
game_states = obs.game_states
d = distance(vec_xz(game_states["agent"]["position"]), vec_xz(game_states["instances"][self.object_id]["position"]))
if d < 0.3:
self.passed = True
return 1, {"distance": d}
else:
return 0, {"distance": d}
class PredicateSpecialActionSuccess(Predicate):
def __init__(self, action_text) -> None:
self.action_text = action_text
self.passed = False
def task_done(self, action: Action, obs: Observation, options, task_setting) -> int:
if self.passed:
return 1, {}
if options[action.action_choice] == self.action_text:
if obs.game_states["option_mode_info"]["feedback"] == "success":
self.passed = True
return 1, {}
return 0, {}
def build_predicate(predicates, obs, old_version) -> List[Predicate]:
pred_list = []
print(predicates)
for i in range(len(predicates)):
def build_one_predicate(predicate):
if old_version:
if predicate.startswith("choose"):
return PredicateChoose(predicate.split(" ", maxsplit=1)[1])
elif predicate.startswith("near"):
splits = predicate.split(" ")
assert len(splits) == 2
if len(splits) == 2:
return PredicateAgentNear(int(splits[1]))
elif predicate.startswith("closer"):
splits = predicate.split(" ")
assert len(splits) == 3
return PredicateCloser(int(splits[1]), int(splits[2]), obs)
elif predicate.startswith("further"):
splits = predicate.split(" ")
assert len(splits) == 3
return PredicateFurther(int(splits[1]), int(splits[2]), obs)
elif predicate.startswith("on"):
splits = predicate.split(" ")
assert len(splits) == 3
return PredicateOn(int(splits[1]), int(splits[2]))
elif predicate.startswith("grab_once"):
splits = predicate.split(" ")
assert len(splits) == 2
return PredicateGrabOnce(int(splits[1]))
elif predicate.startswith("grab"):
splits = predicate.split(" ")
assert len(splits) == 2
return PredicateGrab(int(splits[1]))
elif predicate.startswith("swap"):
splits = predicate.split(" ")
assert len(splits) == 3
return PredicateSwap(int(splits[1]), int(splits[2]), obs)
elif predicate.startswith("in"):
splits = predicate.split(" ")
assert len(splits) == 3 or len(splits) == 4
return PredicateIn([int(_id) for _id in splits[1:-1]], int(splits[-1]))
elif predicate.startswith("noton"):
splits = predicate.split(" ")
assert len(splits) == 3 or len(splits) == 4 or len(splits) == 5
return PredicateNotOn([int(_id) for _id in splits[1:-1]], int(splits[-1]))
else:
if predicate.startswith("choose"):
return PredicateChoose(predicate.split(" ", maxsplit=1)[1])
elif predicate.startswith("agent_at"):
splits = predicate.split(" ")
if len(splits) == 3:
splits = splits[:-1]
assert len(splits) == 2
return PredicateAgentAt(int(splits[1]))
elif predicate.startswith("agent_near"):
splits = predicate.split(" ")
if len(splits) == 3:
splits = splits[:-1]
assert len(splits) == 2
return PredicateAgentNear(int(splits[1]))
elif predicate.startswith("agent_pass"):
splits = predicate.split(" ")
if len(splits) == 3:
splits = splits[:-1]
assert len(splits) == 2
return PredicateAgentPass(int(splits[1]))
elif predicate.startswith("at"):
splits = predicate.split(" ")
assert len(splits) == 3
return PredicateAt(int(splits[1]), int(splits[2]))
elif predicate.startswith("near"):
splits = predicate.split(" ")
assert len(splits) == 3
return PredicateNear(int(splits[1]), int(splits[2]))
elif predicate.startswith("not_near"):
splits = predicate.split(" ")
assert len(splits) == 3
return PredicateNotNear(int(splits[1]), int(splits[2]))
elif predicate.startswith("not_at"):
splits = predicate.split(" ")
assert len(splits) == 3
return PredicateNotAt(int(splits[1]), int(splits[2]))
elif predicate.startswith("closer"):
splits = predicate.split(" ")
assert len(splits) == 3
return PredicateCloser(int(splits[1]), int(splits[2]), obs)
elif predicate.startswith("further"):
splits = predicate.split(" ")
assert len(splits) == 3
return PredicateFurther(int(splits[1]), int(splits[2]), obs)
elif predicate.startswith("grab_once"):
splits = predicate.split(" ")
if len(splits) == 3:
splits = splits[:-1]
assert len(splits) == 2
return PredicateGrabOnce(int(splits[1]))
elif predicate.startswith("grab"):
splits = predicate.split(" ")
if len(splits) == 3:
splits = splits[:-1]
assert len(splits) == 2
return PredicateGrab(int(splits[1]))
elif predicate.startswith("special_action_succes"):
splits = predicate.split(" ", maxsplit=1)
return PredicateSpecialActionSuccess(splits[1])
pred_list.append(build_one_predicate(predicates[i]))
return pred_list
def get_feedback(action: str, prev_obs, obs):
return obs.game_states["option_mode_info"]["feedback"]
action = action.lower()
if action.startswith("grab"):
if prev_obs.game_states["agent_grab_instance"] == -1 and obs.game_states["agent_grab_instance"] != -1:
return "success"
else:
return "failed"
elif action.startswith("put"):
if prev_obs.game_states["agent_grab_instance"] != -1 and obs.game_states["agent_grab_instance"] == -1:
return "success"
else:
return "failed"
else:
return "success"