Config Registry#
The config module stores references to classes and config dataclasses. It’s used internally to know how to create many types of objects in the simulator:
This allows you to register your own class definitions which will work in .yaml
files.
See also
See the swarmsim.config
module for the API.
See the Our YAML module for information on RobotSwarmSimulator’s custom .yaml
parser and tags
agent
and world
Configs#
RobotSwarmSimulator uses dataclasses to define the configurations for agents and worlds. These dataclasses are used to create the corresponding objects in the simulator.
For example, let’s create a new agent class called MyAgent
that has a MyAgentConfig
dataclass.
from dataclasses import dataclass
from swarmsim.config import associated_type, filter_unexpected_fields
from swarmsim.agent.BaseAgent import Agent, BaseAgentConfig
@associated_type("MyAgent")
@filter_unexpected_fields
@dataclass
class MyAgentConfig(BaseAgentConfig):
my_custom_field: int = 999
class MyAgent(Agent):
pass
The associated_type()
decorator associates the MyAgentConfig
dataclass
with the MyAgent
class by adding a config.type = 'MyAgent'
field to the dataclass.
from swarmsim.config import register_agent_type
from MyAgent import MyAgent, MyAgentConfig
register_agent_type('MyAgent', MyAgent, MyAgentConfig)
Once your agent class is registered with the config system, you can
load a .yaml
file with a type: MyAgent
field, RectangularWorld
will know how to create a MyAgentConfig
from your .yaml
and
subsequently create an instance of MyAgent
.
type: "RectangularWorld"
agents:
- type: MyAgent # this becomes MyAgentConfig
my_custom_field: 100
A similar system is used for World
types and their
associated config classes, but there’s currently only one world type: RectangularWorld
Note that objects
are a special type of agent
,
so they also use this system.
Everything Else (dict-like config objects)#
For everything that isn’t an agent
or world
,
the config system doesn’t use dataclasses.
Instead, it uses a dictionary-like object that has a type
field.
This includes everything from spawners
to metrics
and control
.
For example, the StaticController
has a type
field
that is used to determine how to create the controller.
from swarmsim.agent.control.Controller import AbstractController
class SpinningController(AbstractController):
def __init__(self, parent,
angular_velocity: float,
):
super().__init__(parent)
self.angular_velocity = angular_velocity
def get_actions(self, agent):
return 0, self.angular_velocity
Then, register the controller with the config system:
from swarmsim.config import register_dictlike_type
from SpinningController import SpinningController
register_dictlike_type('controller', 'SpinningController', SpinningController)
And then you can use it in a .yaml
file:
type: "RectangularWorld"
agents:
- type: MyAgent
controller:
type: SpinningController
angular_velocity: 0.1 # rad/s