Swarmtaxis
The swarmtaxis algorithm, which moves a swarm of robots towards a beacon.
- Class:
SwarmtaxisSystem - Import:
from skcomplex.systems.engineering.robotics import SwarmtaxisSystem - Field:
engineering/robotics - Topics:
robotics,swarms,agents,taxis - References:
- Bjerknes, Winfield, and Melhuish 2007
- Varughese et al. 2019
Use this system
The block imports the class and simulates the system with its default parameters and its tuned run settings.
from skcomplex.systems.engineering.robotics import SwarmtaxisSystem
trajectory = SwarmtaxisSystem().prepare().simulate()
Parameters
The model parameters take these defaults, and set_params changes them.
| Parameter | Default |
|---|---|
n_robots |
20 |
size |
100.0 |
spread |
3.0 |
speed |
0.2 |
radio_range |
4.0 |
avoid_illuminated |
1.5 |
avoid_shadowed |
1.0 |
random_state |
0 |
solver |
'auto' |
backend |
'auto' |
Tags
The system with its default parameters has these capability tags, which solvers and analyses check.
| Tag | Values |
|---|---|
| Time | discrete |
| Space | continuous |
| Laws | update |
| Randomness | stochastic |
| Domains | boolean, integer, real |
| Structure | variable membership |
Class
The source of the class shows how it builds its components from its model parameters.
class SwarmtaxisSystem(BaseSystem):
"""The swarmtaxis algorithm, which moves a swarm of robots towards a beacon.
Only some of the robots see the beacon. The robots start in a cluster near the left edge of a square arena, with the
beacon at the middle of the right edge. Each robot keeps radio contact with the swarm by turning around when it
loses a connection, and avoids its neighbours. Robots that see the beacon avoid at a larger radius than shadowed
robots, which moves the centre of the swarm towards the beacon. The system has the group `robots` of headings
`heading` and illumination `illuminated`.
Args:
n_robots:
The number of robots.
size:
The side of the arena.
spread:
The standard deviation of the initial positions around the start.
speed:
The distance a robot moves per step.
radio_range:
The range of the radio pings.
avoid_illuminated:
The avoidance radius of illuminated robots.
avoid_shadowed:
The avoidance radius of shadowed robots.
random_state:
The seed or generator of the initial positions and headings.
label:
The label of the system, which names it when it is nested in another one.
solver:
The solver of the problem, or `'auto'` for the one the system chooses.
backend:
The backend that evaluates the compiled functions, or `'auto'` for the one the system chooses.
Examples:
>>> from skcomplex.systems.engineering.robotics import SwarmtaxisSystem
>>> SwarmtaxisSystem(5).prepare().system_.elements[0].size
5
"""
field = 'engineering/robotics'
topics = ('robotics', 'swarms', 'agents', 'taxis')
references = ('Bjerknes, Winfield, and Melhuish 2007', 'Varughese et al. 2019')
until = 300
def __init__(
self,
n_robots: int = 20,
*,
size: float = 100.0,
spread: float = 3.0,
speed: float = 0.2,
radio_range: float = 4.0,
avoid_illuminated: float = 1.5,
avoid_shadowed: float = 1.0,
random_state: int | np.random.Generator | None = 0,
label: str = 'system',
solver: BaseSolver | str = 'auto',
backend: BaseBackend | str = 'auto',
) -> None:
super().__init__(label, solver=solver, backend=backend)
self.n_robots = n_robots
self.size = size
self.spread = spread
self.speed = speed
self.radio_range = radio_range
self.avoid_illuminated = avoid_illuminated
self.avoid_shadowed = avoid_shadowed
self.random_state = random_state
def _prepare(self, context: Sequence[Observable] | None = None) -> None:
"""Set `system_` to the components of the system built from its model parameters."""
super()._prepare(context)
rng = np.random.default_rng(self.random_state)
start = np.array([self.size / 5, self.size / 2])
points = np.clip(start + rng.normal(0.0, self.spread, (self.n_robots, 2)), 0.0, self.size)
arena = ContinuousSpace(extent=(self.size, self.size))
robots = SwarmRobotElement(
'robots',
heading=rng.uniform(0.0, 2 * np.pi, self.n_robots),
speed=self.speed,
radio_range=self.radio_range,
alpha=float(self.n_robots),
avoid_illuminated=self.avoid_illuminated,
avoid_shadowed=self.avoid_shadowed,
beacon_x=self.size - BEACON_MARGIN,
beacon_y=self.size / 2,
arena=self.size,
values={'x': points[:, 0], 'y': points[:, 1]},
size=self.n_robots,
space=arena,
)
self.system_ = System(
self.label,
elements=[robots],
interactions=[SwarmtaxisInteraction('swarmtaxis', target='robots')],
space=arena,
)
Components
The system uses these element and interaction classes, each with its nearest class of the core.
Elements:
SwarmRobotElement, fromElement
Interactions:
SwarmtaxisInteraction, fromInteraction
class SwarmRobotElement(Element):
"""Robot of the swarmtaxis algorithm.
It keeps in radio contact with the swarm and avoids its neighbours, by the rule of `SwarmtaxisInteraction`.
Args:
label:
The label of the agent or group.
heading:
The direction of motion.
connections:
The number of robots within radio range at the last step.
illuminated:
Whether the robot sees the beacon.
speed:
The distance moved per step.
radio_range:
The range of the radio pings.
alpha:
The number of connections below which a robot that loses one turns around.
avoid_illuminated:
The avoidance radius of illuminated robots.
avoid_shadowed:
The avoidance radius of shadowed robots.
shadow_angle:
The half angle of the cone towards the beacon in which another robot casts a shadow.
beacon_x:
The horizontal position of the beacon.
beacon_y:
The vertical position of the beacon.
arena:
The side of the square arena, whose walls stop the robot.
values:
Further values by name, such as the position `x` and `y` of every robot.
size:
The number of robots, or `None` for one.
space:
The arena, a continuous space whose axes `x` and `y` place the robots, or `None` for none.
Examples:
>>> from skcomplex.systems.engineering.robotics import SwarmRobotElement
>>> SwarmRobotElement('robots', values={'x': [1.0, 2.0], 'y': 1.0}, size=2).size
2
"""
visual = VisualSpec('agents', {'color_by': 'illuminated', 'color_range': (0.0, 1.0)})
def __init__(
self,
label: str,
*,
heading: Annotated[object, Period(2 * math.pi)] = 0.0,
connections: Annotated[float, NN] = 0.0,
illuminated: Annotated[float, FiniteEnumeratedSet([0, 1])] = 1.0,
speed: Annotated[float, RealSet.closed_open(0, oo)] = 0.2,
radio_range: Annotated[float, RealSet.open(0, oo)] = 4.0,
alpha: Annotated[float, RealSet.closed_open(0, oo)] = 20.0,
avoid_illuminated: Annotated[float, RealSet.closed_open(0, oo)] = 1.5,
avoid_shadowed: Annotated[float, RealSet.closed_open(0, oo)] = 1.0,
shadow_angle: Annotated[float, RealSet.closed_open(0, oo)] = 0.5,
beacon_x: float = 95.0,
beacon_y: float = 50.0,
arena: Annotated[float, RealSet.open(0, oo)] = 100.0,
values: Mapping[str, object] | None = None,
size: int | None = None,
space: BaseSpace | None = None,
) -> None:
super().__init__(label, values=values, size=size, space=space)
self.heading = heading
self.connections = connections
self.illuminated = illuminated
self.speed = speed
self.radio_range = radio_range
self.alpha = alpha
self.avoid_illuminated = avoid_illuminated
self.avoid_shadowed = avoid_shadowed
self.shadow_angle = shadow_angle
self.beacon_x = beacon_x
self.beacon_y = beacon_y
self.arena = arena
class SwarmtaxisInteraction(Interaction):
"""Rule of the swarmtaxis algorithm, which moves a swarm of robots towards a beacon.
Every step a robot counts the robots within its radio range. When it loses a connection and has fewer than `alpha`
connections, it turns around to stay with the swarm, and when it gains one it turns at random. A robot closer than
its avoidance radius to another turns away from it. A robot is shadowed when another robot lies between it and the
beacon, within the radio range and `shadow_angle` of the bearing to the beacon, and it is illuminated otherwise.
Illuminated robots avoid at a larger radius than shadowed ones, so they move away from the swarm towards the beacon,
and the swarm follows. The robot then moves forward at its speed.
Args:
label:
The label of the interaction.
target:
The path of the group of robots.
Examples:
>>> from skcomplex.systems.engineering.robotics import SwarmtaxisInteraction
>>> SwarmtaxisInteraction('swarmtaxis', target='robots').target
'robots'
"""
draws = True
law_classes = (UpdateLaw,)
def __init__(self, label: str, *, target: Role) -> None:
super().__init__(label)
self.target = target
def _prepare(self, context: SimpleNamespace) -> None:
"""Set the swarmtaxis rule of every robot.
Args:
context:
The namespace of the elements a binding of the roles names.
"""
super()._prepare(context)
self.laws_: list[BaseLaw] = [UpdateLaw(context.target, self.move)]
def _is_shadowed(self, agent: SwarmRobotView, neighbors: Sequence[AgentViewProtocol]) -> bool:
"""Return whether a neighbour lies in the cone from the robot towards the beacon."""
x, y = SwarmtaxisInteraction._get_position(agent)
bearing = SwarmtaxisInteraction._get_bearing(agent.beacon_x - x, agent.beacon_y - y)
return any(
SwarmtaxisInteraction._get_angle_between(
SwarmtaxisInteraction._get_bearing(
SwarmtaxisInteraction._get_position(other)[0] - x,
SwarmtaxisInteraction._get_position(other)[1] - y,
),
bearing,
)
< agent.shadow_angle
for other in neighbors
)
def _turn(self, agent: SwarmRobotView, system: SystemView, count: int) -> float:
"""Return the new heading.
It turns back to the swarm after a lost connection, or at random after a new one.
"""
if count < agent.connections and count < agent.alpha:
return agent.heading + math.pi
if count > agent.connections:
return float(system.rng.uniform(0.0, 2 * math.pi))
return agent.heading
def _avoid(self, agent: SwarmRobotView, neighbors: Sequence[AgentViewProtocol], heading: float) -> float:
"""Return the heading away from the nearest neighbour inside the avoidance radius.
Without such a neighbour the heading is unchanged.
"""
x, y = SwarmtaxisInteraction._get_position(agent)
radius = agent.avoid_illuminated if agent.illuminated else agent.avoid_shadowed
distances = [
math.hypot(
SwarmtaxisInteraction._get_position(other)[0] - x,
SwarmtaxisInteraction._get_position(other)[1] - y,
)
for other in neighbors
]
close = [(distance, other) for distance, other in zip(distances, neighbors, strict=True) if distance < radius]
if not close:
return heading
_, nearest = min(close, key=lambda pair: pair[0])
return SwarmtaxisInteraction._get_bearing(
x - SwarmtaxisInteraction._get_position(nearest)[0],
y - SwarmtaxisInteraction._get_position(nearest)[1],
)
def move(self, agent: SwarmRobotView, system: SystemView) -> None:
"""Update the connections, the shadow, and the heading, and move forward.
Args:
agent:
The robot.
system:
The system.
"""
neighbors = agent.get_neighbors(agent.radio_range)
count = len(neighbors)
agent.illuminated = 0.0 if self._is_shadowed(agent, neighbors) else 1.0
heading = self._avoid(agent, neighbors, self._turn(agent, system, count))
agent.connections = count
agent.heading = heading % (2 * math.pi)
x, y = SwarmtaxisInteraction._get_position(agent)
edge = agent.arena * (1 - EDGE_FRACTION)
x = min(max(x + agent.speed * math.cos(heading), 0.0), edge)
y = min(max(y + agent.speed * math.sin(heading), 0.0), edge)
agent.move_to((x, y))
@staticmethod
def _get_position(agent: AgentViewProtocol) -> tuple[float, float]:
"""Return the position of an agent in a continuous space."""
x, y = np.asarray(agent.location, dtype=float)
return float(x), float(y)
@staticmethod
def _get_bearing(dx: float, dy: float) -> float:
"""Return the angle of a vector."""
return math.atan2(dy, dx)
@staticmethod
def _get_angle_between(first: float, second: float) -> float:
"""Return the absolute difference of two angles, between 0 and pi."""
return abs((first - second + math.pi) % (2 * math.pi) - math.pi)