How dhakalnirajan/baghchal-rl works
Project Type and Main Purpose
• This is a board game implementation of Baagh Chaal, with a twist: the tiger player is powered by a neural network – accelerated with CUDA – and, in one variant (AlphaBaghChal), enhanced with Monte Carlo Tree Search (MCTS) for decision making.
• The project exists in two flavors: a CPU-based version (baaghchaal.cpp and main.c with cuda_kernels.cu) and an enhanced version with MCTS (alphabaghchal.c, alphabaghchal_cuda.cu, alphabaghchal_mcts.c).
File Structure to Identify Logical Components
• Top-Level Files (outside the AlphaBaghChal folder):
• main.c: Hosts game logic, initialization, user interaction, and integrates with the CUDA kernels.
• baaghchaal.cpp: CPU-only implementation with similar game logic but without CUDA.
• cuda_kernels.cu: Contains CUDA kernel functions for neural network computations.
• README.md and LICENSE.md: Provides overall project documentation.
• Inside the AlphaBaghChal Directory:
• alphabaghchal.c / alphabaghchal.h: Contains game logic and integration with MCTS.
• alphabaghchal_cuda.cu: CUDA kernels for neural network computations (mirroring cuda_kernels.cu).
• alphabaghchal_mcts.c / alphabaghchal_mcts.h: Implements the Monte Carlo Tree Search algorithm for improved AI moves.
• A README.md providing detailed explanations specific to the MCTS enhanced version.
Main Components
a. Game Engine & User Interface:
– Modules: main.c or alphabaghchal.c (depending on the variant) and baaghchaal.cpp.
– Responsibilities: Display board state, process user input, manage game state (placement and movement phases), and check game over conditions.
b. Neural Network Engine:
– Modules: Neural network data structures (NN definition in main.c/alphabaghchal.c) and training functions.
– Key Points: One hidden layer network, forward pass (with ReLU activation), softmax for probability distribution, and backpropagation routine.
– Note that both versions initialize and train a neural network (one with CUDA and one on CPU).
c. CUDA Acceleration:
– Modules: cuda_kernels.cu and alphabaghchal_cuda.cu.
– Responsibilities: Provide GPU-accelerated neural network computations, including the forward pass and backpropagation kernels.
– Include details such as: the use of CUDA kernels, shared memory, and external C wrappers for integration.
d. Monte Carlo Tree Search (MCTS):
– Modules: alphabaghchal_mcts.c and alphabaghchal_mcts.h.
– Responsibilities: Implement MCTS logic: tree node creation, node expansion, selection (using UCB values), simulation (rollouts), and backpropagation of simulation results.
– This component is specific to the AlphaBaghChal enhanced version.
e. Training and Self-play:
– Modules: Functions such as train_against_random(), play_random_game(), and learn_from_game() found in main.c/alphabaghchal.c.
– Responsibilities: Updating the neural network based on game outcomes, reporting training progress and win/loss statistics.
How Components Interact
• The UI/Game Engine component invokes the Neural Network Engine when an AI move (tiger’s move) is needed.
• The Neural Network Engine may either run on the CPU or use CUDA Accelerated functions, so show a dependency from game logic to both the CUDA kernels and CPU-based neural network modules.
Architectural Patterns and Design Principles to Highlight
• Separation of Concerns: Different modules are responsible for user interaction (UI/game loop), AI decision making (neural network and MCTS), and hardware acceleration (CUDA kernels).
• Hybrid Execution: Explain that the system supports both CPU and GPU (CUDA) paths for neural network computations.