def dfs1(maze, start, goal, offs, pred={}, flag=[False]): if pred == {}: pred[start] = None cc = start for key, value in offs.items(): #print(value,cc) #print(key) newpos = tuple(cc[i] + value[i] for i in range(len(cc))) # Stop condition: if cc == goal: flag[0] = True return pred #print(is_legal_pos(maze, newpos), "\n", newpos not in pred ) # Check each direction if is_legal_pos(maze, newpos) and newpos not in pred: pred[newpos] = cc #print("\n" , key, "\n") dfs1(maze, newpos, goal, offs, pred, flag) # Check if desired goal reached when coming back if flag[0] == True: return pred
def a_star(maze, start, goal): pq = PriorityQueue() # In the A* trace we said that the F value for the initial cell position would be equal to # the H value...but we are not required to have to calculate that manually because there's only # going to be one item on the queue at this point, it will automatically # have the highest priority so we can set it equal to 0 pq.put(start, 0) predecessors = {start: None} g_values = {start: 0} # In our representation of these mazes, we have cells connected by edges and the weight # of each edge is just one. So the new cost is just one more than the previous cost # putting it in g values and in predecessors is equivalent to saying *we've discovered this while not pq.is_empty(): current_cell = pq.get() if current_cell == goal: return get_path(predecessors, start, goal) for direction in ["up", "right", "down", "left"]: row_offset, col_offset = offsets[direction] neighbour = (current_cell[0] + row_offset, current_cell[1] + col_offset) if is_legal_pos(maze, neighbour) and neighbour not in g_values: new_cost = g_values[current_cell] + 1 g_values[neighbour] = new_cost f_value = new_cost + heuristic(goal, neighbour) pq.put(neighbour, f_value) predecessors[neighbour] = current_cell return None
def dfs(maze, start, goal): # We instantiate a new stack stack = Stack() # We're pushing our start position which is a coordinate/row, column tuples (an immutable list) stack.push(start) # predecessors is a dictionary containing the predecessor of the start position, which is none predecessors = {start: None} while not stack.is_empty(): # While the stack is not empty, we get the current cell by popping current_cell = stack.pop() # We check if it's the goal, if it is, the program is finished! if current_cell == goal: return get_path(predecessors, start, goal) # Otherwise, for every direction in this list, we check the offsets (imported from helper # file and we assign the i, j values from from that so that we can then check the neighbor # The whole point of this is to check the coordinates of the contiguous cells in all four directions for direction in ["up", "right", "down", "left"]: row_offset, col_offset = offsets[direction] # neighbor refers to the cell neighbor = (current_cell[0] + row_offset, current_cell[1] + col_offset) # now we say, for each of those directions, if it's a legal position and it doesn't already exist in our # predecessors list, that means we haven't discovered it yet - so we push it on to our stack. if is_legal_pos(maze, neighbor) and neighbor not in predecessors: # finally we push the neighbor on the stack and update the predecessors stack.push(neighbor) predecessors[neighbor] = current_cell return None
def bfs(maze, start, goal): q = Queue() q.enqueue(start) pred = {start: None} while (not q.is_empty()): cc = q.dequeue() if cc == goal: return get_path(pred, start, goal) else: for key, value in offsets.items(): newpos = tuple(cc[i] + value[i] for i in range(len(cc))) if is_legal_pos(maze, newpos) and newpos not in pred: q.enqueue(newpos) pred[newpos] = cc
def dfs(maze, start, goal): stack = Stack() stack.push(start) predecessors = {start: None} while not stack.is_empty(): current_cell = stack.pop() if current_cell == goal: return get_path(predecessors, start, goal) for direction in ["up", "right", "down", "left"]: row_offset, col_offset = offsets[direction] neighbour = (current_cell[0] + row_offset, current_cell[1] + col_offset) if is_legal_pos(maze, neighbour) and neighbour not in predecessors: stack.push(neighbour) predecessors[neighbour] = current_cell return None
def bfs(maze, start, goal): queue = Queue() queue.enqueue(start) predecessors = {start: None} while not queue.is_empty(): current_cell = queue.dequeue() if current_cell == goal: return get_path(predecessors, start, goal) for direction in ["up", "right", "down", "left"]: row_offset, col_offset = offsets[direction] neighbor = (current_cell[0] + row_offset, current_cell[1] + col_offset) if is_legal_pos(maze, neighbor) and neighbor not in predecessors: queue.enqueue(neighbor) predecessors[neighbor] = current_cell return None
def a_star(maze, start, goal): def fval(cur): return fval1(cur, start, goal) prq = PriorityQueue() prq.put(start, fval(start)) pred = {start: None} while not prq.is_empty(): cc = prq.get() if cc == goal: return get_path(pred, start, goal) for key, value in offsets.items(): newpos = tuple(cc[i] + value[i] for i in range(len(cc))) if is_legal_pos(maze, newpos) and newpos not in pred: prq.put(newpos, fval(newpos)) pred[newpos] = cc
def a_star(maze, start, goal): pq = PriorityQueue() # technically, the 0 should have the heuristic distance, but this is not necessary pq.put(start, 0) predecessors = {start: None} g_values = {start: 0} while not pq.is_empty(): current_cell = pq.get() if current_cell = goal: return get_path(predecessors, start, goal) for direction in ["up","right","down","left"]: row_offset, col_offset = offsets[direction] neighbor = (current_cell[0] + row_offset, current_cell[1] + col_offset) if is_legal_pos(maze, neighbor) and neighbor not in g_values: new_cost = g_values[current_cell] + 1 g_values[neighbor] = new_cost f_value = new_cost + heuristic(goal, neighbor) pq.put(neighbor, f_value) predecessors[neighbor] = current_cell
def dfs(maze, start, goal): stack = Stack() stack.push(start) predecessors = {start: None} while not stack.is_empty(): # getting the top item from the stack (far most right item) current_cell = stack.pop() if current_cell == goal: return get_path(predecessors, start, goal) for direction in ["up", "right", "down", "left"]: # unpacking the tuple row_offset, col_offset = offsets[direction] neighbor = (current_cell[0] + row_offset, current_cell[1] + col_offset) if is_legal_pos(maze, neighbor) and neighbor not in predecessors: stack.push(neighbor) predecessors[neighbor] = current_cell return None # pass only prevents the code from returning an error if you run it pass