from collections import deque
import random

class ReplayBuffer(object):           #从object（python默认的类）继承

    def __init__(self, buffer_size):    # 定义ReplayBuffer类自己的变量
        self.buffer_size = buffer_size
        self.num_experiences = 0
        self.buffer = deque()         ## 双向队列

    def getBatch(self, batch_size):    # 定义ReplayBuffer类自己的函数
        # Randomly sample batch_size examples
        if self.num_experiences < batch_size:
            return random.sample(self.buffer, self.num_experiences)
        else:
            return random.sample(self.buffer, batch_size)

    def size(self):
        return self.buffer_size

    def add(self, state, action, reward, new_state, done):
        experience = (state, action, reward, new_state, done)
        if self.num_experiences < self.buffer_size:
            self.buffer.append(experience)
            self.num_experiences += 1
        else:
            self.buffer.popleft()   #最左端元素出列，即去除最早进入的状态
            self.buffer.append(experience)

    def count(self):
        # if buffer is full, return buffer size
        # otherwise, return experience counter
        return self.num_experiences

    def erase(self):
        self.buffer = deque()
        self.num_experiences = 0