@@ -409,6 +409,14 @@ def sortGoals(currentPosition,goals):
409
409
sortedGoals .sort ()
410
410
return sortedGoals
411
411
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
412
420
413
421
def cornersHeuristic (state , problem ):
414
422
"""
@@ -428,13 +436,7 @@ def cornersHeuristic(state, problem):
428
436
walls = problem .walls # These are the walls of the maze, as a Grid (game.py)
429
437
goals = state .getRemainingGoals ()
430
438
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 )
438
440
439
441
# // return 0 # Default to trivial solution
440
442
@@ -529,7 +531,12 @@ def foodHeuristic(state, problem):
529
531
"""
530
532
position , foodGrid = state
531
533
"*** 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 )
533
540
534
541
class ClosestDotSearchAgent (SearchAgent ):
535
542
"Search for all food using a sequence of searches"
0 commit comments