Memory

The Memory is a Class used in the:class:~Brain to store information. The basic Memory e.g., stores all received sensor information, all actuator values set by the agent, and the reward returned by the environment(s). Additionally, it stores the internal reward calculated by the objective.

Memory as Replay Buffer

The Memory can be used as a replay buffer. The base class can be extended by additional functions and attributes if needed.

How to use a specific memory

Since Memory is initialized in the Brain, memory can be defined as a parameter of the Brain. For this, the path to the Memory class is passed to the params of the Brain class. Additionally, the parameters for the Memory can be defined. For example:

name: mighty_defender
 brain:
  name: palaestrai.agent.dummy_brain:DummyBrain
  params:
    params:
      memory_class: palaestrai.agent.memory:Memory
      memory_params: {}

Warning

The base lists (env_rewards, actions, observations, internal_rewards) are automatically filled in the brain. Do not add values to it, this could result in duplicates. You can manipulate existing values if needed or add values which are not part of the MuscleUpdateRequest. The only exception is the additional data which have to be implemented individually.

API Documentation

Memory

class palaestrai.agent.Memory(size_limit: int = 1000000)[source]

An in-memory data structure to store experiences in a ::~Brain.

Each agent needs a memory to store experiences, regardless of the training algorithm that is used. This class represents this memory. It is an in-memory data structure that uses pandas DataFrames for its public API. The memory stores observations, actions, rewards given from the environment, and the internal reward of the agent (objective value). The memory is passed to an Objective to calculate the objective value from rewards.

Parameters:

size_limit (int = 1e6) – Maximum size the memory is allowed to grow to until old entries are overwritten by new ones.

append(muscle_uid: str, sensor_readings: List[SensorInformation] | None = None, actuator_setpoints: List[ActuatorInformation] | None = None, rewards: List[RewardInformation] | None = None, done: bool | None = None, observations: ndarray | None = None, actions: ndarray | None = None, objective: ndarray | None = None, additional_data: Dict | None = None, *, terminated: bool | None = None, truncated: bool | None = None, decision_index: int | None = None)[source]

Stores a new item in the agent’s memory (append)

Every argument that is not None is appended to the muscle’s queue for that field. An append that carries rewards creates a new row; other fields are associated with rows by their position from the end of their respective queue (see tail()).

Parameters:
  • muscle_uid (str) – UID of the agent (Muscle) whose experiences we store

  • sensor_readings (List[SensorInformation]) – A muscle’s sensor readings as provided by the environment

  • actuator_setpoints (List[ActuatorInformation]) – A muscle’s setpoints as provided to an environment

  • rewards (List[RewardInformation]) – Rewards issued by the environment. It is not necessary that sensor readings, setpoints, and rewards belong to the same time step; usually, rewards at a time step t belong to the sensor readings and actions from t-1.

  • done (bool = False) – Whether this was the last action executed in the environment

  • observations (Optional[np.ndarray] = None) – Observations the Muscle wants to share with its Brain, e.g., transformed/scaled values

  • actions (Optional[np.ndarray] = None,) – Action-related data a Muscle emitted

  • objective (Optional[np.ndarray] = None) – The agent’s objective value describing its own goal.

  • additional_data (Optional[Dict] = None) – Any additional data a Muscle wants to store. Note that tail() iterates over this object when concatenating rows (a dict contributes its keys).

  • terminated (Optional[bool] = None, keyword-only) – Whether the episode reached a terminal state (win/loss/draw).

  • truncated (Optional[bool] = None, keyword-only) – Whether the episode was truncated (e.g., time limit reached).

  • decision_index (Optional[int] = None, keyword-only) – Monotonic identity of the action in its originating worker and episode. None preserves legacy producer compatibility.

field_view(muscle_uid: str, field_name: str) → FieldView[source]

Indexed access to one stored field of one muscle.

The view addresses the field’s queue directly, i.e., with the same positions tail() uses: view[-k] is the value that the k-th last row of muscle_uid shows for this field, provided the queue holds at least k values. Values can be read and replaced in place; the structure (appending, removing) can only be changed through append() and truncate().

Example: replace the newest objective of a muscle:

objectives = memory.field_view(muscle_uid, "objective")
objectives[-1] = np.array([normalized])
Parameters:
  • muscle_uid (str) – Tag the field belongs to. The view resolves the tag on every access, so it can be created before the first append; it never registers muscle_uid as a tag.

  • field_name (str) – Name of a MemoryShard field, e.g., "objective", "rewards", "dones", or "decision_indices".

Raises:

KeyError – If field_name is not a memory field.

property tags: Set[str]

All tags known to this memory

tail(n=1, include_only: List[str] | None = None) → MemoryShard[source]

Returns the n last full entries

Returns a MemoryShard with the n most recent rows, i.e., appends that carried rewards, in chronological order. You access each value through the hierarchy, e.g.,

shard = memory.tail(10) shard.sensor_readings[“uid”].iloc[-1]

Every other field of a row is taken from the same position, counted from the end, in that field’s queue of the same muscle; if the queue is shorter, a default ([], False, NaN, or None) is used.

Parameters:
  • n (int = 1) – How many data items to return, counted from the latest addition. Defaults to 1.

  • include_only (Optional list of str, default: None) – If set, include only data from the given muscles in the list.

Returns:

The selected rows; each field is materialised once for the whole batch.

Return type:

MemoryShard

truncate(n: int)[source]

Truncates the memory: Only the last n entries are retained.

Parameters:

n (int) – How many of the most recent entries should be retained. Negative values of n are treated as abs(n).