-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_and_act_behavior.py
676 lines (618 loc) · 22.8 KB
/
run_and_act_behavior.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
import sys
from copy import deepcopy
import argparse
# You would also have a loader function similar to `load_pddl_problem_line_by_line` adapted for behavior domain
from load_pddl_behavior import load_pddl_problem_line_by_line
#########################################################
# Actions definition for behavior domain
#########################################################
actions = {
"navigate_to": {
"parameters": ["?objto", "?agent"],
"param_types": ["object", "agent"],
"preconditions": ("not", ("in_reach_of_agent", "?objto")),
"effects": {
"add": [("in_reach_of_agent", "?objto")],
# The domain states:
# (forall (?objfrom - object)
# (when (and (in_reach_of_agent ?objfrom) (not (same_obj ?objfrom ?objto)))
# (not (in_reach_of_agent ?objfrom))))
#
# We'll represent this as a forall_remove with a compound condition.
"forall_remove": {
"vars": [("?objfrom", "object")],
"condition_and": [
("in_reach_of_agent", "?objfrom"),
("not", ("same_obj", "?objfrom", "?objto"))
],
"remove_pred": ("in_reach_of_agent", "?objfrom")
}
}
},
"grasp": {
"parameters": ["?obj", "?agent"],
"param_types": ["object", "agent"],
"preconditions": ("and",
("not", ("holding", "?obj")),
("not", ("handsfull", "?agent")),
("in_reach_of_agent", "?obj"),
("not",
("exists", ("?obj2", "object"),
("and",
("inside", "?obj", "?obj2"),
("not", ("open", "?obj2"))
)
)
)
),
"effects": {
"add": [("holding", "?obj"), ("handsfull", "?agent")],
# The action has a forall with multiple removals:
# (forall (?other_obj - object)
# (and
# (not (inside ?obj ?other_obj))
# (not (ontop ?obj ?other_obj))
# (not (under ?obj ?other_obj))
# (not (under ?other_obj ?obj))
# (not (nextto ?obj ?other_obj))
# (not (nextto ?other_obj ?obj))
# (not (onfloor ?obj ?other_obj))
# )
# )
#
# We'll store these as multiple predicates to remove under a single forall_remove.
"forall_remove_multiple": {
"vars": [("?other_obj", "object")],
"remove_preds": [
("inside", "?obj", "?other_obj"),
("ontop", "?obj", "?other_obj"),
("under", "?obj", "?other_obj"),
("under", "?other_obj", "?obj"),
("nextto", "?obj", "?other_obj"),
("nextto", "?other_obj", "?obj"),
("onfloor", "?obj", "?other_obj")
]
}
}
},
"release": {
"parameters": ["?obj", "?agent"],
"param_types": ["object", "agent"],
"preconditions": ("holding", "?obj"),
"effects": {
"del": [("holding", "?obj"), ("handsfull", "?agent")]
}
},
"place_ontop": {
"parameters": ["?obj_in_hand", "?obj", "?agent"],
"param_types": ["object", "object", "agent"],
"preconditions": ("and",
("holding", "?obj_in_hand"),
("in_reach_of_agent", "?obj")
),
"effects": {
"add": [("ontop", "?obj_in_hand", "?obj")],
"del": [("holding", "?obj_in_hand"), ("handsfull", "?agent")]
}
},
"place_inside": {
"parameters": ["?obj_in_hand", "?obj", "?agent"],
"param_types": ["object", "object", "agent"],
"preconditions": ("and",
("holding", "?obj_in_hand"),
("in_reach_of_agent", "?obj"),
("open", "?obj")
),
"effects": {
"add": [("inside", "?obj_in_hand", "?obj")],
"del": [("holding", "?obj_in_hand"), ("handsfull", "?agent")]
}
},
"open": {
"parameters": ["?obj", "?agent"],
"param_types": ["object", "agent"],
"preconditions": ("and",
("in_reach_of_agent", "?obj"),
("not", ("open", "?obj")),
("not", ("handsfull", "?agent"))
),
"effects": {
"add": [("open", "?obj")]
}
},
"close": {
"param_types": ["object", "agent"],
"parameters": ["?obj", "?agent"],
"preconditions": ("and",
("in_reach_of_agent", "?obj"),
("open", "?obj"),
("not", ("handsfull", "?agent"))
),
"effects": {
"del": [("open", "?obj")]
}
},
"slice": {
"parameters": ["?obj", "?knife", "?agent"],
"param_types": ["object", "knife_n_01", "agent"],
"preconditions": ("and",
("holding", "?knife"),
("in_reach_of_agent", "?obj")
),
"effects": {
"add": [("sliced", "?obj")]
}
},
"slice-carvingknife": {
"parameters": ["?obj", "?knife", "?board", "?agent"],
"param_types": ["object", "carving_knife_n_01", "countertop_n_01", "agent"],
"preconditions": ("and",
("in_reach_of_agent", "?obj"),
("holding", "?knife"),
("ontop", "?obj", "?board"),
("not", ("sliced", "?obj"))
),
"effects": {
"add": [("sliced", "?obj")]
}
},
"place_onfloor": {
"parameters": ["?obj_in_hand", "?floor", "?agent"],
"param_types": ["object", "floor_n_01", "agent"],
"preconditions": ("and",
("holding", "?obj_in_hand"),
("in_reach_of_agent", "?floor")
),
"effects": {
"add": [("onfloor", "?obj_in_hand", "?floor")],
"del": [("holding", "?obj_in_hand"), ("handsfull", "?agent")]
}
},
"place_nextto": {
"parameters": ["?obj_in_hand", "?obj", "?agent"],
"param_types": ["object", "object", "agent"],
"preconditions": ("and",
("holding", "?obj_in_hand"),
("in_reach_of_agent", "?obj")
),
"effects": {
"add": [("nextto", "?obj_in_hand", "?obj"),
("nextto", "?obj", "?obj_in_hand")],
"del": [("holding", "?obj_in_hand"), ("handsfull", "?agent")]
}
},
"place_nextto_ontop": {
"parameters": ["?obj_in_hand", "?obj1", "?obj2", "?agent"],
"param_types": ["object", "object", "object", "agent"],
"preconditions": ("and",
("holding", "?obj_in_hand"),
("in_reach_of_agent", "?obj1")
),
"effects": {
"add": [("nextto", "?obj_in_hand", "?obj1"),
("nextto", "?obj1", "?obj_in_hand"),
("ontop", "?obj_in_hand", "?obj2")],
"del": [("holding", "?obj_in_hand"), ("handsfull", "?agent")]
}
},
"clean_stained_brush": {
"parameters": ["?scrub_brush", "?obj", "?agent"],
"param_types": ["scrub_brush_n_01", "object", "agent"],
"preconditions": ("and",
("in_reach_of_agent", "?obj"),
("stained", "?obj"),
("soaked", "?scrub_brush"),
("holding", "?scrub_brush")
),
"effects": {
"del": [("stained", "?obj")]
}
},
"clean_stained_cloth": {
"parameters": ["?rag", "?obj", "?agent"],
"param_types": ["piece_of_cloth_n_01", "object", "agent"],
"preconditions": ("and",
("in_reach_of_agent", "?obj"),
("stained", "?obj"),
("soaked", "?rag"),
("holding", "?rag")
),
"effects": {
"del": [("stained", "?obj")]
}
},
"clean_stained_handowel": {
"parameters": ["?hand_towel", "?obj", "?agent"],
"param_types": ["hand_towel_n_01", "object", "agent"],
"preconditions": ("and",
("in_reach_of_agent", "?obj"),
("stained", "?obj"),
("soaked", "?hand_towel"),
("holding", "?hand_towel")
),
"effects": {
"del": [("stained", "?obj")]
}
},
"clean_stained_towel": {
"parameters": ["?hand_towel", "?obj", "?agent"],
"param_types": ["towel_n_01", "object", "agent"],
"preconditions": ("and",
("in_reach_of_agent", "?obj"),
("stained", "?obj"),
("soaked", "?hand_towel"),
("holding", "?hand_towel")
),
"effects": {
"del": [("stained", "?obj")]
}
},
"clean_stained_dishtowel": {
"parameters": ["?hand_towel", "?obj", "?agent"],
"param_types": ["dishtowel_n_01", "object", "agent"],
"preconditions": ("and",
("in_reach_of_agent", "?obj"),
("stained", "?obj"),
("soaked", "?hand_towel"),
("holding", "?hand_towel")
),
"effects": {
"del": [("stained", "?obj")]
}
},
"clean_stained_dishwasher": {
"parameters": ["?dishwasher", "?obj", "?agent"],
"param_types": ["dishwasher_n_01", "object", "agent"],
"preconditions": ("and",
("holding", "?obj"),
("in_reach_of_agent", "?dishwasher")
),
"effects": {
"del": [("stained", "?obj")]
}
},
"clean_stained_rag": {
"parameters": ["?rag", "?obj", "?agent"],
"param_types": ["rag_n_01", "object", "agent"],
"preconditions": ("and",
("in_reach_of_agent", "?obj"),
("stained", "?obj"),
("soaked", "?rag"),
("holding", "?rag")
),
"effects": {
"del": [("stained", "?obj")]
}
},
"soak": {
"parameters": ["?obj1", "?sink", "?agent"],
"param_types": ["object", "sink_n_01", "agent"],
"preconditions": ("and",
("holding", "?obj1"),
("in_reach_of_agent", "?sink"),
("toggled_on", "?sink")
),
"effects": {
"add": [("soaked", "?obj1")]
}
},
"soak_teapot": {
"parameters": ["?obj1", "?agent", "?teapot"],
"param_types": ["object", "agent", "teapot_n_01"],
"preconditions": ("and",
("holding", "?obj1"),
("in_reach_of_agent", "?teapot")
),
"effects": {
"add": [("soaked", "?obj1")]
}
},
"place_under": {
"parameters": ["?obj_in_hand", "?obj", "?agent"],
"param_types": ["object", "object", "agent"],
"preconditions": ("and",
("holding", "?obj_in_hand"),
("in_reach_of_agent", "?obj")
),
"effects": {
"add": [("under", "?obj_in_hand", "?obj")],
"del": [("holding", "?obj_in_hand"), ("handsfull", "?agent")]
}
},
"toggle_on": {
"parameters": ["?obj", "?agent"],
"param_types": ["object", "agent"],
"preconditions": ("and",
("in_reach_of_agent", "?obj"),
("not", ("handsfull", "?agent"))
),
"effects": {
"add": [("toggled_on", "?obj")]
}
},
"clean_dusty_rag": {
"param_types": ["rag_n_01", "object", "agent"],
"parameters": ["?rag", "?obj", "?agent"],
"preconditions": ("and",
("in_reach_of_agent", "?obj"),
("dusty", "?obj"),
("holding", "?rag")
),
"effects": {
"del": [("dusty", "?obj")]
}
},
"clean_dusty_towel": {
"parameters": ["?towel", "?obj", "?agent"],
"param_types": ["towel_n_01", "object", "agent"],
"preconditions": ("and",
("in_reach_of_agent", "?obj"),
("dusty", "?obj"),
("holding", "?towel")
),
"effects": {
"del": [("dusty", "?obj")]
}
},
"clean_dusty_cloth": {
"parameters": ["?rag", "?obj", "?agent"],
"param_types": ["piece_of_cloth_n_01", "object", "agent"],
"preconditions": ("and",
("in_reach_of_agent", "?obj"),
("dusty", "?obj"),
("holding", "?rag")
),
"effects": {
"del": [("dusty", "?obj")]
}
},
"clean_dusty_brush": {
"parameters": ["?scrub_brush", "?obj", "?agent"],
"param_types": ["scrub_brush_n_01", "object", "agent"],
"preconditions": ("and",
("in_reach_of_agent", "?obj"),
("dusty", "?obj"),
("holding", "?scrub_brush")
),
"effects": {
"del": [("dusty", "?obj")]
}
},
"clean_dusty_vacuum": {
"parameters": ["?vacuum", "?obj", "?agent"],
"param_types": ["vacuum_n_04", "object", "agent"],
"preconditions": ("and",
("in_reach_of_agent", "?obj"),
("dusty", "?obj"),
("holding", "?vacuum")
),
"effects": {
"del": [("dusty", "?obj")]
}
},
"freeze": {
"parameters": ["?obj", "?fridge"],
"param_types": ["object", "electric_refrigerator_n_01"],
"preconditions": ("and",
("inside", "?obj", "?fridge"),
("not", ("frozen", "?obj"))
),
"effects": {
"add": [("frozen", "?obj")]
}
},
"cook": {
"parameters": ["?obj", "?pan"],
"param_types": ["object", "pan_n_01"],
"preconditions": ("and",
("ontop", "?obj", "?pan"),
("not", ("cooked", "?obj"))
),
"effects": {
"add": [("cooked", "?obj")]
}
}
}
#########################################################
# Core Functions (similar to VirtualHome run_and_act.py)
#########################################################
def substitute(args, param_map):
return tuple(param_map.get(a, a) for a in args)
def _check_condition_list(state, cond, param_map, all_objects, characters):
"""Evaluate a nested condition structure."""
if not cond:
return True
op = cond[0]
if op == "and":
return all(_check_condition_list(state, c, param_map, all_objects, characters) for c in cond[1:])
elif op == "or":
return any(_check_condition_list(state, c, param_map, all_objects, characters) for c in cond[1:])
elif op == "not":
return not _check_condition_list(state, cond[1], param_map, all_objects, characters)
elif op == "exists":
var_decl = cond[1]
sub_cond = cond[2]
var_name, var_type = var_decl
for obj in all_objects:
# Type check if needed, else skip
# For now assume all are objects except if var_type == agent, handle it if needed
if var_type == "agent" and obj not in characters:
continue
if var_type == "object" and obj in characters:
continue
new_param_map = {**param_map, var_name: obj}
if _check_condition_list(state, sub_cond, new_param_map, all_objects, characters):
return True
return False
else:
# Simple predicate condition
pred = op
pred_args = substitute(cond[1:], param_map)
return pred in state and pred_args in state[pred]
def check_preconditions(state, action_def, args, all_objects, characters, object_types):
param_map = {p: a for p, a in zip(action_def["parameters"], args)}
# First, check types
if "param_types" in action_def:
for (param, arg, ptype) in zip(action_def["parameters"], args, action_def["param_types"]):
# Check if arg matches ptype
if ptype == "agent":
# arg must be in characters
if arg not in characters:
return False
else:
# arg must be in all_objects or characters, and must match object_types
if arg not in object_types:
return False
# If ptype == "object", it's okay as long as it's not agent.
# If ptype is a specific subtype (e.g., knife_n_01), must match exactly
if ptype != "object" and ptype != "agent":
if object_types[arg] != ptype:
return False
# If ptype == "object", any object (not an agent) is allowed
if ptype == "object":
# Ensure it's not an agent
if arg in characters:
return False
precond = action_def["preconditions"]
if not precond:
return True
return _check_condition_list(state, precond, param_map, all_objects, characters)
def apply_effects(state, effects_list, param_map):
"""Add positive effects."""
for eff in effects_list:
pred = eff[0]
aargs = substitute(eff[1:], param_map)
if pred not in state:
state[pred] = set()
state[pred].add(aargs)
def remove_effects(state, effects_list, param_map):
"""Remove negative effects."""
for eff in effects_list:
pred = eff[0]
dargs = substitute(eff[1:], param_map)
if pred in state and dargs in state[pred]:
state[pred].remove(dargs)
if not state[pred]:
del state[pred]
def apply_action(state, action_def, args, all_objects, characters, object_types):
new_state = deepcopy(state)
param_map = {p: a for p, a in zip(action_def["parameters"], args)}
eff = action_def.get("effects", {})
# Unconditional add/del
if "add" in eff:
apply_effects(new_state, eff["add"], param_map)
if "del" in eff:
remove_effects(new_state, eff["del"], param_map)
# Handle `when` effects if any (similar logic can be added as needed)
# ...
# Handle forall_remove
# This is more complex now since we may have "condition_and" or multiple conditions
if "forall_remove" in eff:
fr = eff["forall_remove"]
var_name, var_type = fr["vars"][0]
for o in all_objects:
if var_type == "object" and o in characters:
continue
if var_type == "agent" and o not in characters:
continue
# Check conditions in condition_and or condition_pred
conditions_met = True
if "condition_and" in fr:
for c in fr["condition_and"]:
if not _check_condition_list(new_state, c, {**param_map, var_name: o}, all_objects, characters):
conditions_met = False
break
elif "condition_pred" in fr:
if not _check_condition_list(new_state, fr["condition_pred"], {**param_map, var_name: o}, all_objects, characters):
conditions_met = False
if conditions_met:
rm_pred = fr["remove_pred"][0]
rm_args = substitute(fr["remove_pred"][1:], {**param_map, var_name: o})
if rm_pred in new_state and rm_args in new_state[rm_pred]:
new_state[rm_pred].remove(rm_args)
if not new_state[rm_pred]:
del new_state[rm_pred]
# Handle forall_remove_multiple
if "forall_remove_multiple" in eff:
fr = eff["forall_remove_multiple"]
var_name, var_type = fr["vars"][0]
# This is unconditional removal of multiple predicates for each object
# No condition specified, so it's always apply these removals to all objects
# If you need conditions, adapt similarly to above.
for o in all_objects:
if var_type == "object" and o in characters:
continue
if var_type == "agent" and o not in characters:
continue
# Remove all listed preds
for pred_tuple in fr["remove_preds"]:
pred = pred_tuple[0]
dargs = substitute(pred_tuple[1:], {**param_map, var_name: o})
if pred in new_state and dargs in new_state[pred]:
new_state[pred].remove(dargs)
if not new_state[pred]:
del new_state[pred]
return new_state
def compute_state_diff(old_state, new_state):
added = {}
removed = {}
all_preds = set(old_state.keys()).union(new_state.keys())
for pred in all_preds:
old_tuples = old_state.get(pred, set())
new_tuples = new_state.get(pred, set())
added_t = new_tuples - old_tuples
removed_t = old_tuples - new_tuples
if added_t:
added[pred] = added_t
if removed_t:
removed[pred] = removed_t
return added, removed
def check_goal(state, goal_conditions):
for pred, tuples in goal_conditions.items():
if pred not in state:
return False
for t in tuples:
if t not in state[pred]:
return False
return True
#########################################################
# Example main execution (similar to VirtualHome)
#########################################################
if __name__ == "__main__":
# Provide paths to PDDL problem and SAS plan
pddl_file_name = "boxing_books_up_for_storage_0_Benevolence_1_int_0_2021-09-10_15-35-47"
problem_file = f"behavior_pddls/{pddl_file_name}.pddl"
plan_file = f"sas_plans/{pddl_file_name}"
initial_state, goal_conditions, all_objects, characters, object_types = load_pddl_problem_line_by_line(problem_file)
with open(plan_file, "r") as f:
sas_plan = [line.strip() for line in f if line.strip() and not line.startswith(";")]
# Parse plan
parsed_plan = []
for action_line in sas_plan:
action_line = action_line.strip("()")
parts = action_line.split()
action_name = parts[0]
args = parts[1:]
parsed_plan.append((action_name, args))
state = deepcopy(initial_state)
for step, (action_name, args) in enumerate(parsed_plan):
print(f"Step {step+1}: {action_name} {args}")
if action_name not in actions:
print("Action not defined in dictionary.")
break
if check_preconditions(state, actions[action_name], args, all_objects, characters, object_types):
old_state = deepcopy(state)
state = apply_action(state, actions[action_name], args, all_objects, characters, object_types)
added, removed = compute_state_diff(old_state, state)
print("Action executed:", action_name, args)
print("Added:", added)
print("Removed:", removed)
print()
if check_goal(state, goal_conditions):
print("Goal reached after step:", step + 1)
break
else:
print(f"Preconditions not met for {action_name} {args}. Execution halted.")
break
else:
print("Plan executed but goal not reached.")