def __init__(self): """ Create the queues """ self.sell = pqdict() self.buy = pqdict()
def test_edgecases(self): keys = ['A', 'B', 'C', 'D', 'E', 'F', 'G'] values = [1, 1, 1, 1, 1, 1, 1] pq = pqdict(zip(keys, values)) pq['B'] = 2 self._check_heap_invariant(pq) pq = pqdict(zip(keys, values)) pq['B'] = 0 self._check_heap_invariant(pq)
def test_topitem(self): # empty pq = pqdict() self.assertRaises(KeyError, pq.top) # non-empty for num_items in range(1,30): items = generate_data('float', num_items) pq = pqdict(items) self.assertEqual(pq.topitem(), min(items, key=lambda x: x[1]))
def test_topitem(self): # empty pq = pqdict() self.assertRaises(KeyError, pq.top) # non-empty for num_items in range(1, 30): items = generate_data('float', num_items) pq = pqdict(items) self.assertEqual(pq.topitem(), min(items, key=lambda x: x[1]))
def test_edgecases(): keys = ["A", "B", "C", "D", "E", "F", "G"] values = [1, 1, 1, 1, 1, 1, 1] pq = pqdict(zip(keys, values)) pq["B"] = 2 _check_heap_invariant(pq) pq = pqdict(zip(keys, values)) pq["B"] = 0 _check_heap_invariant(pq)
def test_topitem(): # empty pq = pqdict() with pytest.raises(KeyError): pq.top() # non-empty for num_items in range(1, 30): items = generate_data("float", num_items) pq = pqdict(items) assert pq.topitem() == min(items, key=lambda x: x[1])
def test_constructor(self): # sequence of pairs pq0 = pqdict( [('A',5), ('B',8), ('C',7), ('D',3), ('E',9), ('F',12), ('G',1)]) pq1 = pqdict( zip(['A', 'B', 'C', 'D', 'E', 'F', 'G'], [5, 8, 7, 3, 9, 12, 1])) # dictionary pq2 = pqdict({'A': 5, 'B': 8, 'C': 7, 'D': 3, 'E': 9, 'F': 12, 'G': 1}) # keyword arguments pq3 = minpq(A=5, B=8, C=7, D=3, E=9, F=12, G=1) self.assertTrue(pq0 == pq1 == pq2 == pq3)
def test_update(self): pq1 = pqdict(sample_items) pq2 = pqdict() pq2['C'] = 3000 pq2['D'] = 4000 pq2['XYZ'] = 9000 pq1.update(pq2) self.assertEqual(pq1['C'], 3000) self.assertEqual(pq1['D'], 4000) self.assertIn('XYZ', pq1) self.assertEqual(pq1['XYZ'], 9000)
def test_constructor(self): # sequence of pairs pq0 = pqdict([('A', 5), ('B', 8), ('C', 7), ('D', 3), ('E', 9), ('F', 12), ('G', 1)]) pq1 = pqdict( zip(['A', 'B', 'C', 'D', 'E', 'F', 'G'], [5, 8, 7, 3, 9, 12, 1])) # dictionary pq2 = pqdict({'A': 5, 'B': 8, 'C': 7, 'D': 3, 'E': 9, 'F': 12, 'G': 1}) # keyword arguments pq3 = minpq(A=5, B=8, C=7, D=3, E=9, F=12, G=1) self.assertTrue(pq0 == pq1 == pq2 == pq3)
def test_update(): pq1 = pqdict(sample_items) pq2 = pqdict() pq2["C"] = 3000 pq2["D"] = 4000 pq2["XYZ"] = 9000 pq1.update(pq2) assert pq1["C"] == 3000 assert pq1["D"] == 4000 assert "XYZ" in pq1 assert pq1["XYZ"] == 9000
def test_update(self): pq1 = pqdict(sample_items) pq2 = pqdict() pq2['C'] = 3000 pq2['D'] = 4000 pq2['XYZ'] = 9000 pq1.update(pq2) self.assertEqual(pq1['C'],3000) self.assertEqual(pq1['D'],4000) self.assertIn('XYZ',pq1) self.assertEqual(pq1['XYZ'],9000)
def test_constructor(): # sequence of pairs pq0 = pqdict([("A", 5), ("B", 8), ("C", 7), ("D", 3), ("E", 9), ("F", 12), ("G", 1)]) pq1 = pqdict( zip(["A", "B", "C", "D", "E", "F", "G"], [5, 8, 7, 3, 9, 12, 1])) # dictionary pq2 = pqdict({"A": 5, "B": 8, "C": 7, "D": 3, "E": 9, "F": 12, "G": 1}) # keyword arguments pq3 = minpq(A=5, B=8, C=7, D=3, E=9, F=12, G=1) assert pq0 == pq1 == pq2 == pq3
def test_precedes(self): pq = pqdict() self.assertEqual(pq.precedes, operator.lt) pq = pqdict(reverse=True) self.assertEqual(pq.precedes, operator.gt) func = lambda x, y: len(x) < len(y) pq = pqdict(precedes=func) pq['a'] = () pq['b'] = (1, ) pq['c'] = (1, 2) pq['d'] = (1, 2, 3) self.assertEqual(list(pq.popvalues()), [(), (1, ), (1, 2), (1, 2, 3)])
def test_precedes(self): pq = pqdict() self.assertEqual(pq.precedes, operator.lt) pq = pqdict(reverse=True) self.assertEqual(pq.precedes, operator.gt) func = lambda x, y: len(x) < len(y) pq = pqdict(precedes=func) pq['a'] = () pq['b'] = (1,) pq['c'] = (1,2) pq['d'] = (1,2,3) self.assertEqual(list(pq.popvalues()), [(), (1,), (1,2), (1,2,3)])
def test_precedes(): pq = pqdict() assert pq.precedes == operator.lt pq = pqdict(reverse=True) assert pq.precedes == operator.gt func = lambda x, y: len(x) < len(y) pq = pqdict(precedes=func) pq["a"] = () pq["b"] = (1, ) pq["c"] = (1, 2) pq["d"] = (1, 2, 3) assert list(pq.popvalues()) == [(), (1, ), (1, 2), (1, 2, 3)]
def test_heapify(self): for size in range(30): items = generate_data('int', size) pq = pqdict(items) self._check_heap_invariant(pq) self.assertTrue(len(pq._heap) == size) self._check_index(pq)
def test_destructive_iteration(self): for trial in range(100): size = random.randrange(1,50) items = generate_data('float', size) keys, values = zip(*items) if trial & 1: # Half of the time, heapify using the constructor pq = pqdict(items) else: # The rest of the time, insert items sequentially pq = pqdict() for key, value in items: pq[key] = value # NOTE: heapsort is NOT a stable sorting method, so keys with equal # priority keys are not guaranteed to have the same order as in the # original sequence. values_heapsorted = list(pq.popvalues()) self.assertEqual(values_heapsorted, sorted(values))
def run_ucs(source, target, roads): junctions = roads.junctions() source_node_info = junctions[source] closed = dict() open = pqdict(precedes=min) #add first node with parent of null and distance 0 open.additem(source, Node(source_node_info, None, 0)) while open is not None: current = pop_node(open) add_to_close(closed, get_node_index(current), current) #if final node reached if get_node_index(current) == target: return build_path(current) #ireate over all links links = current.junction.links for link in links: succ = get_target(junctions, link) cost = get_cost(link.distance, road_speed( link.highway_type)) + current.distance if get_succ_index(succ) in open.keys(): if open[get_succ_index(succ)].distance > cost: update_node_distance(open, junctions, link, current, cost, succ) continue if get_succ_index(succ) in closed: if closed[get_succ_index(succ)].distance > cost: update_distance_in_closed(open, closed, junctions, link, current, cost, succ) continue add_to_open(open, Node(get_target(junctions, link), current, cost)) return None
def test_datetime(): pq = pqdict() dt = datetime.now() pq["a"] = dt pq["b"] = dt + timedelta(days=5) pq["c"] = dt + timedelta(seconds=5) assert list(pq.popkeys()) == ["a", "c", "b"]
def pathfind(self) -> typing.Optional[typing.List[ReservationNode]]: opened = pqdict({self.agent.pos: 0}) closed: typing.Set[ReservationNode] = set() g_costs = {self.agent.pos: 0.0} predecessors: typing.Dict[ReservationNode, ReservationNode] = {} previously_reserved = self._previous_reserved() agent_t = self.agent.pos.t while opened: curr = opened.pop() closed.add(curr) if curr.t == self.agent.pos.t + self.agent.lookahead: path = [curr] while path[-1] in predecessors: path.append(predecessors[path[-1]]) path.reverse() return path # As we go into the future, backpressure decreases, so that # it gradually becomes cheaper for agents to stay put backpressure = max(1, previously_reserved - (curr.t - agent_t)) for (n, cost) in self.neighbors(curr, backpressure): if n in closed: continue considered_g_cost = g_costs[curr] + cost if considered_g_cost >= g_costs.get(n, float("nan")): continue g_costs[n] = considered_g_cost predecessors[n] = curr # Hm. f_cost = (considered_g_cost + (self.agent.pos.t + self.agent.lookahead - n.t)) if n not in opened: opened.additem(n, f_cost) else: opened[n] = f_cost return None
def weighted_a_star(g,start_tuple,goal_tuple,x_coord,y_coord,z_coord,eps,blocks): ''' finds the path whcih is e-suboptimal algorithms is given the project report ''' openlist=pqdict() closedlist=[] fs=eps*np.linalg.norm((x_coord[start_tuple[0]]-x_coord[goal_tuple[0]],\ y_coord[start_tuple[1]]-y_coord[goal_tuple[1]],\ z_coord[start_tuple[2]]-z_coord[goal_tuple[2]])) openlist.update({start_tuple:fs}) node_index=(x_coord.shape[0]+1,y_coord.shape[0]+1,z_coord.shape[0]+1) Parent={} #Weighted A-star algorithm, pseudocode given in the report while(goal_tuple not in closedlist): current_tuple=openlist.pop() children=get_children(current_tuple,x_coord,y_coord,z_coord,g,blocks) closedlist.append(current_tuple) for (child,cij) in children: if(child not in closedlist): temp_val=cij+g[current_tuple[0],current_tuple[1],current_tuple[2]] if(g[child[0],child[1],child[2]]>temp_val): g[child[0],child[1],child[2]]=temp_val Parent.update({child:current_tuple}) f=g[child[0],child[1],child[2]]+eps*np.linalg.norm((x_coord[child[0]]-x_coord[goal_tuple[0]],\ y_coord[child[1]]-y_coord[goal_tuple[1]],\ z_coord[child[2]]-z_coord[goal_tuple[2]])) if(child in list(openlist.keys())): openlist[child]=f else: openlist.update({child:f}) return Parent,closedlist,g
def __init__(self, DG: nx.DiGraph, s: int): """ dijkstra 算法 """ self.__DG = DG self.__s = s n = DG.number_of_nodes() self.__prev = [None] * n self.dist2 = [float('inf')] * n self.dist2[s] = 0 # 将距离向量建堆 pq = pqdict(dict(enumerate(self.dist2))) # 循环弹出距离最近的顶点和距离 for v in pq.popkeys(): # 将最短路径经过的边的颜色改为红色 if v != s: u = self.__prev[v] self.__DG[u][v]['color'] = 'red' # 缩短 v 所有的邻居 w 的距离 for w in self.__DG[v]: new_dist = self.dist2[v] + self.__DG[v][w]['weight'] if new_dist < self.dist2[w]: # 更新w的入边和距离 pq[w] = new_dist self.dist2[w], self.__prev[w] = new_dist, v # 断言条件 assert self.__check()
def test_datetime(self): pq = pqdict() dt = datetime.now() pq['a'] = dt pq['b'] = dt + timedelta(days=5) pq['c'] = dt + timedelta(seconds=5) self.assertEqual(list(pq.popkeys()), ['a', 'c', 'b'])
def __init__(self, height=8, width=8, no_of_mines_in_board=0, use_most_constrained=True, use_probability=True): # loading pattern db cache self._constrained_variables = pqdict(reverse=True) # Set initial height and width self.height = height self.width = width # Keep track of which cells have been clicked on self.opened_cells = set() self.open_information = dict() # Keep track of cells known to be safe or mines self.mines = set() self.prev_state = [] self.first_move = True self.no_of_mines = no_of_mines_in_board self.safes = set() self.surely_safe = set() self.linked_tiles = list() self.last_random = None self.disjoint_dict = dict() self._use_most_constrained = use_most_constrained self._open_count = dict() self.closed_cells = set() self.use_probability = use_probability for i in range(self.height): for j in range(self.width): if (i, j) not in self.mines and (i, j) not in self.opened_cells: self.closed_cells.add((i, j))
def partition(comp_arrow: CompositeArrow) -> List[Set[Arrow]]: """Partitions the comp_arrow into sequential layers of its sub_arrows""" partition_arrows = [] arrow_colors = pqdict() for sub_arrow in comp_arrow.get_sub_arrows(): arrow_colors[sub_arrow] = sub_arrow.num_in_ports() for port in comp_arrow.in_ports(): in_ports = comp_arrow.neigh_in_ports(port) for in_port in in_ports: assert in_port.arrow in arrow_colors, "sub_arrow not in arrow_colors" arrow_colors[in_port.arrow] -= 1 while len(arrow_colors) > 0: arrow_layer = set() view_arrow, view_priority = arrow_colors.topitem() while view_priority == 0: sub_arrow, priority = arrow_colors.popitem() arrow_layer.add(sub_arrow) if len(arrow_colors) == 0: break view_arrow, view_priority = arrow_colors.topitem() partition_arrows.append(arrow_layer) for arrow in arrow_layer: for out_port in arrow.out_ports(): in_ports = comp_arrow.neigh_in_ports(out_port) for in_port in in_ports: if in_port.arrow != comp_arrow: assert in_port.arrow in arrow_colors, "sub_arrow not in arrow_colors" arrow_colors[in_port.arrow] -= 1 return partition_arrows
def UCS(self, start_node, end_node): visited = set( ) # set of nodes that have already been popped from the queue pq = pqdict({start_node: 0}) # priority queue parent = { start_node: None } # dictionary storing the parent (ID) of each traversed node while True: try: cur_cost = pq[pq.top()] # cost of reaching this node cur = int(pq.pop()) # ID of the current node for n in self._nodes[cur]: # neighbors of current node if n not in visited: # calculate the cost to reach node n n_cost = cur_cost + self._getWeight(cur, n) if n in pq: if n_cost < pq[n]: # cheaper route to node discovered pq[n] = n_cost # update priority parent[n] = cur # update parent else: pq[n] = n_cost # add to priority queue parent[n] = cur # remember how we reached n visited.add(cur) # remember that this node was visited if cur == end_node: break except KeyError: # priority queue is empty break self._printResults(parent, end_node)
def crawl(url, xpaths): list_of_url_tuples = [] priority_queue_urls = pqdict({url: 1}, reverse=True) # max-heap priority-queue crawled_urls = set() return crawl_with_priority(crawled_urls, priority_queue_urls, xpaths, list_of_url_tuples)
def test_heapify(): for size in range(30): items = generate_data("int", size) pq = pqdict(items) _check_heap_invariant(pq) assert len(pq._heap) == size _check_index(pq)
def __init__(self, seed): # the larger the more important (reverse=True) self.crawl_frontier = pqdict.pqdict({user_id: 0 for user_id in seed}, reverse=True) self.visited = [] self.graph = nx.DiGraph()
def initialize_frontier(self, start): starting_edges = {} for edge in self.graph[start]: node = edge[0] cost = edge[1] starting_edges[node] = cost return pqdict(starting_edges)
def test_uncomparable(): # non-comparable priority keys (Python 3 only) # Python 3 has stricter comparison than Python 2 pq = pqdict() pq.additem("a", []) with pytest.raises(TypeError): pq.additem("b", 5)
def test_destructive_iteration(): for trial in range(100): size = random.randrange(1, 50) items = generate_data("float", size) keys, values = zip(*items) if trial & 1: # Half of the time, heapify using the constructor pq = pqdict(items) else: # The rest of the time, insert items sequentially pq = pqdict() for key, value in items: pq[key] = value # NOTE: heapsort is NOT a stable sorting method, so keys with equal # priority keys are not guaranteed to have the same order as in the # original sequence. values_heapsorted = list(pq.popvalues()) assert values_heapsorted == sorted(values)
def test_iter(self): # non-destructive n = len(sample_items) pq = pqdict(sample_items) for val in iter(pq): self.assertIn(val, sample_keys) self.assertEqual(len(list(iter(pq))), len(sample_keys)) self.assertEqual(len(pq), n)
def test_copy(): pq1 = pqdict(sample_items) pq2 = pq1.copy() assert pq1 == pq2 key = random.choice(sample_keys) pq2[key] += 1 assert pq1[key] != pq2[key] assert pq1 != pq2
def test_copy(self): pq1 = pqdict(sample_items) pq2 = pq1.copy() self.assertEqual(pq1, pq2) key = random.choice(sample_keys) pq2[key] += 1 self.assertNotEqual(pq1[key], pq2[key]) self.assertNotEqual(pq1, pq2)
def test_updates(self): items = generate_data('int') keys, values = zip(*items) pq = pqdict(items) for _ in range(100): pq[random.choice(keys)] = random.randrange(25) self._check_heap_invariant(pq) self._check_index(pq)
def test_updates(): items = generate_data("int") keys, values = zip(*items) pq = pqdict(items) for _ in range(100): pq[random.choice(keys)] = random.randrange(25) _check_heap_invariant(pq) _check_index(pq)
def test_iter(): # non-destructive n = len(sample_items) pq = pqdict(sample_items) for val in iter(pq): assert val in sample_keys assert len(list(iter(pq))) == len(sample_keys) assert len(pq) == n
def test_delitem(self): n = len(sample_items) pq = pqdict(sample_items) key = random.choice(sample_keys) del pq[key] self.assertEqual(len(pq), n-1) self.assertNotIn(key, pq) self.assertRaises(KeyError, pq.pop, key)
def load(self): ''' Loads the pqdict and experience dict ''' tmp = pkl.load(open("myPER", "rb")) tmp1, tmp2 = tmp self.buffer2 = tmp2 self.pq = pqdict(tmp1, reverse=True) return True
def test_equality(self): # eq pq1 = pqdict(sample_items) pq2 = pqdict(sample_items) self.assertTrue(pq1 == pq2) self.assertFalse(pq1 != pq2) # ne pq2[random.choice(sample_keys)] += 1 self.assertFalse(pq1 == pq2) self.assertTrue(pq1 != pq2) # pqdict == regular dict if they have same key/value pairs adict = dict(sample_items) self.assertEqual(pq1, adict) # TODO: FIX? # pqdicts evaluate as equal even if they have different # key functions and/or precedence functions pq3 = maxpq(sample_items) self.assertEqual(pq1, pq3)
def test_infvalue(self): keys = ['A', 'B', 'C', 'D', 'E', 'F', 'G'] values = [1, 2, 3, 4, 5, 6, 7] pq = pqdict(zip(keys, values)) pq.additem('top', -float('inf')) pq.additem('bot', float('inf')) keys_sorted = [key for key in pq.popkeys()] self.assertEqual(keys_sorted[0], 'top') self.assertEqual(keys_sorted[-1], 'bot')
def test_setitem(self): n = len(sample_items) pq = pqdict(sample_items) pq['new'] = 1.0 # add self.assertEqual(pq['new'], 1.0) self.assertEqual(len(pq), n+1) pq['new'] = 10.0 # update self.assertEqual(pq['new'], 10.0) self.assertEqual(len(pq), n+1)
def test_updateitem(self): pq = pqdict(sample_items) key, value = random.choice(sample_items) # assign same value pq.updateitem(key, value) self.assertEqual(pq[key], value) # assign new value pq.updateitem(key, value + 1.0) self.assertEqual(pq[key], value + 1.0) # can only update existing keys self.assertRaises(KeyError, pq.updateitem, 'does_not_exist', 99.0)
def test_updates_and_deletes(self): items = generate_data('int') keys, values = zip(*items) pq = pqdict(items) for oper in range(100): if oper & 1: # update random item key = random.choice(keys) p_new = random.randrange(25) pq[key] = p_new self.assertTrue(pq[key] == p_new) elif pq: # delete random item key = random.choice(list(pq.keys())) del pq[key] self.assertTrue(key not in pq) self._check_heap_invariant(pq) self._check_index(pq)
def test_heapsort(self): # sequences of operations pq = pqdict() self._check_heap_invariant(pq) self._check_index(pq) # push in a sequence of items items = generate_data('int') added_items = [] for key, value in items: pq.additem(key, value) self._check_heap_invariant(pq) self._check_index(pq) added_items.append((key, value)) # pop out all the items popped_items = [] while pq: key_value = pq.popitem() self._check_heap_invariant(pq) self._check_index(pq) popped_items.append(key_value) self.assertTrue(len(pq._heap) == 0) self._check_index(pq)
def run_astar( source_junction_index, target_junction_index, t0, price_func=price_function, heuristic_func=heuristic_function ): global roads roads = load_map_from_csv() global roads_junctions roads_junctions = roads.junctions() source_junction = roads_junctions[source_junction_index] target_junction = roads_junctions[target_junction_index] closed_list = dict() open_pqdict = pqdict(precedes=comparator_f) source_h_value = heuristic_func(source_junction, target_junction) open_pqdict.additem(source_junction_index, Node(source_junction, None, 0, source_h_value, source_h_value)) while open_pqdict: # while pqdict is not empty best_node = open_pqdict.popitem()[1] closed_list[best_node.junction.index] = best_node if best_node.junction.index == target_junction_index: return format_result(best_node) for lnk_to_son in best_node.junction.links: son = roads_junctions[lnk_to_son.target] son_g_value = price_func(lnk_to_son, t0) + best_node.g_value son_h_value = heuristic_func(son, target_junction) if son.index in open_pqdict.keys(): if open_pqdict[son.index].g_value > son_g_value: son_copy = Node(son, best_node, son_g_value, son_h_value, son_h_value + son_g_value) open_pqdict.updateitem(son.index, son_copy) continue if son.index in closed_list: if closed_list[son.index].g_value > son_g_value: son_copy = Node(son, best_node, son_g_value, son_h_value, son_h_value + son_g_value) del closed_list[son.index] open_pqdict.additem(son_copy.junction.index, son_copy) continue open_pqdict.additem(son.index, Node(son, best_node, son_g_value, son_h_value, son_g_value + son_h_value)) return None
def __init__(self, **args): ''' Prioritized experience replay Currently, the rank-based variant is implemented but not the proportional variant ''' self.size = args['size'] # size of the replay memory self.alpha = args['alpha'] # determine how much prioritization is used, alpha = 0 = uniform case beta_zero = args['beta_zero'] # beta is annealed from beta_zero to 1 self.beta = beta_zero self.batch_size = args['batch_size'] self.k = args['nb_segments'] # k, number of segments self.annealing_beta_steps = args['annealing_beta_steps'] # number of step to anneale beta from beta_zero to 1 self.decrease_step_beta = (1 - beta_zero) / self.annealing_beta_steps #priority queue, reverse = True = max heap #transition must be saved as (priority, (s, a, r, s1, terminal)) self.pq = pqdict({}, reverse=True) self.buffer2 = np.array([(1,1) for i in range(self.size)]) # for test_PER2 #dtypes = dict(names=['s', 'a', 'r', 's1', 't'], formats=[np.ndarray,'i8', 'i8', np.ndarray, 'bool']) #self.buffer2 = np.array([(np.ones(1), 1, 1, np.ones(1), 1) for i in range(self.size)], dtype=dtypes) self.tsp = self.build_tsp()
def test_items(self): pq = pqdict(sample_items) self.assertEqual(sorted(sample_items), sorted(pq.items())) self.assertEqual( sorted(sample_values), [item[1] for item in pq.popitems()])
def a_star(graph: Graph, start: Node, goal: Node, heuristic): """ Standard A* search algorithm. :param graph: Graph A graph with all nodes and connections :param start: Node Start node, where the search starts :param goal: Node End node, the goal for the search :return: shortest_path: list|False Either a list of node ids or false """ # Indexed priority queue queue = pqdict() # All visited connections visited_stack = {} # Add start node visited_stack[start] = True # The costs from start to a node cost_to_node = {} # Full costs from a node to goal full_costs = {} # All paths that have been taken shortest_path = [] # Create a dummy for the start node dummy_connection = Connection(start, start) # Assign it to the queue so we can start queue[dummy_connection] = 0 while queue: # Get next connection from top queue # and remove it (its a get + pop) connection = queue.pop() # Add the node to the shortest path # cause otherwise we would not be here shortest_path.append(connection) cost_to_node[connection.to_node] = connection.cost # We have found the target if connection.to_node.id == goal.id: # Remove all unneded paths and return # a sorted list return clean_route_list(shortest_path, goal.id) # Get all connected nodes next_connections = graph.get_connections(connection.to_node) # Iterate through all connected nodes # and calculate the costs and stuff for c in next_connections: # Calculate total costs from start to the goal node to_goal_cost = heuristic(goal.position, c.to_node.position) # Calculate costs from start to this node current_cost = cost_to_node[connection.to_node] + c.cost # Update lists and costs queue[c] = current_cost cost_to_node[c.to_node] = current_cost full_costs[c.to_node] = current_cost + to_goal_cost visited_stack[c.to_node] = True # Never found the target, so sad ... return False
def test_setdefault(self): pq = pqdict(sample_items) self.assertEqual(pq.setdefault('A',99), 5) self.assertEqual(pq.setdefault('new',99), 99) self.assertEqual(pq['new'], 99)
def test_uncomparable(self): # non-comparable priority keys (Python 3 only) # Python 3 has stricter comparison than Python 2 pq = pqdict() pq.additem('a',[]) self.assertRaises(TypeError, pq.additem, 'b', 5)
def test_keyfn(self): pq = pqdict() self.assertEqual(pq.keyfn(5), 5) pq = pqdict(key=lambda x: len(x)) self.assertEqual(pq.keyfn([1,2,3]), 3)
def test_additem(self): pq = pqdict(sample_items) pq.additem('new', 8.0) self.assertEqual(pq['new'], 8.0) self.assertRaises(KeyError, pq.additem, 'new', 1.5)
def test_values(self): pq = pqdict(sample_items) self.assertEqual(sorted(sample_values), sorted(pq.values())) self.assertEqual(sorted(sample_values), list(pq.popvalues()))
def test_keys(self): pq = pqdict(sample_items) self.assertEqual(sorted(sample_keys), sorted(pq.keys())) self.assertEqual( sorted(sample_values), [pq[key] for key in pq.copy().popkeys()])