academy.behavior¶
Behavior
¶
Agent behavior base class.
All Agent
instances execute a behavior which is
defined by a subclass of the Behavior
. Each
behavior is composed of three parts:
1. The on_startup()
and
on_shutdown()
methods
define callbacks that are invoked once at the start and end of an
agent's execution, respectively. The methods should be used to
initialize and cleanup stateful resources. Resource initialization
should not be performed in __init__
.
2. Action methods annotated with @action
are methods that other agents can invoke on this agent. An agent
may also call it's own action methods as normal methods.
3. Control loop methods annotated with @loop
are executed in separate threads when the agent is executed.
Warning
This class cannot be instantiated directly and must be subclassed.
behavior_actions
¶
Get methods of this behavior type that are decorated as actions.
Returns:
Source code in academy/behavior.py
behavior_loops
¶
behavior_loops() -> dict[str, ControlLoop]
Get methods of this behavior type that are decorated as loops.
Returns:
-
dict[str, ControlLoop]
–Dictionary mapping method names to loop methods.
Source code in academy/behavior.py
behavior_handles
¶
behavior_handles() -> dict[
str,
Handle[Any] | HandleDict[Any, Any] | HandleList[Any],
]
Get instance attributes that are agent handles.
Returns:
-
dict[str, Handle[Any] | HandleDict[Any, Any] | HandleList[Any]]
–Dictionary mapping attribute names to agent handles or data structures of handles.
Source code in academy/behavior.py
behavior_handles_bind
¶
Bind all instance attributes that are agent handles.
Parameters:
-
bind
(Callable[[Handle[BehaviorT]], Handle[BehaviorT]]
) –A callback that takes a handle and returns the same handle or a bound version of the handle.
Source code in academy/behavior.py
behavior_mro
classmethod
¶
Get the method resolution order of the behavior.
Example
>>> from academy.behavior import Behavior
>>>
>>> class A(Behavior): ...
>>> class B(Behavior): ...
>>> class C(A): ...
>>> class D(A, B): ...
>>>
>>> A.behavior_mro()
('__main__.A',)
>>> B.behavior_mro()
('__main__.B',)
>>> C.behavior_mro()
('__main__.C', '__main__.A')
>>> D.behavior_mro()
('__main__.D', '__main__.A', '__main__.B')
Returns:
Source code in academy/behavior.py
on_setup
¶
Setup up resources needed for the agents execution.
This is called before any control loop threads are started.
on_shutdown
¶
Shutdown resources after the agents execution.
This is called after control loop threads have exited.
Action
¶
ControlLoop
¶
Bases: Protocol
Control loop method protocol.
__call__
¶
__call__(shutdown: Event) -> None
Expected signature of methods decorated as a control loop.
Parameters:
-
shutdown
(Event
) –Event indicating that the agent has been instructed to shutdown and all control loops should exit.
Returns:
-
None
–Control loops should not return anything.
Source code in academy/behavior.py
action
¶
Decorator that annotates a method of a behavior as an action.
Marking a method of a behavior as an action makes the method available to other agents. I.e., peers within a multi-agent system can only invoke methods marked as actions on each other. This enables behaviors to define "private" methods.
Example
Source code in academy/behavior.py
loop
¶
Decorator that annotates a method of a behavior as a control loop.
Control loop methods of a behavior are run as threads when an agent
starts. A control loop can run for a well-defined period of time or
indefinitely, provided the control loop exits when the shutdown
event, passed as a parameter to all control loop methods, is set.
Example
Raises:
-
TypeError
–if the method signature does not conform to the
ControlLoop
protocol.
Source code in academy/behavior.py
event
¶
event(
name: str,
) -> Callable[
[Callable[[BehaviorT], None]],
Callable[[BehaviorT, Event], None],
]
Decorator that annotates a method of a behavior as an event loop.
An event loop is a special type of control loop that runs when a
threading.Event
is set. The event is cleared
after the loop runs.
Example
Parameters:
-
name
(str
) –Attribute name of the
threading.Event
to wait on.
Raises:
-
AttributeError
–Raised at runtime if no attribute named
name
exists on the behavior. -
TypeError
–Raised at runtime if the attribute named
name
is not athreading.Event
.
Source code in academy/behavior.py
timer
¶
timer(
interval: float | timedelta,
) -> Callable[
[Callable[[BehaviorT], None]],
Callable[[BehaviorT, Event], None],
]
Decorator that annotates a method of a behavior as a timer loop.
A timer loop is a special type of control loop that runs at a set interval. The method will always be called once before the first sleep.
Example
Parameters: