Spaces:
Sleeping
Sleeping
File size: 4,824 Bytes
eaf2e33 |
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
import abc
class ReplayBuffer(object, metaclass=abc.ABCMeta):
"""
A class used to save and replay data.
"""
@abc.abstractmethod
def add_sample(self, observation, action, reward, next_observation,
terminal, **kwargs):
"""
Add a transition tuple.
"""
pass
@abc.abstractmethod
def terminate_episode(self):
"""
Let the replay buffer know that the episode has terminated in case some
special book-keeping has to happen.
:return:
"""
pass
@abc.abstractmethod
def num_steps_can_sample(self, **kwargs):
"""
:return: # of unique items that can be sampled.
"""
pass
def add_path(self, path):
"""
Add a path to the replay buffer.
This default implementation naively goes through every step, but you
may want to optimize this.
NOTE: You should NOT call "terminate_episode" after calling add_path.
It's assumed that this function handles the episode termination.
:param path: Dict like one outputted by rlkit.samplers.util.rollout
"""
for i, (
obs,
action,
reward,
next_obs,
terminal,
agent_info,
env_info
) in enumerate(zip(
path["observations"],
path["actions"],
path["rewards"],
path["next_observations"],
path["terminals"],
path["agent_infos"],
path["env_infos"],
)):
self.add_sample(
observation=obs,
action=action,
reward=reward,
next_observation=next_obs,
terminal=terminal,
agent_info=agent_info,
env_info=env_info,
)
self.terminate_episode()
def add_paths(self, paths):
for path in paths:
self.add_path(path)
@abc.abstractmethod
def random_batch(self, batch_size):
"""
Return a batch of size `batch_size`.
:param batch_size:
:return:
"""
pass
def get_diagnostics(self):
return {}
def get_snapshot(self):
return {}
def end_epoch(self, epoch):
return
class EnsembleReplayBuffer(object, metaclass=abc.ABCMeta):
"""
A class used to save and replay data.
"""
@abc.abstractmethod
def add_sample(self, observation, action, reward, next_observation,
terminal, **kwargs):
"""
Add a transition tuple.
"""
pass
@abc.abstractmethod
def terminate_episode(self):
"""
Let the replay buffer know that the episode has terminated in case some
special book-keeping has to happen.
:return:
"""
pass
@abc.abstractmethod
def num_steps_can_sample(self, **kwargs):
"""
:return: # of unique items that can be sampled.
"""
pass
def add_path(self, path):
"""
Add a path to the replay buffer.
This default implementation naively goes through every step, but you
may want to optimize this.
NOTE: You should NOT call "terminate_episode" after calling add_path.
It's assumed that this function handles the episode termination.
:param path: Dict like one outputted by rlkit.samplers.util.rollout
"""
for i, (
obs,
action,
reward,
next_obs,
terminal,
agent_info,
env_info,
mask,
) in enumerate(zip(
path["observations"],
path["actions"],
path["rewards"],
path["next_observations"],
path["terminals"],
path["agent_infos"],
path["env_infos"],
path["masks"],
)):
self.add_sample(
observation=obs,
action=action,
reward=reward,
next_observation=next_obs,
terminal=terminal,
mask=mask,
agent_info=agent_info,
env_info=env_info,
)
self.terminate_episode()
def add_paths(self, paths):
for path in paths:
self.add_path(path)
@abc.abstractmethod
def random_batch(self, batch_size):
"""
Return a batch of size `batch_size`.
:param batch_size:
:return:
"""
pass
def get_diagnostics(self):
return {}
def get_snapshot(self):
return {}
def end_epoch(self, epoch):
return |