Skip to content

Commit fb187dc

Browse files
committed
reformat files
1 parent 4572387 commit fb187dc

10 files changed

Lines changed: 60 additions & 36 deletions

File tree

jupyddl/automated_planner.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
logging.getLogger("julia").setLevel(logging.WARNING)
1515

16+
1617
class AutomatedPlanner:
1718
def __init__(self, domain_path, problem_path, log_level="DEBUG"):
1819
# Planning Tool
@@ -27,7 +28,7 @@ def __init__(self, domain_path, problem_path, log_level="DEBUG"):
2728
"basic/zero",
2829
"basic/goal_count",
2930
"delete_relaxation/h_add",
30-
"delete_relaxation/h_max"
31+
"delete_relaxation/h_max",
3132
]
3233

3334
# Logger
@@ -44,7 +45,6 @@ def __run_julia_once(self):
4445
actions = self.available_actions(self.initial_state)
4546
self.transition(self.initial_state, actions[0])
4647

47-
4848
def __init_logger(self, log_level):
4949
import os
5050

@@ -133,7 +133,7 @@ def dijktra_best_first_search(self):
133133
def astar_best_first_search(self, heuristic_key="basic/goal_count"):
134134
if "basic" in heuristic_key:
135135
heuristic = BasicHeuristic(self, heuristic_key)
136-
elif "delete_relaxation" in heuristic_key:
136+
elif "delete_relaxation" in heuristic_key:
137137
heuristic = DeleteRelaxationHeuristic(self, heuristic_key)
138138
else:
139139
logging.fatal("Not yet implemented")

jupyddl/data_analyst.py

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

