-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrbfs.py
52 lines (35 loc) · 1.24 KB
/
rbfs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from __future__ import annotations
from puzzle import Puzzle
from node import Node
from sys import maxsize
count = 1
def rbfs(start: Puzzle, goal: Puzzle) -> (list[str], int):
global count
count = 1
node, _ = rbfs_search(Node(start, None, None, 0), maxsize, goal)
return node.path_from_start(), count
def rbfs_search(node: Node, f_limit: int, goal: Puzzle) \
-> tuple[Node | None, int | None]:
global count
successors: list[tuple[int, Node]] = []
if node.get_puzzle() == goal:
return node, None
children: list[Node] = node.expand()
if not len(children):
return None, maxsize
for child in children:
count = count + 1
successors.append((child.puzzle.h2(), child))
while len(successors):
successors.sort(key=lambda x: x[0])
best_node: Node = successors[0][1]
if best_node.puzzle.h2() > f_limit:
return None, best_node.puzzle.h2()
alternative_value: int = successors[1][0]
result_node, result_value = rbfs_search(
best_node, min(f_limit, alternative_value), goal
)
successors[0] = (result_value, best_node)
if result_node is not None:
break
return result_node, None