[9f010e]: / SAC / replay_memory.py

Download this file

31 lines (24 with data), 1.1 kB

 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
import random
import numpy as np
from itertools import chain
import torch
class PolicyReplayMemory:
def __init__(self, capacity, seed):
random.seed(seed)
self.capacity = capacity
self.buffer = []
self.position = 0
def push(self, state):
if len(self.buffer) < self.capacity:
self.buffer.append(None)
self.buffer[self.position] = state
self.position = (self.position + 1) % self.capacity
def sample(self, batch_size):
batch = random.sample(self.buffer, batch_size)
batch_list = list(chain(*batch))
state, action, reward, next_state, done, h_current, neural_activity, na_idx = map(np.stack, zip(*batch_list))
policy_state_batch = [[list(element)[0] for element in sample]for sample in batch]
policy_state_batch = list(map(torch.FloatTensor, policy_state_batch))
return state, action, reward, next_state, done, h_current, policy_state_batch, neural_activity, na_idx
def __len__(self):
return len(self.buffer)