-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer.py
More file actions
40 lines (30 loc) · 1.57 KB
/
buffer.py
File metadata and controls
40 lines (30 loc) · 1.57 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
31
32
33
34
35
36
37
38
39
40
import numpy as np
import random
import torch
from collections import deque
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
class Experience:
def __init__(self, state, action, reward, next_state, done):
self.state = state
self.action = action
self.reward = reward
self.next_state = next_state
self.done = done
class ReplayBuffer:
def __init__(self, action_size, buffer_size, batch_size):
self.action_size = action_size
self.batch_size = batch_size
self.memory = deque(maxlen=buffer_size)
def sample(self, batch_size) -> tuple:
experiences = random.sample(self.memory, k=batch_size)
states = torch.from_numpy(np.vstack([e.state for e in experiences if e is not None])).float().to(device)
actions = torch.from_numpy(np.vstack([e.action for e in experiences if e is not None])).float().to(device)
rewards = torch.from_numpy(np.vstack([e.reward for e in experiences if e is not None])).float().to(device)
next_states = torch.from_numpy(np.vstack([e.next_state for e in experiences if e is not None])).float().to(device)
dones = torch.from_numpy(np.vstack([e.done for e in experiences if e is not None]).astype(np.uint8)).float().to(device)
return (states, actions, rewards, next_states, dones)
def add(self, state, action, reward, next_state, done):
experience = Experience(state, action, reward, next_state, done)
self.memory.append(experience)
def __len__(self):
return len(self.memory)