Skip to content

Commit 8875cc3

Browse files
committed
add delete relaxation heuristics
1 parent 30d80b5 commit 8875cc3

6 files changed

Lines changed: 67 additions & 24 deletions

File tree

jupyddl/data_analyst.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
class DataAnalyst:
2020
def __init__(self):
2121
logging.info("Instantiating data analyst...")
22-
self.available_heuristics = ["basic/goal_count", "basic/zero"]
22+
self.available_heuristics = ["basic/goal_count", "basic/zero", "delete_relaxation/h_add", "delete_relaxation/h_max"]
2323

2424
def __get_all_pddl_from_data(self, max_pddl_instances=-1):
2525
tested_files = []
@@ -84,7 +84,7 @@ def __gather_data_astar(
8484
for problem, domain in self.__get_all_pddl_from_data(
8585
max_pddl_instances=max_pddl_instances
8686
):
87-
logging.debug("Loading new PDDL instance planned with A*...")
87+
logging.debug("Loading new PDDL instance planned with A* [ " + heuristic_key + " ]")
8888
logging.debug("Domain: " + domain)
8989
logging.debug("Problem: " + problem)
9090
apla = AutomatedPlanner(domain, problem)

jupyddl/heuristics.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,24 +58,29 @@ def compute(self, state):
5858
goals = self.automated_planner.goals
5959
types = state.types
6060
facts = state.facts
61-
6261
fact_costs = self.automated_planner.pddl.init_facts_costs(facts)
6362
while not(len(fact_costs) == self.automated_planner.pddl.length(facts) and self.__facts_eq(fact_costs, facts)):
6463
facts, state = self.automated_planner.pddl.get_facts_and_state(fact_costs, types)
6564
if self.automated_planner.satisfies(goals, state):
66-
costs = [fact_costs[g] for g in goals]
65+
costs = []
66+
fact_costs_str = dict( [(str(k), val) for k, val in fact_costs.items()] )
67+
for g in goals:
68+
if str(g) in fact_costs_str:
69+
costs.append(fact_costs_str[str(g)])
6770
costs.insert(0, 0)
6871
return self.heuristic_keys[self.current_h](costs)
6972

7073
for ax in self.cache.axioms:
71-
fact_costs = self.automated_planner.pddl.compute_costs_one_step_derivatiion(
74+
fact_costs = self.automated_planner.pddl.compute_costs_one_step_derivation(
7275
facts, fact_costs, ax, self.current_h
7376
)
7477

7578
actions = self.automated_planner.available_actions(state)
79+
if not actions:
80+
break
7681
for act in actions:
7782
fact_costs = self.automated_planner.pddl.compute_cost_action_effect(
78-
fact_costs, act, domain, self.cache.preconds, self.cache.additions, self.current_h
83+
fact_costs, act, domain, self.cache.additions, self.current_h
7984
)
8085
return float("inf")
8186

@@ -84,14 +89,13 @@ def __pre_compute(self):
8489
return
8590
domain = self.automated_planner.domain
8691
domain, axioms = self.automated_planner.pddl.compute_hsp_axioms(domain)
87-
preconditions = dict()
92+
# preconditions = dict()
8893
additions = dict()
94+
self.automated_planner.pddl.cache_global_preconditions(domain)
8995
for name, definition in domain.actions.items():
90-
precond = self.automated_planner.pddl.filter_negative_preconds(definition)
91-
preconditions[name] = precond
9296
additions[name] = self.automated_planner.pddl.effect_diff(definition.effect).add
9397
self.cache.additions = additions
94-
self.cache.preconds = preconditions
98+
self.cache.preconds = self.automated_planner.pddl.g_preconditions
9599
self.cache.domain = domain
96100
self.cache.axioms = axioms
97101
self.has_been_precomputed = True

tests/test_astar.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,32 @@
66
sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
77
from jupyddl.automated_planner import AutomatedPlanner
88
from jupyddl.a_star import AStarBestFirstSearch
9+
from jupyddl.heuristics import DeleteRelaxationHeuristic, BasicHeuristic
910

10-
11-
def test_astar_init():
11+
def test_astar_basic():
1212
apla = AutomatedPlanner(
1313
"pddl-examples/flip/domain.pddl", "pddl-examples/flip/problem.pddl"
1414
)
15-
astar = AStarBestFirstSearch(apla, apla.available_heuristics["basic/goal_count"])
16-
assert astar.init.h_cost == apla.available_heuristics["basic/goal_count"](
17-
apla.initial_state, apla
18-
)
15+
heuristic = BasicHeuristic(apla, "basic/goal_count")
16+
astar = AStarBestFirstSearch(apla, heuristic.compute)
17+
assert astar.init.h_cost == heuristic.compute(apla.initial_state)
1918

19+
def test_astar_delete_relaxation():
20+
apla = AutomatedPlanner(
21+
"pddl-examples/flip/domain.pddl", "pddl-examples/flip/problem.pddl"
22+
)
23+
heuristic = DeleteRelaxationHeuristic(apla, "delete_relaxation/h_max")
24+
astar = AStarBestFirstSearch(apla, heuristic.compute)
25+
assert astar.init.h_cost == heuristic.compute(apla.initial_state)
2026

2127
def test_astar_goal():
2228
apla = AutomatedPlanner(
2329
"pddl-examples/flip/domain.pddl", "pddl-examples/flip/problem.pddl"
2430
)
25-
path, _, _ = apla.astar_best_first_search()
26-
assert apla.available_heuristics["basic/goal_count"](path[-1].state, apla) == 0
31+
heuristic = BasicHeuristic(apla, "basic/goal_count")
32+
astar = AStarBestFirstSearch(apla, heuristic.compute)
33+
path, _, _ = astar.search()
34+
assert heuristic.compute(path[-1].state) == 0
2735

2836

2937
def test_astar_path_length():

tests/test_search.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
from jupyddl.dijkstra import DijkstraBestFirstSearch
33
from jupyddl.a_star import AStarBestFirstSearch
44
from jupyddl.bfs import BreadthFirstSearch
5+
from jupyddl.heuristics import BasicHeuristic, DeleteRelaxationHeuristic
56
from jupyddl.dfs import DepthFirstSearch
67
from os import path
78
import coloredlogs
89
import sys
910

1011

11-
def test_searchDFS():
12+
def test_search_dfs():
1213
apla = AutomatedPlanner(
1314
"pddl-examples/dinner/domain.pddl", "pddl-examples/dinner/problem.pddl"
1415
)
@@ -17,7 +18,7 @@ def test_searchDFS():
1718
assert res[1] != 0 # Path, computation time, opened nodes
1819

1920

20-
def test_searchBFS():
21+
def test_search_bfs():
2122
apla = AutomatedPlanner(
2223
"pddl-examples/flip/domain.pddl", "pddl-examples/flip/problem.pddl"
2324
)
@@ -26,7 +27,7 @@ def test_searchBFS():
2627
assert res[1] != 0
2728

2829

29-
def test_searchDijkstra():
30+
def test_search_dijkstra():
3031
apla = AutomatedPlanner(
3132
"pddl-examples/dinner/domain.pddl", "pddl-examples/dinner/problem.pddl"
3233
)
@@ -36,11 +37,23 @@ def test_searchDijkstra():
3637
assert res[-1] > 0 # Assert that it visited some nodes
3738

3839

39-
def test_searchAStar():
40+
def test_search_astar_basic():
4041
apla = AutomatedPlanner(
4142
"pddl-examples/dinner/domain.pddl", "pddl-examples/dinner/problem.pddl"
4243
)
43-
astar = AStarBestFirstSearch(apla, apla.available_heuristics["basic/goal_count"])
44+
heuristic = BasicHeuristic(apla, "basic/goal_count")
45+
astar = AStarBestFirstSearch(apla, heuristic.compute)
4446
res = astar.search() # Goal, computation_time, opened_nodes(in this order)
4547
assert res[1] != 0 # Assert that it took time to compute
4648
assert res[-1] > 0 # Assert that it visited at least one node
49+
50+
def test_search_astar_delete_relaxation():
51+
apla = AutomatedPlanner(
52+
"pddl-examples/dinner/domain.pddl", "pddl-examples/dinner/problem.pddl"
53+
)
54+
heuristic = DeleteRelaxationHeuristic(apla, "delete_relaxation/h_max")
55+
astar = AStarBestFirstSearch(apla, heuristic.compute)
56+
res = astar.search() # Goal, computation_time, opened_nodes(in this order)
57+
assert res[1] != 0 # Assert that it took time to compute
58+
assert res[-1] > 0 # Assert that it visited at least one node
59+

tests/test_temp.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import sys
4+
from os import path
5+
6+
sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
7+
from jupyddl.automated_planner import AutomatedPlanner
8+
from jupyddl.a_star import AStarBestFirstSearch
9+
from jupyddl.heuristics import DeleteRelaxationHeuristic
10+
11+
def test_astar_delete_relaxation():
12+
apla = AutomatedPlanner(
13+
"pddl-examples/tsp/domain.pddl", "pddl-examples/tsp/problem.pddl"
14+
)
15+
heuristic = DeleteRelaxationHeuristic(apla, "delete_relaxation/h_max")
16+
astar = AStarBestFirstSearch(apla, heuristic.compute)
17+
astar.search()
18+
assert astar.init.h_cost == heuristic.compute(apla.initial_state)

0 commit comments

Comments
 (0)