2429
def __get_all_pddl_from_data(self, max_pddl_instances=-1):
2530
tested_files = []
@@ -84,7 +89,11 @@ def __gather_data_astar(
8489
for problem, domain in self.__get_all_pddl_from_data(
8590
max_pddl_instances=max_pddl_instances
8691
):
87-
logging.debug("Loading new PDDL instance planned with A* [ " + heuristic_key + " ]")
92+
logging.debug(
93+
"Loading new PDDL instance planned with A* [ "
94+
+ heuristic_key
95+
+ " ]"
96+
)
8897
logging.debug("Domain: " + domain)
8998
logging.debug("Problem: " + problem)
9099
apla = AutomatedPlanner(domain, problem)
@@ -124,7 +133,11 @@ def __gather_data_astar(
124133
return [0], [0], has_multiple_files_tested
125134

126135
def plot_astar(
127-
self, heuristic_key="basic/goal_count", domain="", problem="", max_pddl_instances=-1
136+
self,
137+
heuristic_key="basic/goal_count",
138+
domain="",
139+
problem="",
140+
max_pddl_instances=-1,
128141
):
129142
if bool(not problem) != bool(not domain):
130143
logging.warning(
@@ -354,10 +367,7 @@ def comparative_astar_heuristic_plot(
354367
times_y.append(data[node_opened])
355368

356369
ax.plot(
357-
nodes_sorted,
358-
times_y,
359-
"-o",
360-
label=h,
370+
nodes_sorted, times_y, "-o", label=h,
361371
)
362372

363373
plt.title("A* heuristics complexity comparison")
@@ -429,10 +439,7 @@ def comparative_data_plot(
429439
for node_opened in nodes_sorted:
430440
times_y.append(data[node_opened])
431441
ax.plot(
432-
nodes_sorted,
433-
times_y,
434-
"-o",
435-
label=planner,
442+
nodes_sorted, times_y, "-o", label=planner,
436443
)
437444
plt.title("Planners complexity comparison")
438445
plt.legend(loc="upper left")

jupyddl/dijkstra.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
from datetime import datetime as timestamp
55
from time import time as now
66

7+
78
def zero_heuristic():
89
return 0
10+
11+
912
class DijkstraBestFirstSearch:
1013
def __init__(self, automated_planner):
1114
self.automated_planner = automated_planner

jupyddl/heuristics.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import logging
22

3+
34
class BasicHeuristic:
45
def __init__(self, automated_planner, heuristic_key):
56
self.automated_planner = automated_planner
67
self.heuristic_keys = {
78
"basic/zero": self.__zero_heuristic,
8-
"basic/goal_count": self.__goal_count_heuristic
9+
"basic/goal_count": self.__goal_count_heuristic,
910
}
1011
if heuristic_key not in list(self.heuristic_keys.keys()):
11-
logging.warning("Heuristic key isn't registered, forcing it to [basic/goal_count]")
12+
logging.warning(
13+
"Heuristic key isn't registered, forcing it to [basic/goal_count]"
14+
)
1215
heuristic_key = "basic/goal_count"
1316

1417
self.current_h = heuristic_key
@@ -31,19 +34,21 @@ class DeleteRelaxationHeuristic:
3134
def __init__(self, automated_planner, heuristic_key):
3235
class DRHCache:
3336
def __init__(self, domain=None, axioms=None, preconds=None, additions=None):
34-
self.domain = domain
37+
self.domain = domain
3538
self.axioms = axioms
3639
self.preconds = preconds
3740
self.additions = additions
38-
41+
3942
self.automated_planner = automated_planner
4043
self.cache = DRHCache()
4144
self.heuristic_keys = {
4245
"delete_relaxation/h_add": self.__h_add,
43-
"delete_relaxation/h_max": self.__h_max
46+
"delete_relaxation/h_max": self.__h_max,
4447
}
4548
if heuristic_key not in list(self.heuristic_keys.keys()):
46-
logging.warning("Heuristic key isn't registered, forcing it to [delete_relaxation/h_add]")
49+
logging.warning(
50+
"Heuristic key isn't registered, forcing it to [delete_relaxation/h_add]"
51+
)
4752
heuristic_key = "delete_relaxation/h_add"
4853

4954
self.current_h = heuristic_key
@@ -56,25 +61,30 @@ def compute(self, state):
5661
self.__pre_compute()
5762
domain = self.cache.domain
5863
goals = self.automated_planner.goals
59-
types = state.types
60-
facts = state.facts
64+
types = state.types
65+
facts = state.facts
6166
fact_costs = self.automated_planner.pddl.init_facts_costs(facts)
62-
while not(len(fact_costs) == self.automated_planner.pddl.length(facts) and self.__facts_eq(fact_costs, facts)):
63-
facts, state = self.automated_planner.pddl.get_facts_and_state(fact_costs, types)
67+
while not (
68+
len(fact_costs) == self.automated_planner.pddl.length(facts)
69+
and self.__facts_eq(fact_costs, facts)
70+
):
71+
facts, state = self.automated_planner.pddl.get_facts_and_state(
72+
fact_costs, types
73+
)
6474
if self.automated_planner.satisfies(goals, state):
6575
costs = []
66-
fact_costs_str = dict( [(str(k), val) for k, val in fact_costs.items()] )
76+
fact_costs_str = dict([(str(k), val) for k, val in fact_costs.items()])
6777
for g in goals:
6878
if str(g) in fact_costs_str:
6979
costs.append(fact_costs_str[str(g)])
7080
costs.insert(0, 0)
7181
return self.heuristic_keys[self.current_h](costs)
72-
82+
7383
for ax in self.cache.axioms:
7484
fact_costs = self.automated_planner.pddl.compute_costs_one_step_derivation(
7585
facts, fact_costs, ax, self.current_h
7686
)
77-
87+
7888
actions = self.automated_planner.available_actions(state)
7989
if not actions:
8090
break
@@ -93,13 +103,14 @@ def __pre_compute(self):
93103
additions = dict()
94104
self.automated_planner.pddl.cache_global_preconditions(domain)
95105
for name, definition in domain.actions.items():
96-
additions[name] = self.automated_planner.pddl.effect_diff(definition.effect).add
106+
additions[name] = self.automated_planner.pddl.effect_diff(
107+
definition.effect
108+
).add
97109
self.cache.additions = additions
98110
self.cache.preconds = self.automated_planner.pddl.g_preconditions
99111
self.cache.domain = domain
100112
self.cache.axioms = axioms
101113
self.has_been_precomputed = True
102-
103114

104115
def __h_add(self, costs):
105116
return sum(costs)
@@ -109,7 +120,6 @@ def __h_max(self, costs):
109120

110121
def __facts_eq(self, facts_dict, facts_set):
111122
for f in facts_set:
112-
if not(f in facts_dict.keys()):
123+
if not (f in facts_dict.keys()):
113124
return False
114125
return True
115-

main.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ def main():
1414
args = args_parser.parse_args()
1515
apla_tbx = AutomatedPlanner(args.domain, args.problem)
1616
apla_tbx.logger.info("Starting the planning script")
17-
apla_tbx.logger.debug(
18-
"Available heuristics: " + str(apla_tbx.available_heuristics)
19-
)
17+
apla_tbx.logger.debug("Available heuristics: " + str(apla_tbx.available_heuristics))
2018

2119
path, computation_time, _ = apla_tbx.dijktra_best_first_search()
2220
apla_tbx.logger.debug(apla_tbx.get_actions_from_path(path))

tests/test_astar.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from jupyddl.a_star import AStarBestFirstSearch
99
from jupyddl.heuristics import DeleteRelaxationHeuristic, BasicHeuristic
1010

11+
1112
def test_astar_basic():
1213
apla = AutomatedPlanner(
1314
"pddl-examples/dinner/domain.pddl", "pddl-examples/dinner/problem.pddl"
@@ -16,6 +17,7 @@ def test_astar_basic():
1617
astar = AStarBestFirstSearch(apla, heuristic.compute)
1718
assert astar.init.h_cost == heuristic.compute(apla.initial_state)
1819

20+
1921
def test_astar_delete_relaxation():
2022
apla = AutomatedPlanner(
2123
"pddl-examples/dinner/domain.pddl", "pddl-examples/dinner/problem.pddl"
@@ -24,6 +26,7 @@ def test_astar_delete_relaxation():
2426
astar = AStarBestFirstSearch(apla, heuristic.compute)
2527
assert astar.init.h_cost == heuristic.compute(apla.initial_state)
2628

29+
2730
def test_astar_goal():
2831
apla = AutomatedPlanner(
2932
"pddl-examples/dinner/domain.pddl", "pddl-examples/dinner/problem.pddl"

tests/test_heuristics.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def test_goal_count_heuristic():
3131
h = heuristic.compute(apla.initial_state)
3232
assert h != 0
3333

34+
3435
def test_delete_relaxation_add_heuristic():
3536
apla = AutomatedPlanner(
3637
"pddl-examples/tsp/domain.pddl", "pddl-examples/tsp/problem.pddl"

tests/test_main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import main
22

3+
34
def test_main():
45
x = main.main()
56
assert x == 0

tests/test_search.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def test_search_astar_basic():
4747
assert res[1] != 0 # Assert that it took time to compute
4848
assert res[-1] > 0 # Assert that it visited at least one node
4949

50+
5051
def test_search_astar_delete_relaxation():
5152
apla = AutomatedPlanner(
5253
"pddl-examples/dinner/domain.pddl", "pddl-examples/dinner/problem.pddl"
@@ -56,4 +57,3 @@ def test_search_astar_delete_relaxation():
5657
res = astar.search() # Goal, computation_time, opened_nodes(in this order)
5758
assert res[1] != 0 # Assert that it took time to compute
5859
assert res[-1] > 0 # Assert that it visited at least one node
59-

tests/test_temp.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
from jupyddl.a_star import AStarBestFirstSearch
99
from jupyddl.heuristics import DeleteRelaxationHeuristic
1010

11+
1112
def test_astar_delete_relaxation():
1213
apla = AutomatedPlanner(
1314
"pddl-examples/tsp/domain.pddl", "pddl-examples/tsp/problem.pddl"
1415
)
1516
heuristic = DeleteRelaxationHeuristic(apla, "delete_relaxation/h_max")
16-
astar = AStarBestFirstSearch(apla, heuristic.compute)
17+
astar = AStarBestFirstSearch(apla, heuristic.compute)
1718
astar.search()
1819
assert astar.init.h_cost == heuristic.compute(apla.initial_state)

0 commit comments

Comments
 (0)