|
| 1 | +from .modules import loading_bar_handler |
| 2 | +from .bfs import BreadthFirstSearch |
| 3 | +from .dijkstra import DijkstraBestFirstSearch |
| 4 | + |
| 5 | +UI = False |
| 6 | + |
| 7 | +if UI: |
| 8 | + loading_bar_handler(False) |
| 9 | +import julia |
| 10 | + |
| 11 | +_ = julia.Julia(compiled_modules=False) |
| 12 | + |
| 13 | +if UI: |
| 14 | + loading_bar_handler(True) |
| 15 | + |
| 16 | +from julia import PDDL |
| 17 | +from time import time as now |
| 18 | + |
| 19 | +class AutomatedPlanner: |
| 20 | + def __init__(self, domain_path, problem_path): |
| 21 | + self.pddl = PDDL |
| 22 | + self.domain = self.pddl.load_domain(domain_path) |
| 23 | + self.problem = self.pddl.load_problem(problem_path) |
| 24 | + self.initial_state = self.pddl.initialize(self.problem) |
| 25 | + |
| 26 | + def __execute_action(self, action, state): |
| 27 | + return self.pddl.execute(action, state) |
| 28 | + |
| 29 | + def transition(self, state, action): |
| 30 | + return self.pddl.transition(self.domain, state, action, check=False) |
| 31 | + |
| 32 | + def available_actions(self, state): |
| 33 | + return self.pddl.available(state, self.domain) |
| 34 | + |
| 35 | + def satisfies(self, asserted_state, state): |
| 36 | + return self.pddl.satisfy(asserted_state, state, self.domain)[0] |
| 37 | + |
| 38 | + def __retrace_path(self, node): |
| 39 | + if not node: |
| 40 | + return [] |
| 41 | + path = [] |
| 42 | + while node.parent: |
| 43 | + path.append(node) |
| 44 | + node = node.parent |
| 45 | + path.reverse() |
| 46 | + return path |
| 47 | + |
| 48 | + def get_actions_from_path(self, path): |
| 49 | + if not path: |
| 50 | + print("Path is empty, can't operate...") |
| 51 | + return [] |
| 52 | + actions = [] |
| 53 | + for node in path: |
| 54 | + actions.append((node.parent_action, node.g_cost)) |
| 55 | + |
| 56 | + cost = self.pddl.get_value(path[-1].state, "total-cost") |
| 57 | + if not cost: |
| 58 | + return actions |
| 59 | + else: |
| 60 | + return (actions, cost) |
| 61 | + |
| 62 | + def get_state_def_from_path(self, path): |
| 63 | + if not path: |
| 64 | + print("Path is empty, can't operate...") |
| 65 | + return [] |
| 66 | + trimmed_path = [] |
| 67 | + for node in path: |
| 68 | + trimmed_path.append(node.state) |
| 69 | + return trimmed_path |
| 70 | + |
| 71 | + def breadth_first_search(self, time_it=False): |
| 72 | + if time_it: |
| 73 | + start_time = now() |
| 74 | + bfs = BreadthFirstSearch(self) |
| 75 | + last_node = bfs.search() |
| 76 | + if time_it: |
| 77 | + total_time = now() - start_time |
| 78 | + path = self.__retrace_path(last_node) |
| 79 | + if time_it: |
| 80 | + return path, total_time |
| 81 | + else: |
| 82 | + return path, None |
| 83 | + |
| 84 | + def dijktra_best_first_search(self, time_it=False): |
| 85 | + if time_it: |
| 86 | + start_time = now() |
| 87 | + dijkstra = DijkstraBestFirstSearch(self) |
| 88 | + last_node = dijkstra.search() |
| 89 | + if time_it: |
| 90 | + total_time = now() - start_time |
| 91 | + path = self.__retrace_path(last_node) |
| 92 | + if time_it: |
| 93 | + return path, total_time |
| 94 | + else: |
| 95 | + return path, None |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == "__main__": |
| 99 | + ap = AutomatedPlanner("..data/domain.pddl", "..data/problem.pddl") |
0 commit comments