| 140 | |
| 141 | # ############################## Replay #################################### |
| 142 | class ReplayBuffer(object): |
| 143 | |
| 144 | def __init__(self, size): |
| 145 | self._storage = [] |
| 146 | self._maxsize = size |
| 147 | self._next_idx = 0 |
| 148 | |
| 149 | def __len__(self): |
| 150 | return len(self._storage) |
| 151 | |
| 152 | def add(self, *args): |
| 153 | if self._next_idx >= len(self._storage): |
| 154 | self._storage.append(args) |
| 155 | else: |
| 156 | self._storage[self._next_idx] = args |
| 157 | self._next_idx = (self._next_idx + 1) % self._maxsize |
| 158 | |
| 159 | def _encode_sample(self, idxes): |
| 160 | b_o, b_a, b_r, b_o_, b_d = [], [], [], [], [] |
| 161 | for i in idxes: |
| 162 | o, a, r, o_, d = self._storage[i] |
| 163 | b_o.append(o) |
| 164 | b_a.append(a) |
| 165 | b_r.append(r) |
| 166 | b_o_.append(o_) |
| 167 | b_d.append(d) |
| 168 | return ( |
| 169 | np.stack(b_o).astype('float32') * ob_scale, |
| 170 | np.stack(b_a).astype('int32'), |
| 171 | np.stack(b_r).astype('float32'), |
| 172 | np.stack(b_o_).astype('float32') * ob_scale, |
| 173 | np.stack(b_d).astype('float32'), |
| 174 | ) |
| 175 | |
| 176 | def sample(self, batch_size): |
| 177 | indexes = range(len(self._storage)) |
| 178 | idxes = [random.choice(indexes) for _ in range(batch_size)] |
| 179 | return self._encode_sample(idxes) |
| 180 | |
| 181 | |
| 182 | # ############################# Functions ################################### |
no outgoing calls
no test coverage detected
searching dependent graphs…