feat: complete problem 6

This commit is contained in:
snsd0805 2024-03-26 21:52:08 +08:00
parent fcfc24956b
commit aa3569e664
Signed by: snsd0805
GPG Key ID: 569349933C77A854
2 changed files with 19 additions and 28 deletions

View File

@ -63,7 +63,6 @@ def tinyMazeSearch(problem):
from game import Directions
s = Directions.SOUTH
w = Directions.WEST
print("Solution:", [s, s, w, s, w, w, s, w])
return [s, s, w, s, w, w, s, w]
def depthFirstSearch(problem: SearchProblem):
@ -98,13 +97,11 @@ def depthFirstSearch(problem: SearchProblem):
((new_state, direction, cost), path) = stack.pop() # ((location, direction, cost), path)
if new_state in visited:
continue
print(new_state, path)
path = path + [direction]
visited.append(new_state)
isGoal = problem.isGoalState(new_state)
for (_state, _direction, _cost) in problem.getSuccessors(new_state): # (state, direction, cost)
stack.push(((_state, _direction, _cost), path))
print(path)
path = path[1:]
return path
@ -130,13 +127,11 @@ def breadthFirstSearch(problem: SearchProblem):
((new_state, direction, cost), path) = queue.pop() # ((location, direction, cost), path)
if new_state in visited:
continue
print(new_state, path)
path = path + [direction]
visited.append(new_state)
isGoal = problem.isGoalState(new_state)
for (_state, _direction, _cost) in problem.getSuccessors(new_state): # (state, direction, cost)
queue.push(((_state, _direction, _cost), path))
print(path)
path = path[1:]
return path
@ -162,14 +157,12 @@ def uniformCostSearch(problem: SearchProblem):
((new_state, direction, cost), path, costs) = queue.pop() # ((location, direction, cost), path)
if new_state in visited:
continue
print(new_state, path)
path = path + [direction]
costs = costs + cost
visited.append(new_state)
isGoal = problem.isGoalState(new_state)
for (_state, _direction, _cost) in problem.getSuccessors(new_state): # (state, direction, cost)
queue.push(((_state, _direction, _cost), path, costs), costs+_cost)
print(path)
path = path[1:]
return path
@ -204,7 +197,6 @@ def aStarSearch(problem: SearchProblem, heuristic=nullHeuristic):
((new_state, direction, cost), path, costs) = queue.pop() # ((location, direction, cost), path)
if new_state in visited and (costs+cost)>=min_cost[new_state]:
continue
print(new_state, path)
path = path + [direction]
costs = costs + cost
min_cost[new_state] = costs
@ -213,9 +205,7 @@ def aStarSearch(problem: SearchProblem, heuristic=nullHeuristic):
if isGoal:
break
for (_state, _direction, _cost) in problem.getSuccessors(new_state): # (state, direction, cost)
print(f" {_state} -> {_cost}")
queue.push(((_state, _direction, _cost), path, costs), costs+_cost)
print(path)
queue.push(((_state, _direction, _cost), path, costs), costs+_cost+heuristic(_state, problem))
path = path[1:]
return path

View File

@ -281,17 +281,14 @@ class CornersProblem(search.SearchProblem):
space)
"""
"*** YOUR CODE HERE ***"
return {
'position': self.startingPosition,
'target_corners': self.corners,
}
return (self.startingPosition, self.corners)
def isGoalState(self, state: Any):
"""
Returns whether this search state is a goal state of the problem.
"""
"*** YOUR CODE HERE ***"
return len(state['target_corners']) == 0
return len(state[1]) == 0
def getSuccessors(self, state: Any):
"""
@ -314,22 +311,20 @@ class CornersProblem(search.SearchProblem):
# hitsWall = self.walls[nextx][nexty]
"*** YOUR CODE HERE ***"
x, y = state['position']
position, target_corners = state
x, y = position
dx, dy = Actions.directionToVector(action)
nextx, nexty = int(x + dx), int(y + dy)
hitsWall = self.walls[nextx][nexty]
if not hitsWall:
# new state (position, corners)
if (nextx, nexty) in state['target_corners']:
new_corners = tuple( i for i in state['target_corners'] if i != (nextx, nexty) ) # remove the corner which reached
if (nextx, nexty) in target_corners:
new_corners = tuple( i for i in target_corners if i != (nextx, nexty) ) # remove the corner which reached
else:
new_corners = state['target_corners']
new_corners = target_corners
new_state = {
'position': (nextx, nexty),
'target_corners': new_corners,
}
new_state = ((nextx, nexty), new_corners)
successors.append((new_state, action, 1))
@ -365,12 +360,18 @@ def cornersHeuristic(state: Any, problem: CornersProblem):
"""
corners = problem.corners # These are the corner coordinates
walls = problem.walls # These are the walls of the maze, as a Grid (game.py)
return 0
"*** YOUR CODE HERE ***"
x, y = state['position']
distances = [ util.manhattanDistance((x, y), corner) for corner in corners ]
return min(distances)
position, corners = state
corners = list(corners)
h = 0
while len(corners) > 0:
min_distance, min_corner = min([ (util.manhattanDistance(position, corner), corner) for corner in corners])
position = min_corner
h += min_distance
corners.remove(min_corner)
return h
class AStarCornersAgent(SearchAgent):
"A SearchAgent for FoodSearchProblem using A* and your foodHeuristic"