forked from thuva4/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBestFirstSearch.py
More file actions
39 lines (38 loc) · 1.23 KB
/
Copy pathBestFirstSearch.py
File metadata and controls
39 lines (38 loc) · 1.23 KB
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
class SearchNode:
def __init__(self, action, state, parent):
self.state = state
self.action = action
self.parent = parent
def path(self):
if self.parent == None:
return [(self.action, self.state)]
else:
return self.parent.path() + [(self.action, self.state)]
def inPath(self, s):
if s == self.state:
return True
elif self.parent == None:
return False
else:
return self.parent.inPath(s)
def breadthFirstSearch(initialState, goalTest, actions, successor):
agenda = Queue()
if goalTest(initialState):
return [(None, initialState)]
agenda.push(SearchNode(None, initialState, None))
while not agenda.isEmpty():
parent = agenda.pop()
newChildStates = []
for a in actions(parent.state):
newS = successor(parent.state, a)
newN = SearchNode(a, newS, parent)
if goalTest(newS):
return newN.path()
elif newS in newChildStates:
pass
elif parent.inPath(newS):
pass
else:
newChildStates.append(newS)
agenda.push(newN)
return None