Skip to content

Commit 6d3dc40

Browse files
authored
Merge pull request #24 from APLA-Toolbox/add-dfs
Fix Issue #11 - Add DFS
2 parents 91730c4 + b571bcf commit 6d3dc40

File tree

4 files changed

+61
-8
lines changed

4 files changed

+61
-8
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ $ julia --color=yes -e 'using Pkg; Pkg.add(Pkg.PackageSpec(path="https://github.
3232
$ julia --color=yes -e 'using Pkg; Pkg.add(Pkg.PackageSpec(path="https://github.com/JuliaPy/PyCall.jl"))'
3333
```
3434

35-
- Install Python dependencies
35+
- Install Python dependencies
3636

3737
```bash
3838
$ python3 -m pip install --upgrade pip
@@ -41,6 +41,10 @@ $ cd pddl-plarser
4141
$ python3 -m pip install -r requirements.txt
4242
```
4343

44+
# Usage
45+
46+
Navigate to the root directory and you can run the tool by using : `python3 main.py "data/domain.pddl" "data/problem.pddl"`
47+
4448
# REFL Mode
4549

4650
- Clone the repository: `git clone https://github.com/APLA-Toolbox/pddl-plarser`

main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import src.automated_planner as plarser
1+
import src.automated_planner as parser
22
import argparse
33

44

@@ -11,8 +11,8 @@ def main():
1111
args_parser.add_argument("problem", type=str, help="PDDL problem file")
1212
args_parser.add_argument("-v", "--verbose", help="Increases the output's verbosity")
1313
args = args_parser.parse_args()
14-
apla_tbx = plarser.AutomatedPlanner(args.domain, args.problem)
15-
path, time = apla_tbx.dijktra_best_first_search(time_it=True)
14+
apla_tbx = parser.AutomatedPlanner(args.domain, args.problem)
15+
path, time = apla_tbx.depth_first_search(time_it=True)
1616
print(apla_tbx.get_actions_from_path(path))
1717
print("Computation time: %.2f" % time)
1818

src/automated_planner.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
from .modules import loading_bar_handler
22
from .bfs import BreadthFirstSearch
3+
from .dfs import DepthFirstSearch
34
from .dijkstra import DijkstraBestFirstSearch
45

5-
loading_bar_handler(False)
6+
UI = False
7+
8+
if UI:
9+
loading_bar_handler(False)
610
import julia
11+
712
_ = julia.Julia(compiled_modules=False)
8-
loading_bar_handler(True)
13+
14+
if UI:
15+
loading_bar_handler(True)
16+
917
from julia import PDDL
1018
from time import time as now
1119

@@ -75,6 +83,19 @@ def breadth_first_search(self, time_it=False):
7583
else:
7684
return path, None
7785

86+
def depth_first_search(self, time_it=False):
87+
if time_it:
88+
start_time = now()
89+
dfs = DepthFirstSearch(self)
90+
last_node = dfs.search()
91+
if time_it:
92+
total_time = now() - start_time
93+
path = self.__retrace_path(last_node)
94+
if time_it:
95+
return path, total_time
96+
else:
97+
return path, None
98+
7899
def dijktra_best_first_search(self, time_it=False):
79100
if time_it:
80101
start_time = now()

src/dfs.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,35 @@
1+
from .node import Node
2+
13
class DepthFirstSearch:
24
def __init__(self, automated_planner):
5+
self.visited = []
36
self.automated_planner = automated_planner
7+
self.init = Node(self.automated_planner.initial_state, automated_planner)
8+
self.stack = [self.init]
49

510
def search(self):
6-
print("-/!\- No path found -/!\-")
7-
return []
11+
while self.stack:
12+
current_node = self.stack.pop(0)
13+
if current_node not in self.visited:
14+
self.visited.append(current_node)
15+
16+
if self.automated_planner.satisfies(
17+
self.automated_planner.problem.goal, current_node.state
18+
):
19+
return current_node
20+
21+
actions = self.automated_planner.available_actions(current_node.state)
22+
for act in actions:
23+
child = Node(
24+
state=self.automated_planner.transition(
25+
current_node.state, act
26+
),
27+
automated_planner=self.automated_planner,
28+
parent_action=act,
29+
parent=current_node,
30+
)
31+
if child in self.visited:
32+
continue
33+
self.stack.append(child)
34+
print("!!! No path found !!!")
35+
return None

0 commit comments

Comments
 (0)