def test(): test_data = [(1, 0), (0.9, 1), (1.1, 2), (1.1, 3), (3.3, 4), (0, 5), (0.93, 6)] BH = binary_heap.BinaryHeap(7) # empty queue print('empty queue') print(BH) print('max priority') print(BH.get_max_priority()) # insert print('\ntest insert') for p, i in test_data: BH.update(p, i) print(BH) print(BH.p2e) print(BH.e2p) # update print('\ntest update') BH.update(9.9, 0) print(BH) print(BH.p2e) print(BH.e2p) # get max priority print('\nmax priority') print(BH.get_max_priority()) # re balance print('\ntest re_balance') BH.balance_tree() print(BH) print(BH.p2e) print(BH.e2p) # full insert print('\ntest full insert') BH.update(9.2, 7) print(BH) # pop print('\ntest pop') p, i = BH.pop() print('pop out: ', p, i) print(BH) print(BH.p2e) print(BH.e2p) # get priority print('\ntest get priority') print(BH.get_priority()) # get e id print('\ntest e id') print(BH.get_e_id()) # p id to e id print('\ntest p id to e id') print(BH.priority_to_experience([2, 3, 6]))
def constructJumbledHeaps(t): first = binary_heap.BinaryHeap() first.insert(9) first.insert(2) first.insert(6) first.insert(1) first.insert(3) t.assertEqual(first.size(), 5) second = binary_heap.BinaryHeap() second.insert(4) second.insert(8) second.insert(5) second.insert(7) second.insert(0) t.assertEqual(second.size(), 5) return [first, second]
def __init__(self, conf=None): if not conf is None: self.size = int(conf['size']) self.replace_flag = conf[ 'replace_old'] if 'replace_old' in conf else True self.alpha = conf['alpha'] if 'alpha' in conf else 0.7 self.beta_zero = conf['beta_zero'] if 'beta_zero' in conf else 0.5 self.batch_size = int(conf['batch_size'] if 'batch_size' in conf else 32) self.learn_start = int(conf['learn_start'] if 'learn_start' in conf else 1000) # http://www.evernote.com/l/ACnDUVK3ShVEO7fDm38joUGNhDik3fFaB5o/ self.total_steps = int(conf['total_steps'] if 'total_steps' in conf else 100000) self.index = 0 self.record_size = 0 self.isFull = False self._experience = {} self.priority_queue = binary_heap.BinaryHeap(self.size) self.build_distribution()
def __init__(self, conf): self.size = conf['size'] # If the transitions should be replaced if the heap is full # A lower priority node is expelled self.replace_flag = conf[ 'replace_old'] if 'replace_old' in conf else True # Represents the max capacity of the heap self.priority_size = conf[ 'priority_size'] if 'priority_size' in conf else self.size # The alpha used in Equation (1) in PER paper self.alpha = conf['alpha'] if 'alpha' in conf else 0.7 # The bias correction term. Usually annealed linearly from # beta_zero to 1. Section 3.4 of the paper self.beta_zero = conf['beta_zero'] if 'beta_zero' in conf else 0.5 self.batch_size = conf['batch_size'] if 'batch_size' in conf else 32 self.learn_start = conf[ 'learn_start'] if 'learn_start' in conf else 1000 self.total_steps = conf['steps'] if 'steps' in conf else 100000 # partition number N, split total size to N part self.partition_num = conf[ 'partition_num'] if 'partition_num' in conf else 100 self.index = 0 self.record_size = 0 self.isFull = False self._experience = {} self.priority_queue = binary_heap.BinaryHeap(self.priority_size) self.distributions = self.build_distributions() self.beta_grad = (1 - self.beta_zero) / (self.total_steps - self.learn_start)
def test_return_whether_heap_is_empty(self): heap = binary_heap.BinaryHeap() self.assertTrue(heap.isEmpty()) heap.insert(1) self.assertFalse(heap.isEmpty()) heap.extractMinimum() self.assertTrue(heap.isEmpty())
def test_handles_1000_shuffled_elements(self): heap = binary_heap.BinaryHeap() input = [] for i in range(0, 1000): input.append(i) # shuffle for i in range(0, 1000): swapWith = math.floor(random.random() * 1000) temp = input[i] input[i] = input[swapWith] input[swapWith] = temp # insert for i in range(0, 1000): heap.insert(input[i]) # extract output = [] errorReported = False counter = 0 while not heap.isEmpty(): output.append(heap.extractMinimum().key) if (not errorReported and counter != output[len(output) - 1]): self.fail( 'the heap property was not maintained (elements in order 0, 1, 2, ..., 997, 998, 999)' ) counter += 1 self.assertEqual(len(output), 1000)
def __init__(self, max_size=100000, alpha=0.7, beta_zero=0.5, batch_size=32, learn_start=1000, total_steps=100000, partition_num=100): self.max_size = max_size self.alpha = alpha self.beta_zero = beta_zero self.batch_size = batch_size self.learn_start = learn_start self.total_steps = total_steps self.partition_num = partition_num self.index = 0 self.record_size = 0 self.isFull = False self.exp_queue = {} self.queue = binary_heap.BinaryHeap(max_len=self.max_size, batch_size=self.batch_size) self.distributions = self.build_distributions() self.beta_grad = (1 - self.beta_zero) / float(self.total_steps - self.learn_start)
def __init__(self, conf): self.size = conf['size'] self.replace_flag = conf[ 'replace_old'] if 'replace_old' in conf else True self.priority_size = conf[ 'priority_size'] if 'priority_size' in conf else self.size self.alpha = conf['alpha'] if 'alpha' in conf else 0.7 self.beta_zero = conf['beta_zero'] if 'beta_zero' in conf else 0.5 self.batch_size = conf['batch_size'] if 'batch_size' in conf else 32 self.learn_start = conf[ 'learn_start'] if 'learn_start' in conf else 1000 self.total_steps = conf['steps'] if 'steps' in conf else 100000 # partition number N, split total size to N part self.partition_num = conf[ 'partition_num'] if 'partition_num' in conf else 100 self.index = 0 self.record_size = 0 self.isFull = False self._experience = {} self.priority_queue = binary_heap.BinaryHeap(self.priority_size) self.distributions = self.build_distributions() self.beta_grad = (1 - self.beta_zero) / (self.total_steps - self.learn_start)
def test_sets_heaps_size_to_zero(self): heap = binary_heap.BinaryHeap() heap.insert(1) heap.insert(2) heap.insert(3) heap.clear() self.assertEqual(heap.size(), 0)
def test_set_heaps_minimum_node_to_none(self): heap = binary_heap.BinaryHeap() heap.insert(1) heap.insert(2) heap.insert(3) heap.clear() self.assertEqual(heap.findMinimum(), None)
def test_insert_items_into_heap(self): heap = binary_heap.BinaryHeap() heap.insert(1) heap.insert(2) heap.insert(3) heap.insert(4) heap.insert(5) self.assertEqual(heap.size(), 5)
def test_gives_empty_heap_after_inserting_and_extracting_1000_reversed_elements( self): heap = binary_heap.BinaryHeap() for i in range(999, -1, -1): heap.insert(i, i) for i in range(0, 1000): heap.extractMinimum() self.assertTrue(heap.isEmpty())
def test_pop(self): BH = binary_heap.BinaryHeap(max_len=10, initial_heap=test_data, batch_size=5) self.assertEqual(BH.pop(), (10, 10)) self.assertEqual(BH.pop(), (9, 9)) self.assertEqual(BH.pop(), (8, 8)) self.assertEqual(BH.pop(), (7, 7)) self.assertEqual(BH.pop(), (6, 6)) self.assertEqual(BH.pop_batch(), [(5, 5), (4, 4), (3, 3), (2, 2), (1, 1)])
def test_return_minimum_item_from_heap(self): heap = binary_heap.BinaryHeap() heap.insert(5) heap.insert(3) heap.insert(1) heap.insert(4) heap.insert(2) self.assertEqual(heap.findMinimum().key, 1)
def test_gives_empty_heap_after_inserting_and_extracting_1000_pseudo_randomized_elements( self): heap = binary_heap.BinaryHeap() for i in range(0, 1000): if i % 2 == 0: heap.insert(i, i) else: heap.insert(999 - i, 999 - i) for i in range(0, 1000): heap.extractMinimum() self.assertTrue(heap.isEmpty())
def test_union_2_heaps_of_size_5_with_overlapping_elements_in_order(self): heap = binary_heap.BinaryHeap() heap.insert(0) heap.insert(2) heap.insert(4) heap.insert(6) heap.insert(8) other = binary_heap.BinaryHeap() other.insert(1) other.insert(3) other.insert(5) other.insert(7) other.insert(9) self.assertEqual(heap.size(), 5) self.assertEqual(other.size(), 5) heap.union(other) self.assertEqual(heap.size(), 10) for i in range(0, 10): self.assertEqual(heap.extractMinimum().key, i) self.assertTrue(heap.isEmpty())
def test_extract_minimum_item_from_heap_with_negatives(self): heap = binary_heap.BinaryHeap() node1 = heap.insert(-9) node4 = heap.insert(6) node3 = heap.insert(3) node5 = heap.insert(10) node2 = heap.insert(-4) self.assertEqual(heap.extractMinimum().key, node1.key) self.assertEqual(heap.extractMinimum().key, node2.key) self.assertEqual(heap.extractMinimum().key, node3.key) self.assertEqual(heap.extractMinimum().key, node4.key) self.assertEqual(heap.extractMinimum().key, node5.key)
def test_extract_the_minimum_item_from_jumbled_heap(self): heap = binary_heap.BinaryHeap() node1 = heap.insert(1) node4 = heap.insert(4) node3 = heap.insert(3) node5 = heap.insert(5) node2 = heap.insert(2) self.assertEqual(heap.extractMinimum().key, node1.key) self.assertEqual(heap.extractMinimum().key, node2.key) self.assertEqual(heap.extractMinimum().key, node3.key) self.assertEqual(heap.extractMinimum().key, node4.key) self.assertEqual(heap.extractMinimum().key, node5.key)
def test_works_with_string_keys(self): heap = binary_heap.BinaryHeap() node3 = heap.insert('f') node4 = heap.insert('o') node2 = heap.insert('c') node1 = heap.insert('a') node5 = heap.insert('q') self.assertEqual(heap.size(), 5) self.assertEqual(heap.extractMinimum().key, node1.key) self.assertEqual(heap.extractMinimum().key, node2.key) self.assertEqual(heap.extractMinimum().key, node3.key) self.assertEqual(heap.extractMinimum().key, node4.key) self.assertEqual(heap.extractMinimum().key, node5.key) self.assertTrue(heap.isEmpty())
def test_gives_max_heap_given_reverse_compare(self): def compare(a, b): return b - a heap = binary_heap.BinaryHeap(compare) node3 = heap.insert(13) node4 = heap.insert(26) node2 = heap.insert(3) node1 = heap.insert(-6) node5 = heap.insert(27) self.assertEqual(heap.size(), 5) self.assertEqual(heap.extractMinimum().key, node5.key) self.assertEqual(heap.extractMinimum().key, node4.key) self.assertEqual(heap.extractMinimum().key, node3.key) self.assertEqual(heap.extractMinimum().key, node2.key) self.assertEqual(heap.extractMinimum().key, node1.key) self.assertTrue(heap.isEmpty())
def __init__(self, conf = None): self.beta_sched = BetaSchedule(conf) print("Initializing rank_based.Experience()") print("conf={}".format(str(conf))) if not conf is None: self.size = int(conf['size']) self.replace_flag = conf['replace_old'] if 'replace_old' in conf else True self.alpha = conf['alpha'] if 'alpha' in conf else 0.7 self.index = 0 self.record_size = 0 self.isFull = False self._experience = {} self.priority_queue = binary_heap.BinaryHeap(self.size) self.build_distribution()
def test_return_size_of_heap(self): heap = binary_heap.BinaryHeap() self.assertEqual(heap.size(), 0) heap.insert(1) self.assertEqual(heap.size(), 1) heap.insert(2) self.assertEqual(heap.size(), 2) heap.insert(3) self.assertEqual(heap.size(), 3) heap.insert(4) self.assertEqual(heap.size(), 4) heap.insert(5) self.assertEqual(heap.size(), 5) heap.insert(6) self.assertEqual(heap.size(), 6) heap.insert(7) self.assertEqual(heap.size(), 7) heap.insert(8) self.assertEqual(heap.size(), 8) heap.insert(9) self.assertEqual(heap.size(), 9) heap.insert(10) self.assertEqual(heap.size(), 10)
def huffman(C): """Return a Huffman tree from a list of HuffmanLeafNodes.""" import binary_heap Q = binary_heap.BinaryHeap(binary_heap.min_priority) n = len(C) for c in C: Q.insert(c) for i in xrange(n - 1): if DEBUG: print "Items in queue:", __list_freq(Q.get_items()) x = Q.extract() if DEBUG: print "Items in queue:", __list_freq(Q.get_items()) y = Q.extract() z = HuffmanBranchNode(x, y) if DEBUG: print "Merged %d and %d into %d" % ( x.get_frequency(), y.get_frequency(), z.get_frequency()) Q.insert(z) root = Q.extract() assert (Q.is_empty()) return root
def test_full(self): BH = binary_heap.BinaryHeap(max_len=4, initial_heap=test_data) self.assertTrue(BH.is_full()) self.assertFalse(BH.push((5,5))) self.assertEqual(BH.get_size(), 4)
def test_push(self): BH = binary_heap.BinaryHeap(max_len=2, initial_heap=test_data) BH.push((3,3)) self.assertEqual(BH.queue, neg_2) self.assertEqual(BH.get_max_priority(), 2)
def test_update(self): BH = binary_heap.BinaryHeap(max_len = 4, initial_heap=test_data) self.assertFalse(BH.update(8, 10)) self.assertTrue(BH.update(4, 5)) self.assertEqual(BH.pop(), (5, 4)) self.assertEqual(BH.queue, [(-3, 3), (-2, 2), (-1, 1)])
def test_prio_id(self): new_test_data = [(5, 4), (4, 3), (3, 2), (2, 1), (1, 0)] BH = binary_heap.BinaryHeap(initial_heap=new_test_data) self.assertEqual(BH.get_e_ids(), [4, 3, 2, 1, 0]) self.assertEqual(BH.get_priorities(), [5, 4, 3, 2, 1])
def test_return_inserted_node(self): heap = binary_heap.BinaryHeap() ret = heap.insert(1, 'foo') self.assertEqual(ret.key, 1) self.assertEqual(ret.value, 'foo')
def test_returns_none_on_empty_heap(self): heap = binary_heap.BinaryHeap() self.assertEqual(heap.extractMinimum(), None)
def test_insert_multiple_items_with_same_key(self): heap = binary_heap.BinaryHeap() heap.insert(1) heap.insert(1) self.assertEqual(1, heap.extractMinimum().key) self.assertEqual(1, heap.extractMinimum().key)