@@ -372,6 +372,36 @@ def getCostOfActions(self, actions):
372
372
return len (actions )
373
373
374
374
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
+
375
405
def cornersHeuristic (state , problem ):
376
406
"""
377
407
A heuristic for the CornersProblem that you defined.
@@ -386,11 +416,21 @@ def cornersHeuristic(state, problem):
386
416
it should be admissible. (You need not worry about consistency for
387
417
this heuristic to receive full credit.)
388
418
"""
389
- corners = problem .corners # These are the corner coordinates
419
+ # corners = problem.corners # These are the corner coordinates
390
420
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
394
434
395
435
class AStarCornersAgent (SearchAgent ):
396
436
"A SearchAgent for FoodSearchProblem using A* and your foodHeuristic"
0 commit comments