refactor: fix some problem. Complete p. 5
This commit is contained in:
parent
8fa4eccf04
commit
fcfc24956b
165
search.py
165
search.py
@ -82,109 +82,97 @@ def depthFirstSearch(problem: SearchProblem):
|
||||
"""
|
||||
"*** YOUR CODE HERE ***"
|
||||
|
||||
actions = []
|
||||
path = []
|
||||
|
||||
curr_state = problem.getStartState()
|
||||
curr_state = problem.getStartState() # (5, 5)
|
||||
isGoal = problem.isGoalState(curr_state)
|
||||
|
||||
visited = list([curr_state])
|
||||
visited = list()
|
||||
stack = util.Stack()
|
||||
stack.push(((curr_state, 'None', 0), path)) # (state, path)
|
||||
|
||||
while not isGoal:
|
||||
# push in the Stack
|
||||
successors = problem.getSuccessors(curr_state)
|
||||
for (state, direction, cost) in successors:
|
||||
if state not in visited:
|
||||
stack.push({
|
||||
'state': (state, direction, cost),
|
||||
'actions': actions
|
||||
})
|
||||
|
||||
# check whether reachable
|
||||
if stack.isEmpty():
|
||||
return []
|
||||
else:
|
||||
obj = stack.pop()
|
||||
(curr_state, direction, cost) = obj['state']
|
||||
visited.append(curr_state)
|
||||
actions = obj['actions'] + [ direction ]
|
||||
isGoal = problem.isGoalState(curr_state)
|
||||
((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 actions
|
||||
return path
|
||||
|
||||
|
||||
|
||||
def breadthFirstSearch(problem: SearchProblem):
|
||||
"""Search the shallowest nodes in the search tree first."""
|
||||
"*** YOUR CODE HERE ***"
|
||||
actions = []
|
||||
path = []
|
||||
|
||||
curr_state = problem.getStartState()
|
||||
curr_state = problem.getStartState() # (5, 5)
|
||||
isGoal = problem.isGoalState(curr_state)
|
||||
|
||||
visited = list([curr_state])
|
||||
visited = list()
|
||||
queue = util.Queue()
|
||||
queue.push(((curr_state, 'None', 0), path)) # (state, path)
|
||||
|
||||
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.append(state)
|
||||
queue.push({
|
||||
'state': (state, direction, cost),
|
||||
'actions': actions
|
||||
})
|
||||
|
||||
# check whether reachable
|
||||
if queue.isEmpty():
|
||||
return []
|
||||
else:
|
||||
obj = queue.pop()
|
||||
(curr_state, direction, cost) = obj['state']
|
||||
actions = obj['actions'] + [ direction ]
|
||||
isGoal = problem.isGoalState(curr_state)
|
||||
((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 actions
|
||||
return path
|
||||
|
||||
|
||||
def uniformCostSearch(problem: SearchProblem):
|
||||
"""Search the node of least total cost first."""
|
||||
"*** YOUR CODE HERE ***"
|
||||
actions = []
|
||||
|
||||
curr_state = problem.getStartState()
|
||||
isGoal = problem.isGoalState(curr_state)
|
||||
path = []
|
||||
costs = 0
|
||||
|
||||
visited = list([curr_state])
|
||||
curr_state = problem.getStartState() # (5, 5)
|
||||
isGoal = problem.isGoalState(curr_state)
|
||||
|
||||
visited = list()
|
||||
queue = util.PriorityQueue()
|
||||
queue.push(((curr_state, 'None', 0), path, costs), 0) # (state, path)
|
||||
|
||||
while not isGoal:
|
||||
# push in the P Queue
|
||||
successors = problem.getSuccessors(curr_state)
|
||||
for (state, direction, cost) in successors:
|
||||
if state not in visited:
|
||||
queue.push({
|
||||
'state': (state, direction, cost),
|
||||
'actions': actions,
|
||||
'costs': costs
|
||||
}, costs + cost) # if visited this successor node, how much cost will the agent need
|
||||
|
||||
# check whether reachable
|
||||
if queue.isEmpty():
|
||||
return []
|
||||
else:
|
||||
obj = queue.pop()
|
||||
(curr_state, direction, cost) = obj['state']
|
||||
visited.append(curr_state)
|
||||
actions = obj['actions'] + [ direction ]
|
||||
costs = obj['costs'] + cost
|
||||
((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:]
|
||||
|
||||
isGoal = problem.isGoalState(curr_state)
|
||||
|
||||
return actions
|
||||
return path
|
||||
|
||||
|
||||
def nullHeuristic(state, problem=None):
|
||||
@ -197,41 +185,40 @@ def nullHeuristic(state, problem=None):
|
||||
def aStarSearch(problem: SearchProblem, heuristic=nullHeuristic):
|
||||
"""Search the node that has the lowest combined cost and heuristic first."""
|
||||
"*** YOUR CODE HERE ***"
|
||||
actions = []
|
||||
|
||||
curr_state = problem.getStartState()
|
||||
isGoal = problem.isGoalState(curr_state)
|
||||
path = []
|
||||
costs = 0
|
||||
|
||||
visited = list([curr_state])
|
||||
curr_state = problem.getStartState() # (5, 5)
|
||||
isGoal = problem.isGoalState(curr_state)
|
||||
|
||||
visited = list()
|
||||
queue = util.PriorityQueue()
|
||||
queue.push(((curr_state, 'None', 0), path, costs), 0) # (state, path)
|
||||
|
||||
min_cost = {curr_state: 0}
|
||||
|
||||
while not isGoal:
|
||||
# push in the P Queue
|
||||
successors = problem.getSuccessors(curr_state)
|
||||
for (state, direction, cost) in successors:
|
||||
if state not in visited:
|
||||
queue.push({
|
||||
'state': (state, direction, cost),
|
||||
'actions': actions,
|
||||
'costs': costs
|
||||
}, costs + cost + heuristic(state, problem)) # if visited this successor node, how much cost will the agent need
|
||||
|
||||
# check whether reachable
|
||||
if queue.isEmpty():
|
||||
return []
|
||||
else:
|
||||
obj = queue.pop()
|
||||
(curr_state, direction, cost) = obj['state']
|
||||
visited.append(curr_state)
|
||||
actions = obj['actions'] + [ direction ]
|
||||
costs = obj['costs'] + cost
|
||||
|
||||
isGoal = problem.isGoalState(curr_state)
|
||||
|
||||
return actions
|
||||
|
||||
((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
|
||||
visited.append(new_state)
|
||||
isGoal = problem.isGoalState(new_state)
|
||||
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)
|
||||
path = path[1:]
|
||||
|
||||
return path
|
||||
|
||||
|
||||
# Abbreviations
|
||||
|
||||
@ -365,9 +365,12 @@ 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 ***"
|
||||
return 0 # Default to trivial solution
|
||||
x, y = state['position']
|
||||
distances = [ util.manhattanDistance((x, y), corner) for corner in corners ]
|
||||
return min(distances)
|
||||
|
||||
class AStarCornersAgent(SearchAgent):
|
||||
"A SearchAgent for FoodSearchProblem using A* and your foodHeuristic"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user