File size: 5,386 Bytes
1e41d82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8426919
 
4954d88
 
 
 
8426919
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
license: mit
task_categories:
- reinforcement-learning
- robotics
tags:
- world-model
- billiards
- jepa
- lewm
- pygame
- hdf5
pretty_name: Billiards World Model Training Dataset
size_categories:
- 100K<n<1M
---

# Billiards World-Model Training Environment

> **Author:** Santosh Jaiswal ([@hellojais](https://huggingface.co/hellojais))  
> **GitHub:** [hellojais/billiards-worldmodel](https://github.com/hellojais/billiards-worldmodel)  
> **Part of:** [LeWM Billiards Research](https://github.com/hellojais/le-wm)  

A 2D billiards simulator built with **Pygame** that acts as a training
environment for a world model (LeWM). A scripted geometric agent plays
the game automatically and all episodes are saved to a compact HDF5 dataset.

---

## Project structure

```
billiards-worldmodel/
├── game.py           # Gymnasium-compatible Pygame environment
├── agent.py          # Scripted geometric agent (no ML)
├── collect_data.py   # Runs agent, records data → HDF5
├── play.py           # Interactive human play (no recording)
├── requirements.txt  # Python dependencies
└── README.md         # This file
```

---

## Installation

```bash
# (Recommended) create a virtual environment first
python -m venv .venv
source .venv/bin/activate      # macOS / Linux
# .venv\Scripts\activate       # Windows

pip install -r requirements.txt
```

---

## Running each file

### 1. `play.py` — Human interactive mode
```bash
python play.py
```
- **Left-click** anywhere on the table to shoot the cue ball toward the cursor.
- Press **R** to reset the episode.
- Press **ESC** or **Q** to quit.
- No data is saved.

---

### 2. `collect_data.py` — Collect expert dataset
```bash
# Default: 5000 episodes → billiards_expert_train.h5
python collect_data.py

# Custom number of episodes and output file
python collect_data.py --episodes 100 --out test_run.h5

# Fix random seed for reproducibility
python collect_data.py --seed 0
```

Progress is printed every 500 episodes. The script prints a final summary:
```
✓ Saved 5000 episodes (348212 frames) to 'billiards_expert_train.h5'
  Success rate : 87.3%
  Elapsed time : 142.0s  (35.2 ep/s)
```

---

### 3. `game.py` — Gymnasium environment (import or quick smoke-test)
```bash
python game.py        # runs a 10-step smoke test with random actions
```
Or use it as a library:
```python
from game import BilliardsEnv

env = BilliardsEnv(render_mode="rgb_array")
obs, info = env.reset(seed=0)
for _ in range(300):
    action = env.action_space.sample()
    obs, reward, terminated, truncated, info = env.step(action)
    frame = env.render()   # numpy array (512, 512, 3) uint8
    if terminated or truncated:
        break
env.close()
```

---

### 4. `agent.py` — Scripted agent (import or quick test)
```bash
python agent.py       # prints 5 sample actions
```

---

## HDF5 dataset format

| Dataset      | Shape                           | dtype   | Description                        |
|--------------|---------------------------------|---------|------------------------------------|
| `pixels`     | `(total_frames, 512, 512, 3)`   | uint8   | RGB frames (one per timestep)      |
| `action`     | `(total_frames, 2)`             | float32 | `(dx, dy)` impulse applied to cue  |
| `state`      | `(total_frames, 10)`            | float32 | Full state vector (see below)      |
| `ep_len`     | `(num_episodes,)`               | int32   | Number of frames in each episode   |
| `ep_offset`  | `(num_episodes,)`               | int64   | Start frame index of each episode  |

**State vector layout** (indices 0–9):

| Index | Value               |
|-------|---------------------|
| 0–1   | cue ball position   |
| 2–3   | cue ball velocity   |
| 4–5   | target ball position|
| 6–7   | target ball velocity|
| 8–9   | nearest pocket (x,y)|

### Reading the dataset
```python
import h5py, numpy as np

with h5py.File("billiards_expert_train.h5", "r") as f:
    pixels    = f["pixels"]     # lazy; index as needed
    actions   = f["action"][:]
    states    = f["state"][:]
    ep_len    = f["ep_len"][:]
    ep_offset = f["ep_offset"][:]

# Reconstruct episode 42
i   = ep_offset[42]
n   = ep_len[42]
frames = pixels[i : i + n]      # shape (n, 512, 512, 3)
```

---

## Environment details

| Parameter        | Value                  |
|------------------|------------------------|
| Window size      | 512 × 512 px           |
| Ball radius      | 15 px                  |
| Pocket radius    | 20 px                  |
| Friction         | 0.985 per step         |
| Max steps/ep     | 300                    |
| Success condition| Target ball enters pocket |

---

## Agent strategy

The scripted agent uses the **ghost-ball** technique from billiards:

1. Identify the pocket nearest to the target ball.
2. Compute the *ghost-ball* position G — where the cue ball's centre
   must be at impact for the target to travel toward that pocket:

$$G = T - \hat{u}_{PT} \cdot 2R$$

where $T$ is the target position, $\hat{u}_{PT}$ is the unit vector
from pocket to target, and $R$ is the ball radius.

3. Apply an impulse in the direction $G - C_\text{cue}$, scaled by
   `impulse_strength`, with small Gaussian noise for episode variety.
4. The impulse is only applied when the cue ball is nearly stationary
   (speed < 0.5 px/step) to avoid stacking impulses mid-roll.