This project implements an AI agent for the popular 2048 game and uses a genetic algorithm to optimize the agent's performance.
This project originated from a CS1010X assignment where I was tasked with finding an optimal solution to the 2048 game. Through the development process, I improved my heuristic functions (particularly smoothness and monotonicity) to create a more effective AI player.
The project consists of:
game.py: Core game logic for 2048, including board manipulation and move validationplayer.py: AI agent implementation with heuristic evaluation functionsGA.py: Genetic algorithm implementation to optimize AI weightsmain.py: User interface for running the genetic algorithm training
The AI uses a combination of heuristic functions to evaluate board states:
- Empty Cells Count: Rewards having more empty cells on the board (more freedom to move)
- Adjacent Tile Penalty: Penalizes differences between adjacent tiles
- Corner Score: Rewards having high-value tiles in the corners
- Monotonicity: Rewards boards where tiles increase/decrease in a consistent pattern
- Smoothness: Rewards boards where adjacent tiles have similar values
Initially, my implementation of smoothness and monotonicity was suboptimal. Through experimentation and the genetic algorithm, I refined these functions to better capture desirable board states.
My early implementation had issues with the smoothness function, which didn't properly filter out zero values when calculating differences between adjacent tiles. The current implementation correctly:
- Filters out zeros before calculating differences
- Ensures there are at least two non-zero values before calculating
- Applies the function to both rows and columns
This improvement significantly enhanced the AI's ability to create mergeable tile patterns.
The genetic algorithm:
- Creates a population of AI players with random weights
- Evaluates each by playing multiple games
- Selects the best performers for reproduction
- Creates new "offspring" through crossover and mutation
- Repeats the process for multiple generations
Through this process, the algorithm discovers effective weight combinations that maximize game score and high-value tiles.
- Python 3.6+
- NumPy
- Matplotlib
git clone https://github.com/yourusername/2048-ai-genetic-algorithm.git
cd 2048-ai-genetic-algorithm
pip install -r requirements.txtTo start the genetic algorithm training with interactive parameter selection:
python main.pyTo run with default parameters:
python main.py --autoThe genetic algorithm typically converges on weights that prioritize:
- A high empty_weight (around 10-15): Emphasizing the importance of maintaining open spaces
- Balanced values for other weights (0.5-2.5): Adapting to the specific game dynamics
The trained AI can consistently achieve tiles of 1024 and occasionally 2048, with average scores above 15,000.
- Implementing a deeper search with minimax or expectimax algorithms
- Adding more sophisticated heuristic functions
- Parallelizing the genetic algorithm evaluation for faster training
- Creating a visual interface to watch the AI play
- CS1010X course for the initial assignment
- Various online resources on 2048 AI strategies