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 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 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