MCPcopy Create free account
hub / github.com/YeWR/EfficientZero / ReplayBuffer

Class ReplayBuffer

core/replay_buffer.py:8–173  ·  view source on GitHub ↗

Reference : DISTRIBUTED PRIORITIZED EXPERIENCE REPLAY Algo. 1 and Algo. 2 in Page-3 of (https://arxiv.org/pdf/1803.00933.pdf

Source from the content-addressed store, hash-verified

6
7@ray.remote
8class ReplayBuffer(object):
9 """Reference : DISTRIBUTED PRIORITIZED EXPERIENCE REPLAY
10 Algo. 1 and Algo. 2 in Page-3 of (https://arxiv.org/pdf/1803.00933.pdf
11 """
12 def __init__(self, config=None):
13 self.config = config
14 self.batch_size = config.batch_size
15 self.keep_ratio = 1
16
17 self.model_index = 0
18 self.model_update_interval = 10
19
20 self.buffer = []
21 self.priorities = []
22 self.game_look_up = []
23
24 self._eps_collected = 0
25 self.base_idx = 0
26 self._alpha = config.priority_prob_alpha
27 self.transition_top = int(config.transition_num * 10 ** 6)
28 self.clear_time = 0
29
30 def save_pools(self, pools, gap_step):
31 # save a list of game histories
32 for (game, priorities) in pools:
33 # Only append end game
34 # if end_tag:
35 if len(game) > 0:
36 self.save_game(game, True, gap_step, priorities)
37
38 def save_game(self, game, end_tag, gap_steps, priorities=None):
39 """Save a game history block
40 Parameters
41 ----------
42 game: Any
43 a game history block
44 end_tag: bool
45 True -> the game is finished. (always True)
46 gap_steps: int
47 if the game is not finished, we only save the transitions that can be computed
48 priorities: list
49 the priorities corresponding to the transitions in the game history
50 """
51 if self.get_total_len() >= self.config.total_transitions:
52 return
53
54 if end_tag:
55 self._eps_collected += 1
56 valid_len = len(game)
57 else:
58 valid_len = len(game) - gap_steps
59
60 if priorities is None:
61 max_prio = self.priorities.max() if self.buffer else 1
62 self.priorities = np.concatenate((self.priorities, [max_prio for _ in range(valid_len)] + [0. for _ in range(valid_len, len(game))]))
63 else:
64 assert len(game) == len(priorities), " priorities should be of same length as the game steps"
65 priorities = priorities.copy().reshape(-1)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected