# dpasca/nnlander architecture

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

- Interactive diagram: https://gitdiagram.com/dpasca/nnlander
- Repository: https://github.com/dpasca/nnlander

## Overview

##### Project Type and Purpose
- This is a simulation/game project that demonstrates different implementations of neural networks for controlling a lunar lander.
- The README clearly explains that the workshop covers concepts of control systems and neural networks, progressively evolving from manual control (Lander01) to advanced training tasks such as genetic algorithms (Lander04) and reinforcement learning (Lander05).

##### File Structure
- The top-level includes configuration files (CMakeLists.txt, build scripts), documentation (README files), and workshop requirements.
- The Common folder holds shared components such as Simulation, UI drawing, neural network implementations, and utility functions.
- Several directories (Lander01 through Lander05) are dedicated to different versions of the lunar lander application:
- Lander01: manual control.
- Lander02: basic neural network implementation.
- Lander03: improved neural network with a random training task.
- Lander04: advanced neural network using a genetic algorithm training task.
- Lander05: uses reinforcement learning (REINFORCE-ES) for training.

a. Components:
- Simulation Engine: Represented by components in Common/Simulation.h and SimulationDisplay.h that manage physics, game logic, and rendering.
- Neural Network Components: Represented by Common/SimpleNeuralNet.h along with the specialized training task files in Lander03 (TrainingTaskRandom.h), Lander04 (TrainingTaskGA.h), and Lander05 (TrainingTaskRES.h).
- User Interface / Input Handling: Captured in Common/DrawUI.h and integrated into each LanderXX main program.
- Individual Lander Variants: Each LanderXX directory contains its own main program that ties simulation, neural network, and task-specific logic together.

b. Relationships & Interactions:
- The Common library provides shared functionalities (simulation, UI, neural networks) that are imported by each Lander variant.
- Each variant (Lander01 through Lander05) compiles into an executable where the main loop interacts with the simulation engine and, depending on the variant, applies either manual controls or neural network-based control strategies.
- Data flows from user input or simulation state into the neural network modules (or training algorithms) and then back into simulation (for example, adjusting thrust).

c. Architectural Patterns & Design Principles:
- Modular design: Separation of concerns is evident since shared functionality is isolated in the Common directory.
- Progressive enhancement: Each Lander variant builds upon a common base but adds specific training or control strategies.
- Use of CMake for build management: Indicates a standard configuration-driven build process.

d. Relevant Technologies & Frameworks:
- C++20 standard, CMake (for build configuration), possibly Raylib (as mentioned for fetching visuals/graphics), and custom implementations of Neural Networks, Genetic Algorithms, and Reinforce-ES training.

##### Specific Elements
- Each major component should have a clear label:

##### Additional Detail Suggestions
- CMake invoking build scripts (build.sh/build.bat) and generating executables.
- How each Lander variant is independent but shares core simulation and neural network logic which emphasizes modularity.

- The layer structure (Common library vs. variant implementations)
- The relationship between simulation, control logic, and rendering
- The progression of different control methodologies across Lander01 to Lander05.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Build System Layer
    BS["Build System"]:::build

    %% Common Library
    subgraph "Common Library"
        SIM["Simulation Engine"]:::common
        UI["UI/Rendering"]:::common
        NN["Neural Network Module"]:::common
    end

    %% Lander Variants Layer
    subgraph "Lander Variants"
        L1["Manual Control (Lander01)"]:::variant
        L2["Basic NN (Lander02)"]:::variant
        L3["Random Training (Lander03)"]:::variant
        RT["Random Training Task"]:::training
        L4["Genetic Algorithm (Lander04)"]:::variant
        GA["Genetic Algorithm Task"]:::training
        L5["Reinforcement Learning (Lander05)"]:::variant
        RL["Reinforcement Learning Task"]:::training
    end

    %% Connections from Build System to shared components
    BS -->|"builds"| SIM
    BS -->|"builds"| UI
    BS -->|"builds"| NN

    %% Lander01 Connections (Manual Control)
    L1 -->|"uses"| SIM
    L1 -->|"uses"| UI

    %% Lander02 Connections (Basic Neural Network)
    L2 -->|"uses"| SIM
    L2 -->|"uses"| NN
    L2 -->|"uses"| UI

    %% Lander03 Connections (Random Training)
    L3 -->|"uses"| SIM
    L3 -->|"uses"| NN
    L3 -->|"uses"| UI
    L3 -->|"trainsWith"| RT

    %% Lander04 Connections (Genetic Algorithm)
    L4 -->|"uses"| SIM
    L4 -->|"uses"| NN
    L4 -->|"uses"| UI
    L4 -->|"trainsWith"| GA

    %% Lander05 Connections (Reinforcement Learning)
    L5 -->|"uses"| SIM
    L5 -->|"uses"| NN
    L5 -->|"uses"| UI
    L5 -->|"trainsWith"| RL

    %% Click Events for Component Mapping
    click SIM "https://github.com/dpasca/nnlander/blob/master/Common/Simulation.h"
    click SIM "https://github.com/dpasca/nnlander/blob/master/Common/SimulationDisplay.h"
    click NN "https://github.com/dpasca/nnlander/blob/master/Common/SimpleNeuralNet.h"
    click UI "https://github.com/dpasca/nnlander/blob/master/Common/DrawUI.h"
    click L1 "https://github.com/dpasca/nnlander/blob/master/Lander01/lander01.cpp"
    click L2 "https://github.com/dpasca/nnlander/blob/master/Lander02/lander02.cpp"
    click L3 "https://github.com/dpasca/nnlander/blob/master/Lander03/lander03.cpp"
    click RT "https://github.com/dpasca/nnlander/blob/master/Lander03/TrainingTaskRandom.h"
    click L4 "https://github.com/dpasca/nnlander/blob/master/Lander04/lander04.cpp"
    click GA "https://github.com/dpasca/nnlander/blob/master/Lander04/TrainingTaskGA.h"
    click L5 "https://github.com/dpasca/nnlander/blob/master/Lander05/lander05.cpp"
    click RL "https://github.com/dpasca/nnlander/blob/master/Lander05/TrainingTaskRES.h"
    click BS "https://github.com/dpasca/nnlander/blob/master/CMakeLists.txt"

    %% Styles
    classDef build fill:#F2D7D5,stroke:#C0392B,stroke-width:2px;
    classDef common fill:#D6EAF8,stroke:#2E86C1,stroke-width:2px;
    classDef variant fill:#D5F5E3,stroke:#28B463,stroke-width:2px;
    classDef training fill:#F9E79F,stroke:#D4AC0D,stroke-width:2px;
```
