-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsum_tree_test.py
31 lines (23 loc) · 1015 Bytes
/
sum_tree_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import Sum_Tree as ST
import numpy as np
import matplotlib.pyplot as plt
import random
A = np.arange(12) # array of priorities
# random.shuffle(A)
sum_tree = ST.Sum_Tree(len(A)) # initialize sum tree
sum_tree.priorities = A # set priority data in sum tree
sum_tree.update_allLeaves() # update the leaves with the priority values. Runs values up the tree as well
# Update a leaf to check if the change is propagating
leaf_no = 2
leaf_value = 6
sum_tree.update_leaf(leaf_no,leaf_value)
# Return priority for a random sum value
sum = np.random.uniform(0,sum_tree.tree_nodes[0])
priority_index,priority = sum_tree.get_priority(sum,0)
# Generate frequency for priorities - Higher priorities should be picked at greater frequency
priority_frequency = np.zeros(len(A))
for run in range(1000000):
sum = np.random.uniform(0, sum_tree.tree_nodes[0])
priority_index, priority = sum_tree.get_priority(sum, 0)
priority_frequency[priority_index]+=1
plt.plot(priority_frequency)