-
Notifications
You must be signed in to change notification settings - Fork 5
/
search.py
179 lines (158 loc) · 5.2 KB
/
search.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
"""Intelligent search algorithms"""
import array
import dataclasses
import queue
import typing
from tkinter import messagebox
import cube
import definition
import evaluate
@dataclasses.dataclass
class Node:
""""""
__slots__ = "op", "father"
op: definition.OP
father: typing.Self
def __lt__(self, *args, **kw) -> bool:
return True
def repeat(op: definition.OP, node: Node) -> bool:
"""judge whether the op is repeat"""
if node and op[0] == node.op[0] and op[1] != node.op[1]:
return True
return False
def get_trace(node: Node) -> list[definition.OP]:
"""get trace from data"""
trace: list[definition.OP] = []
while node:
trace.append(node.op)
node = node.father
return trace[::-1]
def BFS(
data: array.array[int],
counter: typing.Callable[[], bool],
treelight: typing.Callable[[definition.OP], None] | None
) -> list[definition.OP] | None:
"""Breadth First Search"""
open: queue.Queue[tuple[Node, list[int]]] = queue.Queue()
node: Node = None
while counter():
if data == definition.TARGET:
return get_trace(node)
for op in definition.OPS[::-1]:
if not repeat(op, node):
open.put((Node(op, node), data[:]))
node, data = open.get()
cube.MagicCube.set(data, *node.op)
if treelight:
treelight(get_trace(node))
def DFS(
data: array.array[int],
counter: typing.Callable[[], bool],
treelight: typing.Callable[[definition.OP], None] | None,
*,
depth: int
) -> list[definition.OP] | None:
"""Depth First Search"""
open: queue.LifoQueue[tuple[int, Node, list[int]]] = queue.LifoQueue()
node: Node = None
while counter():
if data == definition.TARGET:
return get_trace(node)
if depth:
for op in definition.OPS:
if not repeat(op, node):
open.put((depth - 1, Node(op, node), data[:]))
depth, node, data = open.get()
cube.MagicCube.set(data, *node.op)
if treelight:
treelight(get_trace(node))
def UCS(
data: array.array[int],
counter: typing.Callable[[], bool],
treelight: typing.Callable[[definition.OP], None] | None,
) -> list[definition.OP] | None:
"""Uniform Cost Search"""
open: queue.PriorityQueue[
tuple[int, Node, list[int]]] = queue.PriorityQueue()
node: Node = None
ev: float = 0
while counter():
if data == definition.TARGET:
return get_trace(node)
for op in definition.OPS:
if not repeat(op, node):
f = evaluate.g(ev, op)
open.put((f, Node(op, node), data[:]))
ev, node, data = open.get()
cube.MagicCube.set(data, *node.op)
if treelight:
treelight(get_trace(node))
def AS(
data: array.array[int],
counter: typing.Callable[[], bool],
treelight: typing.Callable[[definition.OP], None] | None,
) -> list[definition.OP] | None:
"""A or A Star"""
open: queue.PriorityQueue[
tuple[int, Node, list[int]]] = queue.PriorityQueue()
node: Node = None
ev: float = 0
while counter():
if data == definition.TARGET:
return get_trace(node)
for op in definition.OPS:
if not repeat(op, node):
f = evaluate.g(ev, op) + evaluate.h(data)
open.put((f, Node(op, node), data[:]))
ev, node, data = open.get()
cube.MagicCube.set(data, *node.op)
if treelight:
treelight(get_trace(node))
def HC(
data: array.array[int],
counter: typing.Callable[[], bool],
treelight: typing.Callable[[definition.OP], None] | None,
) -> list[definition.OP] | None:
"""Hill Climbing"""
try:
open: queue.PriorityQueue[
tuple[int, Node, list[int]]] = queue.PriorityQueue()
node: Node = None
while counter():
if data == definition.TARGET:
return get_trace(node)
for op in definition.OPS:
if not repeat(op, node):
f = evaluate.h(data)
open.put((f, Node(op, node), data[:]))
_, node, data = open.get()
cube.MagicCube.set(data, *node.op)
if treelight:
treelight(get_trace(node))
except RecursionError as error:
messagebox.showwarning("RecursionError", error)
def US(
data: array.array[int],
counter: typing.Callable[[], bool],
treelight: typing.Callable[[definition.OP], None] | None,
*,
depth: int,
algo: definition.Algos
) -> list[definition.OP] | None:
"""Universal Search"""
open: queue.PriorityQueue[
tuple[int, Node, list[int]]] = queue.PriorityQueue()
node: Node = None
ev: float = 0
while counter():
if data == definition.TARGET:
return get_trace(node)
if depth:
for op in definition.OPS:
if not repeat(op, node):
f = evaluate.f(ev, op, data, depth, algo=algo)
open.put((f, Node(op, node), data[:]))
ev, node, data = open.get()
cube.MagicCube.set(data, *node.op)
if treelight:
treelight(get_trace(node))