def search(grid,init,goal,cost): # ---------------------------------------- # insert code here # ---------------------------------------- Nr=len(grid) Nc=len(grid[0]) def childNode(grid, loc, delta, delta_name): child=[] for i in range(len(delta)): move=delta[i] newloc=[loc[0]+move[0], loc[1]+move[1]] if loc[0]+move[0]>=0 and loc[0]+move[0]<Nr \ and loc[1]+move[1]>=0 and loc[1]+move[1]<Nc \ and grid[newloc[0]][newloc[1]] ==0: child.append([newloc,delta_name[i]]) return child visited=[] frontier=Queue() frontier.push([init,0]) while not frontier.isEmpty(): loc, rlen=frontier.pop() if not loc in visited: visited.append(loc) if loc==goal: return [rlen, loc[0], loc[1]] for nextloc, move_name in childNode(grid, loc, delta, delta_name): if not nextloc in visited: frontier.push([nextloc, rlen+cost]) return 'fail'
def linearize_level_ordered(self, level=1): graph_str = '' bfs_queue = Queue() cur_level_count = 0 visited = {key: False for key in self.graph} for root in self.roots: bfs_queue.enqueue(root) cur_level_count += 1 while not bfs_queue.isEmpty() and level > 0: next_level_count = 0 while cur_level_count > 0: cur_item = bfs_queue.dequeue() if not visited[cur_item]: visited[cur_item] = True if global_info.node_labels[cur_item].find( 'name_' ) != -1 or global_info.node_labels[cur_item].find( 'date-entity' ) != -1: # skip name and date entity as global concepts cur_level_count -= 1 continue graph_str += global_info.node_labels[cur_item] + _SPACE for neighbour in self.graph[cur_item].adjacency_list: next_level_count += 1 bfs_queue.enqueue(neighbour[0]) cur_level_count -= 1 cur_level_count = next_level_count level -= 1 return graph_str
def search(grid, init, goal, cost): # ---------------------------------------- # insert code here # ---------------------------------------- Nr = len(grid) Nc = len(grid[0]) expand = [[-1 for j in range(Nc)] for i in range(Nr)] policy = [[' ' for j in range(Nc)] for i in range(Nr)] path = [[' ' for j in range(Nc)] for i in range(Nr)] count = 0 def childNode(grid, loc, delta, delta_name): child = [] for i in range(len(delta)): move = delta[i] newloc = [loc[0] + move[0], loc[1] + move[1]] if loc[0]+move[0]>=0 and loc[0]+move[0]<Nr \ and loc[1]+move[1]>=0 and loc[1]+move[1]<Nc \ and grid[newloc[0]][newloc[1]] ==0: child.append([newloc, delta_name[i]]) return child visited = [] frontier = Queue() frontier.push([init, ' ', 0]) while not frontier.isEmpty(): loc, mov, rlen = frontier.pop() if not loc in visited: visited.append(loc) policy[loc[0]][loc[1]] = mov if loc == goal: path[goal[0]][goal[1]] = '*' while loc != init: x, y = loc if policy[x][y] == 'v': loc = [x - 1, y] path[loc[0]][loc[1]] = 'v' elif policy[x][y] == '^': loc = [x + 1, y] path[loc[0]][loc[1]] = '^' elif policy[x][y] == '>': loc = [x, y - 1] path[loc[0]][loc[1]] = '>' elif policy[x][y] == '<': loc = [x, y + 1] path[loc[0]][loc[1]] = '<' return path for nextloc, move_name in childNode(grid, loc, delta, delta_name): if not nextloc in visited: frontier.push([nextloc, move_name, rlen + cost]) return 'fail'
def AC3(csp): """ Returns False if an inconsistency is found, True otherwise Input: A CSP with components (X, D, C) """ # Initially, the queue has all the arcs in the CSP queue = Queue(csp.arcs()) while not queue.isEmpty(): (Xi, Xj) = queue.dequeue() if revise(csp, Xi, Xj): if len(csp.domain(Xi)) == 0: return False for Xk in csp.neighbors(Xi, Xj): queue.enqueue((Xk, Xi)) return True
def search(grid, init, goal, cost): # ---------------------------------------- # modify code below # ---------------------------------------- Nr = len(grid) Nc = len(grid[0]) expand = [[-1 for j in range(Nc)] for i in range(Nr)] count = 0 def childNode(grid, loc, delta, delta_name): child = [] for i in range(len(delta)): move = delta[i] newloc = [loc[0] + move[0], loc[1] + move[1]] if loc[0]+move[0]>=0 and loc[0]+move[0]<Nr \ and loc[1]+move[1]>=0 and loc[1]+move[1]<Nc \ and grid[newloc[0]][newloc[1]] ==0: child.append([newloc, delta_name[i]]) return child visited = [] frontier = Queue() frontier.push([init, 0]) while not frontier.isEmpty(): loc, rlen = frontier.pop() if not loc in visited: visited.append(loc) expand[loc[0]][loc[1]] = count count += 1 if loc == goal: return expand for nextloc, move_name in childNode(grid, loc, delta, delta_name): if not nextloc in visited: frontier.push([nextloc, rlen + cost]) return 'fail'
def linearize_level_ordered(self, level = 1): graph_str = '' bfs_queue = Queue() cur_level_count = 0 visited = {key:False for key in self.graph} for root in self.roots: bfs_queue.enqueue(root) cur_level_count += 1 while not bfs_queue.isEmpty() and level > 0: next_level_count = 0 while cur_level_count > 0: cur_item = bfs_queue.dequeue() if not visited[cur_item]: visited[cur_item] = True if global_info.node_labels[cur_item].find('name_') != -1 or global_info.node_labels[cur_item].find('date-entity') != -1: # skip name and date entity as global concepts cur_level_count -= 1 continue graph_str += global_info.node_labels[cur_item] + _SPACE attribute_dict = global_info.get_attribute_dict() for neighbour in self.graph[cur_item].adjacency_list: next_level_count += 1 bfs_queue.enqueue(neighbour[0]) attribute_dict[neighbour[1]] = global_info.node_labels[neighbour[0]] for k in sorted(attribute_dict): graph_str += k + _SPACE if k == 'time': graph_str += (attribute_dict[k] if attribute_dict[k] == _EMPTY else attribute_dict[k][12:]) + _SPACE else: graph_str += attribute_dict[k] + _SPACE cur_level_count -= 1 cur_level_count = next_level_count level -= 1 return graph_str
def general_search(problem): """Problem: {doamin, method, graph, coords, start, goal} method: B, D, I, U, A route: graph search (maintain a closed set) except Astar tsp: tree search """ method = problem['method'] if method not in ['B', 'D', 'I', 'U', 'A']: print("Enter Correct method!") return None if problem['start'] not in problem['graph'].get_nodes() or ( problem['domain'] == 'route' and problem['goal'] not in problem['graph'].get_nodes()): print("Enter Correct Start/Goal!") return None # BFS, DFS if method in ['B', 'D']: if method == 'B': frontier = Queue() else: frontier = Stack() start_node = Node(problem['start']) frontier.push(start_node) closed_set = set() while not frontier.isEmpty(): node = frontier.pop() closed_set.add(node.value) if goal_test(problem, node): path = node.get_path() cost = cal_path_cost(problem['graph'], path) return path, cost, frontier.count # Expansion closed_set.add(node.value) adj = problem['graph'].adj(node.value) for child_value in quicksort(adj): if push_or_not(problem, node, child_value, closed_set): child_node = Node(child_value, node) frontier.push(child_node) return None """Uniform Cost Search / Astar Search""" if method in ['U', 'A']: frontier = PriorityQueue() start_node = Node(problem['start']) priority = 0 if method == 'U' else astar_heuristic(problem, start_node) frontier.push(priority, start_node) closed_set = set() while not frontier.isEmpty(): node = frontier.pop() closed_set.add(node.value) if goal_test(problem, node): path = node.get_path() cost = cal_path_cost(problem['graph'], path) return path, cost, frontier.count # Expansion adj = problem['graph'].adj(node.value) for child_value in quicksort(adj): if push_or_not(problem, node, child_value, closed_set): child_cost = node.cost_so_far + \ problem['graph'].get_cost(node.value, child_value) # g_n child_node = Node(child_value, node, child_cost) priority = child_cost if method == 'U' else child_cost + \ astar_heuristic(problem, child_node) frontier.push(priority, child_node) return None """Iterative Deepening Search""" if method == 'I': def depth_limited(problem, limit): """Depth Limited Search""" frontier = Stack() start_node = Node(problem['start']) frontier.push(start_node) closed_set = set() while not frontier.isEmpty(): node = frontier.pop() closed_set.add(node.value) if goal_test(problem, node): path = node.get_path() cost = cal_path_cost(problem['graph'], path) return path, cost, frontier.count if node.depth == limit: pass else: # Expansion adj = problem['graph'].adj(node.value) for child_value in quicksort(adj): if push_or_not(problem, node, child_value, closed_set): child_node = Node(child_value, node) frontier.push(child_node) return None max_depth = 20 for i in range(max_depth): result = depth_limited(problem, i) if result: return result