Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .github/workflows/pyapp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ jobs:
- uses: julia-actions/setup-julia@v1
with:
version: '1.4.1'
- name: Install PDDL.jl
- name: Install Julia dependencies
run: |
julia --color=yes -e 'using Pkg; Pkg.add(Pkg.PackageSpec(path="https://github.com/APLA-Toolbox/PDDL.jl"))'
- name: Install dependencies
julia --color=yes -e 'using Pkg; Pkg.add(Pkg.PackageSpec(path="https://github.com/JuliaPy/PyCall.jl"))'
- name: Install Python dependencies
run: |
python -m pip install --upgrade pip
python -m pip install julia
python -m pip install pycall
- name: Lint with flake8
run: |
python -m pip install flake8
Expand All @@ -38,5 +40,4 @@ jobs:
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
with:
file: ./coverage.xml
name: codecov-umbrella
20 changes: 20 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
codecov:
require_ci_to_pass: yes

coverage:
precision: 2
round: down
range: "70...100"

parsers:
gcov:
branch_detection:
conditional: yes
loop: yes
method: no
macro: no

comment:
layout: "reach,diff,flags,files,footer"
behavior: default
require_changes: no
9 changes: 5 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import src.pddl as plarser
import src.automated_planner as plarser
import argparse


Expand All @@ -11,9 +11,10 @@ def main():
args_parser.add_argument("problem", type=str, help="PDDL problem file")
args_parser.add_argument("-v", "--verbose", help="Increases the output's verbosity")
args = args_parser.parse_args()
apla_tbx = plarser.AutomatedPlanning(args.domain, args.problem)
path = apla_tbx.get_actions_from_path(apla_tbx.breadth_first_search())
print(path)
apla_tbx = plarser.AutomatedPlanner(args.domain, args.problem)
path, time = apla_tbx.dijktra_best_first_search(time_it=True)
print(apla_tbx.get_actions_from_path(path))
print("Computation time: %.2f" % time)


if __name__ == "__main__":
Expand Down
7 changes: 7 additions & 0 deletions src/astar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AStarBestFirstSearch():
def __init__(self, automated_planner):
self.automated_planner = automated_planner

def search(self):
print("-/!\- No path found -/!\-")
return []
99 changes: 99 additions & 0 deletions src/automated_planner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
from .modules import loading_bar_handler
from .bfs import BreadthFirstSearch
from .dijkstra import DijkstraBestFirstSearch

UI = False

if UI:
loading_bar_handler(False)
import julia

_ = julia.Julia(compiled_modules=False)

if UI:
loading_bar_handler(True)

from julia import PDDL
from time import time as now

class AutomatedPlanner:
def __init__(self, domain_path, problem_path):
self.pddl = PDDL
self.domain = self.pddl.load_domain(domain_path)
self.problem = self.pddl.load_problem(problem_path)
self.initial_state = self.pddl.initialize(self.problem)

def __execute_action(self, action, state):
return self.pddl.execute(action, state)

def transition(self, state, action):
return self.pddl.transition(self.domain, state, action, check=False)

def available_actions(self, state):
return self.pddl.available(state, self.domain)

def satisfies(self, asserted_state, state):
return self.pddl.satisfy(asserted_state, state, self.domain)[0]

def __retrace_path(self, node):
if not node:
return []
path = []
while node.parent:
path.append(node)
node = node.parent
path.reverse()
return path

def get_actions_from_path(self, path):
if not path:
print("Path is empty, can't operate...")
return []
actions = []
for node in path:
actions.append((node.parent_action, node.g_cost))

cost = self.pddl.get_value(path[-1].state, "total-cost")
if not cost:
return actions
else:
return (actions, cost)

def get_state_def_from_path(self, path):
if not path:
print("Path is empty, can't operate...")
return []
trimmed_path = []
for node in path:
trimmed_path.append(node.state)
return trimmed_path

def breadth_first_search(self, time_it=False):
if time_it:
start_time = now()
bfs = BreadthFirstSearch(self)
last_node = bfs.search()
if time_it:
total_time = now() - start_time
path = self.__retrace_path(last_node)
if time_it:
return path, total_time
else:
return path, None

