File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ class SearchNode :
2+ def __init__ (self , action , state , parent ):
3+ self .state = state
4+ self .action = action
5+ self .parent = parent
6+ def path (self ):
7+ if self .parent == None :
8+ return [(self .action , self .state )]
9+ else :
10+ return self .parent .path () + [(self .action , self .state )]
11+ def inPath (self , s ):
12+ if s == self .state :
13+ return True
14+ elif self .parent == None :
15+ return False
16+ else :
17+ return self .parent .inPath (s )
18+
19+ def breadthFirstSearch (initialState , goalTest , actions , successor ):
20+ agenda = Queue ()
21+ if goalTest (initialState ):
22+ return [(None , initialState )]
23+ agenda .push (SearchNode (None , initialState , None ))
24+ while not agenda .isEmpty ():
25+ parent = agenda .pop ()
26+ newChildStates = []
27+ for a in actions (parent .state ):
28+ newS = successor (parent .state , a )
29+ newN = SearchNode (a , newS , parent )
30+ if goalTest (newS ):
31+ return newN .path ()
32+ elif newS in newChildStates :
33+ pass
34+ elif parent .inPath (newS ):
35+ pass
36+ else :
37+ newChildStates .append (newS )
38+ agenda .push (newN )
39+ return None
You can’t perform that action at this time.
0 commit comments