forked from batuhan3526/AirSim-PyTorch-Drone-DDQN-Agent
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SumTree.py
89 lines (68 loc) · 2.58 KB
/
SumTree.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import numpy
# SumTree
# a binary tree data structure where the parent’s value is the sum of its children
class SumTree:
write = 0
def __init__(self, capacity):
self.capacity = capacity
self.tree = numpy.zeros(2 * capacity - 1)
self.states = numpy.zeros(capacity, dtype=object)
self.actions = numpy.zeros(capacity, dtype=object)
self.continuous_actions = numpy.array([])
self.rewards = numpy.zeros(capacity, dtype=object)
self.next_states = numpy.zeros(capacity, dtype=object)
self.n_entries = 0
# update to the root node
def _propagate(self, idx, change):
parent = (idx - 1) // 2
self.tree[parent] += change
if parent != 0:
self._propagate(parent, change)
# find sample on leaf node
def _retrieve(self, idx, s):
left = 2 * idx + 1
right = left + 1
if left >= len(self.tree):
return idx
if s <= self.tree[left]:
return self._retrieve(left, s)
else:
return self._retrieve(right, s - self.tree[left])
def total(self):
return self.tree[0]
# store priority and sample
def add(self, p, state, action, reward, next_state):
idx = self.write + self.capacity - 1
self.states[self.write] = state
self.actions[self.write] = action
self.rewards[self.write] = reward
self.next_states[self.write] = next_state
self.update(idx, p)
self.write += 1
if self.write >= self.capacity:
self.write = 0
if self.n_entries < self.capacity:
self.n_entries += 1
def continuous_action_add(self, p, state, action, reward, next_state):
idx = self.write + self.capacity - 1
self.states[self.write] = state
self.continuous_actions = numpy.append(self.continuous_actions, action)
self.rewards[self.write] = reward
self.next_states[self.write] = next_state
self.update(idx, p)
self.write += 1
if self.write >= self.capacity:
self.write = 0
if self.n_entries < self.capacity:
self.n_entries += 1
# update priority
def update(self, idx, p):
change = p - self.tree[idx]
# print(p, self.tree[idx])
self.tree[idx] = p
self._propagate(idx, change)
# get priority and sample
def get(self, s):
idx = self._retrieve(0, s)
dataIdx = idx - self.capacity + 1
return (idx, self.tree[idx], self.states[dataIdx], self.actions[dataIdx], self.rewards[dataIdx], self.next_states[dataIdx])