Sahil Karawade

Chinese Chess AI Engine

Overview

This project implements a sophisticated AI engine for Chinese Chess (Xiangqi) using the groundbreaking AlphaZero approach. The system combines deep reinforcement learning with Monte Carlo Tree Search (MCTS) to develop a self-learning chess player that improves through self-play without requiring human game data or domain-specific heuristics.

Unlike traditional chess engines that rely on extensive databases of human games and hand-crafted evaluation functions, this implementation learns optimal strategies purely through reinforcement learning, making it a true demonstration of artificial general intelligence principles applied to board games.

Technical Stack

Python
PyTorch
NumPy
MCTS

Key Features

AlphaZero Architecture

Implemented the complete AlphaZero framework including a deep neural network that outputs both move probabilities and position evaluations, enabling efficient search tree exploration.

Monte Carlo Tree Search

Developed an optimized MCTS algorithm that balances exploration and exploitation using Upper Confidence Bounds (UCB), guiding the neural network's search toward the most promising moves.

Self-Play Training Pipeline

Built a complete training infrastructure where the agent plays thousands of games against itself, continuously generating new training data and improving its strategic understanding.

Chinese Chess Game Logic

Implemented full Chinese Chess rules including unique pieces (Elephant, Advisor, Cannon) and constraints (river crossing, palace boundaries), ensuring accurate game state representation.

Implementation Details

Neural Network Architecture

The policy-value network uses residual convolutional blocks to process the board state, outputting:

  • Policy head: Probability distribution over all legal moves
  • Value head: Expected outcome from the current position (-1 to +1)

Training Process

The agent iteratively improves through a three-stage cycle: self-play game generation using MCTS guided by the current neural network, neural network training on recent game outcomes, and evaluation against previous versions to ensure consistent improvement.

MCTS Optimization

Implemented efficient tree search with virtual loss for parallel simulations, Dirichlet noise for exploration during training, and progressive widening to focus computational resources on promising variations.

Technical Challenges & Solutions

Computational Efficiency

Challenge: AlphaZero requires extensive computational resources for training.
Solution: Implemented batch processing for neural network inference, optimized MCTS with efficient state caching, and utilized GPU acceleration for both training and gameplay.

Training Stability

Challenge: Neural network can catastrophically forget previous knowledge.
Solution: Maintained a replay buffer of historical games, implemented careful learning rate scheduling, and added checkpoint-based rollback mechanisms.

Game State Representation

Challenge: Efficiently encoding Chinese Chess rules and symmetries.
Solution: Designed multi-channel board representation capturing piece positions, move history, and rule constraints, with data augmentation using board symmetries.

Results & Performance

After training through thousands of self-play games, the AI demonstrates strong strategic understanding and tactical awareness. The agent successfully learned fundamental concepts such as piece development, center control, and endgame techniques without explicit programming of these strategies.

The model shows progressive improvement across training iterations, with win rates against random players exceeding 95% and competitive performance against rule-based heuristic engines. The MCTS-guided search enables the agent to evaluate positions several moves ahead, making sophisticated tactical decisions.

Key Learnings

Reinforcement Learning

Deep understanding of self-play reinforcement learning, policy gradient methods, and the exploration-exploitation tradeoff.

Search Algorithms

Practical experience with MCTS, tree search optimization, and integrating neural networks with classical AI algorithms.

Deep Learning

Expertise in designing and training convolutional neural networks for game state evaluation and policy learning.

System Design

Building scalable training pipelines, managing computational resources, and implementing efficient data structures.

Future Enhancements

  • •Implement distributed training across multiple GPUs for faster convergence
  • •Add opening book integration to improve early-game performance
  • •Develop a web-based interface for human players to challenge the AI
  • •Experiment with transformer architectures for improved positional understanding

References

Silver, D., et al. (2017). "Mastering Chess and Shogi by Self-Play with a General Reinforcement Learning Algorithm." arXiv preprint arXiv:1712.01815

Silver, D., et al. (2016). "Mastering the game of Go with deep neural networks and tree search." Nature, 529(7587), 484-489

Browne, C., et al. (2012). "A Survey of Monte Carlo Tree Search Methods." IEEE Transactions on Computational Intelligence and AI in Games, 4(1), 1-43

Explore the Code

View the complete implementation, documentation, and training logs on GitHub.

View on GitHub