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 ***"
|
"*** YOUR CODE HERE ***"
|
||||||
|
|
||||||
actions = []
|
path = []
|
||||||
|
|
||||||
curr_state = problem.getStartState()
|
curr_state = problem.getStartState() # (5, 5)
|
||||||
isGoal = problem.isGoalState(curr_state)
|
isGoal = problem.isGoalState(curr_state)
|
||||||
|
|
||||||
visited = list([curr_state])
|
visited = list()
|
||||||
stack = util.Stack()
|
stack = util.Stack()
|
||||||
|
stack.push(((curr_state, 'None', 0), path)) # (state, path)
|
||||||
|
|
||||||
while not isGoal:
|
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():
|
if stack.isEmpty():
|
||||||
return []
|
return []
|
||||||
else:
|
else:
|
||||||
obj = stack.pop()
|
((new_state, direction, cost), path) = stack.pop() # ((location, direction, cost), path)
|
||||||
(curr_state, direction, cost) = obj['state']
|
if new_state in visited:
|
||||||
visited.append(curr_state)
|
continue
|
||||||
actions = obj['actions'] + [ direction ]
|
print(new_state, path)
|
||||||
isGoal = problem.isGoalState(curr_state)
|
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):
|
def breadthFirstSearch(problem: SearchProblem):
|
||||||
"""Search the shallowest nodes in the search tree first."""
|
"""Search the shallowest nodes in the search tree first."""
|
||||||
"*** YOUR CODE HERE ***"
|
"*** YOUR CODE HERE ***"
|
||||||
actions = []
|
path = []
|
||||||
|
|
||||||
curr_state = problem.getStartState()
|
curr_state = problem.getStartState() # (5, 5)
|
||||||
isGoal = problem.isGoalState(curr_state)
|
isGoal = problem.isGoalState(curr_state)
|
||||||
|
|
||||||
visited = list([curr_state])
|
visited = list()
|
||||||
queue = util.Queue()
|
queue = util.Queue()
|
||||||
|
queue.push(((curr_state, 'None', 0), path)) # (state, path)
|
||||||
|
|
||||||
while not isGoal:
|
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():
|
if queue.isEmpty():
|
||||||
return []
|
return []
|
||||||
else:
|
else:
|
||||||
obj = queue.pop()
|
((new_state, direction, cost), path) = queue.pop() # ((location, direction, cost), path)
|
||||||
(curr_state, direction, cost) = obj['state']
|
if new_state in visited:
|
||||||
actions = obj['actions'] + [ direction ]
|
continue
|
||||||
isGoal = problem.isGoalState(curr_state)
|
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):
|
def uniformCostSearch(problem: SearchProblem):
|
||||||
"""Search the node of least total cost first."""
|
"""Search the node of least total cost first."""
|
||||||
"*** YOUR CODE HERE ***"
|
"*** YOUR CODE HERE ***"
|
||||||
actions = []
|
path = []
|
||||||
|
|
||||||
curr_state = problem.getStartState()
|
|
||||||
isGoal = problem.isGoalState(curr_state)
|
|
||||||
costs = 0
|
costs = 0
|
||||||
|
|
||||||
visited = list([curr_state])
|
curr_state = problem.getStartState() # (5, 5)
|
||||||
|
isGoal = problem.isGoalState(curr_state)
|
||||||
|
|
||||||
|
visited = list()
|
||||||
queue = util.PriorityQueue()
|
queue = util.PriorityQueue()
|
||||||
|
queue.push(((curr_state, 'None', 0), path, costs), 0) # (state, path)
|
||||||
|
|
||||||
while not isGoal:
|
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():
|
if queue.isEmpty():
|
||||||
return []
|
return []
|
||||||
else:
|
else:
|
||||||
obj = queue.pop()
|
((new_state, direction, cost), path, costs) = queue.pop() # ((location, direction, cost), path)
|
||||||
(curr_state, direction, cost) = obj['state']
|
if new_state in visited:
|
||||||
visited.append(curr_state)
|
continue
|
||||||
actions = obj['actions'] + [ direction ]
|
print(new_state, path)
|
||||||
costs = obj['costs'] + cost
|
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 path
|
||||||
|
|
||||||
return actions
|
|
||||||
|
|
||||||
|
|
||||||
def nullHeuristic(state, problem=None):
|
def nullHeuristic(state, problem=None):
|
||||||
@ -197,41 +185,40 @@ def nullHeuristic(state, problem=None):
|
|||||||
def aStarSearch(problem: SearchProblem, heuristic=nullHeuristic):
|
def aStarSearch(problem: SearchProblem, heuristic=nullHeuristic):
|
||||||
"""Search the node that has the lowest combined cost and heuristic first."""
|
"""Search the node that has the lowest combined cost and heuristic first."""
|
||||||
"*** YOUR CODE HERE ***"
|
"*** YOUR CODE HERE ***"
|
||||||
actions = []
|
path = []
|
||||||
|
|
||||||
curr_state = problem.getStartState()
|
|
||||||
isGoal = problem.isGoalState(curr_state)
|
|
||||||
costs = 0
|
costs = 0
|
||||||
|
|
||||||
visited = list([curr_state])
|
curr_state = problem.getStartState() # (5, 5)
|
||||||
|
isGoal = problem.isGoalState(curr_state)
|
||||||
|
|
||||||
|
visited = list()
|
||||||
queue = util.PriorityQueue()
|
queue = util.PriorityQueue()
|
||||||
|
queue.push(((curr_state, 'None', 0), path, costs), 0) # (state, path)
|
||||||
|
|
||||||
|
min_cost = {curr_state: 0}
|
||||||
|
|
||||||
while not isGoal:
|
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():
|
if queue.isEmpty():
|
||||||
return []
|
return []
|
||||||
else:
|
else:
|
||||||
obj = queue.pop()
|
((new_state, direction, cost), path, costs) = queue.pop() # ((location, direction, cost), path)
|
||||||
(curr_state, direction, cost) = obj['state']
|
if new_state in visited and (costs+cost)>=min_cost[new_state]:
|
||||||
visited.append(curr_state)
|
continue
|
||||||
actions = obj['actions'] + [ direction ]
|
print(new_state, path)
|
||||||
costs = obj['costs'] + cost
|
path = path + [direction]
|
||||||
|
costs = costs + cost
|
||||||
isGoal = problem.isGoalState(curr_state)
|
min_cost[new_state] = costs
|
||||||
|
visited.append(new_state)
|
||||||
return actions
|
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
|
# Abbreviations
|
||||||
|
|||||||
@ -365,9 +365,12 @@ 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 ***"
|
||||||
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):
|
class AStarCornersAgent(SearchAgent):
|
||||||
"A SearchAgent for FoodSearchProblem using A* and your foodHeuristic"
|
"A SearchAgent for FoodSearchProblem using A* and your foodHeuristic"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user