|
1 | 1 | import logging |
| 2 | +from .node import Node |
2 | 3 |
|
3 | 4 |
|
4 | 5 | class BasicHeuristic: |
@@ -129,17 +130,17 @@ def __facts_eq(self, facts_dict, facts_set): |
129 | 130 | return True |
130 | 131 |
|
131 | 132 |
|
132 | | -class CriticalPathHeuristic: |
| 133 | +class RelaxedCriticalPathHeuristic: |
133 | 134 | def __init__(self, automated_planner, critical_path_level=1): |
134 | | - class CPCache: |
| 135 | + class RCPCache: |
135 | 136 | def __init__(self, domain=None, axioms=None, preconds=None, additions=None): |
136 | 137 | self.domain = domain |
137 | 138 | self.axioms = axioms |
138 | 139 | self.preconds = preconds |
139 | 140 | self.additions = additions |
140 | 141 |
|
141 | 142 | self.automated_planner = automated_planner |
142 | | - self.cache = CPCache() |
| 143 | + self.cache = RCPCache() |
143 | 144 | if critical_path_level > 3: |
144 | 145 | logging.warning( |
145 | 146 | "Critical Path level is only implemented until 3, forcing it to 3." |
@@ -176,9 +177,7 @@ def compute(self, state): |
176 | 177 | if str(g) in fact_costs_str: |
177 | 178 | costs.append(fact_costs_str[str(g)]) |
178 | 179 | if self.critical_path_level == 2: |
179 | | - pairs_of_goals = [ |
180 | | - (g1, g2) for g1 in goals for g2 in goals if g1 != g2 |
181 | | - ] |
| 180 | + pairs_of_goals = [(g1, g2) for g1 in goals for g2 in goals if g1 != g2] |
182 | 181 | for gs in pairs_of_goals: |
183 | 182 | if ( |
184 | 183 | str(gs[0]) in fact_costs_str |
@@ -259,3 +258,118 @@ def __facts_eq(self, facts_dict, facts_set): |
259 | 258 | if not (str(f) in fact_costs_str.keys()): |
260 | 259 | return False |
261 | 260 | return True |
| 261 | + |
| 262 | +class CriticalPathHeuristic: |
| 263 | + def __init__(self, automated_planner, critical_path_level=1): |
| 264 | + self.automated_planner = automated_planner |
| 265 | + |
| 266 | + if critical_path_level > 3: |
| 267 | + logging.warning( |
| 268 | + "Critical Path level is only implemented until 3, forcing it to 3." |
| 269 | + ) |
| 270 | + self.critical_path_level = 3 |
| 271 | + if critical_path_level < 1: |
| 272 | + logging.warning( |
| 273 | + "Critical Path level has to be at least 1, forcing it to 1." |
| 274 | + ) |
| 275 | + self.critical_path_level = 1 |
| 276 | + else: |
| 277 | + self.critical_path_level = critical_path_level |
| 278 | + |
| 279 | + self.goals = [] |
| 280 | + |
| 281 | + if self.critical_path_level == 1: |
| 282 | + self.goals = self.automated_planner.goals |
| 283 | + #do a dijkstra search for each goal in the goals |
| 284 | + |
| 285 | + if self.critical_path_level == 2: |
| 286 | + self.goals = [[g1, g2] for g1 in self.automated_planner.goals for g2 in self.automated_planner.goals if g1 != g2] |
| 287 | + #create subgoal1, subgoal2 |
| 288 | + |
| 289 | + if self.critical_path_level == 3: |
| 290 | + self.goals = [ |
| 291 | + [g1, g2, g3] |
| 292 | + for g1 in self.automated_planner.goals |
| 293 | + for g2 in self.automated_planner.goals |
| 294 | + for g3 in self.automated_planner.goals |
| 295 | + if g1 != g2 and g1 != g3 and g2 != g3 |
| 296 | + ] |
| 297 | + |
| 298 | + print (self.goals) |
| 299 | + |
| 300 | + |
| 301 | + def __h_max(self, costs): |
| 302 | + return max(costs) |
| 303 | + |
| 304 | + def compute(self, state): |
| 305 | + costs = [] |
| 306 | + |
| 307 | + for subgoal in self.goals: |
| 308 | + costs.append(self.__dijkstra_search(state, subgoal)) |
| 309 | + |
| 310 | + return self.__h_max(costs) |
| 311 | + |
| 312 | + |
| 313 | + def __hash(self, node): |
| 314 | + sep = ", Dict{Symbol,Any}" |
| 315 | + string = str(node.state) |
| 316 | + return string.split(sep, 1)[0] + ")" |
| 317 | + |
| 318 | + |
| 319 | + def __dijkstra_search(self, state, goal): |
| 320 | + def zero_heuristic(): |
| 321 | + return 0 |
| 322 | + |
| 323 | + init = Node(state, self.automated_planner, is_closed=False, is_open=True, heuristic=zero_heuristic) |
| 324 | + |
| 325 | + open_nodes_n = 1 |
| 326 | + nodes = dict() |
| 327 | + nodes[self.__hash(init)] = init |
| 328 | + |
| 329 | + while open_nodes_n > 0: |
| 330 | + current_key = min( |
| 331 | + [n for n in nodes if nodes[n].is_open], |
| 332 | + key=(lambda k: nodes[k].f_cost), |
| 333 | + ) |
| 334 | + current_node = nodes[current_key] |
| 335 | + |
| 336 | + if self.automated_planner.satisfies(goal, current_node.state): |
| 337 | + return current_node.g_cost |
| 338 | + |
| 339 | + current_node.is_closed = True |
| 340 | + current_node.is_open = False |
| 341 | + open_nodes_n -= 1 |
| 342 | + |
| 343 | + actions = self.automated_planner.available_actions(current_node.state) |
| 344 | + |
| 345 | + for act in actions: |
| 346 | + child = Node( |
| 347 | + state=self.automated_planner.transition(current_node.state, act), |
| 348 | + automated_planner=self.automated_planner, |
| 349 | + parent_action=act, |
| 350 | + parent=current_node, |
| 351 | + heuristic=zero_heuristic, |
| 352 | + is_closed=False, |
| 353 | + is_open=True) |
| 354 | + |
| 355 | + child_hash = self.__hash(child) |
| 356 | + |
| 357 | + if child_hash in nodes: |
| 358 | + if nodes[child_hash].is_closed: |
| 359 | + continue |
| 360 | + |
| 361 | + if not nodes[child_hash].is_open: |
| 362 | + nodes[child_hash] = child |
| 363 | + open_nodes_n += 1 |
| 364 | + |
| 365 | + else: |
| 366 | + if child.g_cost < nodes[child_hash].g_cost: |
| 367 | + nodes[child_hash] = child |
| 368 | + open_nodes_n += 1 |
| 369 | + |
| 370 | + else: |
| 371 | + nodes[child_hash] = child |
| 372 | + open_nodes_n += 1 |
| 373 | + |
| 374 | + self.automated_planner.logger.warning("!!! No path found !!!") |
| 375 | + return float('inf') |
0 commit comments