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
Objectiveto 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
Noneis appended to the muscle’s queue for that field. An append that carriesrewardscreates a new row; other fields are associated with rows by their position from the end of their respective queue (seetail()).- Parameters:
muscle_uid (str) – UID of the agent (
Muscle) whose experiences we storesensor_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
tbelong to the sensor readings and actions fromt-1.done (bool = False) – Whether this was the last action executed in the environment
observations (Optional[np.ndarray] = None) – Observations the
Musclewants to share with itsBrain, e.g., transformed/scaled valuesactions (Optional[np.ndarray] = None,) – Action-related data a
Muscleemittedobjective (Optional[np.ndarray] = None) – The agent’s objective value describing its own goal.
additional_data (Optional[Dict] = None) – Any additional data a
Musclewants to store. Note thattail()iterates over this object when concatenating rows (adictcontributes 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.
Nonepreserves 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 thek-th last row ofmuscle_uidshows for this field, provided the queue holds at leastkvalues. Values can be read and replaced in place; the structure (appending, removing) can only be changed throughappend()andtruncate().Example: replace the newest objective of a muscle:
objectives = memory.field_view(muscle_uid, "objective") objectives[-1] = np.array([normalized])
- Parameters:
- Raises:
KeyError – If
field_nameis not a memory field.
- tail(n=1, include_only: List[str] | None = None) MemoryShard[source]
Returns the n last full entries
Returns a
MemoryShardwith the n most recent rows, i.e., appends that carriedrewards, 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, orNone) 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