Interaction
pamiq_core.interaction.Agent ¶
Bases: ABC
, InteractionEventMixin
, PersistentStateMixin
, ThreadEventMixin
Base agent class for decision making.
An agent receives observations from an environment and decides on actions to take in response. This abstract class defines the interface that all agent implementations must follow.
Agents can contain child agents that will inherit the parent's inference models and data collectors. State persistence and Thread Event is also propagated to all child agents.
Initialize the agent.
PARAMETER | DESCRIPTION |
---|---|
agents
|
Optional mapping of names to child agents. Child agents will inherit inference models and data collectors from the parent, and their states will be saved and loaded together with the parent.
TYPE:
|
Source code in src/pamiq_core/interaction/agent.py
step
abstractmethod
¶
Processes an observation and determines the next action.
PARAMETER | DESCRIPTION |
---|---|
observation
|
The current observation from the environment.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ActType
|
The action to take in response to the observation. |
Source code in src/pamiq_core/interaction/agent.py
attach_inference_models ¶
Attaches inference models dictionary to this agent.
This method is called to provide the agent with access to inference models
for decision making. After attaching, the callback method
on_inference_models_attached
is called. If the agent has child agents,
the models are also attached to them.
PARAMETER | DESCRIPTION |
---|---|
inference_models
|
Dictionary of inference models to attach.
TYPE:
|
Source code in src/pamiq_core/interaction/agent.py
on_inference_models_attached ¶
Callback method for when inference models are attached to the agent.
Override this method to retrieve DNN models for inference. Use
get_inference_model
to retrieve a model.
Source code in src/pamiq_core/interaction/agent.py
attach_data_collectors ¶
Attaches data collectors dictionary to this agent.
This method is called to provide the agent with access to data collectors
for collecting experience data. After attaching, the callback method
on_data_collectors_attached
is called. If the agent has child agents,
the collectors are also attached to them.
PARAMETER | DESCRIPTION |
---|---|
data_collectors
|
Dictionary of data collectors to attach.
TYPE:
|
Source code in src/pamiq_core/interaction/agent.py
on_data_collectors_attached ¶
Callback method for when data collectors are attached to this agent.
Override this method to set up data collection for the agent. Use
get_data_collector
to acquire a collector.
Source code in src/pamiq_core/interaction/agent.py
get_inference_model ¶
Retrieves an inference model by name.
PARAMETER | DESCRIPTION |
---|---|
name
|
Name of the inference model to retrieve.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
InferenceModel
|
The requested inference model. |
RAISES | DESCRIPTION |
---|---|
KeyError
|
If the model with the specified name does not exist. |
Source code in src/pamiq_core/interaction/agent.py
get_data_collector ¶
Acquires a data collector by name for exclusive use.
This method acquires a data collector for exclusive use within the current step. The collector can only be acquired once at a time.
PARAMETER | DESCRIPTION |
---|---|
name
|
Name of the data collector to acquire.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
DataCollector[Any]
|
The requested data collector. |
RAISES | DESCRIPTION |
---|---|
KeyError
|
If the collector is already acquired or not found. |
Source code in src/pamiq_core/interaction/agent.py
setup ¶
Handle interaction setup event.
Propagates the event to all child agents.
teardown ¶
Handle interaction teardown event.
Propagates the event to all child agents.
save_state ¶
Save the agent's state and the states of any child agents.
PARAMETER | DESCRIPTION |
---|---|
path
|
Directory path where to save the states.
TYPE:
|
Source code in src/pamiq_core/interaction/agent.py
load_state ¶
Load the agent's state and the states of any child agents.
PARAMETER | DESCRIPTION |
---|---|
path
|
Directory path from where to load the states.
TYPE:
|
Source code in src/pamiq_core/interaction/agent.py
on_paused ¶
Handle system pause event.
Propagates the pause event to all child agents.
on_resumed ¶
Handle system resume event.
Propagates the resume event to all child agents.
pamiq_core.interaction.Environment ¶
Bases: ABC
, InteractionEventMixin
, PersistentStateMixin
, ThreadEventMixin
Base environment class for agent interaction.
pamiq_core.interaction.Interaction ¶
Bases: InteractionEventMixin
, PersistentStateMixin
, ThreadEventMixin
Class that combines an agent and environment to create an interaction loop.
This class manages the interaction between an agent and its environment, implementing a basic observe-decide-act loop. It also handles state persistence and lifecycle management for both components.
Initialize interaction with an agent and environment.
PARAMETER | DESCRIPTION |
---|---|
agent
|
The agent that makes decisions based on observations.
TYPE:
|
environment
|
The environment that provides observations and receives actions.
TYPE:
|
Source code in src/pamiq_core/interaction/interactions.py
step ¶
Execute one step of the agent-environment interaction loop.
Gets an observation from the environment, passes it to the agent to get an action, and then applies that action to the environment.
Source code in src/pamiq_core/interaction/interactions.py
setup ¶
Initialize the interaction by setting up agent and environment.
Calls the setup methods of both the agent and environment.
Source code in src/pamiq_core/interaction/interactions.py
teardown ¶
Clean up the interaction by tearing down agent and environment.
Calls the teardown methods of both the agent and environment.
Source code in src/pamiq_core/interaction/interactions.py
save_state ¶
Save the current state of the interaction to the specified path.
Creates a directory at the given path and saves the states of both the agent and environment in subdirectories.
PARAMETER | DESCRIPTION |
---|---|
path
|
Directory path where to save the interaction state.
TYPE:
|
Source code in src/pamiq_core/interaction/interactions.py
load_state ¶
Load the interaction state from the specified path.
Loads the states of both the agent and environment from subdirectories at the given path.
PARAMETER | DESCRIPTION |
---|---|
path
|
Directory path from where to load the interaction state.
TYPE:
|
Source code in src/pamiq_core/interaction/interactions.py
on_paused ¶
on_resumed ¶
The method to be called when the thread is resumed.
pamiq_core.interaction.FixedIntervalInteraction ¶
FixedIntervalInteraction(
agent: Agent[ObsType, ActType],
environment: Environment[ObsType, ActType],
adjustor: IntervalAdjustor,
)
Bases: Interaction[ObsType, ActType]
Interaction class that executes steps at fixed time intervals.
This class extends the base Interaction to maintain a consistent timing between steps using an IntervalAdjustor. It ensures the agent-environment interaction loop runs at a specified frequency regardless of how long individual steps take to compute.
Initialize the fixed interval interaction.
PARAMETER | DESCRIPTION |
---|---|
agent
|
The agent that makes decisions based on observations.
TYPE:
|
environment
|
The environment that provides observations and receives actions.
TYPE:
|
adjustor
|
The interval adjustor that maintains consistent timing between steps.
TYPE:
|
Source code in src/pamiq_core/interaction/interactions.py
setup ¶
step ¶
with_sleep_adjustor
classmethod
¶
with_sleep_adjustor(
agent: Agent[ObsType, ActType],
environment: Environment[ObsType, ActType],
interval: float,
offset: float = 0.0,
) -> Self
Create a FixedIntervalInteraction with a SleepIntervalAdjustor.
PARAMETER | DESCRIPTION |
---|---|
agent
|
The agent that makes decisions based on observations.
TYPE:
|
environment
|
The environment that provides observations and receives actions.
TYPE:
|
interval
|
The desired time between each step in seconds.
TYPE:
|
offset
|
Optional initial time offset to adjust for system-specific timing differences. Defaults to 0.0.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Self
|
A new FixedIntervalInteraction instance configured with a SleepIntervalAdjustor. |
Source code in src/pamiq_core/interaction/interactions.py
pamiq_core.interaction.interval_adjustors ¶
IntervalAdjustor ¶
Bases: ABC
Adjusts the loop to run at a fixed interval.
Initial reset time is set to negative infinity for mathematical reasons.
Constructs the IntervalAdjustor.
PARAMETER | DESCRIPTION |
---|---|
interval
|
The desired time between each invocation in seconds.
TYPE:
|
offset
|
The initial time offset to subtract from the interval on the first loop iteration, allowing adjustment of the actual interval for each computer using this parameter.
TYPE:
|
Source code in src/pamiq_core/interaction/interval_adjustors.py
reset ¶
Resets the start time of this adjustor to the current time.
RETURNS | DESCRIPTION |
---|---|
float
|
The start time after resetting the timer, as a high precision time counter.
TYPE:
|
Source code in src/pamiq_core/interaction/interval_adjustors.py
adjust_impl
abstractmethod
¶
The actual implementation of the interval adjustor, which is wrapped
by the public method adjust
.
adjust ¶
Waits until the interval has elapsed since the last adjust
or
reset
call.
RETURNS | DESCRIPTION |
---|---|
float
|
The elapsed time since the last call, ensuring the loop runs at the specified interval.
TYPE:
|
Source code in src/pamiq_core/interaction/interval_adjustors.py
SleepIntervalAdjustor ¶
Bases: IntervalAdjustor
Adjusts the interval using time.sleep
to pause execution until the
next interval begins.
Source code in src/pamiq_core/interaction/interval_adjustors.py
pamiq_core.interaction.modular_env ¶
Sensor ¶
Bases: ABC
, InteractionEventMixin
, PersistentStateMixin
, ThreadEventMixin
Abstract base class for sensors that read data from the environment.
This class provides an interface for reading observations from various sensors. Implementations should handle the specific logic for acquiring sensor readings.
read
abstractmethod
¶
Actuator ¶
Bases: ABC
, InteractionEventMixin
, PersistentStateMixin
, ThreadEventMixin
Abstract base class for actuators that affect the environment.
This class provides an interface for operating actuators based on action commands. Implementations should handle the specific logic for executing actions through hardware or simulation interfaces.
operate
abstractmethod
¶
Execute the specified action through the actuator.
PARAMETER | DESCRIPTION |
---|---|
action
|
The action to be executed.
TYPE:
|
ModularEnvironment ¶
Bases: Environment[ObsType, ActType]
Environment implementation that uses a Sensor and Actuator.
This class provides a modular approach to environment implementation by separating the sensing (observation) and actuation components.
Initialize with a sensor and actuator.
PARAMETER | DESCRIPTION |
---|---|
sensor
|
Component to read observations from the environment.
TYPE:
|
actuator
|
Component to execute actions in the environment.
TYPE:
|
Source code in src/pamiq_core/interaction/modular_env.py
observe ¶
Get observations from the environment using the sensor.
RETURNS | DESCRIPTION |
---|---|
ObsType
|
Current observation from the sensor. |
affect ¶
Apply an action to the environment using the actuator.
PARAMETER | DESCRIPTION |
---|---|
action
|
The action to apply to the environment.
TYPE:
|
setup ¶
Set up the environment by initializing sensor and actuator.
This method is called before starting interaction with the environment.
teardown ¶
Clean up the environment by finalizing sensor and actuator.
This method is called after finishing interaction with the environment.
Source code in src/pamiq_core/interaction/modular_env.py
save_state ¶
Save the state of the environment to the specified path.
Creates a directory at the given path and saves the states of the sensor and actuator in subdirectories.
PARAMETER | DESCRIPTION |
---|---|
path
|
Directory path where to save the environment state.
TYPE:
|
Source code in src/pamiq_core/interaction/modular_env.py
load_state ¶
Load the state of the environment from the specified path.
Loads the states of the sensor and actuator from subdirectories at the given path.
PARAMETER | DESCRIPTION |
---|---|
path
|
Directory path from where to load the environment state.
TYPE:
|
Source code in src/pamiq_core/interaction/modular_env.py
on_paused ¶
on_resumed ¶
The method to be called when the thread is resumed.
from_dict
staticmethod
¶
from_dict(
sensors: Mapping[str, Sensor[Any]], actuators: Mapping[str, Actuator[Any]]
) -> ModularEnvironment[Mapping[str, Any], Mapping[str, Any]]
Create a modular environment from dictionaries of sensors and actuators.
PARAMETER | DESCRIPTION |
---|---|
sensors
|
A mapping of sensor names to sensor instances.
TYPE:
|
actuators
|
A mapping of actuator names to actuator instances.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ModularEnvironment[Mapping[str, Any], Mapping[str, Any]]
|
A modular environment that uses composite sensors and actuators. |
Example
Source code in src/pamiq_core/interaction/modular_env.py
SensorsDict ¶
Bases: Sensor[Mapping[str, Any]]
, UserDict[str, Sensor[Any]]
Dictionary of sensors that acts as a composite sensor.
This class allows grouping multiple sensors together under a single interface. When read, it returns a mapping of sensor names to their readings, providing a way to collect data from multiple sensors simultaneously.
All lifecycle events (setup, teardown, pause, resume) and state persistence operations are propagated to all contained sensors.
read ¶
Read data from all contained sensors.
RETURNS | DESCRIPTION |
---|---|
Mapping[str, Any]
|
A mapping from sensor names to their respective readings. |
setup ¶
teardown ¶
save_state ¶
Save the state of all contained sensors.
Creates a directory at the given path and saves each sensor's state in a subdirectory named after its key in this dictionary.
PARAMETER | DESCRIPTION |
---|---|
path
|
Directory path where to save the state.
TYPE:
|
Source code in src/pamiq_core/interaction/modular_env.py
load_state ¶
Load the state of all contained sensors.
Loads each sensor's state from a subdirectory named after its key in this dictionary.
PARAMETER | DESCRIPTION |
---|---|
path
|
Directory path from where to load the state.
TYPE:
|
Source code in src/pamiq_core/interaction/modular_env.py
on_paused ¶
on_resumed ¶
ActuatorsDict ¶
Bases: Actuator[Mapping[str, Any]]
, UserDict[str, Actuator[Any]]
Dictionary of actuators that acts as a composite actuator.
This class allows grouping multiple actuators together under a single interface. When operated, it distributes actions to individual actuators based on their keys.
All lifecycle events (setup, teardown, pause, resume) and state persistence operations are propagated to all contained actuators.
operate ¶
Operate all contained actuators with their respective actions.
Distributes the actions to the appropriate actuators based on their keys.
PARAMETER | DESCRIPTION |
---|---|
action
|
A mapping from actuator names to their respective actions. Each action will be passed to the corresponding actuator.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
KeyError
|
If action doesn't contain a required actuator key. |
Source code in src/pamiq_core/interaction/modular_env.py
setup ¶
teardown ¶
save_state ¶
Save the state of all contained actuators.
Creates a directory at the given path and saves each actuator's state in a subdirectory named after its key in this dictionary.
PARAMETER | DESCRIPTION |
---|---|
path
|
Directory path where to save the state.
TYPE:
|
Source code in src/pamiq_core/interaction/modular_env.py
load_state ¶
Load the state of all contained actuators.
Loads each actuator's state from a subdirectory named after its key in this dictionary.
PARAMETER | DESCRIPTION |
---|---|
path
|
Directory path from where to load the state.
TYPE:
|
Source code in src/pamiq_core/interaction/modular_env.py
on_paused ¶
on_resumed ¶
pamiq_core.interaction.wrappers ¶
Wrapper ¶
Bases: ABC
, InteractionEventMixin
, PersistentStateMixin
, ThreadEventMixin
Base wrapper class for transforming values.
This abstract class provides an interface for wrapping and transforming values from one type to another. Subclasses should implement the wrap method to define the specific transformation.
wrap
abstractmethod
¶
Transform the input value to another value.
PARAMETER | DESCRIPTION |
---|---|
value
|
Input value to be transformed.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
W
|
Transformed value. |
__call__ ¶
Enable calling the wrapper as a function.
PARAMETER | DESCRIPTION |
---|---|
value
|
Input value to be transformed.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
W
|
Transformed value. |
wrap_sensor ¶
Create a SensorWrapper that combines this wrapper with the provided sensor.
This is a convenience method that applies the current wrapper to a sensor, creating a SensorWrapper that will transform the sensor's readings using this wrapper's transformation logic.
PARAMETER | DESCRIPTION |
---|---|
sensor
|
The sensor to wrap with this wrapper.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
SensorWrapper[T, W]
|
A new SensorWrapper that applies this wrapper's transformation to the sensor. |
Source code in src/pamiq_core/interaction/wrappers.py
wrap_actuator ¶
Create an ActuatorWrapper that combines this wrapper with the provided actuator.
This is a convenience method that applies the current wrapper to an actuator, creating an ActuatorWrapper that will transform actions before passing them to the actuator using this wrapper's transformation logic.
PARAMETER | DESCRIPTION |
---|---|
actuator
|
The actuator to wrap with this wrapper.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ActuatorWrapper[W, T]
|
A new ActuatorWrapper that applies this wrapper's transformation to actions. |
Source code in src/pamiq_core/interaction/wrappers.py
LambdaWrapper ¶
Bases: Wrapper[T, W]
Wrapper that uses a callable function to transform values.
This wrapper enables using lambda functions or any callable to perform the transformation.
Initialize with a transformation function.
PARAMETER | DESCRIPTION |
---|---|
func
|
Function that transforms input values of type T to type W.
TYPE:
|
Source code in src/pamiq_core/interaction/wrappers.py
wrap ¶
Transform the value using the provided function.
PARAMETER | DESCRIPTION |
---|---|
value
|
Input value to be transformed.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
W
|
Transformed value. |
EnvironmentWrapper ¶
EnvironmentWrapper(
env: Environment[ObsType, ActType],
obs_wrapper: Wrapper[ObsType, WrappedObsType] | Callable[[ObsType], WrappedObsType],
act_wrapper: Wrapper[WrappedActType, ActType] | Callable[[WrappedActType], ActType],
)
Bases: Environment[WrappedObsType, WrappedActType]
Wrapper for Environment that transforms observations and actions.
This wrapper transforms the observations returned by the wrapped environment and the actions passed to it.
Initialize with an environment and wrappers.
PARAMETER | DESCRIPTION |
---|---|
env
|
The environment to wrap.
TYPE:
|
obs_wrapper
|
Wrapper for transforming observations from the environment.
TYPE:
|
act_wrapper
|
Wrapper for transforming actions before passing to the environment.
TYPE:
|
Source code in src/pamiq_core/interaction/wrappers.py
observe ¶
Get wrapped observation from the environment.
RETURNS | DESCRIPTION |
---|---|
WrappedObsType
|
Transformed observation. |
affect ¶
Apply wrapped action to the environment.
PARAMETER | DESCRIPTION |
---|---|
action
|
Action to be transformed and applied.
TYPE:
|
setup ¶
teardown ¶
save_state ¶
Save state of the wrapped environment and wrappers.
PARAMETER | DESCRIPTION |
---|---|
path
|
Directory path where to save the state.
TYPE:
|
Source code in src/pamiq_core/interaction/wrappers.py
load_state ¶
Load state of the wrapped environment and wrappers.
PARAMETER | DESCRIPTION |
---|---|
path
|
Directory path from where to load the state.
TYPE:
|
Source code in src/pamiq_core/interaction/wrappers.py
on_paused ¶
The method to be called when the thread is paused.
on_resumed ¶
The method to be called when the thread is on_resumed.
SensorWrapper ¶
Bases: Sensor[W]
Wrapper for Sensor that transforms sensor readings.
This wrapper applies a transformation to the readings from the wrapped sensor.
Initialize with a sensor and a wrapper.
PARAMETER | DESCRIPTION |
---|---|
sensor
|
The sensor to wrap.
TYPE:
|
wrapper
|
Wrapper for transforming sensor readings.
TYPE:
|
Source code in src/pamiq_core/interaction/wrappers.py
read ¶
Get transformed reading from the sensor.
RETURNS | DESCRIPTION |
---|---|
W
|
Transformed sensor reading. |
setup ¶
teardown ¶
save_state ¶
Save state of the wrapped sensor and wrapper.
PARAMETER | DESCRIPTION |
---|---|
path
|
Directory path where to save the state.
TYPE:
|
Source code in src/pamiq_core/interaction/wrappers.py
load_state ¶
Load state of the wrapped sensor and wrapper.
PARAMETER | DESCRIPTION |
---|---|
path
|
Directory path from where to load the state.
TYPE:
|
Source code in src/pamiq_core/interaction/wrappers.py
on_paused ¶
on_resumed ¶
ActuatorWrapper ¶
Bases: Actuator[W]
Wrapper for Actuator that transforms actions.
This wrapper applies a transformation to actions before passing them to the wrapped actuator.
Initialize with an actuator and a wrapper.
PARAMETER | DESCRIPTION |
---|---|
actuator
|
The actuator to wrap.
TYPE:
|
wrapper
|
Wrapper for transforming actions.
TYPE:
|
Source code in src/pamiq_core/interaction/wrappers.py
operate ¶
Apply transformed action to the actuator.
PARAMETER | DESCRIPTION |
---|---|
action
|
Action to be transformed and applied.
TYPE:
|
setup ¶
teardown ¶
save_state ¶
Save state of the wrapped actuator and wrapper.
PARAMETER | DESCRIPTION |
---|---|
path
|
Directory path where to save the state.
TYPE:
|
Source code in src/pamiq_core/interaction/wrappers.py
load_state ¶
Load state of the wrapped actuator and wrapper.
PARAMETER | DESCRIPTION |
---|---|
path
|
Directory path from where to load the state.
TYPE:
|
Source code in src/pamiq_core/interaction/wrappers.py
on_paused ¶
on_resumed ¶
The method to be called when the thread is resumed.