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

View File

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