-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwriter.py
46 lines (40 loc) · 1.71 KB
/
writer.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
import os
def domain2str(F, A):
header = '(define (domain {domain_name})\n{domain_body})'
requirements = '(:requirements :adl)\n'
predicates_str = '(:predicates \n{})\n'
actions = ''
predicates = ''
for atom in F:
predicates += '\t{}\n'.format(str(atom))
for action in A:
actions += '{}\n\n'.format(str(action))
domain_body = requirements + predicates_str.format(predicates) + actions
domain_str = header.format(domain_name='compiled-domain',
domain_body=domain_body)
return domain_str
def problem2str(I, G):
header = '(define (problem {problem_name})\n(:domain {domain_name})\n{problem_body})'
init_facts = '(:init \n{})\n'
goal = '(:goal {})\n'.format(str(G))
init = ''
for atom in I:
init += '\t{}\n'.format(str(atom))
problem_body = init_facts.format(init) + goal
problem_str = header.format(problem_name='compiled-problem',
domain_name='compiled-domain',
problem_body=problem_body)
return problem_str
def output_compiled_problem(F, A, I, G, directory, output):
domain_str = domain2str(F, A)
problem_str = problem2str(I, G)
output_dom_path = os.path.join(directory, output + "_dom.pddl")
output_prob_path = os.path.join(directory, output + "_prob.pddl")
# if not os.path.exists(directory + "output/"):
# os.mkdir(directory + "output/")
with open(output_dom_path, 'w') as out_dom:
out_dom.write(domain_str)
with open(output_prob_path, 'w') as out_prob:
out_prob.write(problem_str)
print("Compiled domain:{}".format(output_dom_path))
print("Compiled problem:{}".format(output_prob_path))