File size: 7,869 Bytes
6389aef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# Tower of Hanoi

**Transfer N disks between three pegs following size constraints**

---

## Overview

The Tower of Hanoi is a classic recursive puzzle consisting of three pegs (labeled A, B, and C) and N disks of different sizes, numbered from 1 (smallest) to N (largest). This puzzle is famous in computer science for demonstrating recursion and exponential time complexity.

### Difficulty Rating: ⭐⭐⭐⭐⭐ (Very Hard - Exponential)

---

## 📊 Statistics

| Metric | Value |
|--------|-------|
| **Total Puzzles** | 60 |
| **Total Moves** | 12,216 |
| **Training Puzzles (N=1-7)** | 42 |
| **Test Puzzles (N=8-10)** | 18 |
| **Difficulty Parameter** | N (number of disks) |
| **Number of Pegs** | 3 (A, B, C) |
| **Solution Length** | L(N) = **2^N - 1** (exponential!) |
| **Transition Locality** | O(N) - must check top disk constraints |

---

## 🎯 Puzzle Rules

### Objective
Transfer all N disks from a designated **start peg** to a **target end peg** while maintaining size ordering (largest at bottom, smallest at top) throughout all intermediate states.

### Constraints
1. **Single Disk Movement**: Only one disk may be moved at a time
2. **Top Disk Access**: Only the topmost disk from any peg can be selected for movement
3. **Size Ordering Constraint**: A larger disk may **never** be placed on top of a smaller disk

### Why Tower of Hanoi is Extremely Challenging

Tower of Hanoi is the **hardest** puzzle in the RecurrReason benchmark:

1. **Exponential Solution Length**: L(N) = 2^N - 1
   - N=3: 7 moves
   - N=7: 127 moves
   - N=10: **1,023 moves!**

2. **Recursive Structure**: Optimal solution requires decomposing problem recursively:
   - Move top N-1 disks to auxiliary peg
   - Move largest disk to target peg
   - Move N-1 disks from auxiliary to target peg

3. **Compounding Errors**: With per-step error rate ε, success probability is:
   ```
   P(success) ≈ (1-ε)^(2^N - 1) → 0 as N grows
   ```

---

## 📋 State Representation

States are represented as **lists of three lists**, where each list represents one peg (A, B, C) containing disks ordered from **top to bottom**.

### Format
```python
[[1, 2, 3], [], []]
```

This represents:
- **Peg A**: Disks 1 (top), 2, 3 (bottom)
- **Peg B**: Empty
- **Peg C**: Empty

**Important**: Disks are numbered 1 (smallest) to N (largest).

### Move Representation
```python
[1, 'A', 'B']
```

This represents: **Move disk 1 from peg A to peg B**

Format: `[disk_number, source_peg, destination_peg]`

---

## 🖼️ Example Puzzle

