import numpy as np
import pygame
from swarmsim.Renderers import BaseRenderer
from swarmsim.Environments import Environment
from swarmsim.Controllers import Controller
[docs]
class BioRenderer(BaseRenderer):
"""
Specialized renderer for bio-inspired simulations with spatial field visualization.
This renderer extends the base renderer to visualize spatial control fields and
environmental gradients that influence bio-inspired agent behavior. It creates
real-time spatial intensity maps showing how controllers respond to different
spatial locations, providing insight into bio-inspired navigation and decision-making.
The renderer generates a continuous spatial field visualization by:
1. **Grid Sampling**: Creates a dense grid across the environment
2. **Controller Evaluation**: Queries the controller at each grid point
3. **Color Mapping**: Maps controller responses to visual intensity
4. **Real-time Update**: Updates the spatial field each frame
Parameters
----------
populations : list of Population
List of agent populations to visualize.
environment : Environment
The simulation environment instance.
config_path : str
Path to YAML configuration file with rendering parameters.
controller : Controller
Controller instance used to generate spatial field visualization.
Attributes
----------
controller : Controller
The controller providing spatial field information.
screen_size : tuple
Pygame window dimensions (width, height). Default: (1366, 768).
arena_size : tuple
Arena rendering dimensions. Default: (1366, 768).
spatial_input : pygame.Surface
Cached surface containing the spatial field visualization.
Implementation Details
----------------------
The spatial field is computed by:
1. Creating a uniform grid spanning the environment dimensions
2. Evaluating ``controller.get_action_in_space()`` at each grid point
3. Mapping controller responses to RGB color values
4. Generating a pygame surface for efficient blitting
Color Mapping
-------------
- **High Response**: Red color (255, 0, 0) for maximum controller activation
- **Low Response**: White color (255, 255, 255) for minimum activation
- **Interpolation**: Linear blending between red and white based on response magnitude
Examples
--------
Basic bio-renderer setup:
.. code-block:: python
from swarmsim.Renderers import BioRenderer
from swarmsim.Controllers import LightSensitiveController
controller = LightSensitiveController(config_path="bio_config.yaml")
renderer = BioRenderer(populations, environment, "render_config.yaml", controller)
# Render with spatial field background
renderer.render()
Configuration example:
.. code-block:: yaml
BioRenderer:
render_mode: "pygame"
render_dt: 0.05
agent_colors: ["blue", "green"]
agent_sizes: [2, 3]
background_color: "black"
Notes
-----
- Requires pygame rendering mode for spatial field visualization
- Controller must implement ``get_action_in_space(positions)`` method
- Spatial field is recomputed each frame if controller state changes
- Best suited for visualizing spatially-varying control policies
"""
[docs]
def __init__(self, populations: list, environment: Environment, config_path: str, controller: Controller):
"""
Initialize the bio-renderer with spatial field visualization capabilities.
Parameters
----------
populations : list of Population
List of agent populations to visualize.
environment : Environment
The simulation environment instance.
config_path : str
Path to YAML configuration file containing rendering parameters.
controller : Controller
Controller instance for generating spatial field visualization.
Must implement ``get_action_in_space(positions)`` method.
Notes
-----
- Initializes spatial field surface during construction
- Controller is used immediately to generate initial spatial visualization
"""
super().__init__(populations, environment, config_path)
self.controller = controller
self.screen_size = (1366,768)
self.arena_size = (1366,768)
self.spatial_input = None
self.create_spatial_input()
[docs]
def pre_render_hook_pygame(self):
"""
Render the spatial field background before drawing agents.
This method applies the cached spatial field surface as a background,
providing visual context for bio-inspired agent behavior. The spatial
field shows the controller's response across the entire environment.
Implementation
--------------
- Blits the pre-computed spatial field surface to the pygame display
- Called automatically before agent rendering in each frame
- Uses cached surface for optimal performance
Notes
-----
- Spatial field surface is generated by ``create_spatial_input()``
- Background visualization updates when controller state changes
- Provides continuous spatial context for discrete agent positions
"""
# Draw surface
pygame.display.get_surface().blit(self.spatial_input, (0, 0))