swarmsim.agent.Agent#

Base Agent Config and Class to be inherited by all agents.

Inheritance diagram of swarmsim.agent.Agent.Agent

class swarmsim.agent.Agent.BaseAgentConfig(position: tuple[float, ...] | numpy.ndarray = (0, 0), angle: float | typing.Any = 0.0, name: str | typing.Any = None, controller: typing.Any | None = None, grounded: bool = False, collides: bool | int = False, sensors: list = <factory>, team: str | None = None)[source]#
Attributes:
controller
name
team

Methods

as_config_dict

as_dict

as_generator

asdict

from_dict

angle: float | Any = 0.0[source]#
as_config_dict()[source]#
as_dict()[source]#
as_generator()[source]#
asdict()[source]#
collides: bool | int = False[source]#
controller: Any | None = None[source]#
classmethod from_dict(env)[source]#
grounded: bool = False[source]#
name: str | Any = None[source]#
position: tuple[float, ...] | ndarray = (0, 0)[source]#
sensors: list[source]#
team: str | None = None[source]#
class swarmsim.agent.Agent.Agent(config, world, name=None, group=0, initialize=True)[source]#
Attributes:
position

Alias for pos.

Methods

copy()

Create a copy of this agent.

from_config(config, world)

Returns a new agent instance from the given config and world.

getPosition()

Alias for pos.

getVelocity()

Alias for dpos.

get_aabb()

Get the agent's AABB.

get_sensors()

Alias for sensors.

orientation_uvec([offset])

Returns the agent's 2D orientation matrix.

set_heading(new_heading)

Set the agent's heading (radians).

set_name(new_name)

Set the name of the agent.

set_pos_vec(vec)

Set the x, y, and angle of the agent.

setup_controller_from_config()

Creates and adds the AgentConfig.controller to the agent.

setup_sensors_from_config()

Creates and adds sensors from the AgentConfig.sensors to the agent.

attach_agent_to_sensors

draw

getFrontalPoint

get_heading

get_name

get_x_pos

get_y_pos

on_key_press

set_x_pos

set_y_pos

step

aabb[source]#

The agent’s AABB. This gets built at least once on init.

angle[source]#

Agent’s heading in radians. 0 is facing frame right.

attach_agent_to_sensors()[source]#
collides[source]#

If True, the agent should be solid.

collision_flag[source]#

Colliders should set to True if a collision was detected.

config[source]#

Agent config.

controller[source]#

The Controller for this agent.

copy()[source]#

Create a copy of this agent.

Returns:

The new agent has the same type as the class/instance it was called from.

Return type:

Agent

Almost all attributes are deep-copied from this agent. However, some attributes do not get recursively deep-copied, such as the world attribute.

The Agent._always_shallow_copy attribute is a list of agent attribute names which determines which attributes do not get deep-copied. By default, this is ['world'].

On the new agent, Attributes of this agent not in this list will be a deep-copy of those in the original agent.

Attributes of this agent in this list will share the same reference as the original agent.

Examples

>>> agent = Agent(config, world)
>>> agent.copy().world is agent.world
True
>>> copy.deepcopy(agent).world is agent.world
False
dpos: ndarray[source]#

Change in position since last step

draw(screen, offset=((0, 0), 1.0)) None[source]#
dtheta[source]#

Change in heading since last step.

classmethod from_config(config, world)[source]#

Returns a new agent instance from the given config and world.

Parameters:
  • config (AgentConfig) – The config to create the agent from.

  • world (World) – This provides a back-reference to the world instance for the new agent.

Returns:

The new agent has the same type as the class/instance it was called from.

Return type:

Agent

getFrontalPoint(offset=0) tuple[source]#
getPosition()[source]#

Alias for pos.

getVelocity()[source]#

Alias for dpos.

get_aabb() AABB[source]#

Get the agent’s AABB.

get_heading() float[source]#
get_name()[source]#
get_sensors()[source]#

Alias for sensors.

get_x_pos() float[source]#
get_y_pos() float[source]#
grounded[source]#

If True, the agent should never change position or be pushed during collision resolution.

name: str | None[source]#

Agent’s name.

on_key_press(event)[source]#
orientation_uvec(offset=0.0)[source]#

Returns the agent’s 2D orientation matrix.

This is a unit vector pointing in the direction of the agent’s heading plus an offset.

Equivalent to

np.array([
    math.cos(self.angle + offset),
    math.sin(self.angle + offset)
], dtype=np.float64)
Parameters:

offset (float, default=0) – The offset to add to the agent’s heading (radians).

Returns:

The 2D orientation matrix.

Return type:

numpy.ndarray (2,)

pos: ndarray[source]#

Agent’s position in the world.

property position[source]#

Alias for pos.

sensors: list[source]#

List of this agent’s sensors.

set_heading(new_heading: float)[source]#

Set the agent’s heading (radians).

set_name(new_name: str | None | Any)[source]#

Set the name of the agent.

Parameters:

new_name (str | None | Any) – The new name of the agent. Strongly recommended to use str.

set_pos_vec(vec)[source]#

Set the x, y, and angle of the agent.

Parameters:

vec (tuple[float, float, float] | numpy.ndarray) – A vector with len() of 3 containing the x, y, angle

set_x_pos(new_x: float)[source]#
set_y_pos(new_y: float)[source]#
setup_controller_from_config()[source]#

Creates and adds the AgentConfig.controller to the agent.

If config.controller is a dict with a type key, then it is created through the Configuration System and set as the agent’s controller. AbstractController instances copied and added to the list.

The controller is also given a back-reference to this agent via its set_agent().

Raises:

TypeError – if the element is not a dict or AbstractSensor instance.

setup_sensors_from_config()[source]#

Creates and adds sensors from the AgentConfig.sensors to the agent.

For each element of config.sensors, if the element is a dict with a type key, then it is created through the Configuration System and then added to the agent’s sensors list. AbstractSensor instances copied and added to the list.

The new sensor instances are also given a back-reference to this agent via their set_agent().

Raises:

TypeError – if the element is not a dict or AbstractSensor instance.

step(*args, **kwargs) None[source]#
team[source]#

The agent’s team.

world[source]#

Back-reference to the world instance.

Classes

Agent(config, world[, name, group, initialize])

BaseAgentConfig(position, ...)