Skip to content

Commit

Permalink
wip: dlr
Browse files Browse the repository at this point in the history
  • Loading branch information
guilyx committed Jan 1, 2021
1 parent 9c78bbd commit 2de1db3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
10 changes: 7 additions & 3 deletions jupyddl/automated_planner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from .dfs import DepthFirstSearch
from .dijkstra import DijkstraBestFirstSearch
from .a_star import AStarBestFirstSearch
from .heuristics import BasicHeuristic
from .heuristics import BasicHeuristic, DeleteRelaxationHeuristic
import coloredlogs
import logging
import julia
Expand All @@ -25,7 +25,9 @@ def __init__(self, domain_path, problem_path, log_level="DEBUG"):
self.goals = self.__flatten_goal()
self.available_heuristics = [
"basic/zero",
"basic/goal_count"
"basic/goal_count",
"delete_relaxation/h_add",
"delete_relaxation/h_max"
]

# Logger
Expand Down Expand Up @@ -131,7 +133,9 @@ def dijktra_best_first_search(self):
def astar_best_first_search(self, heuristic_key="basic/goal_count"):
if "basic" in heuristic_key:
heuristic = BasicHeuristic(self, heuristic_key)
else:
elif "delete_relaxation" in heuristic_key:
heuristic = DeleteRelaxationHeuristic(self, heuristic_key)
else:
logging.fatal("Not yet implemented")
exit()
astar = AStarBestFirstSearch(self, heuristic.compute)
Expand Down
21 changes: 15 additions & 6 deletions jupyddl/heuristics.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,33 @@ def __init__(self, domain=None, axioms=None, preconds=None, additions=None):
self.__pre_compute()
# return self.heuristic_keys[self.current_h](state)

def compute(self, state, goals):
def compute(self, state):
if not self.has_been_precomputed:
self.__pre_compute()
domain = self.cache.domain
goals = self.automated_planner.goals
types = state.types
facts = state.facts

fact_costs = dict([(f, 0) for f in facts])
while not(len(fact_costs) == len(facts) and self.__facts_eq(fact_costs, facts)):
facts_set = self.automated_planner.pddl.Set(self.automated_planner.pddl.keys(fact_costs))
state = self.automated_planner.pddl.create_state(types, facts_set)
fact_costs = self.automated_planner.pddl.init_facts_costs(facts)
while not(len(fact_costs) == self.automated_planner.pddl.length(facts) and self.__facts_eq(fact_costs, facts)):
facts, state = self.automated_planner.pddl.get_facts_and_state(fact_costs, types)
if self.automated_planner.satisfies(goals, state):
costs = [fact_costs[g] for g in goals]
costs.insert(0, 0)
return self.heuristic_keys[self.current_h](costs)

for ax in self.cache.axioms:
pass
fact_costs = self.automated_planner.pddl.compute_costs_one_step_derivatiion(
facts, fact_costs, ax, self.current_h
)

actions = self.automated_planner.available_actions(state)
for act in actions:
fact_costs = self.automated_planner.pddl.compute_cost_action_effect(
fact_costs, act, domain, self.cache.preconds, self.cache.additions, self.current_h
)
return float("inf")

def __pre_compute(self):
if self.has_been_precomputed:
Expand All @@ -86,6 +94,7 @@ def __pre_compute(self):
self.cache.preconds = preconditions
self.cache.domain = domain
self.cache.axioms = axioms
self.has_been_precomputed = True


def __h_add(self, costs):
Expand Down
16 changes: 12 additions & 4 deletions tests/test_heuristics.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,23 @@ def test_zero_heuristic():
apla = AutomatedPlanner(
"pddl-examples/flip/domain.pddl", "pddl-examples/flip/problem.pddl"
)
heuristic = hs.BasicHeuristic(apla, "zero")
heuristic = hs.BasicHeuristic(apla, "basic/zero")
h = heuristic.compute(apla.initial_state)
assert h == 0


def test_goal_count_heuristic():
apla = AutomatedPlanner(
"pddl-examples/flip/domain.pddl", "pddl-examples/flip/problem.pddl"
"pddl-examples/dinner/domain.pddl", "pddl-examples/dinner/problem.pddl"
)
heuristic = hs.BasicHeuristic(apla, "goal_count")
heuristic = hs.BasicHeuristic(apla, "basic/goal_count")
h = heuristic.compute(apla.initial_state)
assert h == 0
assert h != 0

def test_delete_relaxation_add_heuristic():
apla = AutomatedPlanner(
"pddl-examples/tsp/domain.pddl", "pddl-examples/tsp/problem.pddl"
)
heuristic = hs.DeleteRelaxationHeuristic(apla, "delete_relaxation/h_max")
h = heuristic.compute(apla.initial_state)
assert h != 0

0 comments on commit 2de1db3

Please sign in to comment.