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
24 changes: 17 additions & 7 deletions jupyddl/automated_planner.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,30 @@ def get_actions_from_path(self, path):
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
return (actions, cost)
if not node.parent_action:
break
act = str(node.parent_action).replace("<PyCall.jlwrap ", "")
cost = "total-cost = " + str(node.g_cost)
actions.append((act, cost))

return actions

def __stringify_state(self, state):
state_str = str(state).replace("<PyCall.jlwrap PDDL.State(", "")
state_str = state_str.replace("Set(Julog.Term[", "")
state_str = state_str.replace("])", "")
state_str = state_str.replace('Dict{Symbol,Any}(Symbol("total-cost") =>', "total-cost =")
state_str = state_str.replace("))>", "")
return state_str

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

def breadth_first_search(self):
Expand Down
20 changes: 20 additions & 0 deletions jupyddl/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def __init__(
self.state = state
self.parent_action = parent_action
self.parent = parent
self.automated_planner = automated_planner
temp_cost = automated_planner.pddl.get_value(state, "total-cost")
if temp_cost:
self.g_cost = temp_cost
Expand Down Expand Up @@ -51,5 +52,24 @@ def __init__(
self.is_closed = is_closed
self.is_open = is_open

def __stringify_state(self, state):
state_str = str(state).replace("<PyCall.jlwrap PDDL.State(", "")
state_str = state_str.replace("Set(Julog.Term[", "")
state_str = state_str.replace("])", "")
state_str = state_str.replace('Dict{Symbol,Any}(Symbol("total-cost") =>', "total-cost =")
state_str = state_str.replace("))>", "")
return state_str

def __lt__(self, other):
return self.f_cost <= other.f_cost

def __str__(self):
state = self.__stringify_state(self.state)
return "Node { %s | g = %.2f | h = %.2f | open = %s | closed = %s }" % (state, self.g_cost, self.h_cost, self.is_open, self.is_closed)

class Path:
def __init__(self, nodes):
self.nodes = nodes

def __str__(self):
return str([str(n) for n in self.nodes])
2 changes: 2 additions & 0 deletions tests/test_dfs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import sys
from os import path
24 changes: 23 additions & 1 deletion tests/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
from jupyddl.automated_planner import AutomatedPlanner
from jupyddl.node import Node
from jupyddl.node import Node, Path


def test_node_equality_cost():
Expand Down Expand Up @@ -36,3 +36,25 @@ def test_node_equality_no_cost():
assertion2 = next_node < next_node_v2

assert assertion and assertion2

def test_stringified_node():
apla = AutomatedPlanner(
"pddl-examples/dinner/domain.pddl", "pddl-examples/dinner/problem.pddl"
)
actions = apla.available_actions(apla.initial_state)
for act in actions:
next_state = apla.transition(apla.initial_state, act)
next_node = Node(next_state, apla, heuristic_based=True)
assert "<PyCall.jlwrap PDDL.State" not in str(next_node) and "Set(Julog.Term" not in str(next_node)

def test_stringified_path():
apla = AutomatedPlanner(
"pddl-examples/dinner/domain.pddl", "pddl-examples/dinner/problem.pddl"
)
actions = apla.available_actions(apla.initial_state)
path = []
for act in actions:
next_state = apla.transition(apla.initial_state, act)
path.append(Node(next_state, apla, heuristic_based=True))

assert "<PyCall.jlwrap PDDL.State" not in str(Path(path)) and "Set(Julog.Term" not in str(Path(path))