feat: complete cornerProblem setup

This commit is contained in:
snsd0805 2024-03-22 03:49:37 +08:00
parent c80783c695
commit 8fa4eccf04
Signed by: snsd0805
GPG Key ID: 569349933C77A854
2 changed files with 32 additions and 12 deletions

View File

@ -4,7 +4,6 @@ Pacman agents (in searchAgents.py).
Please only change the parts of the file you are asked to. Look for the lines
that say
"*** YOUR CODE HERE ***"
Follow the project description for details.
@ -88,7 +87,7 @@ def depthFirstSearch(problem: SearchProblem):
curr_state = problem.getStartState()
isGoal = problem.isGoalState(curr_state)
visited = set([curr_state])
visited = list([curr_state])
stack = util.Stack()
while not isGoal:
@ -107,7 +106,7 @@ def depthFirstSearch(problem: SearchProblem):
else:
obj = stack.pop()
(curr_state, direction, cost) = obj['state']
visited.add(curr_state)
visited.append(curr_state)
actions = obj['actions'] + [ direction ]
isGoal = problem.isGoalState(curr_state)
@ -123,15 +122,16 @@ def breadthFirstSearch(problem: SearchProblem):
curr_state = problem.getStartState()
isGoal = problem.isGoalState(curr_state)
visited = set([curr_state])
visited = list([curr_state])
queue = util.Queue()
while not isGoal:
print(curr_state)
# push in the Stack
successors = problem.getSuccessors(curr_state)
for (state, direction, cost) in successors:
if state not in visited:
visited.add(state)
visited.append(state)
queue.push({
'state': (state, direction, cost),
'actions': actions
@ -158,7 +158,7 @@ def uniformCostSearch(problem: SearchProblem):
isGoal = problem.isGoalState(curr_state)
costs = 0
visited = set([curr_state])
visited = list([curr_state])
queue = util.PriorityQueue()
while not isGoal:
@ -178,7 +178,7 @@ def uniformCostSearch(problem: SearchProblem):
else:
obj = queue.pop()
(curr_state, direction, cost) = obj['state']
visited.add(curr_state)
visited.append(curr_state)
actions = obj['actions'] + [ direction ]
costs = obj['costs'] + cost
@ -203,7 +203,7 @@ def aStarSearch(problem: SearchProblem, heuristic=nullHeuristic):
isGoal = problem.isGoalState(curr_state)
costs = 0
visited = set([curr_state])
visited = list([curr_state])
queue = util.PriorityQueue()
while not isGoal:
@ -223,7 +223,7 @@ def aStarSearch(problem: SearchProblem, heuristic=nullHeuristic):
else:
obj = queue.pop()
(curr_state, direction, cost) = obj['state']
visited.add(curr_state)
visited.append(curr_state)
actions = obj['actions'] + [ direction ]
costs = obj['costs'] + cost

View File

@ -281,14 +281,17 @@ class CornersProblem(search.SearchProblem):
space)
"""
"*** YOUR CODE HERE ***"
util.raiseNotDefined()
return {
'position': self.startingPosition,
'target_corners': self.corners,
}
def isGoalState(self, state: Any):
"""
Returns whether this search state is a goal state of the problem.
"""
"*** YOUR CODE HERE ***"
util.raiseNotDefined()
return len(state['target_corners']) == 0
def getSuccessors(self, state: Any):
"""
@ -311,7 +314,24 @@ class CornersProblem(search.SearchProblem):
# hitsWall = self.walls[nextx][nexty]
"*** YOUR CODE HERE ***"
x, y = state['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
else:
new_corners = state['target_corners']
new_state = {
'position': (nextx, nexty),
'target_corners': new_corners,
}
successors.append((new_state, action, 1))
self._expanded += 1 # DO NOT CHANGE
return successors
@ -403,4 +423,4 @@ class FoodSearchProblem:
if self.walls[x][y]:
return 999999
cost += 1
return cost
return cost