-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathground.py
More file actions
executable file
·88 lines (66 loc) · 3.04 KB
/
ground.py
File metadata and controls
executable file
·88 lines (66 loc) · 3.04 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
#!/usr/bin/env python
import argparse
import os
import subprocess
import sys
import resources
from build import get_build_dir, build, PROJECT_ROOT
SCRIPT_PATH = os.path.dirname(os.path.realpath(__file__))
def find_domain_filename(task_filename):
"""
Find domain filename for the given task using automatic naming rules.
"""
dirname, basename = os.path.split(task_filename)
domain_basenames = [
"domain.pddl",
basename[:3] + "-domain.pddl",
"domain_" + basename,
"domain-" + basename,
]
for domain_basename in domain_basenames:
domain_filename = os.path.join(dirname, domain_basename)
if os.path.exists(domain_filename):
return domain_filename
def parse_arguments():
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description='Ground STRIPS planning tasks.')
parser.add_argument('-i', '--instance', required=True, help="The path to the problem instance file.")
parser.add_argument('-d', '--domain', default=None,
help="(Optional) The path to the problem domain file. If none is provided, "
"the system will try to automatically deduce it from the instance filename.")
parser.add_argument('-t', '--lp-output', default="reachable.lp",
help="File where the reachability logic program will be written.")
parser.add_argument('-m', '--method', default="fd", help="Grounding method.")
parser.add_argument('-b', '--build', action='store_true',
help="Build the project before running it (Default: False).")
parser.add_argument('--debug',
action="store_true", help="Build in debug mode.")
args = parser.parse_args()
if args.domain is None:
args.domain = find_domain_filename(args.instance)
if args.domain is None:
raise RuntimeError('Could not find domain filename that matches instance file ', args.domain)
return args
def main(args):
domain_file = args.domain
instance_file = args.instance
if not os.path.isfile(domain_file):
sys.stderr.write("Error: Domain file does not exist.\n")
sys.exit()
if not os.path.isfile(instance_file):
sys.stderr.write("Error: Instance file does not exist.\n")
sys.exit()
if args.build:
build(args.debug)
with resources.timing(f"Generating reachability LP", newline=True):
generate_lp(args.domain, args.instance, args.lp_output)
ground_lp(args.lp_output, args.method, args.debug)
def generate_lp(domain, instance, lp_output):
with open(lp_output, 'w') as f:
subprocess.call([os.path.join(PROJECT_ROOT, 'src', 'translate', 'pddl_to_prolog.py'), domain, instance],
stdout=f)
def ground_lp(filename, method, debug_flag):
BUILD_DIR = get_build_dir(debug_flag)
subprocess.check_call([os.path.join(BUILD_DIR, 'grounder'), filename, method])
if __name__ == '__main__':
main(parse_arguments())