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

View File

@ -281,14 +281,17 @@ class CornersProblem(search.SearchProblem):
space) space)
""" """
"*** YOUR CODE HERE ***" "*** YOUR CODE HERE ***"
util.raiseNotDefined() return {
'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 ***"
util.raiseNotDefined() return len(state['target_corners']) == 0
def getSuccessors(self, state: Any): def getSuccessors(self, state: Any):
""" """
@ -311,7 +314,24 @@ class CornersProblem(search.SearchProblem):
# hitsWall = self.walls[nextx][nexty] # hitsWall = self.walls[nextx][nexty]
"*** YOUR CODE HERE ***" "*** 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 self._expanded += 1 # DO NOT CHANGE
return successors return successors