Skip to content

An AI that learns to play Hearts of Iron IV through computer vision and reinforcement learning. Built from scratch with PyTorch.

Notifications You must be signed in to change notification settings

RandyWangRaining/HOI4-AI

ย 
ย 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

30 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

HOI4 Self-Learning AI ๐Ÿง ๐ŸŽฎ

An autonomous AI system that learns to play Hearts of Iron 4 through pure self-play and exploration, without any human demonstrations or hardcoded strategies.

Python PyTorch License

๐ŸŒŸ Features

Ultimate AI Mode (Recommended)

  • DreamerV3-Inspired World Model: Learns game dynamics through imagination
  • Random Network Distillation (RND): Curiosity-driven exploration of game mechanics
  • Neural Episodic Control (NEC): Lightning-fast learning from single experiences
  • Persistent Memory: Remembers strategies across game sessions using ChromaDB

Core Capabilities

  • Pure Self-Play Learning: No human demonstrations required
  • Causal Understanding: Discovers cause-and-effect relationships
  • Strategic Memory: "I remember from 3 games ago..." moments
  • Multi-Modal Perception: OCR + Vision + Game State Understanding

๐Ÿš€ Quick Start

Prerequisites

  • Python 3.8 or higher
  • Windows 10/11 (for pyautogui compatibility)
  • Hearts of Iron 4 (any version)
  • NVIDIA GPU recommended (but not required)

Installation

  1. Clone the repository:
git clone https://github.com/yourusername/hoi4-ai.git
cd hoi4-ai
  1. Install dependencies:
pip install -r requirements.txt
pip install chromadb faiss-cpu einops tensordict
  1. Install Tesseract OCR:
  • Windows: Download from GitHub
  • Set TESSERACT_PATH environment variable or install to default location

Running the AI

  1. Start HOI4 in windowed mode (not fullscreen)
  2. Load a game as Germany 1936
  3. Pause the game (spacebar in HOI4)
  4. Run the AI:
python main.py --mode ultimate
  1. Press F5 to start AI learning

Controls

  • F5: Start/Resume AI
  • F6: Pause AI
  • F7: Save Progress
  • F8: Show Statistics
  • ESC (hold 2s): Stop AI

๐ŸŽฏ AI Modes

Ultimate Mode (Default)

Combines all cutting-edge technologies:

  • World model-based planning
  • Curiosity-driven exploration
  • Fast episodic learning
  • Cross-game memory

Other Modes

  • --mode strategic: Pure reinforcement learning
  • --mode understanding: Focus on game comprehension
  • --mode integrated: Combines understanding with strategy
  • --mode record: Record your gameplay for analysis

๐Ÿ“Š What to Expect

Timeline

  • First 5 minutes: Random exploration, discovering UI elements
  • After 10 minutes: Pattern recognition begins
  • After 30 minutes: Develops opening strategies
  • After 1 hour: Consistent strategic play
  • After multiple games: Cross-game learning and optimization

Milestones to Watch For

  • ๐ŸŽ‰ "Discovered new screen: production" - Finding game menus
  • ๐Ÿ’ก "I remember: Game 1: Built factory..." - Using past experience
  • ๐Ÿ” "High curiosity (2.34) - exploring!" - Discovering new mechanics
  • ๐Ÿญ "Factory growth: +1" - Successfully building infrastructure

๐Ÿ—๏ธ Architecture

Core Components

1. Perception Layer (src/perception/)

  • OCR Engine: Reads game text and numbers
  • Screen Analysis: Understands current game screen

2. Comprehension System (src/comprehension/)

  • Understanding Engine: Builds mental model of game mechanics
  • Curiosity System: Drives exploration of unknown features
  • Language Parser: Interprets game UI text

3. Learning Systems (src/ai/)

  • World Model: Predicts future game states
  • RND Curiosity: Intrinsic motivation system
  • Neural Episodic Control: Fast memory-based learning
  • Strategic Evaluator: Assesses progress toward victory

4. Ultimate AI (src/ai/ultimate/)

  • Integrated System: Combines all components
  • Persistent Memory: Cross-game learning with ChromaDB
  • Training Loop: Manages the learning process

๐Ÿ”ง Configuration

Performance Tuning

# In src/ai/ultimate/ultimate_ai.py
memory_size = 50000        # Reduce if low on RAM
replay_buffer_size = 100000  # Reduce if low on RAM
device = 'cuda'           # Change to 'cpu' if no GPU

Screen Resolution

The AI automatically adapts to any screen resolution. No configuration needed!

Game Speed

The AI controls game speed automatically. Start with the game paused.

๐Ÿ“ˆ Monitoring Progress

Statistics (F8)

  • Total steps taken
  • Average intrinsic reward
  • Memory utilization
  • Discoveries made

Saved Files

  • checkpoints/: Model checkpoints every 5 minutes
  • hoi4_persistent_memory/: Cross-game memories
  • models/: Trained model files

๐Ÿ› Troubleshooting

Common Issues

OCR not reading text

  • Ensure Tesseract is installed and in PATH
  • Check if TESSERACT_PATH environment variable is set
  • Try running tesseract --version in terminal

AI clicking too fast/slow

  • Adjust sleep time in ultimate_ai.py (default 0.1s)
  • Check if game is running at normal speed

High memory usage

  • Reduce memory_size in NeuralEpisodicControl
  • Reduce replay_buffer capacity
  • Use CPU instead of GPU

CUDA out of memory

  • Set device='cpu' in ultimate_ai.py
  • Reduce batch sizes
  • Close other GPU applications

Debug Mode

python main.py --mode ultimate --debug

๐Ÿค Contributing

Contributions are welcome! Areas of interest:

  • Multi-country support (currently optimized for Germany)
  • Multiplayer compatibility
  • Performance optimizations
  • Additional game mechanics understanding

๐Ÿ“š Technical Details

Learning Algorithm

The Ultimate AI uses a combination of:

  1. Model-Based RL: DreamerV3-style world model
  2. Intrinsic Motivation: RND for exploration
  3. Episodic Control: Fast learning from memories
  4. Causal Discovery: Understanding game mechanics

Key Innovations

  • Resolution-independent action space
  • Dynamic curiosity adjustment
  • Persistent cross-game memory
  • Causal relationship discovery

๐Ÿ“„ License

MIT License - See LICENSE file for details

๐Ÿ™ Acknowledgments

  • DreamerV3 paper (Hafner et al., 2023)
  • Random Network Distillation (Burda et al., 2018)
  • Neural Episodic Control (Pritzel et al., 2017)
  • The HOI4 modding community

๐Ÿ“ง Contact

For questions or collaboration: [noahsabaj@gmail.com]


"The AI that learns to conquer the world, one click at a time." ๐ŸŒ

About

An AI that learns to play Hearts of Iron IV through computer vision and reinforcement learning. Built from scratch with PyTorch.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 97.6%
  • C++ 2.4%