# farama-foundation/highwayenv architecture

> Architecture diagram of the GitHub repository farama-foundation/highwayenv, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/farama-foundation/highwayenv
- Repository: https://github.com/farama-foundation/highwayenv

## Overview

Based on our analysis of the file tree and the README, this project is a simulation environment library for autonomous driving decision-making tasks (highway-env). It is designed as an open-source tool that provides multiple driving environments (e.g., highway, merge, roundabout, intersection, parking, racetrack) for training and evaluating reinforcement learning (RL) agents. The codebase is organized into clear modules (environments, road simulation, vehicle dynamics, etc.), with separate directories for documentation, tests, and CI/CD pipelines.

##### Main Components
- Core Environment Module – Represented by the “highway_env” directory.
- Submodules include:
▪ envs: Each driving scenario (highway, merge, roundabout, etc.) is implemented as a separate environment (see files like highway_env/envs/highway_env.py, lane_keeping_env.py, intersection_env.py, etc.).
▪ road: Handles the road geometry, lanes, road regulations, and spline calculations.
▪ vehicle: Contains vehicle dynamics, behavior, controller modules, and uncertainty models.
▪ common utilities: Shared functions in highway_env/utils.py (e.g., for observations and graphics).

- Gymnasium Interface:
- The environments are designed for gymnasium integration (for example, “gym.make('highway-v0')” is seen in the README), so they act as standard gym environments exposing reset(), step(), and render() methods.

- Simulation & Physics Engine:
- Although not a separate “physics engine” per se, the handling of dynamics (kinematics, dynamics, vehicle uncertainty) and road simulation essentially form the simulation core.

- Agent Examples and Experimentation Scripts:
- The “scripts” directory contains notebooks and Python scripts that launch simulations and demonstrate planning/learning agents (e.g., different RL approaches like DQN, PPO, MCTS). This showcases how the environment interfaces with external agent code.

- Documentation & Testing & CI/CD:
- The “docs” directory contains extensive documentation, guides, and even image assets for a visual understanding of the simulation outputs.
- The “tests” folder shows unit tests for verifying different components (envs, road dynamics, vehicle behaviors).
- The GitHub workflows under “.github/workflows” indicate CI/CD pipelines for building, testing, and releasing the project.

##### Relationships and Interactions
- The Gymnasium Interface (front-facing API) is the entry point where agents call into the environment.
- The main environment components interact with:
- The vehicle submodule: Computes vehicle behavior, controller actions, and uncertainty estimation.
- The road submodule: Provides geometric and rules-based input which the environment uses to simulate realistic driving maps.
- The common environment module: Aggregates these components to simulate the task-specific scenarios.
- External agent examples (in scripts) integrate with these modules using well-defined API calls (reset, step, render).
- Testing and CI/CD pipelines ensure that interactions (data flows) between these modules remain robust with each commit.

##### Architectural Patterns and Design Principles
- Modularity: Different driving environments (highway, merge, roundabout, etc.) are implemented in separate modules. The vehicle, road, and uncertainty submodules allow independent tweaking.
- Extensibility: The manner in which new environments or vehicle behaviors can be added is clear from the file structure.
- Integration with Third-Party Libraries: The project leverages gymnasium (for environment API) and standard RL libraries through agent examples (e.g., rl-agents, stable-baselines3).
- CI/CD Best Practices: Use of GitHub workflows, pre-commit configurations, and automated documentation builds.

## Diagram (Mermaid)

```mermaid
flowchart TD
    EA("External Agent"):::agent
    GA("Gymnasium Interface / Environment API"):::api

    EA -->|"calls"| GA

    subgraph "Core Environment Module"
        CE("Core Environment Module"):::core
        DS("Driving Scenarios"):::core
        RS("Road Simulation Module"):::core
        VD("Vehicle Dynamics & Behavior Module"):::core
        CU("Common Utilities"):::core
    end

    GA -->|"initializes"| CE
    CE -->|"selects"| DS
    DS -->|"executes"| RS
    DS -->|"executes"| VD
    DS -->|"utilizes"| CU
    RS -->|"interacts_with"| VD
    VD -->|"feedback_to"| RS

    subgraph "Development & Support"
        AE("Agent Examples / External Scripts"):::aux
        DOC("Documentation"):::aux
        TST("Testing Infrastructure"):::aux
        CICD("CI/CD Pipelines"):::aux
    end

    DS -->|"benchmarks_using"| AE
    GA -->|"sample_usage"| AE
    AE -->|"refers_to"| DOC
    AE -->|"triggers"| TST
    TST -->|"monitored_by"| CICD

    click CE "https://github.com/farama-foundation/highwayenv/tree/master/highway_env/"
    click DS "https://github.com/farama-foundation/highwayenv/tree/master/highway_env/envs/"
    click RS "https://github.com/farama-foundation/highwayenv/tree/master/highway_env/road/"
    click VD "https://github.com/farama-foundation/highwayenv/tree/master/highway_env/vehicle/"
    click CU "https://github.com/farama-foundation/highwayenv/blob/master/highway_env/utils.py"
    click GA "https://github.com/farama-foundation/highwayenv/tree/master/highway_env/envs/"
    click AE "https://github.com/farama-foundation/highwayenv/tree/master/scripts/"
    click DOC "https://github.com/farama-foundation/highwayenv/tree/master/docs/"
    click TST "https://github.com/farama-foundation/highwayenv/tree/master/tests/"
    click CICD "https://github.com/farama-foundation/highwayenv/tree/master/.github/workflows/"

    classDef agent fill:#ffccc9,stroke:#333,stroke-width:2px;
    classDef api fill:#aedbff,stroke:#333,stroke-width:2px;
    classDef core fill:#c3f9c3,stroke:#333,stroke-width:2px;
    classDef aux fill:#ffe9a8,stroke:#333,stroke-width:2px;
```
