ethicalgardeners.renderer.GraphicalRenderer

class ethicalgardeners.renderer.GraphicalRenderer(cell_size=32, colors=None, post_analysis_on=False, display=False, out_dir_path=None)[source]

Bases: Renderer

Pygame-based implementation of the Renderer interface.

This renderer creates a graphical visualization of the simulation using the Pygame library. It provides a colorful and intuitive representation of the environment that updates in real-time and/or can be saved for post-simulation video generation.

The renderer displays:

  • Grid cells with colors indicating their state (empty, obstacles)

  • Agents represented by distinct colored shapes

  • Flowers with colors reflecting their type and growth stage and different colors for different flower types

  • Pollution levels shown as color intensity

display

Inherited from Renderer. Flag to enable rendering. If True, the renderer will display the environment in a Pygame window.

Type:

bool

cell_size

The size of each cell in pixels, determining the overall window dimensions.

Type:

int

colors

Mapping of environment elements to their RGB color representations. The default mapping includes:

  • ‘background’: (200, 200, 200) - Light gray background

  • ‘obstacle’: (100, 100, 100) - Gray for obstacles

  • ‘ground’: (70, 255, 70) - Green for ground cells (the green component changes dynamically based on pollution level)

Type:

dict

agent_colors

Dictionary to store colors for agents

Type:

dict

flower_colors

Dictionary to store colors for flowers

Type:

dict

pygame

Reference to the Pygame module for graphical rendering.

Type:

pygame

window

The Pygame surface where the environment is rendered.

Type:

pygame.Surface

clock

Clock object to control rendering frame rate.

Type:

pygame.time.Clock

font

Font object for rendering text in the environment (e.g., for displaying agent information).

Type:

pygame.font.Font

post_analysis_on

Flag indicating whether to save frames for post-simulation video generation.

Type:

bool

out_dir_path

Directory path where output files will be saved when post_analysis_on is True.

Type:

str

frames

Collection of rendered frames for video generation when post_analysis_on is True.

Type:

list

_run_id

Unique identifier for the run, used to name output files when post_analysis_on is True.

Type:

int

__init__(cell_size=32, colors=None, post_analysis_on=False, display=False, out_dir_path=None)[source]

Create the graphical renderer.

Parameters:
  • cell_size (int, optional) – The size of each cell in pixels. Defaults to 32.

  • colors (dict, optional) – Mapping of environment elements to their RGB color representations. Defaults to a basic set with light gray for background, gray for obstacles, and green for ground.

  • post_analysis_on (bool, optional) – Flag to enable saving frames for post-simulation video generation. Defaults to False.

  • display (bool, optional) – Flag to enable rendering. If True, the renderer will display the environment in a Pygame window.

  • out_dir_path (str, optional) – Directory path where output files will be saved. Required if post_analysis_on is True. Defaults to None.

Methods

__init__([cell_size, colors, ...])

Create the graphical renderer.

display_render()

Display the rendered frame in the Pygame window.

end_render()

Finalize the rendering process and clean up resources.

init(grid_world)

Initialize the Pygame window based on the grid world dimensions.

render(grid_world, agents)

Render the current state of the grid world using Pygame.

_generate_colors(grid_world)[source]

Generate distinct colors for each agent and flower type using predefined palettes.

Parameters:

grid_world (GridWorld) – The grid world containing agents and flower data

display_render()[source]

Display the rendered frame in the Pygame window.

This method updates the Pygame display with the current frame.

end_render()[source]

Finalize the rendering process and clean up resources.

This method shuts down the Pygame display and, if post_analysis_on is True, generates and saves a video from the collected frames using opencv.

init(grid_world)[source]

Initialize the Pygame window based on the grid world dimensions.

This method creates the Pygame window with dimensions calculated from the grid world size and the cell size. It sets up the display surface where the environment will be rendered.

Parameters:

grid_world (GridWorld) – The grid world environment to be rendered.

render(grid_world, agents: dict)[source]

Render the current state of the grid world using Pygame.

This method draws the grid world, including cells, agents, flowers, and pollution levels. Doesn’t display the frame directly; instead, it prepares the frame for rendering in the Pygame window. If post_analysis_on is True, saves the current frame for later video generation.

Parameters:
  • grid_world (GridWorld) – The current state of the world grid to render.

  • agents (dict) – Dictionary mapping the agent’s gymnasium ID to the Agent instance of the agents to display.