Skip to content

Commit 8306fd0

Browse files
committed
CornersHeuristic is added (done with Q5) and a small bug fix at search.py (init total cost with cost instead of 0)
1 parent 8c1a5ec commit 8306fd0

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

search.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def __init__(self, state, parent, action,cost):
137137
if self.parent!=None:
138138
self.totalCost = self.cost + self.parent.totalCost
139139
else:
140-
self.totalCost=0
140+
self.totalCost=cost
141141

142142
def __str__(self):
143143
return "State: " + str(self.state) + "\n" + \

searchAgents.py

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,36 @@ def getCostOfActions(self, actions):
372372
return len(actions)
373373

374374

375+
376+
def numOfWallsBlockOnTheWay(walls,current,goal):
377+
#since the walls are calculated as vartical and horizontal blocks
378+
#the minimum number of step to pass a block and keep going vartical/horizontal will be 2
379+
MIN_NUMBER_OF_STEPS_TO_PASS_BLOCK=2
380+
verticalCounter=horizontalCounter=0
381+
currentX,currentY=current
382+
goalX,goalY=goal
383+
blockStarted=False
384+
while goalX!=currentX:
385+
currentX+=1 if goalX>currentX else -1
386+
if walls.data[currentX][current[1]]:
387+
if not blockStarted:
388+
horizontalCounter+=1
389+
blockStarted=True
390+
else:
391+
blockStarted=False
392+
blockStarted=False
393+
while goalY!=currentY:
394+
currentY+=1 if goalY>currentY else -1
395+
if walls.data[current[0]][currentY]:
396+
if not blockStarted:
397+
verticalCounter+=1
398+
blockStarted=True
399+
else:
400+
blockStarted=False
401+
return (verticalCounter*MIN_NUMBER_OF_STEPS_TO_PASS_BLOCK),(horizontalCounter*MIN_NUMBER_OF_STEPS_TO_PASS_BLOCK)
402+
403+
404+
375405
def cornersHeuristic(state, problem):
376406
"""
377407
A heuristic for the CornersProblem that you defined.
@@ -386,11 +416,21 @@ def cornersHeuristic(state, problem):
386416
it should be admissible. (You need not worry about consistency for
387417
this heuristic to receive full credit.)
388418
"""
389-
corners = problem.corners # These are the corner coordinates
419+
#corners = problem.corners # These are the corner coordinates
390420
walls = problem.walls # These are the walls of the maze, as a Grid (game.py)
391-
392-
"*** YOUR CODE HERE ***"
393-
return 0 # Default to trivial solution
421+
sortedGoals=[]
422+
goals = state.getRemainingGoals()
423+
currentPosition = state.getPosition()
424+
for goal in goals:
425+
sortedGoals.append({util.manhattanDistance(currentPosition,goal):goal})
426+
sortedGoals.sort()
427+
closestGoal=sortedGoals.pop().values()[0]
428+
v,h=numOfWallsBlockOnTheWay(walls,currentPosition,closestGoal)
429+
if len(sortedGoals)==0:
430+
return v+h
431+
return util.manhattanDistance(currentPosition,closestGoal)+v+h
432+
433+
# // return 0 # Default to trivial solution
394434

395435
class AStarCornersAgent(SearchAgent):
396436
"A SearchAgent for FoodSearchProblem using A* and your foodHeuristic"

0 commit comments

Comments
 (0)