Skip to content

Commit a06b381

Browse files
Changed ReplayMemory class to use deque instead of list to clean up code. (#842)
Co-authored-by: holly1238 <77758406+holly1238@users.noreply.github.com>
1 parent 3708a18 commit a06b381

File tree

1 file changed

+4
-9
lines changed

1 file changed

+4
-9
lines changed

intermediate_source/reinforcement_q_learning.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
import numpy as np
6464
import matplotlib
6565
import matplotlib.pyplot as plt
66-
from collections import namedtuple
66+
from collections import namedtuple, deque
6767
from itertools import count
6868
from PIL import Image
6969

@@ -115,16 +115,11 @@
115115
class ReplayMemory(object):
116116

117117
def __init__(self, capacity):
118-
self.capacity = capacity
119-
self.memory = []
120-
self.position = 0
118+
self.memory = deque([],maxlen=capacity)
121119

122120
def push(self, *args):
123-
"""Saves a transition."""
124-
if len(self.memory) < self.capacity:
125-
self.memory.append(None)
126-
self.memory[self.position] = Transition(*args)
127-
self.position = (self.position + 1) % self.capacity
121+
"""Save a transition"""
122+
self.memory.append(Transition(*args))
128123

129124
def sample(self, batch_size):
130125
return random.sample(self.memory, batch_size)

0 commit comments

Comments
 (0)