Repository navigation
Expand file tree
/
Copy pathmemory.py
More file actions
55 lines (44 loc) · 2.59 KB
/
Copy pathmemory.py
File metadata and controls
55 lines (44 loc) · 2.59 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'''Module memory.py
This module contains the source code for the Replay buffer, which can store the agent's observations.
Observations are stored as namedtuple.
Initial part of the code has been adapted from Udacity's Deep Reinforcement learning course (DRL), 2021, and later
modified to fit the use case (to store the Tennis environemnts observations).
@author: Rohith Banka
'''
import numpy as np
import torch
from collections import namedtuple, deque
import random
from utils import get_device
device = get_device()
class ReplayBuffer(object):
"""Fixed-size buffer to store experience tuples."""
def __init__(self, buffer_size, batch_size, seed):
"""Initialize a ReplayBuffer object.
Params
======
buffer_size (int): maximum size of buffer
batch_size (int): size of each training batch
"""
self.memory = deque(maxlen=buffer_size) # internal memory (deque)
self.batch_size = batch_size
self.experience = namedtuple("Experience", field_names=['state', 'full_state', 'action', 'reward', 'next_state', 'next_full_state','done'])
self.seed = random.seed(seed)
def add(self, state, full_state, action, reward, next_state, next_full_state, done):
"""Add a new experience to memory."""
e = self.experience(state, full_state, action, reward, next_state, next_full_state, done)
self.memory.append(e)
def sample(self):
"""Randomly sample a batch of experiences from memory."""
experiences = random.sample(self.memory, k=self.batch_size)
states = torch.from_numpy(np.array([e.state for e in experiences if e is not None])).float().to(device)
full_states = torch.from_numpy(np.array([e.full_state for e in experiences if e is not None])).float().to(device)
actions = torch.from_numpy(np.array([e.action for e in experiences if e is not None])).float().to(device)
rewards = torch.from_numpy(np.array([e.reward for e in experiences if e is not None])).float().to(device)
next_states = torch.from_numpy(np.array([e.next_state for e in experiences if e is not None])).float().to(device)
next_full_states = torch.from_numpy(np.array([e.next_full_state for e in experiences if e is not None])).float().to(device)
dones = torch.from_numpy(np.array([e.done for e in experiences if e is not None]).astype(np.uint8)).float().to(device)
return (states, full_states, actions, rewards, next_states, next_full_states, dones)
def __len__(self):
"""Return the current size of internal memory."""
return len(self.memory)