Skip to content

Commit 4bea1ef

Browse files
committed
increase coverage and add tests
1 parent 9db6599 commit 4bea1ef

7 files changed

Lines changed: 113 additions & 39 deletions

File tree

jupyddl/automated_planner.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ def available_actions(self, state):
6767
try:
6868
return self.pddl.available(state, self.domain)
6969
except (RuntimeError, TypeError, NameError):
70-
self.logger.warning("Runtime, Type or Name error occured when fetching available action from state" + str(state))
70+
self.logger.warning(
71+
"Runtime, Type or Name error occured when fetching available action from state"
72+
+ str(state)
73+
)
7174
return []
7275

7376
def satisfies(self, asserted_state, state):

jupyddl/data_analyst.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,7 @@ def comparative_astar_heuristic_plot(
367367
times_y.append(data[node_opened])
368368

369369
ax.plot(
370-
nodes_sorted,
371-
times_y,
372-
"-o",
373-
label=h,
370+
nodes_sorted, times_y, "-o", label=h,
374371
)
375372

376373
plt.title("A* heuristics complexity comparison")
@@ -442,10 +439,7 @@ def comparative_data_plot(
442439
for node_opened in nodes_sorted:
443440
times_y.append(data[node_opened])
444441
ax.plot(
445-
nodes_sorted,
446-
times_y,
447-
"-o",
448-
label=planner,
442+
nodes_sorted, times_y, "-o", label=planner,
449443
)
450444
plt.title("Planners complexity comparison")
451445
plt.legend(loc="upper left")

jupyddl/heuristics.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,8 @@ def compute(self, state):
7878
return self.heuristic_keys[self.current_h](costs)
7979

8080
for ax in self.cache.axioms:
81-
fact_costs = (
82-
self.automated_planner.pddl.compute_costs_one_step_derivation(
83-
facts, fact_costs, ax, self.current_h
84-
)
81+
fact_costs = self.automated_planner.pddl.compute_costs_one_step_derivation(
82+
facts, fact_costs, ax, self.current_h
8583
)
8684

8785
actions = self.automated_planner.available_actions(state)
@@ -90,7 +88,9 @@ def compute(self, state):
9088
fact_costs, act, domain, self.cache.additions, self.current_h
9189
)
9290

93-
if len(fact_costs) == self.automated_planner.pddl.length(facts) and self.__facts_eq(fact_costs, facts):
91+
if len(fact_costs) == self.automated_planner.pddl.length(
92+
facts
93+
) and self.__facts_eq(fact_costs, facts):
9494
break
9595

9696
return float("inf")

tests/test_basic_astar.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@ def test_astar_path_length():
3535
path, _, _ = apla.astar_best_first_search()
3636
assert len(path) > 0
3737

38+
3839
def test_astar_path_no_path():
3940
apla = AutomatedPlanner(
4041
"pddl-examples/vehicle/domain.pddl", "pddl-examples/vehicle/problem.pddl"
4142
)
4243
path, _, _ = apla.astar_best_first_search()
4344
assert len(path) == 0
4445

46+
4547
def test_astar_path_no_heuristic():
4648
apla = AutomatedPlanner(
4749
"pddl-examples/flip/domain.pddl", "pddl-examples/flip/problem.pddl"

tests/test_basic_search.py

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from jupyddl.automated_planner import AutomatedPlanner
2-
from jupyddl.dijkstra import DijkstraBestFirstSearch
2+
from jupyddl.dijkstra import DijkstraBestFirstSearch, zero_heuristic
33
from jupyddl.a_star import AStarBestFirstSearch
44
from jupyddl.bfs import BreadthFirstSearch
55
from jupyddl.heuristics import BasicHeuristic, DeleteRelaxationHeuristic
@@ -37,6 +37,33 @@ def test_search_dijkstra():
3737
assert res[-1] > 0 # Assert that it visited some nodes
3838

3939

40+
def test_search_dijkstra_no_path():
41+
apla = AutomatedPlanner(
42+
"pddl-examples/vehicle/domain.pddl", "pddl-examples/vehicle/problem.pddl"
43+
)
44+
dijk = DijkstraBestFirstSearch(apla)
45+
res = dijk.search() # Goal, computation_time, opened_nodes(in this order)
46+
assert not res[0]
47+
48+
49+
def test_search_dfs_no_path():
50+
apla = AutomatedPlanner(
51+
"pddl-examples/vehicle/domain.pddl", "pddl-examples/vehicle/problem.pddl"
52+
)
53+
dfs = DepthFirstSearch(apla)
54+
res = dfs.search() # Goal, computation_time, opened_nodes(in this order)
55+
assert not res[0]
56+
57+
58+
def test_search_bfs_no_path():
59+
apla = AutomatedPlanner(
60+
"pddl-examples/vehicle/domain.pddl", "pddl-examples/vehicle/problem.pddl"
61+
)
62+
bfs = BreadthFirstSearch(apla)
63+
res = bfs.search() # Goal, computation_time, opened_nodes(in this order)
64+
assert not res[0]
65+
66+
4067
def test_search_astar_basic():
4168
apla = AutomatedPlanner(
4269
"pddl-examples/dinner/domain.pddl", "pddl-examples/dinner/problem.pddl"
@@ -48,15 +75,19 @@ def test_search_astar_basic():
4875
assert res[-1] > 0 # Assert that it visited at least one node
4976

5077

51-
def test_search_astar_delete_relaxation():
78+
def test_search_astar_basic_no_path():
5279
apla = AutomatedPlanner(
53-
"pddl-examples/dinner/domain.pddl", "pddl-examples/dinner/problem.pddl"
80+
"pddl-examples/vehicle/domain.pddl", "pddl-examples/vehicle/problem.pddl"
5481
)
55-
heuristic = DeleteRelaxationHeuristic(apla, "delete_relaxation/h_max")
82+
heuristic = BasicHeuristic(apla, "basic/goal_count")
5683
astar = AStarBestFirstSearch(apla, heuristic.compute)
5784
res = astar.search() # Goal, computation_time, opened_nodes(in this order)
58-
assert res[1] != 0 # Assert that it took time to compute
59-
assert res[-1] > 0 # Assert that it visited at least one node
85+
assert not res[0]
86+
87+
88+
def test_zero_heuristic():
89+
assert zero_heuristic() == 0
90+
6091

6192
def test_search_getter_costs():
6293
apla = AutomatedPlanner(
@@ -69,6 +100,7 @@ def test_search_getter_costs():
69100

70101
assert path and plan and state_plan
71102

103+
72104
def test_search_getter_no_costs():
73105
apla = AutomatedPlanner(
74106
"pddl-examples/cargo/domain.pddl", "pddl-examples/cargo/problem.pddl"
@@ -79,6 +111,3 @@ def test_search_getter_no_costs():
79111
state_plan = apla.get_state_def_from_path(path)
80112

81113
assert path and plan and state_plan
82-
83-
84-

tests/test_hsp_astar.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,24 @@
88
from jupyddl.a_star import AStarBestFirstSearch
99
from jupyddl.heuristics import DeleteRelaxationHeuristic
1010

11-
# def test_astar_hmax():
12-
# apla = AutomatedPlanner(
13-
# "pddl-examples/dinner/domain.pddl", "pddl-examples/dinner/problem.pddl"
14-
# )
15-
# heuristic = DeleteRelaxationHeuristic(apla, "delete_relaxation/h_max")
16-
# astar = AStarBestFirstSearch(apla, heuristic.compute)
17-
# assert astar.init.h_cost == heuristic.compute(apla.initial_state)
18-
19-
20-
# def test_astar_hadd():
21-
# apla = AutomatedPlanner(
22-
# "pddl-examples/dinner/domain.pddl", "pddl-examples/dinner/problem.pddl"
23-
# )
24-
# heuristic = DeleteRelaxationHeuristic(apla, "delete_relaxation/h_max")
25-
# astar = AStarBestFirstSearch(apla, heuristic.compute)
26-
# assert astar.init.h_cost == heuristic.compute(apla.initial_state)
11+
12+
def test_astar_hmax():
13+
apla = AutomatedPlanner(
14+
"pddl-examples/dinner/domain.pddl", "pddl-examples/dinner/problem.pddl"
15+
)
16+
heuristic = DeleteRelaxationHeuristic(apla, "delete_relaxation/h_max")
17+
astar = AStarBestFirstSearch(apla, heuristic.compute)
18+
assert astar.init.h_cost == heuristic.compute(apla.initial_state)
19+
20+
21+
def test_astar_hadd():
22+
apla = AutomatedPlanner(
23+
"pddl-examples/dinner/domain.pddl", "pddl-examples/dinner/problem.pddl"
24+
)
25+
heuristic = DeleteRelaxationHeuristic(apla, "delete_relaxation/h_max")
26+
astar = AStarBestFirstSearch(apla, heuristic.compute)
27+
assert astar.init.h_cost == heuristic.compute(apla.initial_state)
28+
2729

2830
def test_astar_hmax_sensible_domain():
2931
apla = AutomatedPlanner(

tests/test_node.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.node import Node
9+
10+
11+
def test_node_equality_cost():
12+
apla = AutomatedPlanner(
13+
"pddl-examples/tsp/domain.pddl", "pddl-examples/tsp/problem.pddl"
14+
)
15+
actions = apla.available_actions(apla.initial_state)
16+
if not actions:
17+
assert 0 == 1
18+
return
19+
next_state = apla.transition(apla.initial_state, actions[0])
20+
next_node = Node(next_state, apla, heuristic_based=True)
21+
next_node_v2 = Node(next_state, apla)
22+
23+
assertion = next_node_v2 < next_node
24+
assertion2 = next_node < next_node_v2
25+
26+
assert assertion and assertion2
27+
28+
29+
def test_node_equality_no_cost():
30+
apla = AutomatedPlanner(
31+
"pddl-examples/dinner/domain.pddl", "pddl-examples/dinner/problem.pddl"
32+
)
33+
actions = apla.available_actions(apla.initial_state)
34+
if not actions:
35+
assert 0 == 1
36+
return
37+
next_state = apla.transition(apla.initial_state, actions[0])
38+
next_node = Node(next_state, apla, heuristic_based=True)
39+
next_node_v2 = Node(next_state, apla)
40+
41+
assertion = next_node_v2 < next_node
42+
assertion2 = next_node < next_node_v2
43+
44+
assert assertion and assertion2

0 commit comments

Comments
 (0)