summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.vscode/launch.json30
-rw-r--r--searchAgents.py50
2 files changed, 74 insertions, 6 deletions
diff --git a/.vscode/launch.json b/.vscode/launch.json
index 4788109..af599a5 100644
--- a/.vscode/launch.json
+++ b/.vscode/launch.json
@@ -79,7 +79,7 @@
"--frameTime=0.02",
"--pacman=SearchAgent",
"--agentArgs=fn=uniformCostSearch,prob=CornersProblem",
- "--layout=${input:corners_mazeLayout}",
+ "--layout=${input:cornersLayout}",
]
},
{
@@ -90,7 +90,19 @@
"args": [
"--frameTime=0.02",
"--pacman=AStarCornersAgent",
- "--layout=${input:corners_mazeLayout}",
+ "--layout=${input:cornersLayout}",
+ ]
+ },
+ {
+ "name": "USCFoodSearchAgent",
+ "type": "python",
+ "request": "launch",
+ "program": "pacman.py",
+ "args": [
+ "--frameTime=0.02",
+ "--pacman=SearchAgent",
+ "--agentArgs=fn=uniformCostSearch,prob=FoodSearchProblem",
+ "--layout=${input:foodLayout}",
]
},
{
@@ -101,7 +113,7 @@
"args": [
"--frameTime=0.02",
"--pacman=AStarFoodSearchAgent",
- "--layout=${input:corners_mazeLayout}",
+ "--layout=${input:foodLayout}",
]
}
],
@@ -129,7 +141,7 @@
"default": "nullHeuristic"
},
{
- "id": "corners_mazeLayout",
+ "id": "cornersLayout",
"type": "pickString",
"description": "Select the maze layout",
"options": [
@@ -138,6 +150,16 @@
"bigCorners"
],
"default": "tinyCorners"
+ },
+ {
+ "id": "foodLayout",
+ "type": "pickString",
+ "description": "Select the maze layout",
+ "options": [
+ "testSearch",
+ "trickySearch"
+ ],
+ "default": "tinyCorners"
}
]
} \ No newline at end of file
diff --git a/searchAgents.py b/searchAgents.py
index 9a6f0ab..9305c14 100644
--- a/searchAgents.py
+++ b/searchAgents.py
@@ -482,8 +482,54 @@ def foodHeuristic(state, problem):
problem.heuristicInfo['wallCount']
"""
position, foodGrid = state
- "*** YOUR CODE HERE ***"
- return 0
+ wallGrid = problem.walls
+
+ def getDistance(food):
+ distance = abs(food[0] - position[0]) + abs(food[1] - position[1])
+
+ if position[0] == food[0] or position[1] == food[1]:
+ 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]:
+ distance += 2
+
+ if wallGrid[food[0] + (xOffset * -1) ][food[1]] and wallGrid[food[0]][food[1] + (yOffset * -1)]:
+ distance += 2
+
+ return distance
+
+ foodList = foodGrid.asList()
+ foodLeft = len(foodList)
+
+ if foodLeft == 0:
+ return 0
+
+ foodDist = map(getDistance, foodList)
+
+ maxFood = max(foodDist)
+ minFood = min(foodDist)
+
+ return max((maxFood, minFood + foodLeft - 1))
+
class ClosestDotSearchAgent(SearchAgent):
"Search for all food using a sequence of searches"