def __init__(self, curtime: int, window: int) -> None: """ Initialize the TweetGraph :param curtime: The starting time :param window: The sliding window """ self.latest = curtime self.edges = {} # type: Dict[Tuple[str, str], int] self.queue = heapdict() self.window = window
def Dijkstras(G, s): CONST = 1000000 s_dist = {} # distance from source to v is a list processed = [] # list of processed vertices for v in G: s_dist[v] = CONST s_dist[s] = 0 # distance from source to source hd =heapdict() # contains all vertices which haven't been processed for v in G: # initialize the heap dictionary hd[v] = s_dist[v] while len(hd) != 0: v = hd.popitem()[0] # returns key from (key, priority) tuple with the lowest priority if s_dist[v] == CONST: break processed.append(v) index = 0 # for accessing edge_lenghts for w in G[v]: # grab adjacency list of v if w not in processed: alt = s_dist[v] + G_dist[v][index] if alt < s_dist[w]: s_dist[w] = alt hd[w] = s_dist[w] # update the priority value index += 1 return(s_dist)
def dijkstra_kernel(graph, start, previous, cost_fn): """ Generator for dijkstra search. Each iteration yields visited node id and cost to reach it from start node. :param graph: adjacency list of graph :param start: start node id :param previous: dictionary for storing settled nodes :param cost_fn: cost function applied for each edge, must be stateless """ queue = heapdict({start: 0.0}) previous.update({start: (None, 0.0, None)}) while queue: left, cost = queue.popitem() yield left, cost for right, payload in graph.get(left, []): alt_cost = cost + cost_fn(payload) # if there was no cost associated or cost is lower if right not in previous or alt_cost < previous[right][1]: queue[right] = alt_cost previous[right] = (left, alt_cost, payload)
def a_star(start, heuristic, stats): start_time = default_timer() closed = set() g_cost = {start: 0} f_cost = heapdict() f_cost[start] = g_cost[start] + heuristic(start) prev = {} # dict {state: (prev_state, move)} while f_cost: stats['states'] += 1 s, f = f_cost.popitem() if s == goal: moves = [] while s != start: s, move = prev[s] moves.append(move) stats['moves'] += len(moves) stats['time'] += default_timer()-start_time return moves[::-1] closed.add(s) g2 = g_cost[s] + 1 for move, s2 in adjanced(s): if s2 in closed: continue if s2 not in f_cost or g_cost[s2] > g2: g_cost[s2] = g2 f_cost[s2] = g2+heuristic(s2) prev[s2] = s, move stats['time'] += default_timer()-start_time return None
def likes_hist(like_users, all_likes_list): hd =heapdict() for like_id in like_users: key = all_likes_list[like_id] freq = len(like_users[like_id]) if freq > CUT_OFF: hd[key] = -freq return hd
def bootstrapHeap(G,s): h = hpdict.heapdict(); for head in T[s]: cost = G[head][s] h[head] = cost; #print heapToString(h) return h;
def test_popitem_ties(self): h = heapdict() for i in range(N): h[i] = 0 for i in range(N): k, v = h.popitem() self.assertEqual(v, 0) self.check_invariants(h)
def make_data(self): pairs = [(random.random(), random.random()) for i in range(N)] h = heapdict() d = {} for k, v in pairs: h[k] = v d[k] = v pairs.sort(key=lambda x: x[1], reverse=True) return h, pairs, d
def __init__(self, n, d, on_evict=None, weight=lambda k, v: 1): self.d = d self.n = n self.heap = heapdict() self.i = 0 if callable(on_evict): on_evict = [on_evict] self.on_evict = on_evict or [] self.weight = weight self.total_weight = 0 self.weights = dict()
def get_unshuffle_mapping(to_unshuffle, order): hd = heapdict() for x in xrange(to_unshuffle.size): hd[x] = -to_unshuffle[x] items_ordered = [] while hd.keys(): items_ordered.append(hd.popitem()) new_ordering = frac_ordering(order) unshuffle_mapping = [] for idx, member in enumerate(new_ordering): unshuffle_mapping.append((items_ordered[idx][0], member)) return unshuffle_mapping, items_ordered
def dijkstra(g, start_vert): hd = heapdict() shortest_paths = {start_vert: 0} add_new_verts_to_heap_dict(g, start_vert, hd, shortest_paths) while hd: (newmin_vert, score) = hd.popitem() shortest_paths[newmin_vert] = score add_new_verts_to_heap_dict(g, newmin_vert, hd, shortest_paths) return shortest_paths
def test_pop(self): # verify that we raise IndexError on empty heapdict empty_heap = heapdict() self.assertRaises(IndexError, empty_heap.pop) # test adding a bunch of random values at random priorities h, pairs, _ = self.make_data() last_priority = 0 while pairs: v1 = h.pop() (v2, priority) = pairs.pop() # confirm that our heapdict is returning things in sorted order self.assertEqual(v1, v2) # verify that our items are increasing in priority value self.assertGreater(priority, last_priority) last_priority = priority # make sure that we got everything out of the heap self.assertEqual(len(h), 0) # now verify that we raise KeyError if we try to remove something # by key that is not present empty_heap = heapdict() self.assertRaises(KeyError, empty_heap.pop, "missing") # verify that we do *not* get a KeyError if we specify a default empty_heap = heapdict() self.assertEqual(empty_heap.pop("missing", 123), 123) # confirm that we can get a value by key if it is present h = heapdict() h["foo"] = 10 self.assertEqual(len(h), 1) self.assertEqual(h.pop("foo"), 10) self.assertEqual(len(h), 0) # verify that removing keys does the right thing to a heap h = heapdict() h["c"] = 30 h["a"] = 10 h["b"] = 20 self.assertEqual(len(h), 3) self.assertEqual(h.pop("b"), 20) self.assertEqual(h.pop(), "a") self.assertEqual(h.pop(), "c") self.assertEqual(len(h), 0)
def __init__(self, iterable, G): """ Modularity holds {key: (0/1, priority)} pairs """ super(ModCommunities, self).__init__(iterable, G.k, G.A.diagonal()) self.modularity = heapdict() self.node_mods = {} self.changed = False self.network_modularity = 0.0 for i in iterable: q = modularity.single_node_modularity(G, i) self.modularity[i] = (0, q) self.node_mods[i] = q self.network_modularity += q
def pairs_hist(like_users, all_likes_list, all_likes): ## compute frequency for each item which is greater that threshold CUT_OFF likes_freq = likes_hist(like_users, all_likes_list) ## use only likes of high frequency to compute frequency pairs hd = heapdict() to_check = likes_freq.keys() while to_check: itm = to_check.pop() s = set(like_users[all_likes[itm]]) for other_itm in to_check: pair = (itm, other_itm) freq = len(s & (set(like_users[all_likes[other_itm]]))) if freq > CUT_OFF: hd[pair] = -freq return hd
def a_star(start_nodes, end_pos): "standard A* implementation" visited = set() parents = {} queue = heapdict() g_score = {} f_score = {} for node in start_nodes: g_score[node] = 0 f_score[node] = g_score[node] + (end_pos - node.pos).length() queue[node] = f_score[node] while queue: current = queue.popitem()[0] visited.add(current) if current.pos == end_pos: res = [] curr = current parent = parents.get(curr) while parent: res.append(parent.segment_to_neighbour(curr)) curr = parent parent = parents.get(curr) res.reverse() return res for neigh in current.neighbours(): tentative_g_score = g_score[current] + \ current.dist_to_neighbour(neigh) tentative_f_score = tentative_g_score + \ (end_pos - neigh.pos).length() if (neigh in visited) and (tentative_f_score >= f_score[neigh]): continue else: parents[neigh] = current g_score[neigh] = tentative_g_score f_score[neigh] = tentative_f_score queue[neigh] = f_score[neigh] return []
def aStar(aMap, goalNode): global noroutefound noroutefound = False global G G = aMap global openSet global closedSet global traversal traversal = list() global frontier frontier = list() #openSet = list() openSet = heapdict() startIndex = getIndexFromWorldPoint(pose.position.x, pose.position.y) openSet[startIndex] = G[startIndex].f # openSet.append(G[startIndex]) #Add first node to openSet # set priority to distance closedSet = list() #everything that has been examined print "start a*" print len(openSet) #print openSet[0].index while openSet: # true when openSet (the frontier) is not empty, if open set is empty, that means no path can be found try: # exception handler so that you can cntrl^c the program pop = openSet.popitem() i = pop[0] #find the node index/identifier of node in openSet with lowest travel cost current = G[i] if current in frontier: # this is for graphical representation in rviz frontier.remove(current) #print G[i].cameFrom if finalCheck(current.index, goalNode): # (current.index == goalIndex): # found the destination return reconPath(current, startIndex) #pass closedSet.append(current) # add current node to closedSet (list of visited nodes) adjCellList = adjCellCheck(current) # look at neihboring nodes and add them to openset except KeyboardInterrupt: pass print "No route to goal" noroutefound = True
def __init__(self, data=None, path=None, available_memory=None, dump=partial(pickle.dump, protocol=1), load=pickle.load, key_to_filename=key_to_filename, on_miss=_do_nothing, on_overflow=_do_nothing, open=open, open_many=_open_many, mode='b'): # In memory storage self.inmem = data or dict() # A set of keys held both in memory or on disk self._keys = {} # Was a path given or no? If not we'll clean up the directory later self._explicitly_given_path = path is not None # Diretory where the on-disk data will be held self.path = path or tempfile.mkdtemp('.chest') # Amount of memory we're allowed to use self.available_memory = (available_memory if available_memory is not None else DEFAULT_AVAILABLE_MEMORY) self.memory_usage = sum(map(nbytes, self.inmem.values())) # Functions to control disk I/O self.load = load self.dump = dump self.mode = mode self.open = open self.open_many = open_many self._key_to_filename = key_to_filename keyfile = os.path.join(self.path, '.keys') try: with self.open(keyfile, mode='r'+self.mode) as f: self._keys = dict(self.load(f)) except: if not os.path.exists(self.path): os.mkdir(self.path) self.lock = Lock() # LRU state self.counter = 0 self.heap = heapdict() # Debug self._on_miss = on_miss self._on_overflow = on_overflow
def matchAllFilesInMetaFile(metafile, directorycache, quick): metafileFiles = metafile.getMetafileFiles() # We can narrow down the search space by only considering DirectoryCacheFiles that are the same size potentialMatchesForMetafileFile = findPotentialMatches(metafileFiles, directorycache) if potentialMatchesForMetafileFile == False: log.warn("Skipping metafile because we couldn't find match for a file in metafile: {}".format(metafile)) return log.debug("Mapping metafile pieces to metafile files") log.debug("Match by hash:") piecesToVerify = metafile.getPieces() verifiedAnEntirePiece = set() log.debug("Resolving pieces in least-complex first order") # First we need a min-heap of the remaining pieces (compared by how many combinations they have) pieceHeap = heapdict() # TODO: This is nlog(n) construction, because there's no heapify constructor for piece in piecesToVerify: if piece.oneFileInPiece(): if not matchPieceToFiles(quick, piece, potentialMatchesForMetafileFile, verifiedAnEntirePiece): log.debug("Necessary pieces could not be verified. Skipping this metafile.") return else: #This is a more complicated piece. Solve it later. pieceHeap[piece] = getNumberOfCombinations(potentialMatchesForMetafileFile, piece) # TODO: This handles cross-piece files very poorly. For pieces that contain an entire file in them and # have multiple matches with a cross-piece file, if that cross-piece file has matches eliminated later, # we might select files for this piece that are wrong. # POSSIBLE SOLUTION: If a piece that contains multiple files in it has multuple matching combinations, # insert it into a "re-check" queue that will try to match it again after more pieces has been # 100% matched to a single combination of files # REASON THIS ISN'T FIXED: It's VERY unlikely. We'd have to have several hash collisions, which I don't # claim to resolve anyway try: pieceTuple = pieceHeap.popitem() except IndexError, e: pieceTuple = None
def dijkstra(W, seed_index): ''' Input: W: n by n scipy.sparse.csr_matrix Edge *symmetric* weight matrix. We use the scipy.sparse.csgraph convention that non-edges are denoted by non-entries. seed_index: the index of the starting vertex Output: distances: numpy array of size n holding the distance from seed_index to every vertex in the graph ''' assert type(W) == scipy.sparse.csr.csr_matrix (n, n_other) = W.shape assert n == n_other assert (W.data >= 0).all() assert 0 <= seed_index < n distances = np.empty(n) distances.fill(np.inf) visited = np.zeros(n, np.bool) heap = heapdict.heapdict() heap[seed_index] = 0.0 W_indptr = W.indptr W_indices = W.indices W_data = W.data global heap_pop_count while len(heap) > 0: (i, dist_i) = heap.popitem() heap_pop_count += 1 distances[i] = dist_i for pos in xrange(W_indptr[i], W_indptr[i+1]): j = W_indices[pos] if distances[j] == np.inf: if j not in heap: heap[j] = dist_i + W_data[pos] else: alt_dist = dist_i + W_data[pos] if alt_dist < heap[j]: heap[j] = alt_dist return distances
def dijkstra(G, s): initialize(G,s) q = heapdict() for v,d in G.nodes_iter(data=True): q[v] = d['distance'] done = set() while len(q) > 0: u,d = q.popitem() done.add(u) for v in G.edge[u]: if v not in done: relax(G,u,v) q[v] = G.node[v]['distance'] for v in G.nodes_iter(): prnt = G.node[v]['parent'] # prime example where is not None is important if prnt is not None: G.node[prnt]['children'].add(v)
def aStar(start,end,circle_map,neighbours): visited = set() parents = {} queue = heapdict() g_score = {} f_score = {} g_score[start] = 0 f_score[start] = g_score[start] + (start - end).length() queue[start] = f_score[start] while queue: current = queue.popitem()[0] visited.add(current) if current == end: res = [] curr = current while parents.get(curr): res.append(curr) curr = parents[curr] res.append(curr) res.reverse() return res for neigh in neighbours[current]: tentative_g_score = g_score[current] + edge_length(current,neigh,circle_map,parents) tentative_f_score = tentative_g_score + (neigh - end).length() if (neigh in visited) and (tentative_f_score >= f_score[neigh]): continue if (not (neigh in visited)) or (tentative_f_score < f_score[neigh]): parents[neigh] = current g_score[neigh] = tentative_g_score f_score[neigh] = tentative_f_score queue[neigh] = f_score[neigh] return []
def expandClusterOrder(self, i): heap = heapdict() #heap = priority_dict() heap[i] = sys.float_info.max #float('inf') while heap: #logging.info('heap {}'.format( ','.join("({},{}) ".format(k,v) for k,v in heap.iteritems()))) currentId, currentReachability = heap.popitem() #currentId, currentReachability = heap.pop_smallest() #logging.info('processing: {} {} {}'.format(i, currentId, currentReachability)) self.clusterOrder.append(ClusterEntry(i=currentId, reachability=currentReachability)) if currentId in self.processedIds: logging.info('currentId in processedIds {}'.format(currentId)) self.processedIds.add(currentId) neighbours = self.getNearestNeighbours(currentId) n_neighbours = len(neighbours) if n_neighbours > 0 and n_neighbours > self.minPts: #coreDistance2 = neighbours[-1].distance2 nn_last = neighbours[self.minPts] #coreDistance = neighbours[self.minPts-1].distance coreDistance = self.getDistance(self.nodes[currentId], self.nodes[nn_last]) #logging.info('neighbours: {} {} -> {}'.format(n_neighbours, coreDistance2, neighbours)) for nn in neighbours: #logging.info('processedIds: {} {}'.format( (nn.id in self.processedIds), self.processedIds)) if nn == currentId or nn in self.processedIds: continue nn_distance = self.getDistance(self.nodes[currentId], self.nodes[nn]) newReachability = max(nn_distance, coreDistance) oldReachability = heap.get(nn, None) if oldReachability is None or newReachability < oldReachability: heap[nn] = newReachability return
def aStar(start,goal): visited = set() parents = {} queue = heapdict() g_score = {} f_score = {} g_score[start] = 0 f_score[start] = g_score[start] + start.heuristic(goal) queue[start] = f_score[start] while queue: current = queue.popitem()[0] visited.add(current) if current == goal: res = [] curr = current while parents.get(curr): res.append(curr) curr = parents[curr] res.reverse() return res for neigh in current.neighbours(): tentative_g_score = g_score[current] + current.edgeLength(neigh) tentative_f_score = tentative_g_score + neigh.heuristic(goal) if (neigh in visited) and (tentative_f_score >= f_score[neigh]): continue if (not (neigh in visited)) or (tentative_f_score < f_score[neigh]): parents[neigh] = current g_score[neigh] = tentative_g_score f_score[neigh] = tentative_f_score queue[neigh] = f_score[neigh] return None
def constructBPT(image, baseAdjacency, computeFusionWeights, preMergeFlatZone=False): adjacency = baseAdjacency.getCopy(optimizedRemove=True, copyData=True) parent = [-1] * len(image) level = [0] * len(image) active = [False] * len(adjacency) inEdges = [] for _ in range(len(image)): inEdges.append([]) heap = heapdict.heapdict() __initHeap(image, adjacency, heap, active) while len(heap) > 0: fusionEdge, weight = heap.popitem() if active[fusionEdge]: newParent = adjacency.addVertex() region1, region2, weight = adjacency.getEdgeFromIndex(fusionEdge) parent[region1] = newParent parent[region2] = newParent parent.append(-1) level.append(weight) inEdges.append([]) adjacency.removeEdge(fusionEdge) deletedEdges = adjacency.edgeList[region1] | adjacency.edgeList[region2] neighbourEdges = __findNeighbours(adjacency, deletedEdges, region1, region2, active, inEdges) if len(neighbourEdges) > 0: __computeNewEdges(image, adjacency, neighbourEdges, fusionEdge, computeFusionWeights, newParent, heap, active, inEdges) return Tree(TreeType.PartitionHierarchy, parent, level, image)
def dijkstra_distance(self, player, source, destination): """ Return the two distance between source and destination for player. The two distance is 0 if source = destination, 1 if source is adjacent to destination, and 1 + the second smallest two distance between source and destination's neighbors otherwise. """ cell_set = heapdict() second = {} for cell in self.empty_cells(): cell_set[cell] = float("INF") second[cell] = float("INF") for edge in self.EDGES: cell_set[edge] = float("INF") second[cell] = float("INF") cell_set[source] = 0 second[source] = 0 while cell_set: cell, distance = cell_set.popitem() if cell == destination: return second[cell] for neighbor in self.connected_neighbors(cell, player): if neighbor not in cell_set: continue if cell == source: cell_set[neighbor] = 1 second[neighbor] = 1 else: alternate = distance + 1 if alternate <= cell_set[neighbor]: second[neighbor] = cell_set[neighbor] cell_set[neighbor] = alternate return second[destination]
def shortest(n, edges): """ Given an undirected graph, find the shortest path from source (node 0) to target (node n-1). """ heap_queue = heapdict({i: float("inf") for i in xrange(n)}) neighbor, prev = defaultdict(list), defaultdict(lambda: -1) target = n - 1 heap_queue[0] = 0 for a, b, cost in edges: neighbor[a].append((b, cost)) neighbor[b].append((a, cost)) while heap_queue: node, dist = heap_queue.popitem() if node == target: return dist, back_trace(prev, target) for adj_v, cost in neighbor[node]: tmp = dist + cost if adj_v in heap_queue and tmp < heap_queue[adj_v]: heap_queue[adj_v] = tmp prev[adj_v] = node
def merge_bicliques(bicliques, max_overlap=0.55): """Greedily merge biclique set B by maximum item overlap. TODO: add minimum merged density constraint. Args: bicliques: [(row, col)], row=[int], col=[int] of bicliques to merge max_overlap: float 0 <= x <= 1 of maximum allowable overlap of items without merge Returns: {} """ # Calculate all pairs overlap # a symmetric distance matrix that I can update # a max heap to keep track of most overlapping biclique pair B = dict(enumerate(bicliques)) # uniquely index bicliques, but allow constant access D = heapdict() for i in B: for j in filter(lambda j: j > i, B): # higher percentage overlap is lower "priority" D[tuple(sorted((i, j)))] = -get_overlap(B[i], B[j]) # Merge top pair while it is above threshold pair, overlap = D.popitem() while -overlap > max_overlap: i, j = sorted(pair) B[i] = (B[i][0] | B[j][0], B[i][1] | B[j][1]) del B[j] # Update all distances including i, delete those with j for k in filter(lambda k: k != i and k != j, B): D[tuple(sorted((i, k)))] = -get_overlap(B[i], B[k]) # distance to merged biclique i del D[tuple(sorted((j, k)))] # biclique j no longer exists if len(D): pair, overlap = D.popitem() # get next pair else: break return B.values()
def prim(graph, start): mst, keys, pi, total_weight = list(), heapdict(), dict(), 0
ranks = leaderboard.calculate_ranks(our_scores, by_input) except IOError: print("Failed reading from saved scores!") for file in os.listdir(path): if "." in file and file.split(".")[1] == "in": name = file.split('.')[0] infile = f"{path}/{file}" outfile = f"{path}/out/{name}.out" print(f"Running {name}") score = runfile(infile, outfile) if score < our_scores[name]: print(f"- Improved score from {our_scores[name]} to {score} (-{our_scores[name] - score})") our_scores[name] = score raise KeyboardInterrupt priorities = heapdict.heapdict({key: calc_priority(our_scores[key], key, top_scores, ranks) for key in our_scores}) print("Running in order of priority") try: while True: name, p = priorities.peekitem() to_beat = our_scores[name] print(f"Running {name} with priority {p} (our score {to_beat}, top score {top_scores[name]}, our rank {ranks[name]})") score = runfile(f"{path}/{name}.in", f"{path}/out/{name}.out", score_to_beat=to_beat) if score < to_beat: print(f"+ Improved score from {to_beat} to {score} (-{to_beat - score})") our_scores[name] = score ranks[name] = leaderboard.calculate_rank(score, name, by_input) priorities[name] = calc_priority(score, name, top_scores, ranks) with open(sys.argv[2], "w") as f: f.write(json.dumps(dict(our_scores)))
while len(chars) > 1: l = chars.popitem() r = chars.popitem() for i in l[0]: code[i] += '0' deep[i] += 1 for i in r[0]: code[i] += '1' deep[i] += 1 z = l[0] + r[0] zfreq = chars[z] = l[1] + r[1] chars[z] = zfreq if __name__ == '__main__': chars = heapdict.heapdict() code = {} deep = {} for c in 'mississippi': if c in chars: chars[c] += 1 else: chars[c] = 1 code[c] = '' deep[c] = 0 freq = {} for i in chars: freq[i] = chars[i]
def __init__(self, max_count=10000): self.max_count = max_count self.store = heapdict.heapdict() self.stats = {"hit": 0, "miss": 0, "bad_input": 0, "bad_output": 0}
def __init__(self, values=[]): self.counter = 0 self.heap = heapdict.heapdict() for value in values: self.add(value)
q[adj] = c + w visited.add(v) n, e = map(int, input().split()) graph = collections.defaultdict(list) dis = [float('inf')] * n dis[0] = 0 p = [-1] * n visited = set() for _ in range(e): u, v, w = map(int, input().split()) graph[u - 1].append((v - 1, w)) graph[v - 1].append((u - 1, w)) q = hd.heapdict() q[0] = 0 #print(graph) dijkstra(q, dis, p, visited, graph) #print('parent',p) #print('here',n,p[n-1]) if p[n - 1] == -1: print(-1) else: n -= 1 res = [] while n != -1: res.append(n + 1) n = p[n] print(*res[::-1])
def a_star_multi(graph, agents, add_to_visgraph, heuristic=lambda x, y: 0): frontiers = {} g_scores = {} Ps = {} locations = {} accum_dists = {} for (origin, destination) in agents: frontiers[origin] = heapdict() g_scores[origin] = {} Ps[origin] = {} Ps[origin][origin] = origin locations[origin] = origin frontiers[origin][origin] = 0 g_scores[origin][origin] = 0 accum_dists[origin] = 0 # Each timestep of A* agents_copy = list(agents) while len(agents_copy) > 0: occupied = set() # comment this out for sort agents via f score agents_copy.sort(reverse=True, key=lambda x: accum_dists[x[0]] + edge_distance(x[1], locations[x[0]])) # or comment this out for shuffle randomly #random.shuffle(agents_copy) for (origin, destination) in agents_copy: frontier = frontiers[origin] v = None while frontier: v, _ = frontier.popitem() if v not in occupied: break # if no paths, remove agent from agents if v is None: Ps[origin] = None agents_copy.remove((origin, destination)) continue occupied.add(v) locations[origin] = v P = Ps[origin] accum_dists[origin] += edge_distance(v, P[v]) # if found result, remove agent from agents if v == destination: agents_copy.remove((origin, destination)) # else expand edges = graph[v] if add_to_visgraph != None and len(add_to_visgraph[v]) > 0: edges = add_to_visgraph[v] | graph[v] g_score = g_scores[origin] for e in edges: w = e.get_adjacent(v) new_score = g_score[v] + edge_distance(v, w) if w not in g_score or new_score < g_score[w]: g_score[w] = new_score h = heuristic(w, destination) # comment this block out for look_ahead heuristics: ''' nbs = neighbors(w, add_to_visgraph[w] | graph[w]) if destination not in nbs: turn = False for x in nbs: if destination in neighbors(x, add_to_visgraph[x] | graph[x]): turn = True break if turn: h *= 1.4 else: h *= 2 ''' frontier[w] = (new_score + h, w) P[w] = v return Ps
pk.dump(CITY_STATE_GRAPH, open(f"prebuilt_graphs/city_state_graph_m{m}.pk", "wb")) print("Graph Creation Complete") time_betn = lambda x, y: CITY_STATE_GRAPH[x][y] # hrs # Get Start City State START_CITY_STATE = [ CITY_STATE(CITY(*d), MAXCHARGE) for d in NetworkSpec if d[0] == START_CITY_NAME ][0] # Initialize Variables print("Node Count:", node_count := len(CITY_STATE_GRAPH), "Edge Count:", edge_count := sum([len(n) for n in CITY_STATE_GRAPH])) shortest_time_heap, shortest_time_to = heapdict(), defaultdict( lambda: float('inf')) shortest_time_heap[START_CITY_STATE], shortest_time_to[START_CITY_STATE] = 0, 0 prev_city = defaultdict(lambda: "unknown") visited = defaultdict(lambda: False) # Time complexity calculated for HeapQ implementation of the dictionary. def get_nearest_unvisited_city(): return shortest_time_heap.popitem()[0] if shortest_time_heap else False # ------------------------------------ PseudoCode Start ------------------------------------ # Define Update Function def update_shortest_path(city): for neighbor in CITY_STATE_GRAPH[city]:
from Graph import Graph from heapdict import heapdict from Results import makePartition from pFunctionC import * import sys ############# GLOBAL VARIABLES ############ d1, d2 = heapdict(), heapdict() # heapdict {node index: pF} C1, C2 = [], [] # subsets of the generalized two-mode core pValues1, pValues2 = {}, {} # lists of property function values ############## REMOVAL FROM MIN-HEAP INSIDE MAIN LOOP ############## def RemoveFromHeap(dRemove, removing, t, CRemove, network, pValues, COther, dOther, pC, mode): # Save size of min-heap before removal starts length = len(dRemove) while True: # Check if the removal of nodes is done: the condition # for the end is empty min-heap if len(CRemove) == 0: # If the min-heap is empty before this while loop, # no node was removed if length == 0: removing = False # Else at least one node was removed else: removing = True break # The variable top is the node with the smallest value # of the property function in the min-heap. top = dRemove.peekitem() # If the node top suffice the condition we end the removing of nodes if top[1] >= t:
def run( self, collisions=10, time=0.001, *, animate=False, dist_centre=False, dist_rel=False, speed=False, pressure=False, dataset=False, test_pressure=False, KE=False, test_temperature=False, temperature=False, brownian=False, progress=True, ): """ Runs the 2D rigid disc particle collision simulation. Parameters: collisions (int, optional): Number of collisions in the simulation. time (float, optional): Time period between animation frames. animate (boolean, optional): If True, produces animation. dist_centre (boolean, optional): If True, records distances of all balls from origin for all collisions. dist_rel (boolean, optional): If True, records relative distances between all balls for all collisions. speed (boolean, optional): If True, records speeds of all balls for all collisions. pressure (boolean, optional): If True, records pressure for every 100 collisions with container. dataset (boolean, optional): If True, records dataset of all information of the simulation. test_pressure (boolean, optional): If True, records average pressure of the system. KE (boolean, optional): If True, records kinetic energy of the system for every collision. test_temperature (boolean, optional): If True, records temperature of the system for every collision. temperature (boolean, optional): If True, records average temperature of the system. brownian (boolean, optional): If True, records data for Brownian Motion investigation. progress (boolean, optional): If True, displays the progress bar. Returns: d_output (dict): Dictionary that contains the required datasets. """ self._global_time = 0 self._pq = hd.heapdict() self._collisions = collisions print( f"starting {self._N_ball} balls, r_ball = {self._r_ball}, speed range = {self._vel_range}, r_container = {self._r_container}, {self._collisions} collisions" ) if dataset: self._dataset = np.zeros( (self._N_ball * (self._collisions + 1), 9)) # Initialising animation if animate: self.init_patch() plt.figure(num="Simulation Animation") plt.rcParams.update(plt.rcParamsDefault) ax = plt.axes( xlim=(-self._r_container, self._r_container), ylim=(-self._r_container, self._r_container), aspect="equal", ) ax.add_patch(self._c_outline) for patch in self._b_patch: ax.add_patch(patch) plt.pause(time) if brownian: self.record_brownian() self.record_data_states( dist_centre=dist_centre, speed=speed, KE=KE, test_temperature=test_temperature, temperature=temperature, dist_rel=dist_rel, dataset=dataset, ) # Running first collision self.init_collision_time() self.init_next_event() self.move_balls() self._global_time = self._min_dt if animate: self.update_patch() if brownian: path = self.trace_brownian() ax.add_line(path) plt.pause(time) self.collide_balls(pressure, test_pressure, brownian) self.record_data_states( dist_centre=dist_centre, speed=speed, KE=KE, test_temperature=test_temperature, temperature=temperature, dist_rel=dist_rel, dataset=dataset, ) if progress: self._time_epoch = tm.time() for i in range(2, collisions + 1): if progress: progress_bar(self._time_epoch, i, collisions) self.collision_time() self.next_event() self.move_balls() self._global_time = self._min_dt if animate: self.update_patch() if brownian: path = self.trace_brownian() ax.add_line(path) plt.pause(time) self.collide_balls(pressure, test_pressure, brownian) self.record_data_states( dist_centre=dist_centre, speed=speed, KE=KE, test_temperature=test_temperature, temperature=temperature, dist_rel=dist_rel, dataset=dataset, ) if animate: plt.show() if temperature: self.record_average_temperature() self.record_data_pressures(pressure=pressure, test_pressure=test_pressure) if brownian: self.record_brownian(df=True) d_output = self.append_data( dist_centre=dist_centre, dist_rel=dist_rel, test_pressure=test_pressure, speed=speed, KE=KE, test_temperature=test_temperature, temperature=temperature, pressure=pressure, dataset=dataset, brownian=brownian, ) print( f"end of {self._N_ball} balls, r_ball = {self._r_ball}, speed range = {self._vel_range}, r_container = {self._r_container}, {self._collisions} collisions" ) return d_output
def main(): data = pd.read_csv('user-ct-test-collection-01.txt', encoding='utf-8', sep="\t") #data = pd.read_csv('test.txt', encoding='utf-8', sep="\t") querys = data["Query"].dropna() token_dict = {} for token in querys: arr = token.split(' ') for a in arr: if a not in token_dict: token_dict[a] = 1 else: token_dict[a] += 1 inFreq = findInFreq(token_dict) Freq = findMostFreq(token_dict) randFreq = findRandomFreq(token_dict) plotError_Freq(inFreq, token_dict) plotError_Freq(Freq, token_dict) plotError_Freq(randFreq, token_dict) """ sketches with heaps Freq has contains actual top-100 TODO: maintain a top-1000 coutns in a minheap """ x_list = [2**10, 2**12, 2**14, 2**16, 2**18] y_list = [] y_list1 = [] dataset = [] for query in querys: arr = query.split(' ') for a in arr: dataset.append(a) for R in x_list: # heapdict + dict来做. dict充当minheap, heapdict充当minheap里面的计数 sketch = CountSketch(4, R) # min heap dict = {} hd = heapdict.heapdict() for token in dataset: sketch.add(token) """如果存在minheap中,那么就更新它的次数""" if token in dict.keys(): dict[token] += 1 hd[token] = dict[token] else: """如果元素不在minheap中,那么下面继续判断数量是否超过1000""" if len(dict) < 1000: # 直接加入到minheap中 dict[token] = 1 hd[token] = dict[token] else: """the number of elements in minheap is more than 1000""" est_val = sketch[token] minElem = hd.popitem() if est_val < minElem[1]: # keep the same dict[minElem[0]] = minElem[1] hd[minElem[0]] = minElem[1] else: """have to update the minheap""" #fixed the bug, in the beginning, forget to delete element in minheap dict.pop(minElem[0]) dict[token] = est_val hd[token] = dict[token] count = 0 for key, value in dict.items(): if key in Freq.keys(): count += 1 y_list.append(count) for R in x_list: minsketch = CountMinSketch(4, R) # min heap dict = {} hd = heapdict.heapdict() for token in dataset: minsketch.add(token) """如果存在minheap中,那么就更新它的次数""" if token in dict.keys(): dict[token] += 1 hd[token] = dict[token] else: if len(dict) < 1000: # 直接加入到minheap中 dict[token] = 1 hd[token] = dict[token] else: """elements in minheap are more than 1000""" est_val = minsketch[token] minElem = hd.popitem() if est_val < minElem[1]: # keep the same dict[minElem[0]] = minElem[1] hd[minElem[0]] = minElem[1] else: """have to update the minheap""" #fixed the bug, in the beginning, forget to delete element in minheap dict.pop(minElem[0]) dict[token] = est_val hd[token] = dict[token] count = 0 for key, value in dict.items(): if key in Freq.keys(): count += 1 # print(count) y_list1.append(count) x_list_str = [] for x in x_list: x_list_str.append(str(x)) plt.figure(figsize=(10, 10)) plt.xlabel('R-value', fontsize=24) plt.ylabel('Intersection Size', fontsize=24) plt.xticks(fontsize=12) plt.yticks(fontsize=20) plt.plot(x_list_str, y_list, "x-", label="Count_sketch") plt.plot(x_list_str, y_list1, "+-", label="Count_Min_sketch") plt.legend(loc='lower right') plt.show()
def cheapest_attack_path(self, source, target): ''' This function uses Dijkstra's algorithm to calculate a cheapest valid attack path between two territories if such a path exists. There may be multiple valid cheapest attack paths (in which case it doesn't matter which this function returns), or there may be no valid attack paths (in which case the function returns None). Args: source (int): territory_id of source node target (int): territory_id of target node Returns: [int]: a list of territory_ids representing the valid attack path; if no path exists, then it returns None instead ''' ''' Create a dictionary whose keys are territories and values are path Set dictionary[source] = [source] ++ Create a PRIORITY queue ++ Enqueue source onto the PRIORITY queue WITH PRIORITY 0 Create a set of visited territories Add source to the set ++ While the PRIORITY queue is not empty ++ Dequeue current_territory from the PRIORITY queue If current_territory is the target return the dictionary[current_territory] For each territory in the neighbors of current_territory that is not in the visited set Make a copy of dictionary[current_territory] Push territory onto the copy ++ CALCULATE THE PRIORITY OF THE PATH AS PRIORITY OF CURRENT_TERRITORY + NUMBER OF ARMIES ON TERRITORY ++ IF TERRITORY NOT IN THE PRIORITY QUEUE Set dictionary[current_territory] = copy + territory ++ Enqueue territory WITH PRIORITY ++ ELSE, IF THE NEW PRIORITY IS LESS THEN THE PRIORITY IN THE QUEUE Set dictionary[current_territory] = copy + territory ++ UPDATE THE TERRITORY'S PRIORITY IN THE PRIORITY QUEUE WITH THE NEW PRIORITY Add current_territory to the visited set''' if not self.can_attack(source, target): return None territories = {} territories[source] = [source] pq = heapdict.heapdict() pq[source] = 0 visited = [source] player_id = self.owner(source) while pq: (cur_ter, cur_ter_priority) = pq.popitem() board_info = [ country for country in self.neighbors(cur_ter) if (country not in visited and self.owner(country) != player_id ) ] for territory in board_info: if territory == target: path = territories[cur_ter] path.append(territory) return path copy_path = copy.deepcopy(territories[cur_ter]) copy_path.append(territory) priority = self.armies(territory) + cur_ter_priority if territory not in pq: territories[territory] = copy_path pq[territory] = cur_ter_priority + self.armies(territory) elif priority <= pq[territory]: territories[territory] = copy_path pq[territory] = priority visited.append(cur_ter)
def __init__(self): self.latest = 0 self.edges = {} self.queue = heapdict()
def __init__(self): self.h = heapdict()
out_f.write(header) for lst in tree: line = ' '.join(str(el) for el in lst) + '\n' out_f.write(line) #################################### # Test #################################### if __name__ == '__main__': # Test TreeNode RD = np.random.random(10) start_1 = 0 end_1 = 6 LM_1 = heapdict.heapdict(zip(range(start_1, end_1), RD[start_1:end_1])) start_1_1 = 0 end_1_1 = 3 LM_1_1 = heapdict.heapdict( zip(range(start_1_1, end_1_1), RD[start_1_1:end_1_1])) start_1_2 = 3 end_1_2 = 6 LM_1_2 = heapdict.heapdict( zip(range(start_1_2, end_1_2), RD[start_1_2:end_1_2])) hr_node_1 = TreeNode(start_1, end_1, LM_1) hr_node_1_1 = TreeNode(start_1_1, end_1_1, LM_1_1) hr_node_1_2 = TreeNode(start_1_2, end_1_2, LM_1_2)
e = np.where(data == str('KS')) eR = int(e[0]) eC = int(e[1]) # Declare arrays. Mark unexplored distances as inf in pursuit of the best path dist = np.array([[float('inf') for x in range(numC)] for y in range(numR)]) visited = np.array([[False for x in range(numC)] for y in range(numR)]) prev = {(x, y): 0 for x in range(numC) for y in range(numR)} # Provide cardinal directions dr = [-1, +1, 0, 0] dc = [0, 0, +1, -1] # Add priority queue data structure. Use key-value pairs (node index, dist) hd = heapdict() # Add first node and mark it visisted with no distance weight dist[sR, sC] = 0 hd[sR, sC] = 0 visited[sR, sC] = True reached_end = False def explore(r, c): for i in range(0, 4): rr = r + dr[i] cc = c + dc[i] if rr < 0 or cc < 0: continue
def expand_when_stuck(net1, net2, seeds): marks = heapdict() imp_t, imp_h = set(), set() unused, used, matches = set(seeds[:]), set(), set() net_curried = lambda x: net_degree_dist(net1, net2, x) def add_neighbor_marks(pair, disp=False): ct = 0 if disp: print "add_neighbor_marks pair: ", pair for neighbor in itertools.product(net1.neighbors(pair[0]), net2.neighbors(pair[1])): ct += 1 if ct % 100000 == 0 and disp: print "add_neighbor_marks ct: ", ct if neighbor[0] in imp_t or neighbor[1] in imp_h: continue if neighbor in marks: marks[neighbor] -= 1 else: marks[neighbor] = -1 def get_new_unused(match_list): new_unused = set() for match in match_list: for neighbor in itertools.product(net1.neighbors(match[0]), net2.neighbors(match[1])): if neighbor in used: continue if neighbor[0] in imp_t: continue if neighbor[1] in imp_h: continue new_unused.add(neighbor) return new_unused def get_new_matches(marks, used): new_matches = set() while len(marks) and marks.peekitem()[1] < -2: curr_pair = marks.popitem()[0] if curr_pair in used: continue if curr_pair[0] in imp_t: continue if curr_pair[1] in imp_h: continue imp_t.add(curr_pair[0]) imp_h.add(curr_pair[1]) marks[curr_pair] = 0 new_matches.add(curr_pair) print "newmatch len, peekval, currpair: ", len(new_matches), marks.peekitem(), curr_pair add_neighbor_marks(curr_pair) used.add(curr_pair) return marks, used, new_matches print "begin stage 0" while unused: print "len unused: ", len(unused) for curr_pair in unused: add_neighbor_marks(curr_pair, disp=True) used.add(curr_pair) marks, used, new_matches = get_new_matches(marks, used) matches = matches.union(new_matches) print "matches in actuality: ", len(matches) unused = get_new_unused(matches) print "matches in actuality, fin: ", len(matches) return list(matches)
from heapdict import heapdict lines = readInput("input_15.txt") xmax = len(lines[0]) ymax = len(lines) cavern = np.zeros(shape=(xmax, ymax)) for y in range(ymax): for x in range(xmax): cavern[(x, y)] = int(lines[y][x]) start = (0, 0) end = (xmax - 1, ymax - 1) neighs = [(0, -1), (0, 1), (-1, 0), (1, 0)] shortest = np.ones(shape=(xmax, ymax)) * -1 shortest[0, 0] = 0 que = heapdict() que[start] = 0 visited = set() while que: node, dist = que.popitem() visited.add(node) for n in neighs: neigh = (node[0] + n[0], node[1] + n[1]) if (0 <= neigh[0] < xmax and 0 <= neigh[1] < ymax and neigh not in visited): new_dist = shortest[node[0], node[1]] + cavern[neigh[0], neigh[1]] if new_dist < shortest[neigh[0], neigh[1]] or shortest[neigh[0], neigh[1]] == -1:
i = i + 1 else: for j in range(i+1, len(msg)): if msg[i:j] in decoder.keys(): d_msg = d_msg + decoder[msg[i:j]] i = j break return d_msg if __name__== "__main__": # frequencies of letters as an array of tuples freq = [('A',5),('B',2), ('R',2), ('C',1), ('D',1)] h = heapdict.heapdict() # we enter the chars on the heap on the order they appear # on the incoming string to break ties (?), this is the guideline from hacker rank problem for f in reversed(freq): n = Node(f[1], f[0], True) h[n] = n.prob root = build_huffman(h) print printTree(root, '') buildDecoder(root, []) print decoder print decode("01111001100011010111100")
def optics(X, eps, minpts): """ Cluster analysis using OPTICS algorithm. X: a PointCloud class that represent pos data structure eps: epsilon is the largest search distance in searching for minpts-th neighbors. minpts: minpts-th neighbor (contains itself) for defining core distance. minpts is in range [1, inf). where minpts=1 means the center ion itself minpts=2 means the 1st neighbor of center ion minpts=3 means the 2nd neighbor of center ion ... return: ordered_lst, an numpy array of ordered index of all ions. reachability_distance, a numpy array of RD for corresponding data in X. (order should be preserved, such as RD at idx=0 is for the first point in X) core_distance, a numpy array of RD for corresponding data in X. (order should be preserved, such as RD at idx=0 is for the first point in X) """ #################################-------------------------################## # A small helper functions Here!!!!!!!!!!!!!!!! #################################-------------------------################## def set_core_distance(X, idx, eps, minpts): """ Set the core distance of point with index idx with respect to eps, minpts. Note: the minpts has to be at least 2 to be meaningful in this implementation \ (cause minpts=1 returns the idx point itself). If minpts is 1 or there is no other \ points within eps, the core distance will be remained as Nan as default. """ nonlocal core_distance dist, _ = X.query_neighbors(idx, minpts) if dist[-1] < eps and len(dist) > 1: core_distance[idx]=dist[-1] ###################################--------------------#################### # A more important helper function ####################################-------------------#################### def update(X, current_idx, seeds, eps, minpts): """ Helper function to optics. """ nonlocal core_distance nonlocal reachability_distance nonlocal processed neighbor_dist, neighbor_idx = X.query_radius(current_idx, eps) core_dist = core_distance[current_idx] for item in range(len(neighbor_idx)): N_idx = neighbor_idx[item] if not processed[N_idx]: new_reach_dist = np.maximum(core_dist, neighbor_dist[item]) # np.nan > any number if np.isnan(reachability_distance[N_idx]): reachability_distance[N_idx] = new_reach_dist seeds[N_idx] = new_reach_dist else: if new_reach_dist < reachability_distance[N_idx]: reachability_distance[N_idx] = new_reach_dist seeds[N_idx] = new_reach_dist return ###################################--------------------#################### # Helper function ends here ####################################-------------------#################### num_data = X.get_number_of_data() processed = np.zeros(num_data, dtype=bool) reachability_distance = np.empty(num_data) reachability_distance.fill(np.nan) core_distance = np.empty(num_data) core_distance.fill(np.nan) ordered_lst = np.empty(num_data, dtype=int) ordered_lst.fill(np.nan) count = 0 for idx in range(num_data): if not processed[idx]: processed[idx] = True set_core_distance(X, idx, eps, minpts) ordered_lst[count] = idx # Track progress count += 1 percentage = count/num_data if np.mod(percentage, 10) == 0: print('OPTICS progress:', '{:.0%}'.format(percentage)) if not np.isnan(core_distance[idx]): seeds = heapdict.heapdict() update(X, idx, seeds, eps, minpts) while len(seeds) != 0: current_idx = seeds.popitem()[0] # if current_idx == 25: # the priority queue heapdict does not preserve the order of elements somehow, # # for example, in the old implementation 9 is returned while in the new one # # 25 is returned, even though nothing related with heapdict changed. This may # # cause some randomness in the output but should not affect the result. Though # # could be troublesome for unit testing. # print('25 RD is', reachability_distance[25]) # print('9 RD is', reachability_distance[9]) processed[current_idx] = True set_core_distance(X, current_idx, eps, minpts) ordered_lst[count] = current_idx # track progress count += 1 percentage = count/num_data*100 if np.mod(percentage, 10) == 0: print('OPTICS progress:', '{}%'.format(percentage)) if not np.isnan(core_distance[current_idx]): update(X, current_idx, seeds, eps, minpts) return ordered_lst, reachability_distance, core_distance
def ucs(grid, h, start, goal): path = [] path_cost = 0 heapDictionary = heapdict.heapdict( ) # justification: heap dictionary is a way to combine the logics of data structures priority queue and key-value pair logic of dictionary heapDictionary[start] = 0 visited = set(start) branch = {} found = False while heapDictionary: item = heapDictionary.popitem() current_node = item[0] if current_node == start: current_cost = 0.0 else: current_cost = branch[current_node][0] if current_node == goal: print('Found a path.') found = True break else: for action in valid_actions(grid, current_node): # get the tuple representation da = action.delta next_node = (current_node[0] + da[0], current_node[1] + da[1]) branch_cost = current_cost + action.cost if next_node not in visited: visited.add(next_node) branch[next_node] = (branch_cost, current_node, action) heapDictionary[next_node] = branch_cost elif next_node in heapDictionary.keys( ) and branch_cost < heapDictionary[ next_node]: #found a better (lower cost) path to a node => update the stored cost and the path print("node encountered again....path is better...cost" + branch_cost + "<" + heapDictionary[next_node]) heapDictionary[next_node] = branch_cost branch[next_node] = (branch_cost, current_node, action) if (next_node == goal): print("A path to goal found with cost={}".format( branch_cost)) if found: # retrace steps n = goal path_cost = branch[n][0] path.append(goal) while branch[n][1] != start: path.append(branch[n][1]) n = branch[n][1] path.append(branch[n][1]) else: print('**********************') print('Failed to find a path!') print('**********************') return path[::-1], path_cost
def search(self, sent, verbose=False): adjenda = heapdict() # grammar dicts gr_right = defaultdict(list) for item in self._grammar: gr_right[item[1]].append(item) # initial arcs init_arcs = defaultdict(set) for lh, word, pr in self.terminals(self._grammar): init_arcs[word].add((lh, word, pr)) # initialize pool = set([]) for i, w in enumerate(sent): for lh, word, pr in init_arcs[w]: hyp1 = Arc(lh, tuple([]), tuple([word]), i, i, pr) hyp2 = Arc(word, tuple([]), tuple([]), i, i + 1, 1) prob = hyp1.prob * hyp2.prob new_arc = hyp1 + hyp2 adjenda[new_arc] = prob pool.add(new_arc) # initialize chart chart = defaultdict(set) chart_init = defaultdict(lambda: defaultdict(set)) chart_after = defaultdict(lambda: defaultdict(set)) # main loop while adjenda.items(): arc, _ = adjenda.popitem() print arc.pretty_print(sent, size=10, verbose=verbose).encode('utf-8') # add to chart # Use set to remove deplicated items chart[(arc.start, arc.end)].add(arc) chart_init[arc.lhs][(arc.start, arc.end)].add(arc) if arc.after: chart_after[arc.after[0]][(arc.start, arc.end)].add(arc) # active edge if arc.after: y = arc.after[0] for (s, e), arcs in [((s, e), arcs) for (s, e), arcs in chart_init[y].items() if s == arc.end]: for hyp in [hyp for hyp in arcs if not hyp.after]: con = arc + hyp if con not in pool: adjenda[con] = arc.prob * hyp.prob pool.add(con) else: for (s, e), arcs in [ ((s, e), arcs) for (s, e), arcs in chart_after[arc.lhs].items() if e == arc.start ]: for hyp in [hyp for hyp in arcs if hyp.after]: con = hyp + arc if con not in pool: adjenda[con] = hyp.prob * arc.prob pool.add(con) # recommend new arc if arc.lhs in gr_right: for gr in gr_right[arc.lhs]: predarc = PredArc( Arc(gr[0], tuple([]), tuple(gr[1:][:-1]), arc.start, arc.start, gr[-1])) if predarc not in pool: adjenda[predarc] = predarc.prob pool.add(predarc) return chart
def seqAStar(n): global startCoordinate global goalCoordinate global OPEN global CLOSED global GVALS global PARENTSTABLE global w1 global w2 global nodesExpanded for i in range(0,n): fringe = heapdict() OPEN.append(fringe) CLOSED.append([]) gVals = [] #Create 120x160 table for g values for row in range(120): gVals.append([]) for column in range(160): gVals[row].append(0) #Initialize all values to infinity for row in range(120): for column in range(160): gVals[row][column] = 999999999 startX = startCoordinate[1] startY = startCoordinate[0] #g for start cell is 0 gVals[startY][startX] = 0 goalX = goalCoordinate[1] goalY = goalCoordinate[0] gVals[goalY][goalX] = 999999999 GVALS.append(gVals) parentsTable = [] #Create 120x160 table to link each coordiante's parent for row in range(120): parentsTable.append([]) for column in range(160): parentsTable[row].append(0) # Append a cell #Initialize all values to null for row in range(120): for column in range(160): parentsTable[row][column] = [] PARENTSTABLE.append(parentsTable) OPEN[i][(startX, startY)] = Key(startX, startY, i) while(OPEN[0].peekitem()[1] < 999999999): for i in range(1,n): if(OPEN[i].peekitem()[1] <= w2 * OPEN[0].peekitem()[1]): if(GVALS[i][goalY][goalX] <= OPEN[i].peekitem()[1]): if(GVALS[i][goalY][goalX] < 999999999): print("PATH FOUND") return i else: s = OPEN[i].popitem() nodesExpanded += 1 ExpandStates(s[0][0],s[0][1],i) if(not (s[0][0],s[0][1]) in CLOSED[i]): CLOSED[i].append((s[0][0],s[0][1])) else: if( GVALS[0][goalY][goalX] <= OPEN[0].peekitem()[1]): if(GVALS[0][goalY][goalX] <999999999 ): print("PATH FOUND") return 0 else: s = OPEN[0].popitem() nodesExpanded += 1 ExpandStates(s[0][0],s[0][1],0) if(not (s[0][0],s[0][1]) in CLOSED[0]): CLOSED[0].append((s[0][0],s[0][1]))
def my_a_star(my_map, start_loc, goal_loc, h_values, agent, constraints, edge_constraints=[]): #print("starting with",constraints,"edge constraints",edge_constraints) tie_breaker = 1 open_list = heapdict() closed_list = {} constraint_table = my_build_constraint_table(constraints) edge_constraint_table = my_build_edge_table(edge_constraints) node = { 'prev': None, 'location': start_loc, 'g': 0, 'h': h_values[start_loc[0]][start_loc[1]] } open_list[start_loc] = (0, 0, (-1, -1)) closed_list[(-1, -1)] = None while open_list: location, (f, g, prev) = open_list.popitem() node = { 'location': location, 'prev': closed_list[prev], 'g': g, 'h': h_values[location[0]][location[1]] } closed_list[location] = node if location == goal_loc: path = get_path(node) return path for direction in directions: new_loc = (node['location'][0] + direction[0], node['location'][1] + direction[1]) if not is_valid_move(my_map, node['location'], new_loc, constraint_table, edge_constraint_table, agent, node['g'] + 1): continue successor = { 'prev': node, 'location': new_loc, 'g': node['g'] + 1, 'h': h_values[new_loc[0]][new_loc[1]] } closed_node = closed_list.get(new_loc) open_node = open_list.get(new_loc) if closed_node != None: if closed_node['g'] > successor['g']: closed_list[new_loc] = successor open_list[new_loc] = successor elif open_node != None: f, g, prev = open_node if g <= successor['g']: continue else: open_list[new_loc] = (successor['g'] + successor['h'], successor['g'], location) else: open_list[new_loc] = (successor['g'] + successor['h'], successor['g'], location) return None
def __init__(self): self.__queue = heapdict() for station in data.STATIONS: self.__queue[station] = (float('Inf'), station, [])
import threading import Queue import heapdict argv = sys.argv """ if len(sys.argv) < 3: sys.exit("invalid arguments, please insert query and a number") """ #query = argv[1] #total = argv[2] query = "ggg" total = 500 # split keyword into list e.g. "dog cat" => ["dog", "cat"] keywords = query.lower().split() queue = heapdict.heapdict() # container of url, used as heap in next dict = {} # hash table of url, (url, score) def computeScore(content): """ this is used to computer priority score of that page, use naive method compute the number of keywords in the content of that page """ content = content.lower() content = content.split() priorityScore = 0 for keyword in keywords: for word in content: if keyword == word: priorityScore += 1 return priorityScore
def runGreedy(trajs, distPairs, strajCov, ptStraj, strajPth, trajCov, c1, c2, c3): """ Run the greedy algorithm for pathlet cover. At each step, the algorithm either chooses to leave a point unassigned, or picks a pathlet and a set of subtrajectories assigned to the pathlet (at most one from each subtrajectory) depending on whichever has the highest coverage-cost ratio. The points that are covered by the sets picked up in each greedy step are said to be "processed". Args: trajs ({int : traj}): dict mapping ID to traj objects. distPairs ({(pathlet, int) : [(subTraj, float)]): dict mapping a pathlet-trajID pair to a list of subtraj-float pairs, where the subtraj belong to the resp. traj, and the float values are computed Frechet distances. strajCov ({subtraj : int}) : dict storing coverage (#points) in all subtrajs in distPairs. ptStraj ({pt : {subtraj}}) : dict storing for each point, the set of subtrajs in distPairs containing it. strajPth ({subtraj: [pathlet]) : dict storing for each subtraj in distPairs, the list of pathlets associated with it. trajCov ({int : int}) : dict storing the #points in each trajectory. c1,c2,c3 (float): parameters of the greedy algorithm. Returns: Pathlet assignments and unassigned points as determined by the greedy algorithm, alongwith other relevant info about the pathlets picked. """ # Initialize coverage-cost ratios for each pathlet. # pthOptCovCost is a dict mapping a pathlet to its optimal coverage-cost ratio. # pthOptStrajs is a dict of the form {pathlet : {int : (subtraj, dist)}} mapping # a pathlet to the optimal assignment of subtrajectories to it, alongwith the Frechet # distances. Both these dicts evolve as the greedy algorithm progresses. pthOptCovCost, pthOptStrajs = {}, {} # Build skeleton of pthOptStrajs. for key, value in distPairs.iteritems(): pth, trID, strajDists = key[0], key[1], value if pthOptStrajs.has_key(pth) is False: pthOptStrajs[pth] = {} pthOptStrajs[pth][trID] = (None, None) # Compute pthOptStrajs. for key, value in distPairs.iteritems(): pth = key[0] affectedTrajs = pthOptStrajs[pth].keys() computeOptStrajsAdvanced(pth, distPairs, pthOptStrajs, strajCov, c1, c3, len(ptStraj), affectedTrajs) # Compute pthOptCovCost from pthOptStrajs. for key, value in pthOptStrajs.iteritems(): pth = key pthOptCovCost[pth] = computeCovCostRatio(value, c1, c3, strajCov) if pthOptCovCost[pth] == 0: print "Error" # Initialize a max priority queue of pathlets ordered by coverage cost ratios. queue1 = heapdict() for pth, ccratio in pthOptCovCost.iteritems(): queue1[ pth] = -1.0 * ccratio # Need to negate, since heapdict is a min-heap. # Initialize a priority queue of trajs, with coverage to cost ratio of a singleton set, i.e., |T|/c2. queue2 = heapdict() for trID, cov in trajCov.iteritems(): queue2[trID] = -(1.0 * cov) / (1.0 * c2) # Initial pathlet assignments, it is of the form {pathlet : [subtraj]} and stores the # list of subtraj assigned to the pathlets pthAssignments = {} pthStats = [ ] # this is of the form [(pathlet,ccRatio,iter,fracThickness)], where iter was # the iteration of the greedy algorithm when pathlet was picked, # and fracThickness is the proportion of points of a traj assigned to pth, # summed over all trajs. Note that the same pathlet can be picked multiple # times in different iterations. # Unassigned points unassignedPts = [] count = 0 numUnprocessedPts = [len(ptStraj)] while numUnprocessedPts[0] > 0: print "num of points is %d" % numUnprocessedPts[0] x1, x2 = queue1.peekitem(), queue2.peekitem() # Set of the form {(pth, trID)} whose coverage changes after new points are # processed in the current iteration. affectedPths = set() # If the most beneficial pathlet is more beneficial than leaving a point unassigned. if x1[1] <= x2[1]: x = queue1.popitem() pth = x[0] fracThickness = 0 for trID, pair in pthOptStrajs[pth].iteritems(): straj = pair[0] if straj is None: # possible when all strajs of trID in pth's kitty have zero coverage continue if pthAssignments.has_key(pth): pthAssignments[pth].append(straj) else: pthAssignments[pth] = [straj] fracThickness += 1.0 * strajCov[straj] / len( trajs[straj.trajID].pts) # Process the straj, and also return affected pathlets, i.e., pathlets whose optimal # coverage-cost ratio changes since some points that could have been assigned to it # have now been processed. affectedPths = affectedPths.union( processSubtraj(straj, strajCov, trajs, trajCov, ptStraj, strajPth, distPairs, numUnprocessedPts, queue2)) pthStats.append((pth, pthOptCovCost[pth], count, fracThickness)) # If more beneficial to leave a point unassigned than picking a pathlet. else: x = queue2.popitem() trID = x[0] # Process the unassigned point, and also return pathlets whose optimal coverage-cost ratio # changes. affectedPths = affectedPths.union( processTraj(trID, ptStraj, strajCov, strajPth, trajs, trajCov, distPairs, numUnprocessedPts, queue2, unassignedPts)) # Update coverage-cost ratio of affected pathlets. affectedPathlets = {path for (path, traID) in affectedPths} affectedTrajs = {traID for (path, traID) in affectedPths} for path in affectedPathlets: computeOptStrajsAdvanced(path, distPairs, pthOptStrajs, strajCov, c1, c3, len(ptStraj), affectedTrajs) pthOptCovCost[path] = computeCovCostRatio(pthOptStrajs[path], c1, c3, strajCov) queue1[path] = -1.0 * pthOptCovCost[path] count += 1 return (pthAssignments, pthStats, unassignedPts)
def make_data(self): pairs = [(random.random(), random.random()) for i in range(N)] h = heapdict(pairs) d = dict(pairs) pairs.sort(key=lambda x: x[1], reverse=True) return h, pairs, d
def my_a_star(my_map, start_loc, goal_loc, h_values, agent, constraints, edge_constraints=[]): #print("starting with agent:",agent,"\nconst",constraints,"\nedge constraints",edge_constraints) counter = 1 open_list = heapdict() closed_list = {} constraint_table = my_build_constraint_table(constraints) edge_constraint_table = my_build_edge_table(edge_constraints) node = { 'prev': None, 'location': start_loc, 'g': 0, 'h': h_values[start_loc[0]][start_loc[1]] } open_list[(start_loc, 0)] = (0, 0, (-1, -1)) closed_list[((-1, -1), -1)] = None while open_list: stop_flag = False (location, _), (f, h, prev) = open_list.popitem() g = f - h #print(f"agent:{agent} \n open:{location}\n prev:{prev}\n f:{f} h:{h} g:{g}") node = { 'location': location, 'prev': closed_list[(prev, g - 1)], 'g': g, 'h': h_values[location[0]][location[1]] } closed_list[(location, g)] = node if location == goal_loc: path = get_path(node) return path for direction in directions: new_loc = (node['location'][0] + direction[0], node['location'][1] + direction[1]) if not is_valid_move(my_map, node['location'], new_loc, constraint_table, edge_constraint_table, agent, node['g'] + 1): continue w = wait_for_constraint(agent, node['location'], new_loc, node['g'] + 1, constraint_table, edge_constraint_table) if w == -1: continue for i in range(w): tmp_node = { 'location': location, 'prev': closed_list[(location, g + i)], 'g': node['g'] + i + 1, 'h': h_values[location[0]][location[1]] } closed_list[(location, tmp_node['g'])] = tmp_node successor = { 'prev': node, 'location': new_loc, 'g': node['g'] + w + 1, 'h': h_values[new_loc[0]][new_loc[1]] } closed_node = closed_list.get((new_loc, successor['g'])) open_node = open_list.get((new_loc, successor['g'])) if successor['h'] == math.inf: continue if closed_node != None: continue elif open_node != None: continue else: #print(new_loc,"+++",g) counter += 1 open_list[(new_loc, successor['g'])] = (successor['g'] + successor['h'], successor['h'], location) return None
def bottom_up(data, k, calc_error=sqr_residual, max_error=float('inf')): ''' Merge time series data points to produce trend segments using bottom-up method where k is the average segment length. The data should be a list of values. Terminates when average segment length is more than k. OR If max_error > 0, when the maximum error of any segment (in terms of a residual with the original data underlying the segment) has reached the limit. calc_error(segment, data) provides the specific implementation to compute the error of a segment. Returns a subset of the data in the form of [(index, value)]. ''' ## INITIALIZATION STEP n = len(data) # split into segments of length 2 # a segment is of the form ((index, value), (index, value)) segments = [((2*i,data[2*i]), (2*i+1,data[2*i+1])) for i in xrange(n/2)] # create linked list of pairs of segments # and a heap dictionary of (pair -> residual error) pairs = llist.dllist() res_heap = hd.heapdict() for i in xrange(len(segments)-1): # create the pair left = segments[i] right = segments[i+1] seg_pair = (left, right) # get the residual error res = calc_error(merge_segs(left, right), data) # add to linked list and heap node = pairs.append(seg_pair) res_heap[node] = res ## MERGE STEP # merge segments while number of segments at most n/k while len(pairs) > n/k: pair, res = res_heap.popitem() if res > max_error: break new_seg = merge_segs(pair.value[0], pair.value[1]) # update second segment of left pair in linked list and also # delete old left pair from heap and re-add new pair with new res left = pair.prev if left != None: del res_heap[left] lpair = left.value left.value = (lpair[0], new_seg) merged = merge_segs(left.value[0], left.value[1]) res_heap[left] = calc_error(merged, data) # update first segment of right pair right = pair.next if right != None: del res_heap[right] rpair = right.value right.value = (new_seg, rpair[1]) merged = merge_segs(right.value[0], right.value[1]) res_heap[right] = calc_error(merged, data) # remove old pair from pairs linked list pairs.remove(pair) # form a list of (index, value) keeping # only the values after being segmented segmented_data = [] # retrieve unique segments head = pairs.first while head != None: unique_seg = head.value[0] segmented_data.append(unique_seg[0]) segmented_data.append(unique_seg[1]) head = head.next # add the last data point segmented_data.append(pairs.last.value[1][1]) return segmented_data
successors.append((xTemp,yTemp)) #check cell left of x, y xTemp = currentCell[0][0] - 1 yTemp = currentCell[0][1] successorCoordinate = [xTemp,yTemp] if(checkHighwayInBounds(xTemp, yTemp) and not grid[yTemp][xTemp] == '4' and not successorCoordinate in closedList): successors.append((xTemp,yTemp)) return successors fringe = heapdict() #s is the item popped off from the fringe, exists in the form (f, xCoord, yCoord) def inFringe(s): global fringe tempX = s[0][0] tempY = s[0][1] for triplet in fringe: if(triplet[0] == tempX and triplet[1] == tempY): return True return False def calculateCost(sx, sy, sprimex, sprimey): #should never be blocked or out of bounds
def __init__(self): self.queue = heapdict()
print ' \n Rank data read from file {} \n ' .format(FILE) pprint.pprint(rank) # A dictionary with preferences of each person (men and women) print '-------------------------------------------------- \n ' N = len(rank)/ 2 # Define a priority queue to keep the free men FreeMen = heapdict() for i in range(len(men)): FreeMen[men[i]] = i + N # priorities are offset by N to accomodate later insertions at the front # Define dictionaries for matching: {m1:w1, m2:w2, ..., m_n:w_n} {w1:m1 ...} matchedM = dict() matchedW = dict() count = np.zeros(N) # Number of proposals of each man # While some man is free and hasn't proposed to every woman while FreeMen.__len__() != 0 and count[men.index(FreeMen.peekitem()[0])] < N: m = FreeMen.popitem()[0] # choose such a man m w = rank[m][int(count[men.index(m)])] # 1st woman to which he hasn't yet proposed print '{} proposes to {}' .format(m,w)