def depthFirstSearch(problem): """ Search the deepest nodes in the search tree first. Your search algorithm needs to return a list of actions that reaches the goal. Make sure to implement a graph search algorithm. To get started, you might want to try some of these simple commands to understand the search problem that is being passed in: print "Start:", problem.getStartState() print "Is the start a goal?", problem.isGoalState(problem.getStartState()) print "Start's successors:", problem.getSuccessors(problem.getStartState()) """ "*** YOUR CODE HERE ***" startstate = problem.getStartState() currentposition = problem.getStartState() arewethereyet = problem.isGoalState(currentposition) visited = [startstate] instructions = [] plan = [] from collections import defaultdict relations = defaultdict(list) directions = defaultdict(list) import numpy from game import Directions from game import Actions actions = Actions() n = Directions.NORTH s = Directions.SOUTH e = Directions.EAST w = Directions.WEST from util import Stack stack = Stack() while (arewethereyet == False): successors = problem.getSuccessors(currentposition) print("Successors for " + str(currentposition) + " are " + str(successors)) for item in successors: stack.push(item) relations[currentposition].append(item[0]) moved = False while (not moved): poppeditem = stack.pop() if (poppeditem[0] not in visited): directions[poppeditem[0]] = poppeditem[1] currentposition = poppeditem[0] visited.append(currentposition) moved = True arewethereyet = problem.isGoalState(currentposition) print("Relations = " + str(relations)) print("Length of Relations = " + str(len(relations))) print("Directions = " + str(directions)) print("Length of Directions = " + str(len(directions))) numhops = 0 print("Visited = " + str(visited)) print("Length of Visited = " + str(len(visited))) for i in range(0, len(visited) - 1): print(visited[i + 1]) print(relations[visited[i]]) if visited[i + 1] not in relations[visited[i]]: numhops += 1 print("Number of Hops = " + str(numhops)) instructions = visited print("Original Length of Instructions = " + str(len(instructions))) for i in range(0, numhops): for j in range(0, len(instructions) - 1): if visited[j + 1] not in relations[visited[j]]: rightend = instructions[j + 1] rightendindex = j + 1 print("Right End = " + str(rightend)) break for k in range(0, len(instructions) - 1): if rightend in relations[visited[k]]: leftend = instructions[k] leftendindex = k print("Left End = " + str(leftend)) break instructions[leftendindex + 1:rightendindex] = [] print("Instructions = " + str(instructions)) print("Final Length of Instructions = " + str(len(instructions))) for m in range(0, len(instructions) - 1): plan.append(directions[visited[m + 1]]) print("Plan = " + str(plan)) print("Length of Plan = " + str(len(plan))) return plan