swarmsim.config#
Lazy initialization of built-in types
Holds registry of classes for the initialization system. prior to first use. This is to avoid circular imports.
These modules can then be accessed via the store
object
and the getting functions in this module
for use with initialization from .yaml
files or config objects.
It also provides the interface for registering third-party types/classes.
See also
- class swarmsim.config.LazyKnownModules[source]#
Holds the registry of known classes
This class is used internally by the config system.
Caution
Users should avoid adding new entries directly and instead use the functions in the
config
module.The registry will be initialized with all the built-in classes just before the first access of any of its known types. This is to avoid circular imports.
- Attributes:
- agent_types
- dictlike_types
- world_types
- swarmsim.config.store = <swarmsim.config.LazyKnownModules object>[source]#
Holds the registry of known classes. Instance of
LazyKnownModules
Functions#
- swarmsim.config.get_agent_class(config)[source]#
Retrieve the
agent
class from the agent type registry- Parameters:
config (dict or config dataclass) – A config dict with a
type
entry, or a config dataclass with aassociated_type
field.- Returns:
type – The agent class which will be used to create the agent
AgentConfig – The config
- Raises:
AttributeError –
associated_type
field not found on config dataclass ortype
entry not found in config dictKeyError – Agent type not found in registry
TypeError – Found an entry in the registry for the requested agent type, but it’s not the correct format of
(agent_class, agent_config_class)
Examples
cls, config = get_agent_class(config) agent = cls(config, world)
- swarmsim.config.get_class_from_dict(key: str, config: dict, copy=True, raise_errors=True) tuple[object, dict] | None [source]#
Retrieve a class from the registry for a given config
dict
- Parameters:
- Returns:
returns (type, modified_config_dict) * type: The class * modified_config_dict: The config dict with the
type
key removed- Return type:
- Raises:
AttributeError – The config dict is missing the
type
keyKeyError – The namespace or
type
was not found in the registry.TypeError – Received a config that is not of
type(dict)
Examples
from swarmsim.config import get_class_from_dict from swarmsim.agent.StaticAgent import StaticAgent, StaticAgentConfig agent = StaticAgent(StaticAgentConfig(), world=None) config = {'type': 'StaticController', 'output': [0.1, 0]} cls = get_class_from_dict('controller', config) controller = cls(agent, config)
- swarmsim.config.register_agent_type(name: str, agent_type, agent_config=None)[source]#
Register an agent class with the config system
- Parameters:
name (str) –
The name of the agent type, i.e. ‘MazeAgent’
We recommend choosing it as the name of the
agent
class, i.e.agent_type.__name__
agent_type (_type_) – The
agent
class to registeragent_config (_type_) – The agent config dataclass type to register
- swarmsim.config.register_world_type(name: str, world_type, world_config=None)[source]#
Register a world class with the config system
- Parameters:
name (str) –
The name of the world type, i.e. ‘RectangularWorld’
We recommend choosing it as the name of the
world
class, i.e.world_type.__name__
world_type (_type_) – The
world
class to registerworld_config (_type_) – The world config dataclass type to register
- swarmsim.config.register_dictlike_namespace(key: str)[source]#
Register a new namespace for classes configured using dicts
- Parameters:
key (str) – The namespace to register, i.e. ‘controller’, ‘spawner’, etc.
Decorators#
- swarmsim.config.associated_type(type_name: str)[source]#
Decorate a config dataclass to add an
associated_type
fieldNormally, dataclasses will raise an error if you try to create an instance with an argument that is not in the dataclass.
The decorator cause the dataclass to detect unexpected arguments and set them as attributes on the config object.
Examples
MyAgentConfig.py#from dataclasses import dataclass from swarmsim.config import associated_type, filter_unexpected_fields @associated_type("MyAgent") @filter_unexpected_fields # optional @dataclass class MyAgentConfig: my_custom_field: int = 999 config = MyAgentConfig() config.associated_type == "MyAgent" # True
- swarmsim.config.filter_unexpected_fields(cls)[source]#
Decorate a dataclass to filter out unexpected fields
Normally, dataclasses will raise an error if you try to create an instance with an argument that is not in the dataclass.
The decorator cause the dataclass to detect unexpected arguments and set them as attributes on the config object.
Examples
@filter_unexpected_fields @dataclass class MyAgentConfig: my_custom_field: int = 999 config = MyAgentConfig(unexpected_field="hello") config.unexpected_field == "hello" # True
See also
There is no swarmsim.config.get_world_class
function,
world type lookup is handled inside World_from_config
.
Module Attributes
Holds the registry of known classes. |
Functions
|
Decorate a config dataclass to add an |
Decorate a dataclass to filter out unexpected fields |
|
|
Retrieve the |
|
Retrieve a class from the registry for a given config |
Makes the config system aware of all the built-in classes |
|
|
Register an agent class with the config system |
Register a new namespace for classes configured using dicts |
|
|
Register a class for a given namespace |
|
Register a world class with the config system |
Classes
Holds the registry of known classes |
Modules
This class defines a configuration for how the World will output a large numpy array representing the pixels on the screen |
|