-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSearch_Algorithms.py
103 lines (84 loc) · 3.38 KB
/
Search_Algorithms.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
from State import State
from queue import PriorityQueue
from queue import Queue
from queue import LifoQueue
#Breadth-first Search
def BFS(given_state , n):
root = State(given_state, None, None, 0, 0)
if root.test():
return root.solution()
frontier = Queue()
frontier.put(root)
explored = []
while not(frontier.empty()):
current_node = frontier.get()
explored.append(current_node.state)
children = current_node.expand(n)
for child in children:
if child.state not in explored:
if child.test():
return child.solution(), len(explored)
frontier.put(child)
return
#Depth-first Search with limited depth
def DFS(given_state , n):
root = State(given_state, None, None, 0, 0)
if root.test():
return root.solution()
frontier = LifoQueue()
frontier.put(root)
explored = []
while not(frontier.empty()):
current_node = frontier.get()
max_depth = current_node.depth #current depth
explored.append(current_node.state)
if max_depth == 30:
continue #go to the next branch
children = current_node.expand(n)
for child in children:
if child.state not in explored:
if child.test():
return child.solution(), len(explored)
frontier.put(child)
return (("Couldn't find solution in the limited depth."), len(explored))
def Greedy(given_state , n):
frontier = PriorityQueue()
explored = []
counter = 0
root = State(given_state, None, None, 0, 0)
#root.evaluation()
evaluation = root.Manhattan_Distance(n) #we can use Misplaced_Tiles() instead.
frontier.put((evaluation[0], counter, root)) #based on greedy evaluation
while not frontier.empty():
current_node = frontier.get()
current_node = current_node[2]
explored.append(current_node.state)
if current_node.test():
return current_node.solution(), len(explored)
children = current_node.expand(n)
for child in children:
if child.state not in explored:
counter += 1
evaluation = child.Manhattan_Distance(n) #we can use Misplaced_Tiles() instead.
frontier.put((evaluation[0], counter, child)) #based on greedy evaluation
return
def AStar_search(given_state , n):
frontier = PriorityQueue()
explored = []
counter = 0
root = State(given_state, None, None, 0, 0)
evaluation = root.Manhattan_Distance(n) #we can use Misplaced_Tiles() instead.
frontier.put((evaluation[1], counter, root)) #based on A* evaluation
while not frontier.empty():
current_node = frontier.get()
current_node = current_node[2]
explored.append(current_node.state)
if current_node.test():
return current_node.solution(), len(explored)
children = current_node.expand(n)
for child in children:
if child.state not in explored:
counter += 1
evaluation = child.Manhattan_Distance(n) #we can use Misplaced_Tiles() instead.
frontier.put((evaluation[1], counter, child)) #based on A* evaluation
return