-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplanning.py
75 lines (60 loc) · 2.16 KB
/
planning.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
import os
import re
import shutil
import subprocess
from typing import Iterable
PPDL_DIR = "../ppdl"
PLANNER_DIR = "../fast-downward"
def plan(goal: str, agent_name: str) -> Iterable[str]:
"""Plans given a goal and an agent name, using the
domain and initial state files present in `./ppdl`.
This uses subprocess to call `fast-downward`, which needs
to be built before it can be used. Follow these instructions:
`cd ./fast-downward`
`./build.py`.
:param goal: The goal (as generated by randGoal or supplied).
:param agent_name: The name of the agent to use.
:returns: The plan in the form of a list of actions in PPDL format.
"""
# Clear out the temp directory before populating it
if os.path.exists("temp"):
shutil.rmtree("temp")
# Recreate the temporary directory
os.mkdir("temp")
# Switch to the temporary directory to avoid extra content in strings
# Then replace the "replace-with-" markers in the initial_state.ppdl
os.chdir("temp")
with open(PPDL_DIR + "/initial_state.ppdl", "r") as f:
ppdl_str = f.read()
ppdl_str = ppdl_str.replace("replace-with-agent", agent_name)
ppdl_str = ppdl_str.replace("replace-with-goal", goal)
with open("initial_state.ppdl", "w") as f:
f.write(ppdl_str)
# Run the planner
with open(agent_name + ".soln", "w") as solution:
calc = subprocess.call(
[
PLANNER_DIR +
"/fast-downward.py",
PPDL_DIR +
"/domain.ppdl",
"initial_state.ppdl",
"--search",
"astar(ipdb())"],
stdout=solution)
# Using a regex, pull the solution out of the output file
soln_regex = r"([a-z ]+) \(\d\)$"
soln = []
with open(agent_name + ".soln", "r") as solution:
for line in solution:
if re.match(soln_regex, line.rstrip()):
soln.append(line.rstrip())
# Cleanup
os.chdir("..")
shutil.rmtree("temp")
return soln
if __name__ == "__main__":
print(
plan(
"(and (has Aladdin lamp) (captive you Aladdin) (used lamp))",
"Aladdin"))