def _orient_normals(self, k): print 'Orienting normals' # find pt with maximum z value index = np.argmax([pt.position[2] for pt in self.points]) root = self.points[index] if root.normal[2] > 0: root.normal *= -1 parents = {} heap = BinaryHeap() for pt in self.points: if pt == root: heap.insert(0, pt) parents[root] = root else: heap.insert(float('inf'), pt) while not heap.is_empty(): pt = heap.extract_min() if pt in parents: prev = parents[pt] else: prev = self.nearest_neighbors(pt, 1, parents.keys())[0] parents[pt] = prev if np.dot(prev.normal, pt.normal) < 0: pt.normal *= -1 neighbors = self.nearest_neighbors(pt, k) for pt2 in neighbors: if pt2 not in parents: old_dist = heap.get_key(pt2) dist = 1. - np.abs(np.dot(pt.normal, pt2.normal)) if dist < old_dist: parents[pt2] = pt heap.update_key(dist, pt2) return parents
def singleSourceShortest(G, src): """ Given graph G return dictionary of shortest paths to other vertices from vertex src. All vertices in G must be drawn from the range 0..n-1 and src must also be from same range. """ # Initialize dist[] matrix to be Infinity for all but src infinity = sys.maxsize n = 0 dist = {} for v in range(len(G)): n += 1 dist[v] = infinity dist[src] = 0 # optimized construction for BinaryHeap pq = BinaryHeap(n, src, infinity) while not pq.isEmpty(): u = pq.pop() for v, weight in G.neighbors(u): newLen = dist[u] + weight if newLen < dist[v]: pq.decreaseKey(v, newLen) dist[v] = newLen return dist
def test_decreaseKeyMany(self): self.bh = BinaryHeap(1000, 0, 999) for _ in range(999, 0, -1): self.bh.decreaseKey(_, _) for _ in range(1000): self.assertEqual(_, self.bh.pop())
def __init__(self, filename, to_crop=False): self.maze = Maze(filename, to_crop=to_crop) self.nodes = self.maze.node_dict self.priority_que = BinaryHeap([list(a) for a in zip(self.nodes.keys(), [float('inf')] * len(self.nodes))]) # Want to use zip function, but need each item to be mutable. therefore, the `list(a) for a in zip...` notation self.visited_nodes = set() self.path = []
def test_decreaseKeyRandom(self): self.bh = BinaryHeap(1000, 0, 999) for _ in range(999, 0, -1): self.bh.decreaseKey(_, random.randint(1, 999)) result = [] for _ in range(1000): result.append(self.bh.pop()) self.assertEqual(list(range(1000)), sorted(result))
def test_multipleDecreaseKey(self): self.bh = BinaryHeap(50, 0, 999) for n in range(1, 50): for p in range(1, n): self.bh.decreaseKey(n, p) result = [] for _ in range(50): result.append(self.bh.pop()) self.assertEqual(list(range(50)), sorted(result))
def least_cost_path(graph, start, dest, cost): """Find and return a least cost path in graph from start vertex to dest vertex. Efficiency: If E is the number of edges, the run-time is O( E log(E) ). Args: graph (Graph): The digraph defining the edges between the vertices. start: The vertex where the path starts. It is assumed that start is a vertex of graph. dest: The vertex where the path ends. It is assumed that dest is a vertex of graph. cost: A class with a method called "distance" that takes as input an edge (a pair of vertices) and returns the cost of the edge. For more details, see the CostDistance class description below. Returns: list: A potentially empty list (if no path can be found) of the vertices in the graph. If there was a path, the first vertex is always start, the last is always dest in the list. Any two consecutive vertices correspond to some edge in graph. """ reached = {} # empty dictionary events = BinaryHeap() # empty heap events.insert((start, start), 0) # vertex s burns at time 0 while len(events) > 0: edge, time = events.popmin() if edge[1] not in reached: reached[edge[1]] = edge[0] for nbr in graph.neighbours(edge[1]): events.insert((edge[1], nbr), time + cost.distance( (edge[1], nbr))) # if the dest is not in reached, then no route was found if dest not in reached: return [] current = dest route = [current] # go through the reached vertices until we get back to start and append # each vertice that we "stop" at while current != start: current = reached[current] route.append(current) # reverse the list because we made a list that went from the dest to start route = route[::-1] return route
def __init__(self): self.totalState = 10 self.totalAction = 2 self.epsilon = 0.1 self.stepSize = 0.25 #self.stepSize = 0.05 self.discount = 1.0 - 1.0 / self.totalState self.maxIter = 10**6 self.repeatNo = 10 self.minibatch = 5 self.alpha = 0.7 self.beta = 0.5 self.replayMemory = [] self.maxWeight = 0 self.mode = 'FA' #self.mode = 'Tablar' #self.samplePolicy = 'uniform' #self.samplePolicy = 'maxPriority' self.samplePolicy = 'rank' self.binaryHeap = BinaryHeap() print 'replay memory size : %s' % (2**(self.totalState + 1) - 2)
def main(): heap = BinaryHeap(5) heap.insert(2) print("Root node: {}".format(heap.root.data)) leaf = heap.getLeaf(heap.root) print("Leaf: {}".format(leaf.data))
def setUp(self): # create BinaryHeap with initial 4 elements, and src is 2. Use # 999 for infinity self.bh = BinaryHeap(15, 2, 999)