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.
- 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
- 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
- Python 3.8 or higher
- Windows 10/11 (for pyautogui compatibility)
- Hearts of Iron 4 (any version)
- NVIDIA GPU recommended (but not required)
- Clone the repository:
git clone https://github.com/yourusername/hoi4-ai.git
cd hoi4-ai- Install dependencies:
pip install -r requirements.txt
pip install chromadb faiss-cpu einops tensordict- Install Tesseract OCR:
- Windows: Download from GitHub
- Set TESSERACT_PATH environment variable or install to default location
- Start HOI4 in windowed mode (not fullscreen)
- Load a game as Germany 1936
- Pause the game (spacebar in HOI4)
- Run the AI:
python main.py --mode ultimate- Press F5 to start AI learning
- F5: Start/Resume AI
- F6: Pause AI
- F7: Save Progress
- F8: Show Statistics
- ESC (hold 2s): Stop AI
Combines all cutting-edge technologies:
- World model-based planning
- Curiosity-driven exploration
- Fast episodic learning
- Cross-game memory
--mode strategic: Pure reinforcement learning--mode understanding: Focus on game comprehension--mode integrated: Combines understanding with strategy--mode record: Record your gameplay for analysis
- 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
- ๐ "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
- OCR Engine: Reads game text and numbers
- Screen Analysis: Understands current game screen
- Understanding Engine: Builds mental model of game mechanics
- Curiosity System: Drives exploration of unknown features
- Language Parser: Interprets game UI text
- World Model: Predicts future game states
- RND Curiosity: Intrinsic motivation system
- Neural Episodic Control: Fast memory-based learning
- Strategic Evaluator: Assesses progress toward victory
- Integrated System: Combines all components
- Persistent Memory: Cross-game learning with ChromaDB
- Training Loop: Manages the learning process
# 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 GPUThe AI automatically adapts to any screen resolution. No configuration needed!
The AI controls game speed automatically. Start with the game paused.
- Total steps taken
- Average intrinsic reward
- Memory utilization
- Discoveries made
checkpoints/: Model checkpoints every 5 minuteshoi4_persistent_memory/: Cross-game memoriesmodels/: Trained model files
OCR not reading text
- Ensure Tesseract is installed and in PATH
- Check if
TESSERACT_PATHenvironment variable is set - Try running
tesseract --versionin 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_sizeinNeuralEpisodicControl - Reduce
replay_buffercapacity - Use CPU instead of GPU
CUDA out of memory
- Set
device='cpu'inultimate_ai.py - Reduce batch sizes
- Close other GPU applications
python main.py --mode ultimate --debugContributions are welcome! Areas of interest:
- Multi-country support (currently optimized for Germany)
- Multiplayer compatibility
- Performance optimizations
- Additional game mechanics understanding
The Ultimate AI uses a combination of:
- Model-Based RL: DreamerV3-style world model
- Intrinsic Motivation: RND for exploration
- Episodic Control: Fast learning from memories
- Causal Discovery: Understanding game mechanics
- Resolution-independent action space
- Dynamic curiosity adjustment
- Persistent cross-game memory
- Causal relationship discovery
MIT License - See LICENSE file for details
- DreamerV3 paper (Hafner et al., 2023)
- Random Network Distillation (Burda et al., 2018)
- Neural Episodic Control (Pritzel et al., 2017)
- The HOI4 modding community
For questions or collaboration: [noahsabaj@gmail.com]
"The AI that learns to conquer the world, one click at a time." ๐