rts-commander / HEURISTIC_ANALYSIS_EXPLAINED.txt
Luigi's picture
docs: Explain heuristic vs LLM analysis
8d87603
# ๐Ÿค– Heuristic Analysis - Quick Answer
## What does "heuristic" mean?
**Heuristic** = **Rule-based** analysis, NOT using the AI model.
---
## ๐ŸŽฏ Simple Comparison
### ๐Ÿค– LLM Analysis (AI Model)
```
Uses: Qwen2.5-Coder-1.5B language model
Time: ~15 seconds
Quality: โญโญโญโญโญ Very specific and natural
Example:
"Your tank forces are positioned well but enemy
helicopters are harassing your northwest harvesters.
Build SAM sites near refineries and advance your
tank column while they're distracted."
```
### ๐Ÿ“Š Heuristic Analysis (Simple Rules)
```
Uses: IF-THEN rules (count units, compare numbers)
Time: Instant (0.001 seconds)
Quality: โญโญโญ Generic but always correct
Example:
"Battlefield is balanced. Scout and take map control.
Build more units and protect your harvesters."
```
---
## โšก Why Use Heuristic?
### 3 Reasons:
1. **Model Not Ready** โณ
- Game just started
- Model still loading (takes 15s)
- Heuristic gives instant advice while waiting
2. **Model Busy** ๐Ÿ”„
- LLM processing another request
- You get heuristic INSTANTLY
- Better than waiting or getting nothing
3. **Model Timeout** โŒ
- LLM taking too long (>15s)
- Automatic fallback to heuristic
- Guaranteed advice, always works
---
## ๐Ÿ“Š How It Works
```python
# Heuristic = Simple Math + Rules
1. COUNT everything:
- Your units: 10
- Enemy units: 8
- Your buildings: 5
- Enemy buildings: 4
2. CALCULATE score:
score = (10-8) + 0.5ร—(5-4) = 2.5
3. DETERMINE situation:
score > 1 โ†’ "You're AHEAD"
4. SELECT advice:
"You hold the initiative.
Maintain pressure and expand."
5. ADD specific tips:
- Low power? โ†’ "Build Power Plant"
- Low credits? โ†’ "Protect Harvester"
- Fewer units? โ†’ "Train Infantry and Tanks"
6. DONE! (0.001 seconds)
```
---
## ๐ŸŽฎ What You See
### Both Look Professional:
**LLM Analysis:**
```
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
Summary: Your tank forces are positioned
well in the center. Enemy is vulnerable
on north flank - consider flanking attack
with infantry support.
Tips:
โ€ข Build 2 more tanks for flanking force
โ€ข Send infantry to north position
โ€ข Protect harvesters during advance
โ€ข Scout enemy rear for vulnerabilities
Coach: Great positioning! Execute the
flank now while enemy is committed south.
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
```
**Heuristic Analysis:**
```
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
Summary: You hold the initiative.
Maintain pressure and expand.
Tips:
โ€ข Train Infantry and add Tanks
โ€ข Scout enemy base position
โ€ข Protect Harvester and secure ore
โ€ข Build Power Plant if needed
Coach: Keep your economy safe and
strike when you see an opening.
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
```
**Notice:**
- Heuristic is more **generic** but still **useful**
- LLM is more **specific** to your exact situation
- Both provide **valid tactical advice**
---
## โœ… Benefits of Heuristic
| Benefit | Description |
|---------|-------------|
| **Instant** | No waiting (0.001s vs 15s) |
| **Reliable** | Never fails, always works |
| **No CPU** | Doesn't use model (saves resources) |
| **Fallback** | Guarantees users always get advice |
| **Multi-language** | English, French, Chinese support |
---
## ๐ŸŽฏ Bottom Line
### Question: Is heuristic analysis bad?
**Answer: NO!** It's a **smart fallback system**.
### Think of it like:
```
๐Ÿค– LLM = Professional coach analyzing replay (15s)
โ†’ Amazing advice, very specific
๐Ÿ“Š Heuristic = Quick tips from experienced player (instant)
โ†’ Good advice, always available
```
**Both are useful!**
- Heuristic ensures you **ALWAYS** get advice
- LLM makes the advice **AMAZING** when available
### System Design:
```
Try LLM first (best quality)
โ†“
If busy/timeout โ†’ Use Heuristic (instant backup)
โ†“
User gets advice ALWAYS โœ…
```
---
## ๐Ÿ” How to Tell?
Check the `source` field in the response:
```json
{
"summary": "...",
"tips": [...],
"coach": "...",
"source": "heuristic" โ† This tells you!
}
```
Or:
```json
{
"summary": "...",
"tips": [...],
"coach": "...",
"source": "llm" โ† Using AI model
}
```
---
## ๐Ÿ“ Real Example from Game
### Scenario: You just sent 3 NL commands quickly
```
[00:00] You: "move tanks north" โ†’ LLM busy (15s)
[00:01] You: "build infantry" โ†’ LLM queued
[00:02] You: "attack enemy base" โ†’ LLM queued
[00:03] AI Analysis triggers...
โ†’ LLM busy with your commands!
โ†’ Heuristic returns INSTANTLY โœ…
You see: "Battlefield is balanced. Scout
and take map control."
[00:18] LLM finishes your commands
[00:35] Next AI analysis uses LLM โœ…
You see: "Your tank column is advancing
well. Enemy is fortifying north
position - consider flanking from
east instead."
```
**You got advice at both times!**
- First: Heuristic (instant, generic but useful)
- Second: LLM (waited, specific and strategic)
---
## ๐ŸŽ‰ Summary
**Heuristic = Smart instant fallback**
- Not AI, just simple rules
- Instant response (no waiting)
- Always works (100% reliable)
- Generic but valid advice
- Ensures you NEVER wait without feedback
**It's a FEATURE, not a bug!** โœ…
The game prioritizes **user experience**:
- Fast response > Perfect response
- Some advice > No advice
- Reliable > Fancy
When the LLM is available and not busy, you get the amazing AI analysis. When it's busy, you get instant heuristic advice. **Win-win!** ๐ŸŽฏ