memory_tools

class xuance.common.memory_tools.Buffer(observation_space: gymnasium.Space, action_space: gymnasium.Space, auxiliary_info_shape: dict | None, num_envs: int, buffer_size: int)[source]

Bases: ABC

Basic buffer single-agent DRL algorithms.

Parameters:
  • observation_space – the space for observation data.

  • action_space – the space for action data.

  • auxiliary_info_shape – the shape for auxiliary data if needed.

abstractmethod clear(*args)[source]
finish_path(*args)[source]
property full
abstractmethod sample(*args)[source]
abstractmethod store(*args)[source]
class xuance.common.memory_tools.DummyOffPolicyBuffer(observation_space: gymnasium.Space, action_space: gymnasium.Space, auxiliary_shape: dict | None, n_envs: int, buffer_size: int, batch_size: int)[source]

Bases: Buffer

Replay buffer for off-policy DRL algorithms.

Parameters:
  • observation_space – the observation space of the environment.

  • action_space – the action space of the environment.

  • auxiliary_shape – data shape of auxiliary information (if exists).

  • n_envs – number of parallel environments.

  • buffer_size – the total size of the replay buffer.

  • batch_size – size of transition data for a batch of sample.

clear()[source]
sample(batch_size=None)[source]
store(obs, acts, rews, terminals, next_obs)[source]
class xuance.common.memory_tools.DummyOffPolicyBuffer_Atari(observation_space: gymnasium.Space, action_space: gymnasium.Space, auxiliary_shape: dict | None, n_envs: int, buffer_size: int, batch_size: int)[source]

Bases: DummyOffPolicyBuffer

Replay buffer for off-policy DRL algorithms and Atari tasks.

Parameters:
  • observation_space – the observation space of the environment.

  • action_space – the action space of the environment.

  • auxiliary_shape – data shape of auxiliary information (if exists).

  • n_envs – number of parallel environments.

  • buffer_size – the total size of the replay buffer.

  • batch_size – batch size of transition data for a sample.

clear()[source]
class xuance.common.memory_tools.DummyOnPolicyBuffer(observation_space: gymnasium.Space, action_space: gymnasium.Space, auxiliary_shape: dict | None, n_envs: int, horizon_size: int, use_gae: bool = True, use_advnorm: bool = True, gamma: float = 0.99, gae_lam: float = 0.95)[source]

Bases: Buffer

Replay buffer for on-policy DRL algorithms.

Parameters:
  • observation_space – the observation space of the environment.

  • action_space – the action space of the environment.

  • auxiliary_shape – data shape of auxiliary information (if exists).

  • n_envs – number of parallel environments.

  • horizon_size – max length of steps to store for one environment.

  • use_gae – if use GAE trick.

  • use_advnorm – if use Advantage normalization trick.

  • gamma – discount factor.

  • gae_lam – gae lambda.

clear()[source]
finish_path(val, i)[source]
property full
sample(indexes)[source]
store(obs, acts, rews, value, terminals, aux_info=None)[source]
class xuance.common.memory_tools.DummyOnPolicyBuffer_Atari(observation_space: gymnasium.Space, action_space: gymnasium.Space, auxiliary_shape: dict | None, n_envs: int, horizon_size: int, use_gae: bool = True, use_advnorm: bool = True, gamma: float = 0.99, gae_lam: float = 0.95)[source]

Bases: DummyOnPolicyBuffer

Replay buffer for on-policy DRL algorithms and Atari tasks.

Parameters:
  • observation_space – the observation space of the environment.

  • action_space – the action space of the environment.

  • auxiliary_shape – data shape of auxiliary information (if exists).

  • n_envs – number of parallel environments.

  • horizon_size – max length of steps to store for one environment.

  • use_gae – if use GAE trick.

  • use_advnorm – if use Advantage normalization trick.

  • gamma – discount factor.

  • gae_lam – gae lambda.

clear()[source]
class xuance.common.memory_tools.EpisodeBuffer[source]

Bases: object

Episode buffer for DRQN agent.

