def check_goal(sn, problem): #take a SearchNode as an argument. If the SearchNode is the goal, return the path from the start node to goal node. if problem.goal_test(sn.state): path = [] stack = Stack() stack.push(sn.action) while sn.parent != None: stack.push(sn.parent.action) sn = sn.parent while not stack.is_empty(): path.append(stack.pop()) return path[1:] else: return None
def check_goal(sn, problem): if problem.goal_test(sn.state): path = [] stack = Stack() stack.push(sn.action) while sn.parent != None: stack.push(sn.parent.action) sn = sn.parent while not stack.is_empty(): path.append(stack.pop()) return path[1:] else: return None
def solve(problem): """ *** YOUR CODE HERE *** """ # util.raise_not_defined() #Remove this line when you have implemented BrFS stack = Stack() depth_limit = 0 s0 = problem.get_initial_state() sn_root = SearchNode(s0) # depth += 1 check = None while True: explored = [] explored.append(s0) stack = Stack() stack.push(sn_root) while not stack.is_empty(): # print("here") # check_limit = stack.peek() frontier = stack.pop() # if depth_limit != 0: # print(str(frontier.action) + " "+ str(frontier.depth)) check = check_goal(frontier, problem) if check != None: return check else: if (frontier.depth == depth_limit): continue for successor, action, cost in problem.get_successors( frontier.state): # if successor != frontier.state: if successor not in explored: explored.append(successor) sn = SearchNode(successor, action, cost, frontier, frontier.depth + 1) # if stack.find(sn) stack.push(sn) depth_limit += 1 print(depth_limit) print(" ")
def dls(problem, limit): # *** YOUR CODE HERE *** frontiers = Stack() s0 = problem.get_initial_state() #initiate the frontier by the start point #state,action,cost,parent,depth a = SearchNode(s0) frontiers.push(a) while not frontiers.is_empty(): cur = frontiers.pop() #goal_test if problem.goal_test(cur.state): return route(cur) if cur.depth != limit: #add frontiers for successor, action, cost in problem.get_successors(cur.state): b = SearchNode(successor, action, cur.path_cost + cost, cur, cur.depth + 1) if valid_node(b): frontiers.push(b)
def solve(problem): """ *** YOUR CODE HERE *** """ # util.raise_not_defined() #Remove this line when you have implemented BrFS depth_limit = 0 s0 = problem.get_initial_state() sn_root = SearchNode(s0) check = None while True: frontier = Stack() frontier.push(sn_root) while not frontier.is_empty(): current_node = frontier.pop() #check if this node is the goal or not check = check_goal(current_node, problem) if check != None: #if check != None: reached goal, so "check" contains the path return check else: if (current_node.depth == depth_limit): continue for successor, action, cost in problem.get_successors( current_node.state): # if the successor is not explored yet, append this new successor to the explored dictionary tmp = current_node flag = True while tmp.parent != None: if successor == tmp.parent.state: flag = False break tmp = tmp.parent if flag: sn = SearchNode(successor, action, cost, current_node, current_node.depth + 1) frontier.push(sn) print("depth limit = " + str(depth_limit)) depth_limit += 1
def dls_search(problem, depth_limit): # The frontier is a Stack (defined in frontiers.py), for its LIFO queuing policy. frontier = Stack() # Use SearchNode (defined in search_strategies.py) as the class of elements in frontier, and initialize frontier. frontier.push(SearchNode(problem.get_initial_state())) explored = [] # The closed list (of nodes already explored). while not frontier.is_empty(): # Check the first node in frontier. node = frontier.pop() if problem.goal_test(node.state): return node if node.depth == depth_limit: continue for point in explored[::]: if point.depth > node.depth: explored.remove(point) # Expand this node if it is not the goal, nor does it reach the depth_limit. for successor, action, cost in problem.get_successors(node.state): # Avoid revisiting the explored states. if successor in [x.state for x in explored]: continue else: new_node = SearchNode(state=successor, action=action, path_cost=node.path_cost + cost, parent=node, depth=node.depth + 1) # Add the successor node to frontier and explored. frontier.push(new_node) explored.append(new_node) else: print(depth_limit) return 'cutoff'