Skip to content

Proposed Base Class architecture#47

Draft
StefanHamm wants to merge 7 commits intoTUDelft-CNS-ATM:mainfrom
StefanHamm:feature/baseClass
Draft

Proposed Base Class architecture#47
StefanHamm wants to merge 7 commits intoTUDelft-CNS-ATM:mainfrom
StefanHamm:feature/baseClass

Conversation

@StefanHamm
Copy link

Description

This PR addresses the modularity and rendering issues defined in #46 by introducing a refined class hierarchy and a unified rendering system.

1. Architecture: BaseEnv(gym.Env)

The new BaseEnv serves as the foundation for all environments, reducing boilerplate and enforcing structure.

  • Configuration: Global variables (e.g., NUM_INTRUDERS) are replaced with overridable ClassVars for better configuration management.
  • Encapsulation: The step and reset methods are handled internally. Subclasses implementing specific logic via protected hooks (e.g., _is_terminated, _is_truncated, _get_action, _get_reward).
  • Rendering: Implements a standard render loop handling canvas generation and event processing.
  • Projection: Enforces a mandatory projection callback where lat, lon, alt are converted to frame x, y coordinates, decoupling logic from display.

2. Mixin Classes (Cooperative Inheritance)

Functionality is now composed using Mixin classes. These handle common logic for specific features and chain their initialization via Python's MRO (Method Resolution Order).

  • HasIntruders Mixin:

    • Manages parameters like intruder_speed, intrusion_distance, and intrusion_penalty.
    • Handles the initialization loop for num_intruders.
    • Provides shared logic for reward calculation (e.g., _check_horizontal_intrusion).
    • Note: The specific spawn strategy is left to the child class to implement.
  • HasWaypoints Mixin:

    • Iteratively calls _create_waypoint on reset based on configuration.
    • User implements _create_waypoint to return a single Waypoint object.
    • Includes built-in reward logic for waypoint checking.

This structure allows for easy future extensions, such as PolygonMixin or AirportMixin.

3. Visualizer Class

Rendering logic has been extracted into a standalone Visualizer class to eliminate code duplication across environments.

  • Projection Agnostic: Initialized with a callback to the environment's projection method.
  • Logic: The visualizer accepts simulation coordinates (lat, lon), projects them internally, and draws to the provided canvas.
  • Standard Assets: Includes reusable methods for:
    • draw_horizontal_ownship
    • draw_all_horizontal_intruders
    • draw_all_waypoints
    • draw_horizontal_line

4. Utilities

  • types.py: Introduced dataclasses (e.g., Waypoint) for stronger typing and clearer data structures.
  • constants.py: Centralized shared constants (e.g., NM2KM) to ensure consistency across environments.

5. Example Class

horizontal_cr_envV1.py show how this can be used.

class HorizontalCrEnv(WaypointsMixin,IntrudersMixin,BaseEnv):

6. Wrappers

Should be perfectly aligned with wrappers. For them these Mixins are invisible.

7 Summary

This is still in early stages, some visualizations are still bugged.

@StefanHamm
Copy link
Author

StefanHamm commented Jan 26, 2026

Added Observer pattern for overlay underlay drawing.

Overlay get registered on init as callback.
Base Env calls the render method when its time to render.

Drawbacks:
if e.g. the wrapper wants to change how the world is rendered, this is not easily doable since the original world gets rendered in one render_world call. This could be split up into single render calls but would make it more complicate.

E.g. each mixin has its own function how to draw a intruder, how to draw the polygon, how to draw the agent, etc. In the end render_world calls all render calls from the Mixins. Could be similar made as observer pattern as overlays and underlays.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant