-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpreprocessing.py
More file actions
790 lines (493 loc) · 22.1 KB
/
preprocessing.py
File metadata and controls
790 lines (493 loc) · 22.1 KB
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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
import re
from predicates import *
import inspect
import predicates
import importlib.util
def initialize_predicates(predicates_file_path):
"""
Load predicates from a given predicates.py file and store them globally.
Args:
predicates_file_path (str): Path to the predicates.py file.
Returns:
None
"""
global predicate_functions
# Load the predicates module dynamically
module_name = "predicates"
spec = importlib.util.spec_from_file_location(module_name, predicates_file_path)
predicates_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(predicates_module)
# Extract all functions from the module
predicate_functions = {
name: func
for name, func in inspect.getmembers(predicates_module, inspect.isfunction)
}
print(f"Loaded predicates: {list(predicate_functions.keys())}")
# Global dictionary to store predicate functions
initialize_predicates("predicates.py")
precondition_param_mapping = {
"controllable": [["?obj"]],
"overlapping": [["?obj", "?to"]],
"carrying": [["?item"], ["?obj"], ["?key"]], # Multiple alternatives
"inventory_full": [[]], # No parameters needed beyond `state`
"locked": [["?door"]],
"open_door": [["?door"]],
"unlocks": [["?key", "?door"]],
"clear": [["?door"]],
"next_to": [["?item", "?adjacent"], ["?door", "?obj"], ["?obj", "?to"]],
"blocking": [["?obj", "?door"]],
"agent_moved_away": [["?door"]],
"stationary": [["?adjacent"]],
"not_near_door": [["?obj"]],
"found": [["?key"]],
"unfound": [["?box"]], # Explicit mapping for negated found
"went_through": [["?agent", "?door"]],
"safe_to_collect": [["?diamond"]],
"collected": [["?diamond"]],
"diamond_bag_full": [[]], # No parameters needed beyond `state`
"no_more_boxes": [["?boxes"]], # No parameters needed beyond `state`
"unstored_box": [["?box"]], # No parameters needed beyond `state`
"boxes_stuck": [[]], # No parameters needed beyond `state`
}
# explicit_type_mapping = {
# "overlapping": {
# "?obj": ["red_agent"],
# "?to": ["red_ball", "blue_ball", "green_ball", "yellow_ball"]
# },
# "open_door": {
# "?door": ["closed_red_door", "open_purple_door", "locked_yellow_door"]
# },
# "unlock": {
# "?door": ["locked_red_door", "locked_yellow_door"],
# "?key": ["red_key", "yellow_key"]
# },
# "pickup": {
# "?item": ["red_ball", "blue_box", "green_key"]
# },
# # Add other predicates as needed
# }
def domain_specific_type_system_mapping_BABYAI(state):
"""
Automatically generate the explicit type mapping based on the domain-specific rules.
Args:
state (dict): The current game state.
Returns:
dict: The explicit type mapping for all predicates.
"""
explicit_type_mapping = {}
# 1. "controllable"
explicit_type_mapping["controllable"] = {
"?obj": ["red_agent"]
}
# 2. "overlapping"
explicit_type_mapping["overlapping"] = {
"?obj": ["red_agent"],
"?to": [entity for entity in state.keys() if entity != "grey_wall"]
}
# 3. "open_door"
explicit_type_mapping["open_door"] = {
"?door": [entity for entity in state.keys() if entity.endswith("_door")]
}
# 4. "unlocks"
explicit_type_mapping["unlocks"] = {
"?door": [entity for entity in state.keys() if entity.startswith("locked_") and entity.endswith("_door")],
"?key": [entity for entity in state.keys() if entity.endswith("_key")]
}
# 5. "pickup"
explicit_type_mapping["pickup"] = {
"?item": [entity for entity in state.keys() if entity.endswith(("_ball", "_box", "_key"))]
}
# 6. "carrying"
explicit_type_mapping["carrying"] = {
"?item": [entity for entity in state.keys() if entity.endswith(("_ball", "_box", "_key"))],
"?obj": [entity for entity in state.keys() if entity.endswith(("_ball", "_box", "_key"))],
"?key": [entity for entity in state.keys() if entity.endswith("_key")]
}
# 7. "locked"
explicit_type_mapping["locked"] = {
"?door": [entity for entity in state.keys() if entity.endswith("_door")]
}
# 8. "clear"
explicit_type_mapping["clear"] = {
"?door": [entity for entity in state.keys() if entity.endswith("_door")]
}
# 9. "next_to"
explicit_type_mapping["next_to"] = {
"?item": [entity for entity in state.keys() if entity != "grey_wall"],
"?adjacent": [entity for entity in state.keys() if entity != "grey_wall"]
}
# 10. "blocking"
explicit_type_mapping["blocking"] = {
"?obj": [entity for entity in state.keys() if entity.endswith(("_box", "_key", "_ball"))],
"?door": [entity for entity in state.keys() if entity.endswith("_door")]
}
# 11. "agent_moved_away"
explicit_type_mapping["agent_moved_away"] = {
"?door": [entity for entity in state.keys() if entity.endswith("_door")]
}
# 12. "not_near_door"
explicit_type_mapping["not_near_door"] = {
"?obj": [entity for entity in state.keys() if entity.endswith(("_key", "_box", "_ball"))]
}
# 13. "clear"
explicit_type_mapping["clear"] = {
"?door": [entity for entity in state.keys() if entity.endswith("_door")]
}
# 14. "found"
explicit_type_mapping["found"] = {
"?key": [entity for entity in state.keys() if entity.endswith("_key")]
}
# 13. "unfound"
explicit_type_mapping["unfound"] = {
"?box": [entity for entity in state.keys() if entity.endswith("_box")]
}
# breakpoint()
return explicit_type_mapping
def load_predicate_functions(module):
"""
Load all functions from the given module dynamically.
Args:
module: The module from which to extract functions.
Returns:
dict: A dictionary mapping function names to their implementations.
"""
return {
name: func
for name, func in inspect.getmembers(module, inspect.isfunction)
}
def preprocess_subplan(subplan):
"""
Processes a given subplan string based on the specified action type.
Depends on Domain file and level of abstraction of plans.
The function supports two actions:
1. 'form_rule': Removes the numeric suffix from each parameter.
2. 'move_to': Extracts the base object name and converts the numeric suffix to a zero-based index.
Parameters:
subplan (str): A string representing the action and its parameters. The format is
"action param1 param2 ...", where each parameter may have a numeric
suffix separated by an underscore.
Returns:
list: A list of processed parameters.
- For 'form_rule', it returns a list of strings with numeric suffixes removed.
Example:
Input: 'form_rule flag_word is_word win_word'
Output: ['flag_word', 'is_word', 'win_word']
- For 'move_to', it returns a list alternating between the base object names
and their corresponding zero-based indices.
Example:
Input: 'move_to baba_obj flag_obj'
Output: ['baba_obj', 'flag_obj']
"""
action, *params = subplan.split()
# formatted_params = []
# if action in ['form_rule', 'break_rule']:
# formatted_params = [param for param in params]
# if action in ['move_to', 'push_to']:
# formatted_params = [param for param in params]
return [param for param in params]
def parse_domain_file(domain_file):
with open(domain_file, 'r') as file:
lines = file.readlines()
domain_data = {}
current_action = None
for line in lines:
line = line.strip()
if line.startswith("(:action"):
current_action = line.split()[1]
domain_data[current_action] = {"parameters": [], "preconditions": [], "effects": []}
elif current_action and line.startswith(":parameters"):
parameters = re.findall(r'\?\w+', line)
domain_data[current_action]["parameters"] = parameters
elif current_action and line.startswith(":precondition"):
precondition_str = line.replace(":precondition", "").strip()
preconditions = parse_logical_expression(precondition_str)
domain_data[current_action]["preconditions"] = preconditions
elif current_action and line.startswith(":effect"):
effect_str = line.replace(":effect", "").strip()
effects = parse_logical_expression(effect_str)
domain_data[current_action]["effects"] = effects
elif current_action and line == ")":
current_action = None
return domain_data
def parse_logical_expression(expression):
tokens = re.findall(r'\(|\)|\w+|not', expression)
stack = []
current = []
for token in tokens:
if token == '(':
stack.append(current)
current = []
elif token == ')':
if stack:
parent = stack.pop()
parent.append(current)
current = parent
else:
current.append(token)
return current[0] if len(current) == 1 else current # Flatten top-level nesting
def operator_extractor(domain_file, subplan):
domain_data = parse_domain_file(domain_file)
operator = subplan.split()[0]
if operator in domain_data:
parameters = domain_data[operator]["parameters"]
preconditions = extract_predicates(domain_data[operator]["preconditions"])
effects = extract_predicates(domain_data[operator]["effects"])
# breakpoint()
# depends on abstraction level of domain file used
formatted_args = preprocess_subplan(subplan)
# if operator == 'unblock':
# breakpoint()
# if operator == 'move_to':
# breakpoint()
return {"operator": operator, "parameters": parameters, "preconditions": preconditions, "effects": effects, "grounding_Python": formatted_args}
else:
raise ValueError(f"Operator {operator} not found in domain file.")
def extract_predicates(conditions):
predicates = []
if isinstance(conditions, list):
if conditions[0] == 'not': # Handle negation
predicates.append(f"not {conditions[1][0]}") # Add 'not' with predicate name
elif conditions[0] in ['and', 'or']: # Handle logical operators
for sub_condition in conditions[1:]:
predicates.extend(extract_predicates(sub_condition)) # Recurse
else: # Direct predicate name
predicates.append(conditions[0])
return predicates
def checker(state, predicates, operators):
results = []
grounding = {param: value for param, value in zip(operators["parameters"], operators["grounding_Python"])}
# if operators['operator'] == 'put_next_to':
# breakpoint()
for predicate in predicates:
print(f"Evaluating predicate: {predicate}")
# if predicate == "blocking":
# breakpoint()
# if predicate == "put_next_to":
# breakpoint()
is_negated = predicate.startswith("not ")
predicate_name = predicate[4:] if is_negated else predicate
if predicate_name not in predicate_functions:
raise ValueError(f"Unknown predicate: {predicate_name}")
# Get all possible parameter sets for the predicate
possible_param_sets = precondition_param_mapping.get(predicate_name, [])
args = None
for param_set in possible_param_sets:
try:
# Attempt to resolve all parameters in the set
args = [grounding[param] for param in param_set]
break # Stop once we successfully resolve a parameter set
except KeyError:
continue # Try the next parameter set
if args is None:
# breakpoint()
raise KeyError(f"No matching parameter set for {predicate_name}. Grounding: {grounding}")
# Call the predicate with state and resolved arguments
result = predicate_functions[predicate_name](state, *args)
print(*args)
if is_negated:
result = not result
# if operators["operator"] == "put_next_to":
# breakpoint()
results.append(result)
# if operators["operator"] == "put_next_to":
# breakpoint()
# if operators["operator"] == "move_to":
# breakpoint()
print("Predicate evaluations:", results)
return all(results)
def is_and_expression(subplan):
"""Check if the subplan is an AND expression."""
return subplan.startswith('AND(')
def evaluate_and_expression(domain_file, expression, state):
"""
Evaluates a string of the form AND(subplan1, subplan2) and returns the AND result.
Args:
domain_file (str): Path to the PDDL domain file.
expression (str): The AND expression containing two subplans.
state (dict): The current game state.
Returns:
bool: True if both subplans are satisfied, False otherwise.
"""
# Modify the regex to extract subplans without quotes
match = re.match(r'AND\((.+?),\s*(.+?)\)', expression)
if not match:
raise ValueError("Expression is not in the correct AND format.")
# Extract the two subplans
subplan_1 = match.group(1).strip()
subplan_2 = match.group(2).strip()
# Extract operator and preconditions for the first subplan
operator_1 = operator_extractor(domain_file, subplan_1)
preconditions_1 = operator_1['preconditions']
result_1 = checker(state, preconditions_1, operator_1)
# Extract operator and preconditions for the second subplan
operator_2 = operator_extractor(domain_file, subplan_2)
preconditions_2 = operator_2['preconditions']
result_2 = checker(state, preconditions_2, operator_2)
# breakpoint()
# result_1 = not result
# Return the AND of both results
return not result_1 and not result_2
# def enumerate_groundings(domain_file, state):
# """
# Enumerate all possible groundings for operators in a domain given a state.
# Args:
# domain_file (str): Path to the PDDL domain file.
# state (dict): Current state dictionary.
# Returns:
# dict: A dictionary where keys are operators and values are lists of possible groundings.
# """
# domain_data = parse_domain_file(domain_file)
# groundings = {}
# # Match entities in state to PDDL types
# type_mapping = {key: [] for key in ["object", "door"]}
# for entity in state.keys():
# if entity.endswith("_door"):
# type_mapping["door"].append(entity)
# elif entity not in {"red_agent", "agent_direction", "agent_carrying"}: # Exclude agent-specific keys
# type_mapping["object"].append(entity)
# # Enumerate groundings for each operator
# for operator, data in domain_data.items():
# param_types = data["parameters"]
# # Deduce types from the domain file
# param_types_cleaned = []
# for param in param_types:
# match = re.match(r"\?\w+ - (\w+)", param)
# param_type = match.group(1) if match else "object"
# param_types_cleaned.append(param_type)
# # Generate possible groundings using Cartesian product
# try:
# param_entities = [type_mapping[param_type] for param_type in param_types_cleaned]
# operator_groundings = list(product(*param_entities))
# except KeyError as e:
# print(f"Type {e} not found in type mapping for operator {operator}. Defaulting to empty.")
# operator_groundings = []
# groundings[operator] = operator_groundings
# return groundings
from itertools import product
# dictionary method
# def enumerate_possible_subplans(state, domain_file):
# """
# Enumerate all possible subplans by generating all combinations of parameters
# for operators based on the state dictionary.
# Args:
# state (dict): The current game state.
# domain_file (str): The domain file containing operators.
# Returns:
# dict: A dictionary where keys are operator names and values are lists of grounded subplans.
# """
# # Load domain data
# domain_data = parse_domain_file(domain_file)
# # Ignore these keys while generating combinations
# ignored_keys = {"agent_direction", "agent_carrying"}
# # Extract valid keys from the state dictionary
# entities = [
# key for key in state.keys()
# if key not in ignored_keys and isinstance(state[key], list) and state[key] # Ensure the key has positions
# ]
# grounded_subplans = {}
# # Generate grounded subplans for each operator
# for operator, data in domain_data.items():
# param_count = len(data["parameters"]) # Number of parameters for the operator
# # Generate all combinations of entities for the parameters
# param_combinations = product(entities, repeat=param_count)
# # Create grounded subplans using string formatting
# grounded_subplans[operator] = [
# f"{operator} " + " ".join(params) for params in param_combinations
# ]
# return grounded_subplans
def enumerate_possible_subplans(state, domain_file):
"""
Enumerate all possible subplans for the given state and domain.
Args:
state (dict): The current game state.
domain_file (str): Path to the PDDL domain file.
Returns:
list: A flattened list of all grounded subplans in the format:
["operator arg1 arg2 ...", ...]
"""
# Parse the domain file to get operator data
domain_data = parse_domain_file(domain_file)
# Get all keys from the state (exclude agent-related keys)
state_keys = [key for key in state.keys() if key not in ['red_agent', 'agent_direction', 'agent_carrying']]
subplans = []
# Enumerate grounded subplans for each operator
for operator, data in domain_data.items():
param_names = data["parameters"] # e.g., ['?obj', '?to']
# Create all combinations of parameters
grounded_combinations = product(state_keys, repeat=len(param_names))
for grounding in grounded_combinations:
# Format the subplan as "operator arg1 arg2 ..."
subplan = f"{operator} " + " ".join(grounding)
subplans.append(subplan)
return subplans
def validate_arguments(predicate, args, explicit_type_mapping):
"""
Validate arguments against the explicit type mapping.
Args:
predicate (str): The predicate name (e.g., "overlapping").
args (dict): A dictionary of argument bindings (e.g., {"?obj": "red_agent", "?to": "red_ball"}).
explicit_type_mapping (dict): The explicit type mapping dictionary.
Returns:
bool: True if the arguments are valid, False otherwise.
"""
if predicate not in explicit_type_mapping:
return False # Unknown predicate
valid_args = explicit_type_mapping[predicate]
for arg, value in args.items():
if arg not in valid_args or value not in valid_args[arg]:
return False
return True
def prune_invalid_subplans_TYPE(possible_subplans, type_mapping, domain_file):
"""
Prune invalid subplans based on the type constraints defined for preconditions.
Args:
possible_subplans (list): List of possible subplans (grounded operators).
type_mapping (dict): Explicit type mapping for each predicate.
domain_file (str): The domain file to extract operator details.
Returns:
list: A list of valid subplans after pruning.
"""
valid_subplans = []
for subplan in possible_subplans:
parts = subplan.split() # Split into operator and arguments
operator, *args = parts # Extract the operator and arguments
try:
# Extract operator details from the domain file
operator_details = operator_extractor(domain_file, subplan)
preconditions = operator_details["preconditions"]
# Check if arguments satisfy type constraints for all preconditions
is_valid = True
for predicate in preconditions:
# Extract predicate name and parameters
is_negated = predicate.startswith("not ")
predicate_name = predicate[4:] if is_negated else predicate
# Skip unknown predicates
if predicate_name not in type_mapping:
print(f"Skipping unknown predicate '{predicate_name}' in subplan '{subplan}'.")
continue
# Get type constraints for the predicate
type_constraints = type_mapping[predicate_name]
# Resolve arguments for this predicate
param_mappings = precondition_param_mapping.get(predicate_name, [])
for param_set in param_mappings:
try:
resolved_args = {param: args[i] for i, param in enumerate(param_set)}
# Validate each argument against its type constraints
for param, value in resolved_args.items():
if param in type_constraints and value not in type_constraints[param]:
print(f"Skipping subplan '{subplan}': Argument '{value}' does not match valid values for '{param}' in predicate '{predicate_name}'.")
is_valid = False
break
if not is_valid:
break
except IndexError:
print(f"Skipping subplan '{subplan}': Argument mismatch in predicate '{predicate_name}'.")
is_valid = False
break
if not is_valid:
break
if is_valid:
valid_subplans.append(subplan)
except ValueError as e:
print(f"Error processing subplan '{subplan}': {e}")
return valid_subplans