diff options
author | Toby Vincent <tobyv13@gmail.com> | 2021-09-12 22:39:52 -0500 |
---|---|---|
committer | Toby Vincent <tobyv13@gmail.com> | 2021-09-12 22:39:52 -0500 |
commit | 52c54b8a02e1f9dba50b5c51442b234ddfae9180 (patch) | |
tree | dfc5ccdcda54dd94f2912f09f04bedf1006a2294 | |
parent | 7413f09234fcf58d7b185baea6aae11668a0a8fb (diff) |
added wall bisection penalty when calculating distance
-rw-r--r-- | .vscode/launch.json | 3 | ||||
-rw-r--r-- | searchAgents.py | 46 |
2 files changed, 36 insertions, 13 deletions
diff --git a/.vscode/launch.json b/.vscode/launch.json index 80848a3..9396892 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,6 +9,9 @@ "type": "python", "request": "launch", "program": "autograder.py", + "args": [ + "--question=q7", + ] }, { "name": "tinyMazeSearch", diff --git a/searchAgents.py b/searchAgents.py index fa2fabe..126f171 100644 --- a/searchAgents.py +++ b/searchAgents.py @@ -513,20 +513,40 @@ def foodHeuristic(state, problem): # adds weight for walls around the positions being looked at # -54 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 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 + def wallPenalty(wallArray, lowerBound, upperBound): + wallsBetween = wallArray[lowerBound:upperBound] + if all(wallsBetween): + penalty = 2 + lowerWall = lowerBound + 1 + upperWall = upperBound + 1 + while lowerWall >= 0 and upperWall < len(wallArray) and wallArray[lowerWall] and wallArray[upperWall]: + penalty += 1 + lowerWall += 1 + upperWall += 1 + return penalty + return 0 + + xLowerBound = min((pos2[0], pos1[0])) + xUpperBound = max((pos2[0], pos1[0])) + + yLowerBound = min((pos2[1], pos1[1])) + yUpperBound = max((pos2[1], pos1[1])) + + if yLowerBound != yUpperBound: + for x in range(xLowerBound, xUpperBound): + penalty = wallPenalty(wallGrid[x], yLowerBound, yUpperBound) + if penalty > 0: + return distance + penalty + + if xLowerBound != xUpperBound: + for y in range(yLowerBound, yUpperBound): + penalty = wallPenalty([axis[y] for axis in wallGrid], xLowerBound, xUpperBound) + if penalty > 0: + return distance + penalty + + + if pos2[0] == pos1[0] or pos2[1] == pos1[1]: return distance # normalizes the x and y offsets to either 1 or -1 |