def __init__(self, start_node, goal_node, grid): self.grid = grid self.position = start_node.xy self.start_node = start_node self.goal_node = goal_node self.open_list = Heap() self.start_node.set_val("g", 0) self.start_node.set_val("f", start_node.h_val) self.open_list.add(start_node, start_node.f_val) self.search()
def dijkstra(self, s): sdist = dict(((n, 1E9) for n in self.graph.nodes())) sdist[s] = 0 pathprev = {s: None} marked = {} pq = [[sdist[n], n] for n in self.graph.nodes()] Ref = dict([(pq[i][1], i) for i in range(len(pq))]) heap = Heap(pq, Ref) heap.heapify() #print pq #print Ref while pq: node_distance, node = heap.heappop() marked[node] = True for nei in self.graph.get_neighbours(node): if nei not in marked: nei_new_dist = node_distance + self.graph.get_weight( nei, node) if sdist[nei] > nei_new_dist: #print pq heap.decrease_key(Ref[nei], nei_new_dist) #print pq sdist[nei] = nei_new_dist pathprev[nei] = node return sdist, pathprev
def run(self): while True: if self.need_to_exit: break if self.idle: sleep(TIME_TO_SLEEP_SECS) continue self.start_new_search = False first_step = Solution(self.board, 0, heuristic(self.board), []) all_options = Heap() all_options.push(first_step) all_options.push(first_step) while len(all_options) > 1 and not self.start_new_search: # something left at the pool # and no solution has been found yet cur_solution = all_options.pop() if cur_solution.board.is_winning_board(): self.return_results(cur_solution.moves_list) self.idle = True break elif cur_solution.board.is_losing_board(): continue else: for n_board, move in cur_solution.board.get_successors(): n_moves_list = cur_solution.moves_list[:] + [move] n_walked_so_far = cur_solution.distance_so_far + 1 n_heuristic_v = heuristic(n_board) all_options.push(Solution(n_board, n_walked_so_far, n_heuristic_v, n_moves_list)) # didn't find any solution self.idle = True continue print("ai daemon dies now")
def dijkstra(self, s): sdist = dict( ((n, 1E9) for n in self.graph.nodes()) ) sdist[s] = 0 pathprev = { s:None } marked = {} pq = [ [sdist[n], n] for n in self.graph.nodes() ] Ref = dict([ (pq[i][1],i) for i in range(len(pq)) ]) heap = Heap(pq, Ref) heap.heapify() #print pq #print Ref while pq: node_distance, node = heap.heappop() marked[node] = True for nei in self.graph.get_neighbours(node): if nei not in marked: nei_new_dist = node_distance + self.graph.get_weight(nei, node) if sdist[nei] > nei_new_dist: #print pq heap.decrease_key(Ref[nei], nei_new_dist) #print pq sdist[nei] = nei_new_dist pathprev[nei] = node return sdist, pathprev
class SearchAgent(object): def __init__(self, start_node, goal_node, grid): self.grid = grid self.position = start_node.xy self.start_node = start_node self.goal_node = goal_node self.open_list = Heap() self.start_node.set_val("g", 0) self.start_node.set_val("f", start_node.h_val) self.open_list.add(start_node, start_node.f_val) self.search() def __repr__(self): return "agent at - ( " + str(self.position) + " )" def expand(self, node): if node == self.goal_node: print "Success we got to the goal" self.success() return next_nodes = node.get_neighbours() node.visited = True for s_next in next_nodes: new_g_val = node.g_val + s_next.get_cost() if s_next.g_val == None: # First time g val setting s_next.set_val("g", new_g_val) f_val = new_g_val + s_next.h_val s_next.set_val("f", f_val) # Set this node as parent to retrace from goal s_next.parent = node self.open_list.add(s_next, f_val) elif s_next.g_val > new_g_val: # Old parent to this node is not the shortest path make new one s_next.set_val("g", new_g_val) f_val = new_g_val + s_next.h_val s_next.set_val("f", f_val) # Change parent s_next.parent = node if s_next.i == None: # Not in the heap yet self.open_list.add(s_next, f_val) else: # update key in heap self.open_list.update_key(s_next.i, f_val) # self.grid.root.after(800, self.search) self.search() def search(self): node = self.open_list.pop() if node.f_val > self.goal_node.f_val: print "Search Failed, path can not be reached" self.grid.root.destroy() return None self.expand(node) def success(self): path_list = [] node = self.goal_node path_list.append(node) while True: # node.fill_box("yellow") node = node.parent path_list.append(node) if node == self.start_node: break self.move_agent(path_list) def move_agent(self, path): # Move agent along the path provided # Check for changing cost if len(path) == 0: self.grid.root.destroy() return None next_node = path.pop() self.start_node.clear_agent() self.start_node = next_node next_node.place_agent() self.grid.root.after(800, lambda: self.move_agent(path))
def median_maintenance(nums): min_heap = Heap(min) max_heap = Heap(max) for num in nums: if max_heap.root() is None: max_heap.add(num) elif min_heap.root() is None: min_heap.add(num) elif num < max_heap.root(): max_heap.add(num) else: min_heap.add(num) if (len(min_heap) - len(max_heap)) > 1: max_heap.add(min_heap.extract_root()) elif (len(max_heap) - len(min_heap)) > 1: min_heap.add(max_heap.extract_root()) if len(max_heap) > len(min_heap): result = max_heap.root() elif len(min_heap) > len(max_heap): result = min_heap.root() else: result = (max_heap.root() + min_heap.root()) / 2.0 return result
class SearchAgent(object): def __init__(self, start_node, goal_node, grid): self.grid = grid self.position = start_node.xy self.start_node = start_node self.goal_node = goal_node self.open_list = Heap() self.start_node.set_val("g", 0) self.start_node.set_val("f", start_node.h_val) self.open_list.add(start_node, start_node.f_val) self.search() def __repr__(self): return "agent at - ( " + str(self.position) + " )" def expand(self, node): if node == self.goal_node: print "Success we got to the goal" self.success() return next_nodes = node.get_neighbours() node.visited = True for s_next in next_nodes: new_g_val = node.g_val + s_next.get_cost() if s_next.g_val == None: # First time g val setting s_next.set_val("g", new_g_val) f_val = new_g_val + s_next.h_val s_next.set_val("f", f_val) # Set this node as parent to retrace from goal s_next.parent = node self.open_list.add(s_next, f_val) elif s_next.g_val > new_g_val: # Old parent to this node is not the shortest path make new one s_next.set_val("g", new_g_val) f_val = new_g_val + s_next.h_val s_next.set_val("f", f_val) # Change parent s_next.parent = node if s_next.i == None: # Not in the heap yet self.open_list.add(s_next, f_val) else: # update key in heap self.open_list.update_key(s_next.i, f_val) # self.grid.root.after(800, self.search) self.search() def search(self): node = self.open_list.pop() if node.f_val > self.goal_node.f_val: print "Search Failed, path can not be reached" self.grid.root.destroy() return None self.expand(node) def success(self): path_list = [] node = self.goal_node path_list.append(node) while True: # node.fill_box("yellow") node = node.parent path_list.append(node) if node == self.start_node: break self.move_agent(path_list) def move_agent(self,path): # Move agent along the path provided # Check for changing cost if len(path) == 0: self.grid.root.destroy() return None next_node = path.pop() self.start_node.clear_agent() self.start_node = next_node next_node.place_agent() self.grid.root.after(800, lambda:self.move_agent(path))
from utils import Heap heap = Heap([3, 1, 2]) heap.push(1) heap.push(2) a = [i for i in heap] assert (a == [1, 1, 2, 2, 3])