put(transition)[source]
sample(lookup_step=None, idx=None) Dict[str, numpy.ndarray][source]
class xuance.common.memory_tools.PerOffPolicyBuffer(observation_space: gymnasium.Space, action_space: gymnasium.Space, auxiliary_shape: dict | None, n_envs: int, buffer_size: int, batch_size: int, alpha: float = 0.6)[source]

Bases: Buffer

Prioritized Replay Buffer.

Parameters:
  • observation_space – the observation space of the environment.

  • action_space – the action space of the environment.

  • auxiliary_shape – data shape of auxiliary information (if exists).

  • n_envs – number of parallel environments.

  • buffer_size – the total size of the replay buffer.

  • batch_size – batch size of transition data for a sample.

  • alpha – prioritized factor.

clear()[source]
sample(beta)[source]
store(obs, acts, rews, terminals, next_obs)[source]
update_priorities(idxes, priorities)[source]
class xuance.common.memory_tools.RecurrentOffPolicyBuffer(observation_space: gymnasium.Space, action_space: gymnasium.Space, auxiliary_shape: dict | None, n_envs: int, buffer_size: int, batch_size: int, episode_length: int, lookup_length: int)[source]

Bases: Buffer

Replay buffer for DRQN-based algorithms.

Parameters:
  • observation_space – the observation space of the environment.

  • action_space – the action space of the environment.

  • auxiliary_shape – data shape of auxiliary information (if exists).

  • n_envs – number of parallel environments.

  • buffer_size – the size of replay buffer that stores episodes of data.

  • batch_size – batch size of transition data for a sample.

  • episode_length – data length for an episode.

  • lookup_length – the length of history data.

can_sample()[source]
clear(*args)[source]
property full
sample()[source]
store(episode)[source]
class xuance.common.memory_tools.SequentialReplayBuffer(observation_space: gymnasium.Space, action_space: gymnasium.Space, auxiliary_shape: dict | None, n_envs: int, buffer_size: int, batch_size: int)[source]

Bases: Buffer

Sequential Replay buffer for Dreamerv3

Parameters:
  • observation_space – the observation space of the environment.

  • action_space – the action space of the environment.

  • auxiliary_shape – data shape of auxiliary information (if exists).

  • n_envs – number of parallel environments.

  • buffer_size – the total size of the replay buffer.

  • batch_size – size of transition data for a batch of sample.

clear()[source]
sample(seq_len: int)[source]

Sample elements from the replay buffer in a sequential manner, without considering the episode boundaries. :param seq_len: :type seq_len: int

Returns:

the sampled dictionary with a shape of [envs, sequence_length, batch_size, …].

Return type:

Dict[str, np.ndarray]

store(obs, acts, rews, terms, truncs, is_first)[source]
Parameters:
  • arrays (all arguments are numpy) – [envs, ~] if ~ != 1 else [envs, ]

  • shape – [envs, ~] if ~ != 1 else [envs, ]

Returns:

xuance.common.memory_tools.create_memory(shape: tuple | dict | None, n_envs: int, n_size: int, dtype: type = numpy.float32)[source]

Create a numpy array for memory data.

Parameters:
  • shape – data shape.

  • n_envs – number of parallel environments.

  • n_size – length of data sequence for each environment.

  • dtype – numpy data type.

Returns:

numpy.zeros())

Return type:

An empty memory space to store data. (initial

xuance.common.memory_tools.sample_batch(memory: numpy.ndarray | dict | None, index: numpy.ndarray | tuple | None)[source]

Sample a batch of data from the selected memory.

Parameters:
  • memory – memory that contains experience data.

  • index – pointer to the location for the selected data.

Returns:

A batch of data.

xuance.common.memory_tools.store_element(data: numpy.ndarray | dict | float | None, memory: dict | numpy.ndarray, ptr: int)[source]

Insert a step of data into current memory.

Parameters:
  • data – target data that to be stored.

  • memory – the memory where data will be stored.

  • ptr – pointer to the location for the data.