# uchiha-itachi0/self_driving_car architecture

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

- Interactive diagram: https://gitdiagram.com/uchiha-itachi0/self_driving_car
- Repository: https://github.com/uchiha-itachi0/self_driving_car

## Overview

##### Main Components
- Core Simulation Engine – Represented by src/index.js which initializes and orchestrates the simulation.
- AI Module – Under src/AI, this component includes:
- NeuralNetwork.js for the feedforward neural network.
- Population.js for handling genetic algorithm operations.
- SmartCar.js which links the neural network to the car’s behavior.
- Game Module – Under src/Game, includes:
- Car.js (game object for the car, likely with simulation physics).
- Track.js (defines the course boundaries and track layout).
- Geometry Module – Under src/Geometry, provides essential tools:
- Sketch/UI Module – Under src/Sketch:
- Contains multiple view files (e.g., gameOver.js, human.js, menu.js) that manage different states or screens of the simulation.
- Sketch/state.js and an index.js for overall UI management.
- Utility Module – Under src/Utils:
- Files like Ray.js and Sensors.js that likely help in sensor simulation and raycasting.

##### Relationships and Interactions
- The simulation begins in src/index.js where the game loop integrates multiple modules.
- The Game Module (Car and Track) represents the scene; here, each Car object is controlled by an AI component.
- The AI Module controls car behavior:
- SmartCar uses sensor data (from src/Utils/Sensors.js and possibly Geometry computations) as input for its NeuralNetwork.
- The network computes motor control signals which then determine the car’s acceleration and steering.
- The Genetic Algorithm in Population.js interacts with multiple SmartCar instances:
- It evaluates performance per generation.
- It selects, crosses over, and mutates network weights to produce a new population.
- The Sketch Module organizes user views:
- Different screens (e.g., initial, training, replay, game over) indicate the state of the simulation and offer user interaction.
- The Utility and Geometry modules provide supporting functions (like collision detection or sensor raycasting) and are used across other modules.

##### Architectural Patterns and Design Principles
- Modular Organization – Each folder (AI, Game, Geometry, Sketch, Utils) encapsulates a distinct responsibility.
- Separation of Concerns – AI computations, game physics, UI states, and utility functions are clearly delineated.
- Feedback Loop – There is a cyclical process where sensor inputs feed into the neural network, which then alters the game state, and the outcomes are used to train via the genetic algorithm.

##### Incorporate Relevant Technologies and Frameworks
- Pure JavaScript – The project is implemented using standard JavaScript (likely run in a browser given the index.html).
- Frontend Rendering – The simulation views under src/Sketch indicate potential use of a canvas or similar rendering library (possibly p5.js or native HTML5 Canvas).
- Algorithm Implementation – The feedforward Neural Network and Genetic Algorithm are key AI components.

- Sub-components if necessary (e.g., within the AI module, show the breakdown into NeuralNetwork, Population, and SmartCar).
- That each view or UI state (from src/Sketch/Views) is connected to show how user interactions and simulation states are rendered.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Core Simulation Engine
    subgraph "Simulation Engine"
        SIM["Core Simulation Engine"]:::core
        click SIM "https://github.com/uchiha-itachi0/self_driving_car/blob/main/src/index.js"
    end

    %% AI Module
    subgraph "AI Module"
        NN["Neural Network"]:::ai
        click NN "https://github.com/uchiha-itachi0/self_driving_car/blob/main/src/AI/NeuralNetwork.js"
        GA["Genetic Algorithm"]:::ai
        click GA "https://github.com/uchiha-itachi0/self_driving_car/blob/main/src/AI/Population.js"
        SC["Smart Car Controller"]:::ai
        click SC "https://github.com/uchiha-itachi0/self_driving_car/blob/main/src/AI/SmartCar.js"
    end

    %% Game Module
    subgraph "Game Module"
        CS["Car Simulation"]:::game
        click CS "https://github.com/uchiha-itachi0/self_driving_car/blob/main/src/Game/Car.js"
        TL["Track Layout"]:::game
        click TL "https://github.com/uchiha-itachi0/self_driving_car/blob/main/src/Game/Track.js"
    end

    %% Geometry Module
    subgraph "Geometry Module"
        BC["Boundary Calculations"]:::geometry
        click BC "https://github.com/uchiha-itachi0/self_driving_car/blob/main/src/Geometry/Boundary.js"
        BX["Box Geometry"]:::geometry
        click BX "https://github.com/uchiha-itachi0/self_driving_car/blob/main/src/Geometry/Box.js"
        GU["Geometry Utilities"]:::geometry
        click GU "https://github.com/uchiha-itachi0/self_driving_car/blob/main/src/Geometry/Utils.js"
    end

    %% Sketch/UI Module
    subgraph "Sketch/UI Module"
        SM["Main Sketch & State Management"]:::ui
        click SM "https://github.com/uchiha-itachi0/self_driving_car/blob/main/src/Sketch/index.js"
        SS["UI State"]:::ui
        click SS "https://github.com/uchiha-itachi0/self_driving_car/blob/main/src/Sketch/state.js"
        UV["UI Views"]:::ui
        click UV "https://github.com/uchiha-itachi0/self_driving_car/tree/main/src/Sketch/Views"
    end

    %% Utility Module
    subgraph "Utility Module"
        RR["Sensor Raycasting"]:::utility
        click RR "https://github.com/uchiha-itachi0/self_driving_car/blob/main/src/Utils/Ray.js"
        SH["Sensor Handling"]:::utility
        click SH "https://github.com/uchiha-itachi0/self_driving_car/blob/main/src/Utils/Sensors.js"
    end

    %% Assets
    subgraph "Assets"
        DA["Configuration/Data Assets"]:::asset
        click DA "https://github.com/uchiha-itachi0/self_driving_car/tree/main/assets"
    end

    %% Relationships and Data Flows
    SIM -->|"init"| SC
    SIM -->|"init"| CS
    SIM -->|"render"| SM

    SH -->|"SensorData"| SC
    RR -->|"RayData"| SC
    GU -->|"GeomData"| NN

    NN -->|"ControlSignals"| SC
    SC -->|"Drive"| CS

    CS -->|"Evaluate"| GA
    GA -->|"Evolve"| SC

    DA -->|"UIConfig"| SM
    DA -->|"CarConfig"| SC

%% Styles
classDef core fill:#FFA07A,stroke:#333,stroke-width:2px;
classDef ai fill:#ADD8E6,stroke:#333,stroke-width:2px;
classDef game fill:#90EE90,stroke:#333,stroke-width:2px;
classDef geometry fill:#FFFF99,stroke:#333,stroke-width:2px;
classDef ui fill:#F5DEB3,stroke:#333,stroke-width:2px;
classDef utility fill:#D3D3D3,stroke:#333,stroke-width:2px;
classDef asset fill:#C0C0C0,stroke:#333,stroke-width:2px;
```
