-
Notifications
You must be signed in to change notification settings - Fork 18
/
searches.py
118 lines (108 loc) · 3.61 KB
/
searches.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
'''
Code written by Ricardo Soares
------------------------------
University of Aberdeen
MSc Artificial Intelligence
Written in Python 2.7.14
'''
import puzzle
from Queue import Queue
from Queue import LifoQueue
from Queue import PriorityQueue
import node
import sys
class Search:
'''
Instantiates the class, defining the start node
'''
def __init__(self, puzzle):
self.start = node.Node(puzzle)
'''
Best First Search Algorithm - Based in the pseudo code
in "Artificial Intelligence: A Modern Approach - 3rd Edition"
'''
def bestFirst(self):
closed = list()
leaves = Queue()
leaves.put(self.start)
while True:
if leaves.empty():
return None
actual = leaves.get()
if actual.goalState():
return actual
elif actual.state.puzzle not in closed:
closed.append(actual.state.puzzle)
succ = actual.succ()
while not succ.empty():
leaves.put(succ.get())
'''
Iterative Deepening Search Algorithm - Based in the pseudo code
in "Artificial Intelligence: A Modern Approach - 3rd Edition"
'''
def iterativeDeepening(self):
depth = 0
result = None
while result == None:
result = self.depthLimited(depth)
depth +=1
return result
'''
Depth Limited Search Algorithm - Based in the pseudo code
in "Artificial Intelligence: A Modern Approach - 3rd Edition"
'''
def depthLimited(self, depth):
leaves = LifoQueue()
leaves.put(self.start)
while True:
if leaves.empty():
return None
actual = leaves.get()
if actual.goalState():
return actual
elif actual.depth is not depth:
succ = actual.succ()
while not succ.empty():
leaves.put(succ.get())
'''
Greedy Search Algorithm - Based in the pseudo code
in "Artificial Intelligence: A Modern Approach - 3rd Edition"
'''
def greedy(self, heuristic):
actual = self.start
leaves = PriorityQueue()
leaves.put((actual.costHeur(heuristic), actual))
closed = list()
while True:
if leaves.empty():
return None
actual = leaves.get()[1]
if actual.goalState():
return actual
elif actual.state.puzzle not in closed:
closed.append(actual.state.puzzle)
succ = actual.succ()
while not succ.empty():
child = succ.get()
leaves.put((child.costHeur(heuristic), child))
'''
A* Search Algorithm - Based in the pseudo code
in "Artificial Intelligence: A Modern Approach - 3rd Edition"
'''
def aSearch(self, heuristic):
actual = self.start
leaves = PriorityQueue()
leaves.put((actual.costHeur(heuristic), actual))
closed = list()
while True:
if leaves.empty():
return None
actual = leaves.get()[1]
if actual.goalState():
return actual
elif actual.state.puzzle not in closed:
closed.append(actual.state.puzzle)
succ = actual.succ()
while not succ.empty():
child = succ.get()
leaves.put((child.costHeur(heuristic)+child.depth, child))