Skip to content

Commit 3e5f6da

Browse files
committed
done with Q3, uniformCostSearch added
1 parent 2e5431c commit 3e5f6da

File tree

2 files changed

+57
-22
lines changed

2 files changed

+57
-22
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,5 @@ venv.bak/
102102

103103
# mypy
104104
.mypy_cache/
105+
.vscode/launch.json
106+
.vscode/settings.json

search.py

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@
1515

1616

1717

18+
1819
class SearchProblem:
1920
"""
2021
This class outlines the structure of a search problem, but doesn't implement
2122
any of the methods (in object-oriented terminology: an abstract class).
2223
2324
You do not need to change anything in this class, ever.
2425
"""
25-
26+
27+
2628
def getStartState(self):
2729
"""
2830
Returns the start state for the search problem
@@ -70,29 +72,32 @@ def tinyMazeSearch(problem):
7072
return [s,s,w,s,w,w,s,w]
7173

7274
def depthFirstSearch(problem):
73-
return bdFirstSearch(problem,BDFS(util.Stack()))
75+
return bdFirstSearch(problem,BDFS(dfs))
7476

7577

7678
def breadthFirstSearch(problem):
77-
return bdFirstSearch(problem,BDFS(util.Queue()))
79+
return bdFirstSearch(problem,BDFS(bfs))
7880

7981

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+
9196

9297
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+
96101

97102
def nullHeuristic(state, problem=None):
98103
"""
@@ -106,6 +111,18 @@ def aStarSearch(problem, heuristic=nullHeuristic):
106111
"*** YOUR CODE HERE ***"
107112
util.raiseNotDefined()
108113

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)
109126

110127
# Abbreviations
111128
bfs = breadthFirstSearch
@@ -139,27 +156,43 @@ def getActionsFromStart(self):
139156
actionList.append(currNode.getAction())
140157
currNode = currNode.parent
141158
actionList.reverse()
159+
print len(actionList)
142160
return actionList
143161

144162

145-
146163
class BDFS:
147164
'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+
150175
ds=None
176+
dsName=None
151177
Visited = set()
152178

153179
def NotEmpty(self):
154180
return not self.ds.isEmpty()
155181

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+
158188
self.Visited.add(item.getState())
159189

160190
def NotVisited(self,item):
161191
return not item in self.Visited
162192

193+
def GetSearchType(self):
194+
return self.dsName
195+
163196
def Pop(self):
164197
return self.ds.pop()
165198

0 commit comments

Comments
 (0)