Skip to content

Commit b4bdd26

Browse files
committed
done with Q8 used closestGoalHeuristic as both corner & food heuristics
1 parent 21ee05b commit b4bdd26

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

searchAgents.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,14 @@ def sortGoals(currentPosition,goals):
409409
sortedGoals.sort()
410410
return sortedGoals
411411

412+
def closestGoalHeuristic(currentPosition,goals):
413+
result=0
414+
while len(goals)>0:
415+
goals=sortGoals(currentPosition,goals)
416+
temp=goals.pop(0).values()[0]
417+
result+=util.manhattanDistance(currentPosition,temp)
418+
currentPosition=temp
419+
return result
412420

413421
def cornersHeuristic(state, problem):
414422
"""
@@ -428,13 +436,7 @@ def cornersHeuristic(state, problem):
428436
walls = problem.walls # These are the walls of the maze, as a Grid (game.py)
429437
goals = state.getRemainingGoals()
430438
currentPosition = state.getPosition()
431-
result=0
432-
while len(goals)>0:
433-
goals=sortGoals(currentPosition,goals)
434-
temp=goals.pop(0).values()[0]
435-
result+=util.manhattanDistance(currentPosition,temp)
436-
currentPosition=temp
437-
return result
439+
return closestGoalHeuristic(currentPosition,goals)
438440

439441
# // return 0 # Default to trivial solution
440442

@@ -529,7 +531,12 @@ def foodHeuristic(state, problem):
529531
"""
530532
position, foodGrid = state
531533
"*** YOUR CODE HERE ***"
532-
return 0
534+
goals = set()
535+
for i in range(len(foodGrid.data)):
536+
for j in range (len(foodGrid.data[i])):
537+
if foodGrid.data[i][j]:
538+
goals.add((i,j))
539+
return closestGoalHeuristic(position,goals)
533540

534541
class ClosestDotSearchAgent(SearchAgent):
535542
"Search for all food using a sequence of searches"

0 commit comments

Comments
 (0)