![Tower of Hanoi Example](https://github.com/gowravmannem/Recurrent-Reasoning-on-Puzzles/blob/main/assets/tower_of_hanoi.png?raw=true)

### Example Trajectory (N=2)

**Initial State**: `[[], [], [1, 2]]` (both disks on peg C)  
**Goal State**: `[[], [1, 2], []]` (both disks on peg B)  
**Start Peg**: C  
**Goal Peg**: B  
**Optimal Solution Length**: 3 moves (2^2 - 1 = 3)

**Step-by-step solution:**

| Step | Current State | Next State | Move | Description |
|------|--------------|-----------|------|-------------|
| 0 | `[[], [], [1, 2]]` | `[[1], [], [2]]` | `[1, 'C', 'A']` | Move disk 1 from C to A |
| 1 | `[[1], [], [2]]` | `[[1], [2], []]` | `[2, 'C', 'B']` | Move disk 2 from C to B |
| 2 | `[[1], [2], []]` | `[[], [1, 2], []]` | `[1, 'A', 'B']` | Move disk 1 from A to B |
| 3 | `[[], [1, 2], []]` | `[[], [1, 2], []]` | `['_', '_', '_']` | Goal reached! |

### Recursive Pattern

The recursive pattern for N disks:
```
function HANOI(n, source, target, auxiliary):
    if n == 1:
        move disk 1 from source to target
    else:
        HANOI(n-1, source, auxiliary, target)  # Move n-1 to aux
        move disk n from source to target       # Move largest
        HANOI(n-1, auxiliary, target, source)  # Move n-1 to target
```

---

## 📁 CSV Column Descriptions

### Columns

| Column | Type | Description |
|--------|------|-------------|
| `N` | int | Number of disks (difficulty parameter) |
| `start_state` | string | Initial configuration of all three pegs |
| `goal_state` | string | Target configuration to achieve |
| `start_peg` | string | Starting peg ('A', 'B', or 'C') |
| `goal_peg` | string | Target peg ('A', 'B', or 'C') |
| `current_state` | string | State before this move |
| `next_state` | string | State after applying this move |
| `move` | string | Action taken: `[disk, source_peg, dest_peg]` |
| `num_moves` | int | Total moves in optimal solution (2^N - 1) |

### Data Format

Each row represents one **move** in a solution trajectory.

**Example CSV rows:**
```csv
N,start_state,goal_state,start_peg,goal_peg,current_state,next_state,move,num_moves
2,"[[],[],[1,2]]","[[],[1,2],[]]",C,B,"[[],[],[1,2]]","[[1],[],[2]]","[1,'C','A']",3
2,"[[],[],[1,2]]","[[],[1,2],[]]",C,B,"[[1],[],[2]]","[[1],[2],[]]","[2,'C','B']",3
2,"[[],[],[1,2]]","[[],[1,2],[]]",C,B,"[[1],[2],[]]","[[],[1,2],[]]","[1,'A','B']",3
2,"[[],[],[1,2]]","[[],[1,2],[]]",C,B,"[[],[1,2],[]]","[[],[1,2],[]]","['_','_','_']",3
```

---

## 💡 Usage Tips

### For Model Training

⚠️ **Warning**: Tower of Hanoi is **difficult** for current sequence models.

Suggested approaches:
1. **Add explicit subgoal markers**: Annotate when recursive subproblems start/end
2. **Hierarchical representations**: Encode recursive structure explicitly
3. **Search augmentation**: Use beam search or MCTS during decoding
4. **Curriculum learning**: Start with N=1, slowly increase (but likely still fails at N≥3)

### For Evaluation

```python
from datasets import load_dataset

# Load Tower of Hanoi
dataset = load_dataset("gmannem/RecurrReason", "tower_of_hanoi")

# WARNING: Expect very low success rates!
# Models typically solve only N=1

def evaluate_hanoi(model, example):
    """
    Evaluation with strict constraints.
    
    A single size-ordering violation = immediate failure.
    """
    current = example['start_state']
    goal = example['goal_state']
    steps = 0
    max_steps = 2 * example['num_moves']  # 2 × (2^N - 1)
    
    while steps < max_steps:
        next_state = model.predict(current, goal)
        
        # Check size ordering (CRITICAL!)
        if violates_size_constraint(next_state):
            return "INVALID_MOVE", steps
            
        if next_state == goal:
            return "SUCCESS", steps
            
        current = next_state
        steps += 1
    
    return "TIMEOUT", steps

def violates_size_constraint(state):
    """Check if any peg has larger disk on top of smaller."""
    for peg in state:
        for i in range(len(peg) - 1):
            if peg[i] > peg[i+1]:  # Larger disk on top!
                return True
    return False
```

---

## 🔬 Research Directions

Tower of Hanoi poses fundamental challenges for sequence models:

1. **Hierarchical Planning**: How to encode recursive subgoals?

2. **Search Integration**: Can we augment models with A* or MCTS?

3. **Neuro-Symbolic Approaches**: Combine neural prediction with symbolic constraint checking

4. **Explicit Memory**: External memory to track subproblem state

5. **Length Generalization**: Current models cannot extrapolate from short to long sequences on this task

**Key Insight**: Success on Tower of Hanoi likely requires **search** or **explicit hierarchical representations**, not just larger models.

---


## 📚 References

**Main Paper:**
```bibtex
@inproceedings{mannem2026recurrent,
  title={Recurrent Reasoning on Symbolic Puzzles with Sequence Models},
  author={Gowrav Mannem and Chowdhury Marzia Mahjabin and Jason Chen and Shivank Garg and Kevin Zhu},
  booktitle={ICLR 2026 Workshop on Logical Reasoning of Large Language Models},
  year={2026}
}
```

**Classic Reference:**
```bibtex
@article{lucas1883tower,
  title={Récréations mathématiques},
  author={Lucas, Édouard},
  journal={Gauthier-Villars},
  year={1883}
}
```

---

[← Back to Main README](README.md)