|
|
|
@ -87,69 +87,85 @@ def depthFirstSearch(problem):
|
|
|
|
|
print "Start's successors:", problem.getSuccessors(problem.getStartState())
|
|
|
|
|
"""
|
|
|
|
|
"*** YOUR CODE HERE ***"
|
|
|
|
|
# start state
|
|
|
|
|
startState = problem.getStartState()
|
|
|
|
|
# have exit
|
|
|
|
|
exitState = []
|
|
|
|
|
#use stack to dfs
|
|
|
|
|
States = util.Stack()
|
|
|
|
|
States.push((startState,[]))
|
|
|
|
|
#repeat a circle to confirm if it is successful
|
|
|
|
|
while (not States.isEmpty()) and (not problem.isGoalState(startState)):
|
|
|
|
|
State,Actions = States.pop()
|
|
|
|
|
if State not in exitState:
|
|
|
|
|
exitState.append(State)
|
|
|
|
|
Successor = problem.getSuccessors(State)
|
|
|
|
|
for Node in Successor:
|
|
|
|
|
Coordinates = Node[0]
|
|
|
|
|
Direction = Node[1]
|
|
|
|
|
if not Coordinates in exitState:
|
|
|
|
|
States.push((Coordinates,Actions + [Direction]))
|
|
|
|
|
startState = Coordinates
|
|
|
|
|
return Actions + [Direction]
|
|
|
|
|
visited_node = []
|
|
|
|
|
myStack= util.Stack()
|
|
|
|
|
actions = []
|
|
|
|
|
s = problem.getStartState()
|
|
|
|
|
if problem.isGoalState(s):
|
|
|
|
|
return actions
|
|
|
|
|
myStack.push((s, actions))
|
|
|
|
|
while not myStack.isEmpty():
|
|
|
|
|
state = myStack.pop()
|
|
|
|
|
if state[0] in visited_node:
|
|
|
|
|
continue
|
|
|
|
|
visited_node.append(state[0])
|
|
|
|
|
actions = state[1]
|
|
|
|
|
if (problem.isGoalState(state[0])):
|
|
|
|
|
return actions
|
|
|
|
|
for successor in problem.getSuccessors(state[0]):
|
|
|
|
|
child_state = successor[0]
|
|
|
|
|
action = successor[1]
|
|
|
|
|
sub_action = list(actions)
|
|
|
|
|
if not child_state in visited_node:
|
|
|
|
|
sub_action.append(action)
|
|
|
|
|
myStack.push((child_state, sub_action))
|
|
|
|
|
return actions
|
|
|
|
|
util.raiseNotDefined()
|
|
|
|
|
|
|
|
|
|
def breadthFirstSearch(problem):
|
|
|
|
|
"""Search the shallowest nodes in the search tree first."""
|
|
|
|
|
"*** YOUR CODE HERE ***"
|
|
|
|
|
startState = problem.getStartState()
|
|
|
|
|
exitState = []
|
|
|
|
|
States = util.Queue()
|
|
|
|
|
States.push((startState,[]))
|
|
|
|
|
while (not States.isEmpty()) and (not problem.isGoalState(startState)):
|
|
|
|
|
State, Actions = States.pop()
|
|
|
|
|
if State not in exitState:
|
|
|
|
|
Successor = problem.getSuccessors(State)
|
|
|
|
|
exitState.append(State)
|
|
|
|
|
for Node in Successor:
|
|
|
|
|
Coordinates = Node[0]
|
|
|
|
|
Direction = Node[1]
|
|
|
|
|
if Coordinates not in exitState:
|
|
|
|
|
States.push((Coordinates,Actions+[Direction]))
|
|
|
|
|
startState = Coordinates
|
|
|
|
|
return Actions + [Direction]
|
|
|
|
|
visited_node = []
|
|
|
|
|
myQueue = util.Queue()
|
|
|
|
|
actions = []
|
|
|
|
|
s = problem.getStartState()
|
|
|
|
|
if problem.isGoalState(s):
|
|
|
|
|
return actions
|
|
|
|
|
myQueue.push((s, actions))
|
|
|
|
|
while not myQueue.isEmpty():
|
|
|
|
|
state = myQueue.pop()
|
|
|
|
|
if state[0] in visited_node:
|
|
|
|
|
continue
|
|
|
|
|
visited_node.append(state[0])
|
|
|
|
|
actions = state[1]
|
|
|
|
|
if (problem.isGoalState(state[0])):
|
|
|
|
|
return actions
|
|
|
|
|
for successor in problem.getSuccessors(state[0]):
|
|
|
|
|
child_state = successor[0]
|
|
|
|
|
action = successor[1]
|
|
|
|
|
sub_action = list(actions)
|
|
|
|
|
if not child_state in visited_node:
|
|
|
|
|
sub_action.append(action)
|
|
|
|
|
myQueue.push((child_state, sub_action))
|
|
|
|
|
return actions
|
|
|
|
|
util.raiseNotDefined()
|
|
|
|
|
|
|
|
|
|
def uniformCostSearch(problem):
|
|
|
|
|
"""Search the node of least total cost first."""
|
|
|
|
|
"*** YOUR CODE HERE ***"
|
|
|
|
|
startState = problem.getStartState()
|
|
|
|
|
exitState = []
|
|
|
|
|
States = util.PriorityQueue()
|
|
|
|
|
States.push((startState, []),0)
|
|
|
|
|
while (not States.isEmpty()) and (not problem.isGoalState(startState)):
|
|
|
|
|
State, Actions = States.pop()
|
|
|
|
|
if State not in exitState:
|
|
|
|
|
Successor = problem.getSuccessors(State)
|
|
|
|
|
exitState.append(State)
|
|
|
|
|
for Node in Successor:
|
|
|
|
|
Coordinates = Node[0]
|
|
|
|
|
Direction = Node[1]
|
|
|
|
|
if Coordinates not in exitState:
|
|
|
|
|
newAction = Actions + [Direction]
|
|
|
|
|
States.push((Coordinates, newAction),problem.getCostOfActions(newAction))
|
|
|
|
|
startState = Coordinates
|
|
|
|
|
return Actions + [Direction]
|
|
|
|
|
visited_node = []
|
|
|
|
|
mypriorityQueue = util.PriorityQueue()
|
|
|
|
|
actions = []
|
|
|
|
|
s = problem.getStartState()
|
|
|
|
|
if problem.isGoalState(s):
|
|
|
|
|
return actions
|
|
|
|
|
mypriorityQueue.push((s, actions), 0)
|
|
|
|
|
while not mypriorityQueue.isEmpty():
|
|
|
|
|
state = mypriorityQueue.pop()
|
|
|
|
|
if state[0] in visited_node:
|
|
|
|
|
continue
|
|
|
|
|
visited_node.append(state[0])
|
|
|
|
|
actions = state[1]
|
|
|
|
|
if (problem.isGoalState(state[0])):
|
|
|
|
|
return actions
|
|
|
|
|
for successor in problem.getSuccessors(state[0]):
|
|
|
|
|
child_state = successor[0]
|
|
|
|
|
action = successor[1]
|
|
|
|
|
sub_action = list(actions)
|
|
|
|
|
if not child_state in visited_node:
|
|
|
|
|
sub_action.append(action)
|
|
|
|
|
mypriorityQueue.push((child_state, sub_action), problem.getCostOfActions(sub_action))
|
|
|
|
|
return actions
|
|
|
|
|
util.raiseNotDefined()
|
|
|
|
|
|
|
|
|
|
def nullHeuristic(state, problem=None):
|
|
|
|
@ -162,25 +178,29 @@ def nullHeuristic(state, problem=None):
|
|
|
|
|
def aStarSearch(problem, heuristic=nullHeuristic):
|
|
|
|
|
"""Search the node that has the lowest combined cost and heuristic first."""
|
|
|
|
|
"*** YOUR CODE HERE ***"
|
|
|
|
|
startState = problem.getStartState()
|
|
|
|
|
exitState = []
|
|
|
|
|
States = util.PriorityQueue()
|
|
|
|
|
h_n = heuristic(startState,problem)
|
|
|
|
|
States.push((startState, []), h_n)
|
|
|
|
|
while (not States.isEmpty()) and (not problem.isGoalState(startState)):
|
|
|
|
|
State, Actions = States.pop()
|
|
|
|
|
if State not in exitState:
|
|
|
|
|
Successor = problem.getSuccessors(State)
|
|
|
|
|
exitState.append(State)
|
|
|
|
|
for Node in Successor:
|
|
|
|
|
Coordinates = Node[0]
|
|
|
|
|
Direction = Node[1]
|
|
|
|
|
if Coordinates not in exitState:
|
|
|
|
|
newAction = Actions + [Direction]
|
|
|
|
|
newCost = problem.getCostOfActions(newAction) + heuristic(Coordinates,problem)
|
|
|
|
|
States.push((Coordinates, newAction), newCost)
|
|
|
|
|
startState = Coordinates
|
|
|
|
|
return Actions + [Direction]
|
|
|
|
|
visited_node = []
|
|
|
|
|
mypriorityQueue = util.PriorityQueue()
|
|
|
|
|
actions = []
|
|
|
|
|
s = problem.getStartState()
|
|
|
|
|
if problem.isGoalState(s):
|
|
|
|
|
return actions
|
|
|
|
|
mypriorityQueue.push((s, actions), 0)
|
|
|
|
|
while not mypriorityQueue.isEmpty():
|
|
|
|
|
state = mypriorityQueue.pop()
|
|
|
|
|
if state[0] in visited_node:
|
|
|
|
|
continue
|
|
|
|
|
visited_node.append(state[0])
|
|
|
|
|
actions = state[1]
|
|
|
|
|
if (problem.isGoalState(state[0])):
|
|
|
|
|
return actions
|
|
|
|
|
for successor in problem.getSuccessors(state[0]):
|
|
|
|
|
child_state = successor[0]
|
|
|
|
|
action = successor[1]
|
|
|
|
|
sub_action = list(actions)
|
|
|
|
|
if not child_state in visited_node:
|
|
|
|
|
sub_action.append(action)
|
|
|
|
|
mypriorityQueue.push((child_state, sub_action),heuristic(child_state, problem) + problem.getCostOfActions(sub_action))
|
|
|
|
|
return actions
|
|
|
|
|
util.raiseNotDefined()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|