|
1 | | -def zero_heuristic(state, automated_planner): |
2 | | - return 0 |
| 1 | +import logging |
3 | 2 |
|
4 | 3 |
|
5 | | -def goal_count_heuristic(state, automated_planner): |
6 | | - count = 0 |
7 | | - for goal in automated_planner.goals: |
8 | | - if not automated_planner.state_has_term(state, goal): |
9 | | - count += 1 |
10 | | - return count |
| 4 | +class BasicHeuristic: |
| 5 | + def __init__(self, automated_planner, heuristic_key): |
| 6 | + self.automated_planner = automated_planner |
| 7 | + self.heuristic_keys = { |
| 8 | + "basic/zero": self.__zero_heuristic, |
| 9 | + "basic/goal_count": self.__goal_count_heuristic, |
| 10 | + } |
| 11 | + if heuristic_key not in list(self.heuristic_keys.keys()): |
| 12 | + logging.warning( |
| 13 | + "Heuristic key isn't registered, forcing it to [basic/goal_count]" |
| 14 | + ) |
| 15 | + heuristic_key = "basic/goal_count" |
| 16 | + |
| 17 | + self.current_h = heuristic_key |
| 18 | + |
| 19 | + def compute(self, state): |
| 20 | + return self.heuristic_keys[self.current_h](state) |
| 21 | + |
| 22 | + def __zero_heuristic(self, state): |
| 23 | + return 0 |
| 24 | + |
| 25 | + def __goal_count_heuristic(self, state): |
| 26 | + count = 0 |
| 27 | + for goal in self.automated_planner.goals: |
| 28 | + if not self.automated_planner.state_has_term(state, goal): |
| 29 | + count += 1 |
| 30 | + return count |
| 31 | + |
| 32 | + |
| 33 | +class DeleteRelaxationHeuristic: |
| 34 | + def __init__(self, automated_planner, heuristic_key): |
| 35 | + class DRHCache: |
| 36 | + def __init__(self, domain=None, axioms=None, preconds=None, additions=None): |
| 37 | + self.domain = domain |
| 38 | + self.axioms = axioms |
| 39 | + self.preconds = preconds |
| 40 | + self.additions = additions |
| 41 | + |
| 42 | + self.automated_planner = automated_planner |
| 43 | + self.cache = DRHCache() |
| 44 | + self.heuristic_keys = { |
| 45 | + "delete_relaxation/h_add": self.__h_add, |
| 46 | + "delete_relaxation/h_max": self.__h_max, |
| 47 | + } |
| 48 | + if heuristic_key not in list(self.heuristic_keys.keys()): |
| 49 | + logging.warning( |
| 50 | + "Heuristic key isn't registered, forcing it to [delete_relaxation/h_add]" |
| 51 | + ) |
| 52 | + heuristic_key = "delete_relaxation/h_add" |
| 53 | + |
| 54 | + self.current_h = heuristic_key |
| 55 | + self.has_been_precomputed = False |
| 56 | + self.__pre_compute() |
| 57 | + # return self.heuristic_keys[self.current_h](state) |
| 58 | + |
| 59 | + def compute(self, state): |
| 60 | + if not self.has_been_precomputed: |
| 61 | + self.__pre_compute() |
| 62 | + domain = self.cache.domain |
| 63 | + goals = self.automated_planner.goals |
| 64 | + types = state.types |
| 65 | + facts = state.facts |
| 66 | + fact_costs = self.automated_planner.pddl.init_facts_costs(facts) |
| 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 | + ) |
| 74 | + if self.automated_planner.satisfies(goals, state): |
| 75 | + costs = [] |
| 76 | + fact_costs_str = dict([(str(k), val) for k, val in fact_costs.items()]) |
| 77 | + for g in goals: |
| 78 | + if str(g) in fact_costs_str: |
| 79 | + costs.append(fact_costs_str[str(g)]) |
| 80 | + costs.insert(0, 0) |
| 81 | + return self.heuristic_keys[self.current_h](costs) |
| 82 | + |
| 83 | + for ax in self.cache.axioms: |
| 84 | + fact_costs = self.automated_planner.pddl.compute_costs_one_step_derivation( |
| 85 | + facts, fact_costs, ax, self.current_h |
| 86 | + ) |
| 87 | + |
| 88 | + actions = self.automated_planner.available_actions(state) |
| 89 | + if not actions: |
| 90 | + break |
| 91 | + for act in actions: |
| 92 | + fact_costs = self.automated_planner.pddl.compute_cost_action_effect( |
| 93 | + fact_costs, act, domain, self.cache.additions, self.current_h |
| 94 | + ) |
| 95 | + return float("inf") |
| 96 | + |
| 97 | + def __pre_compute(self): |
| 98 | + if self.has_been_precomputed: |
| 99 | + return |
| 100 | + domain = self.automated_planner.domain |
| 101 | + domain, axioms = self.automated_planner.pddl.compute_hsp_axioms(domain) |
| 102 | + # preconditions = dict() |
| 103 | + additions = dict() |
| 104 | + self.automated_planner.pddl.cache_global_preconditions(domain) |
| 105 | + for name, definition in domain.actions.items(): |
| 106 | + additions[name] = self.automated_planner.pddl.effect_diff( |
| 107 | + definition.effect |
| 108 | + ).add |
| 109 | + self.cache.additions = additions |
| 110 | + self.cache.preconds = self.automated_planner.pddl.g_preconditions |
| 111 | + self.cache.domain = domain |
| 112 | + self.cache.axioms = axioms |
| 113 | + self.has_been_precomputed = True |
| 114 | + |
| 115 | + def __h_add(self, costs): |
| 116 | + return sum(costs) |
| 117 | + |
| 118 | + def __h_max(self, costs): |
| 119 | + return max(costs) |
| 120 | + |
| 121 | + def __facts_eq(self, facts_dict, facts_set): |
| 122 | + for f in facts_set: |
| 123 | + if not (f in facts_dict.keys()): |
| 124 | + return False |
| 125 | + return True |
0 commit comments