# yashmittalgroot/ai-car architecture

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

- Interactive diagram: https://gitdiagram.com/yashmittalgroot/ai-car
- Repository: https://github.com/yashmittalgroot/ai-car

## Overview

##### Project Type and Purpose
- This is a simulation/training project for an autonomous car, which uses sensors, a feedforward Neural Network, and a Genetic Algorithm. Essentially, the project combines AI and game simulation (via Pygame) to evolve a set of neural weights that enable a car to navigate a track without hitting walls.

##### Key Components Based on the File Structure and README
a. Car Component:
- Files: car.py, track.png, car1.png, movement1.png (documentation images)
- Role: Represents the car entity including its graphics, movement, and interactions on the track.

b. Sensor Component:
- File: sensor.py
- Role: Simulate sensors on the car that detect the track’s walls by capturing pixel data.

c. Neural Network Component:
- File: nn.py and reference to Keras (a deep learning framework)
- Role: Models the decision-making process. It takes sensor inputs and outputs direction commands for the car.

d. Genetic Algorithm Component:
- Files: geneticAlgoModified.py and genotype.py
- Role: Evolves and tunes the neural network’s weights (genotype) by simulating evolution and selection mechanisms. It also includes modification aspects as mentioned in the README.

e. Game Loop / Simulation Component:
- Files: main.py and game.py

f. Data Storage / Evaluation Component:
- Files: data.npy and check_solution.py
- Role: data.npy stores the neural network weights that have successfully navigated the track. check_solution.py is used to validate a set of weights.

##### Relationships and Interactions Between Components
- The car (car.py) is the entity being controlled. It receives direction commands generated by the neural network (nn.py), which processes sensor input collected from sensor.py.
- The genetic algorithm (geneticAlgoModified.py) uses a genotype (genotype.py) representation of the neural network’s weights to evolve parameters over successive iterations.
- The training loop in main.py integrates the above components together: it simulates the car moving along the track, processes sensor data, runs the neural network, applies genetic algorithm logic for evolution, and, if a successful navigation is achieved, records the weights in data.npy.
- Additionally, game.py offers a fallback to manually control the car, enabling comparison or testing against the autonomous approach.
- The data stored in data.npy can later be tested using check_solution.py, ensuring that the improved weights indeed produce successful outcomes.

##### Architectural Patterns and Design Principles
- Modular Design: Each component is encapsulated in its own file (and conceptually separate), making the system modular.
- Simulation Loop: The central game/train loop (in main.py/game.py) handles recurring simulation cycles.
- Evolutionary Computation: The use of a Genetic Algorithm is a key pattern where fitness evaluations determine the evolution of solution weights.
- Separation of Concerns: AI decision-making (neural network), optimization (genetic algorithm), sensing (sensor module), and control (car game logic) are all separate.

##### Final Recommendations
- Clear labels for every component.
- Modularity by visually separating the AI components from the simulation and control components.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Subgraph: Simulation & Game Loop
    subgraph "Simulation & Game Loop"
        Sim["Simulation Loop (main.py)"]:::simulation
        Game["Game (game.py)"]:::simulation
    end

    %% Subgraph: Car System
    subgraph "Car System"
        Car["Car (car.py)"]:::simulation
        Sensor["Sensor (sensor.py)"]:::sensor
    end

    %% Subgraph: AI Components
    subgraph "AI Components"
        NN["Neural Network (nn.py, Keras)"]:::ai
        GA_Main["Genetic Algorithm (geneticAlgoModified.py)"]:::ai
        Genotype["Genotype (genotype.py)"]:::ai
    end

    %% Subgraph: Data & Evaluation
    subgraph "Data & Evaluation"
        DataStore["Data Storage (data.npy)"]:::data
        Check["Check Solution (check_solution.py)"]:::data
    end

    %% Subgraph: External Assets
    subgraph "External Assets"
        Assets["Assets (README.md, track.png, car1.png, movement1.png)"]:::external
    end

    %% Connections
    Sim -->|"initiate"| Sensor
    Sensor -->|"SensorData"| NN
    NN -->|"DirectionCommands"| Car
    Sim -->|"EvaluateFitness"| GA_Main
    GA_Main -->|"UpdatesWeights"| NN
    GA_Main -->|"UsesGenotype"| Genotype
    Sim -->|"StoreWeights"| DataStore
    DataStore -->|"RetrieveValidation"| Check
    Assets -->|"ProvideAssets"| Car
    Sim -->|"ManualControl"| Game

    %% Styles
    classDef simulation fill:#d1e8ff,stroke:#333,stroke-width:2px;
    classDef sensor fill:#fff2cc,stroke:#333,stroke-width:2px;
    classDef ai fill:#d9ead3,stroke:#333,stroke-width:2px;
    classDef data fill:#f9cb9c,stroke:#333,stroke-width:2px;
    classDef external fill:#c9daf8,stroke:#333,stroke-width:2px;

    %% Click Events
    click Sim "https://github.com/yashmittalgroot/ai-car/blob/master/main.py"
    click Game "https://github.com/yashmittalgroot/ai-car/blob/master/game.py"
    click Car "https://github.com/yashmittalgroot/ai-car/blob/master/car.py"
    click Sensor "https://github.com/yashmittalgroot/ai-car/blob/master/sensor.py"
    click NN "https://github.com/yashmittalgroot/ai-car/blob/master/nn.py"
    click GA_Main "https://github.com/yashmittalgroot/ai-car/blob/master/geneticAlgoModified.py"
    click Genotype "https://github.com/yashmittalgroot/ai-car/blob/master/genotype.py"
    click DataStore "https://github.com/yashmittalgroot/ai-car/blob/master/data.npy"
    click Check "https://github.com/yashmittalgroot/ai-car/blob/master/check_solution.py"
    click Assets "https://github.com/yashmittalgroot/ai-car/blob/master/README.md"
```
