summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorToby Vincent <tobyv13@gmail.com>2021-09-08 13:27:12 -0500
committerToby Vincent <tobyv13@gmail.com>2021-09-08 13:27:12 -0500
commit3794a0084e66515858f48a2c7ada989818f0c4eb (patch)
tree7229173d5a6967093ed4f839b03c692ad37419c5
parent0fc7d46cf68ed76a1ade1037f029570f37d97161 (diff)
added upper bound as closest food, then closest food to furthest food
Co-authored-by: Andreas1282 <Andreas1282@users.noreply.github.com>
-rw-r--r--searchAgents.py63
1 files changed, 33 insertions, 30 deletions
diff --git a/searchAgents.py b/searchAgents.py
index 9305c14..489a267 100644
--- a/searchAgents.py
+++ b/searchAgents.py
@@ -481,54 +481,57 @@ def foodHeuristic(state, problem):
Subsequent calls to this heuristic can access
problem.heuristicInfo['wallCount']
"""
- position, foodGrid = state
- wallGrid = problem.walls
-
- def getDistance(food):
- distance = abs(food[0] - position[0]) + abs(food[1] - position[1])
+ def getDistance(pos1, pos2):
+ distance = abs(pos1[0] - pos2[0]) + abs(pos1[1] - pos2[1])
+
+ # ~ -60 nodes expanded (8577/8631)
+ if pos2[0] == pos1[0]:
+ # lowerBound = min((pos2[1], pos1[1]))
+ # upperBound = max((pos2[1], pos1[1]))
+ # wallsBetween = wallGrid[pos2[0]][lowerBound:upperBound]
+ # if any(wallsBetween):
+ # return distance + 2
+ return distance
- if position[0] == food[0] or position[1] == food[1]:
+ if pos2[1] == pos1[1]:
+ # lowerBound = min((pos2[0], pos1[0]))
+ # upperBound = max((pos2[0], pos1[0]))
+ # wallsBetween = map(lambda yVals: yVals[pos2[1]], wallGrid[lowerBound:upperBound])
+ # if any(wallsBetween):
+ # return distance + 2
return distance
- # if position[0] == food[0]:
- # lowerBound = min((position[1], food[1]))
- # upperBound = max((position[1], food[1]))
- # wallsBetween = wallGrid[position[0]][lowerBound:upperBound]
- # if any(wallsBetween):
- # return distance + 2
- # return distance
-
- # if position[1] == food[1]:
- # lowerBound = min((position[0], food[0]))
- # upperBound = max((position[0], food[0]))
- # wallsBetween = map(lambda yVals: yVals[position[1]], wallGrid[lowerBound:upperBound])
- # if any(wallsBetween):
- # return distance + 2
- # return distance
-
- xOffset = (food[0] - position[0]) / abs(food[0] - position[0])
- yOffset = (food[1] - position[1]) / abs(food[1] - position[1])
-
- if wallGrid[position[0] + xOffset][position[1]] and wallGrid[position[0]][position[1] + yOffset]:
+ xOffset = (pos1[0] - pos2[0]) / abs(pos1[0] - pos2[0])
+ yOffset = (pos1[1] - pos2[1]) / abs(pos1[1] - pos2[1])
+
+ if wallGrid[pos2[0] + xOffset][pos2[1]] and wallGrid[pos2[0]][pos2[1] + yOffset]:
distance += 2
- if wallGrid[food[0] + (xOffset * -1) ][food[1]] and wallGrid[food[0]][food[1] + (yOffset * -1)]:
+ if wallGrid[pos1[0] + (xOffset * -1) ][pos1[1]] and wallGrid[pos1[0]][pos1[1] + (yOffset * -1)]:
distance += 2
return distance
+ position, foodGrid = state
+ wallGrid = problem.walls
+
foodList = foodGrid.asList()
foodLeft = len(foodList)
if foodLeft == 0:
return 0
- foodDist = map(getDistance, foodList)
-
+ foodDist = map(lambda food: getDistance(food, position), foodList)
+
maxFood = max(foodDist)
minFood = min(foodDist)
- return max((maxFood, minFood + foodLeft - 1))
+ maxFoodPos = foodList[foodDist.index(maxFood)]
+ minFoodPos = foodList[foodDist.index(minFood)]
+
+ bounds = (minFood + getDistance(maxFoodPos, minFoodPos), minFood + foodLeft - 1)
+
+ return max(bounds)
class ClosestDotSearchAgent(SearchAgent):