Skip to content

Trying to find an optimal solution to 2048 using Genetic Algorithms.

Notifications You must be signed in to change notification settings

ParryPee/2048Solver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

2048 AI with Genetic Algorithm

This project implements an AI agent for the popular 2048 game and uses a genetic algorithm to optimize the agent's performance.

Background

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.

How It Works

Game Implementation

The project consists of:

  • game.py: Core game logic for 2048, including board manipulation and move validation
  • player.py: AI agent implementation with heuristic evaluation functions
  • GA.py: Genetic algorithm implementation to optimize AI weights
  • main.py: User interface for running the genetic algorithm training

AI Strategy

The AI uses a combination of heuristic functions to evaluate board states:

  1. Empty Cells Count: Rewards having more empty cells on the board (more freedom to move)
  2. Adjacent Tile Penalty: Penalizes differences between adjacent tiles
  3. Corner Score: Rewards having high-value tiles in the corners
  4. Monotonicity: Rewards boards where tiles increase/decrease in a consistent pattern
  5. 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.

Evolution of the Implementation

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.

Weight Optimization with Genetic Algorithm

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.

Getting Started

Prerequisites

  • Python 3.6+
  • NumPy
  • Matplotlib

Installation

git clone https://github.com/yourusername/2048-ai-genetic-algorithm.git
cd 2048-ai-genetic-algorithm
pip install -r requirements.txt

Running the Project

To start the genetic algorithm training with interactive parameter selection:

python main.py

To run with default parameters:

python main.py --auto

Results

The 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.

Future Improvements

  • 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

License

MIT License

Acknowledgments

  • CS1010X course for the initial assignment
  • Various online resources on 2048 AI strategies

About

Trying to find an optimal solution to 2048 using Genetic Algorithms.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages