15
15
16
16
17
17
18
+
18
19
class SearchProblem :
19
20
"""
20
21
This class outlines the structure of a search problem, but doesn't implement
21
22
any of the methods (in object-oriented terminology: an abstract class).
22
23
23
24
You do not need to change anything in this class, ever.
24
25
"""
25
-
26
+
27
+
26
28
def getStartState (self ):
27
29
"""
28
30
Returns the start state for the search problem
@@ -70,29 +72,32 @@ def tinyMazeSearch(problem):
70
72
return [s ,s ,w ,s ,w ,w ,s ,w ]
71
73
72
74
def depthFirstSearch (problem ):
73
- return bdFirstSearch (problem ,BDFS (util . Stack () ))
75
+ return bdFirstSearch (problem ,BDFS (dfs ))
74
76
75
77
76
78
def breadthFirstSearch (problem ):
77
- return bdFirstSearch (problem ,BDFS (util . Queue () ))
79
+ return bdFirstSearch (problem ,BDFS (bfs ))
78
80
79
81
80
- def bdFirstSearch (problem ,searchAlg ):
81
- searchAlg .PushAndMarkExplored (Node (problem .getStartState (),None ,None ))
82
- while (searchAlg .NotEmpty ()):
83
- current = searchAlg .Pop ()
84
- if problem .isGoalState (current .getState ()):
85
- return current .getActionsFromStart ()
86
- else :
87
- successors = problem .getSuccessors (current .getState ())
88
- for item in successors :
89
- if searchAlg .NotVisited (item [0 ]):
90
- searchAlg .PushAndMarkExplored (Node (item [0 ],current ,item [1 ]))
82
+
83
+
84
+
85
+
86
+ DISTANCE_FROM_GOAL = "distanceFromGoal"
87
+
88
+ def calculateH (problem ,state ,method ):
89
+ if (method == DISTANCE_FROM_GOAL ):
90
+ goalX = problem .goal [0 ]
91
+ goalY = problem .goal [1 ]
92
+ currX = state .state [0 ]
93
+ currY = state .state [1 ]
94
+ return abs (goalX - currX )+ abs (goalY - currY )
95
+
91
96
92
97
def uniformCostSearch (problem ):
93
- "Search the node of least total cost first. "
94
- "*** YOUR CODE HERE ***"
95
- util . raiseNotDefined ()
98
+ return bdFirstSearch ( problem , BDFS ( ucs ))
99
+
100
+
96
101
97
102
def nullHeuristic (state , problem = None ):
98
103
"""
@@ -106,6 +111,18 @@ def aStarSearch(problem, heuristic=nullHeuristic):
106
111
"*** YOUR CODE HERE ***"
107
112
util .raiseNotDefined ()
108
113
114
+ def bdFirstSearch (problem ,searchAlg ):
115
+ searchAlg .PushAndMarkExplored (Node (problem .getStartState (),None ,None ),None )
116
+ while (searchAlg .NotEmpty ()):
117
+ current = searchAlg .Pop ()
118
+ if problem .isGoalState (current .getState ()):
119
+ return current .getActionsFromStart ()
120
+ else :
121
+ successors = problem .getSuccessors (current .getState ())
122
+ for item in successors :
123
+ position , direction , stepPrice = item
124
+ if searchAlg .NotVisited (position ):
125
+ searchAlg .PushAndMarkExplored (Node (position ,current ,direction ),stepPrice )
109
126
110
127
# Abbreviations
111
128
bfs = breadthFirstSearch
@@ -139,27 +156,43 @@ def getActionsFromStart(self):
139
156
actionList .append (currNode .getAction ())
140
157
currNode = currNode .parent
141
158
actionList .reverse ()
159
+ print len (actionList )
142
160
return actionList
143
161
144
162
145
-
146
163
class BDFS :
147
164
'BDFS containg the entire data structure needed to perform best/deep first search'
148
- def __init__ (self ,dataStructure ):
149
- self .ds = dataStructure
165
+ def __init__ (self ,dsName ):
166
+ self .dsName = dsName
167
+ if (self .dsName == dfs ):
168
+ self .ds = util .Stack ()
169
+ elif (self .dsName == bfs ):
170
+ self .ds = util .Queue ()
171
+ elif (self .dsName == ucs ):
172
+ self .ds = util .PriorityQueue ()
173
+
174
+
150
175
ds = None
176
+ dsName = None
151
177
Visited = set ()
152
178
153
179
def NotEmpty (self ):
154
180
return not self .ds .isEmpty ()
155
181
156
- def PushAndMarkExplored (self ,item ):
157
- self .ds .push (item )
182
+ def PushAndMarkExplored (self ,item ,priority ):
183
+ if (self .dsName == dfs or self .dsName == bfs ):
184
+ self .ds .push (item )
185
+ else :
186
+ self .ds .push (item ,priority )
187
+
158
188
self .Visited .add (item .getState ())
159
189
160
190
def NotVisited (self ,item ):
161
191
return not item in self .Visited
162
192
193
+ def GetSearchType (self ):
194
+ return self .dsName
195
+
163
196
def Pop (self ):
164
197
return self .ds .pop ()
165
198
0 commit comments