def BFS3(start, goal, cycle_detect=False, verbose=False, ranking=False): frontier = [] if ranking: bases, _, patterns, columns = rank.get_information(start) start_int = rank.rank(start) frontier.append([start_int]) #figure out size of visited array (by multiplying bases) and initialize it prod = 1 for i in bases: prod *= i visited = [False for u in range(prod)] visited[start_int] = True else: frontier.append([start]) while frontier: path = frontier.pop(0) #select and remove first path from frontier last_vertex = path[-1] if ranking: # convert from integer to matrix to check if goal and/or find neighbors last_vertex = unrank.unrank(last_vertex, bases, patterns, columns) ####!!!!!#### if is_goal(last_vertex, goal): #check if last vertex in path is goal if verbose: #if asked by user, print each vertex(matrix) in path if ranking: print(path) else: for matrices in path: for line in matrices: print(line) print() print(len(path)) #print length of solution for convenience return path #enter procedure here, if user asked to use ranking/unranking if ranking: for next_vertex in neighbors(last_vertex): ###!!!### int_next = rank.rank(next_vertex) #convert neighbor matrix to integer if cycle_detect: if visited[int_next]: continue new_path = path + [int_next] visited[int_next] = True frontier.append(new_path) #enter procedure here, if user did not want to use ranking/unranking else: for next_vertex in neighbors(last_vertex): if cycle_detect: #include cycle detection if asked for if next_vertex in path: continue new_path = path + [next_vertex] frontier.append(new_path) return None
def DFS_prune(start, goal, verbose=False): frontier = [] frontier.append([start]) while frontier: path = frontier.pop() #select and remove last path from frontier last_vertex = path[-1] if is_goal(last_vertex, goal): #check if last vertex in path is goal if verbose: #if asked by user, print each vertex(matrix) in path for matrices in path: for line in matrices: print(line) print() print("length:", len(path)) #print length of solution for convenience return path in_path = False for next_vertex in neighbors(last_vertex): for item in frontier: if next_vertex in item: in_path = True continue if in_path: continue new_path = path + [next_vertex] frontier.append(new_path) return None
def DFS_bound2(start, goal, depth, cycle_detection=True, verbose=False): bases, _, patterns, columns = rank.get_information(start) #parameters for unrank frontier = [] start_int = rank.rank(start) #convert start matrix to its rank integer frontier.append([start_int]) while frontier: path = frontier.pop() last_vertex = path[-1] # convert to matrix for goal and neighbors matrix_last = unrank.unrank(last_vertex, bases, patterns, columns) if is_goal(matrix_last, goal): if verbose: #if asked by user, print path print(path) print("length:", len(path)) return path if len(path) == depth: continue for next_vertex in neighbors(matrix_last): int_next = rank.rank(next_vertex) #convert neighbor matrix to integer if cycle_detection: if int_next in path: continue new_path = path + [int_next] frontier.append(new_path) return None
def DFS_prune2(start, goal, verbose=False): bases, _, patterns, columns = rank.get_information(start) #parameters for unrank frontier = [] start_int = rank.rank(start) #convert start matrix to its rank integer frontier.append([start_int]) #figure out size of visited array (by multiplying bases) and initialize it prod = 1 for i in bases: prod *= i visited = [False for u in range(prod)] visited[start_int] = True while frontier: path = frontier.pop() last_vertex = path[-1] # convert to matrix for goal and neighbors matrix_last = unrank.unrank(last_vertex, bases, patterns, columns) if is_goal(matrix_last, goal): if verbose: #if asked by user, print path print(path) print("length:", len(path)) return path for next_vertex in neighbors(matrix_last): int_next = rank.rank(next_vertex) #convert neighbor matrix to integer if visited[int_next]: continue new_path = path + [int_next] visited[int_next] = True frontier.append(new_path) return None
def DEEP(start, goal, cycle_detection=True, verbose=False): max_length = 0 frontier = [] while True: if not frontier: max_length += 1 frontier.append([start]) path = frontier.pop() #select and remove last path from frontier last_vertex = path[-1] if is_goal(last_vertex, goal): #check if last vertex in path is goal if verbose: #if asked by user, print each vertex(matrix) in path for matrices in path: for line in matrices: print(line) print() print(len(path)) #print length of solution for convenience return path if len(path) == max_length: continue for next_vertex in neighbors(last_vertex): if cycle_detection: if next_vertex in path: continue new_path = path + [next_vertex] frontier.append(new_path) return None
def LCFS(start, goal, cycle_detection=False, verbose=False): #frontier will look like [(cost, [path]), (cost, [path]), (cost, [path]),...] frontier = [] #this frontier will be used as a heap heapq.heappush(frontier, (0, [start])) #heap prioritized by first element of tuple while frontier: #path_tuple = (cost, [path]) path_tuple = heapq.heappop( frontier) #select and remove first path tuple from frontier #last_vertex = path[-1] from path_tuple last_vertex = path_tuple[1][-1] if is_goal(last_vertex, goal): #check if last vertex in path is goal if verbose: #if asked by user, print each vertex(matrix) in path for matrices in path_tuple[1]: for line in matrices: print(line) print() print("cost\t:", path_tuple[0]) #print cost of soln for convenience print("length\t:", len( path_tuple[1])) #print length of solution for convenience return path_tuple for next_tuple in neighbors(last_vertex, with_cost=True): if cycle_detection: #include cycle detection if asked for if next_tuple[1] in path_tuple[1]: continue new_path = (path_tuple[0] + next_tuple[0], path_tuple[1] + [next_tuple[1]]) heapq.heappush(frontier, new_path) return None
def LCFS2(start, goal, pruning=True, verbose=False): bases, _, patterns, columns = rank.get_information( start) #parameters for unrank start_int = rank.rank(start) #convert start matrix to its rank integer #frontier will look like [(cost, [path]), (cost, [path]), (cost, [path]),...] frontier = [] #this frontier will be used as a heap heapq.heappush( frontier, (0, [start_int])) #heap prioritized by first element of tuple # figure out size of visited array (by multiplying bases) and initialize it prod = 1 for i in bases: prod *= i visited = [False for u in range(prod)] visited[start_int] = True while frontier: #path_tuple = (cost, [path]) path_tuple = heapq.heappop( frontier) #select and remove first path tuple from frontier #last_vertex = path[-1] from path_tuple last_vertex = path_tuple[1][-1] # convert from integer to matrix to check if goal and/or find neighbors matrix_last = unrank.unrank(last_vertex, bases, patterns, columns) if is_goal(matrix_last, goal): #check if last vertex in path is goal if verbose: #if asked by user, print each vertex(matrix) in path print(path_tuple[1]) print("cost\t:", path_tuple[0]) #print cost of soln for convenience print("length\t:", len( path_tuple[1])) #print length of solution for convenience return path_tuple for next_tuple in neighbors(matrix_last, with_cost=True): int_next = rank.rank(next_tuple[1]) if pruning: #include cycle detection if asked for if visited[int_next]: continue new_path = (path_tuple[0] + next_tuple[0], path_tuple[1] + [int_next]) visited[int_next] = True heapq.heappush(frontier, new_path) return None