def dijktra_best_first_search(self, time_it=False):
if time_it:
start_time = now()
dijkstra = DijkstraBestFirstSearch(self)
last_node = dijkstra.search()
if time_it:
total_time = now() - start_time
path = self.__retrace_path(last_node)
if time_it:
return path, total_time
else:
return path, None


if __name__ == "__main__":
ap = AutomatedPlanner("..data/domain.pddl", "..data/problem.pddl")
26 changes: 26 additions & 0 deletions src/bfs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from .node import Node

class BreadthFirstSearch():
def __init__(self, automated_planner):
self.visited = []
self.automated_planner = automated_planner
self.init = Node(self.automated_planner.initial_state, automated_planner)
self.queue = [self.init]

def search(self):
while self.queue:
current_node = self.queue.pop(0)
if current_node not in self.visited:
self.visited.append(current_node)

if self.automated_planner.satisfies(self.automated_planner.problem.goal, current_node.state):
return current_node

actions = self.automated_planner.available_actions(current_node.state)
for act in actions:
child = Node(state=self.automated_planner.transition(current_node.state, act), automated_planner=self.automated_planner, parent_action=act, parent=current_node)
if child in self.visited:
continue
self.queue.append(child)
print("!!! No path found !!!")
return None
7 changes: 7 additions & 0 deletions src/dfs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class DepthFirstSearch():
def __init__(self, automated_planner):
self.automated_planner = automated_planner

def search(self):
print("-/!\- No path found -/!\-")
return []
49 changes: 49 additions & 0 deletions src/dijkstra.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from .node import Node
import math
from .heuristics import zero_heuristic

class DijkstraBestFirstSearch():
def __init__(self, automated_planner):
self.automated_planner = automated_planner
self.init = Node(self.automated_planner.initial_state, automated_planner, is_closed=False, is_open=True)
self.open_nodes_n = 1
self.nodes = dict()
self.nodes[self.__hash(self.init)] = self.init

def __hash(self, node):
sep = ", Dict{Symbol,Any}"
string = str(node.state)
return string.split(sep, 1)[0] + ")"

def search(self):
while self.open_nodes_n > 0:
current_key = min([n for n in self.nodes if self.nodes[n].is_open], key=(lambda k: self.nodes[k].f_cost))
current_node = self.nodes[current_key]

if self.automated_planner.satisfies(self.automated_planner.problem.goal, current_node.state):
return current_node

current_node.is_closed = True
current_node.is_open = False
self.open_nodes_n -= 1

actions = self.automated_planner.available_actions(current_node.state)
for act in actions:
child = Node(state=self.automated_planner.transition(current_node.state, act), automated_planner=self.automated_planner, parent_action=act, parent=current_node, heuristic=zero_heuristic, is_closed=False, is_open=True)
child_hash = self.__hash(child)
if child_hash in self.nodes:
if self.nodes[child_hash].is_closed:
continue
if not self.nodes[child_hash].is_open:
self.nodes[child_hash] = child
self.open_nodes_n += 1
else:
if child.g_cost < self.nodes[child_hash].g_cost:
self.nodes[child_hash] = child
self.open_nodes_n += 1

else:
self.nodes[child_hash] = child
self.open_nodes_n += 1
print("!!! No path found !!!")
return None
2 changes: 2 additions & 0 deletions src/heuristics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def zero_heuristic(state, pddl):
return 0
27 changes: 27 additions & 0 deletions src/node.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Node():
def __init__(self, state, automated_planner, is_closed=None, is_open=None, parent_action=None, parent=None, g_cost=0, heuristic=None):
self.state = state
self.parent_action = parent_action
self.parent = parent
temp_cost = automated_planner.pddl.get_value(state, "total-cost")
if temp_cost:
self.g_cost = temp_cost
if heuristic:
self.h_cost = heuristic(state, automated_planner)
else:
self.h_cost = 0
self.f_cost = self.g_cost + self.h_cost
else:
self.g_cost = 1
if heuristic:
self.h_cost = heuristic(state, automated_planner)
else:
self.h_cost = 0
self.f_cost = self.g_cost + self.h_cost


self.is_closed = is_closed
self.is_open = is_open

def __lt__(self, other):
return self.f_cost <= other.f_cost
80 changes: 0 additions & 80 deletions src/pddl.py

This file was deleted.

5 changes: 0 additions & 5 deletions src/state.py

This file was deleted.

Loading