Datasets:
Publish fluidic pinball control comparison reproduction
Browse files
publication/hugging-face-control-comparison.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
## Reproducing the fluidic pinball control comparison
|
| 2 |
+
|
| 3 |
+
The final DreamerV3 policy reduced the regional mean horizontal tracking error by **53.252644770515%** and the mean pointwise velocity error by **39.285158772587%** across ten trained-policy rollouts and ten stationary-cylinder rollouts.
|
| 4 |
+
|
| 5 |
+
| Tracking metric | Trained policy (m/s) | No control (m/s) | Relative reduction (%) | 95% bootstrap CI for reduction (%) |
|
| 6 |
+
|---|---:|---:|---:|---:|
|
| 7 |
+
| Regional mean horizontal tracking error | 0.012586046684686648 | 0.026923548129944564 | 53.252644770514635 | [49.79375777871708, 56.51995002929369] |
|
| 8 |
+
| Mean pointwise velocity error | 0.03131580573428728 | 0.05157850222648336 | 39.285158772586556 | [37.47715016716165, 41.015005600731506] |
|
| 9 |
+
|
| 10 |
+
### Metrics and uncertainty
|
| 11 |
+
|
| 12 |
+
For each displayed half and each sample, the **regional mean horizontal tracking error** is the absolute difference between the spatial mean of the horizontal component and its target, `abs(mean(u) - u_target)`. The **mean pointwise velocity error** is the spatial mean of the Euclidean velocity residual, `mean(hypot(u - u_target, v))`. The vertical target is zero throughout. Both halves target −0.082 m/s horizontally for the first 30 s. The displayed upper half (native rows 8–15) targets zero after 30 s, while the lower half retains −0.082 m/s. Both components are converted from pixels per image pair to m/s with the published factor of 0.041.
|
| 13 |
+
|
| 14 |
+
Within [0, 60) s, each field is held until the next sample. For gaps longer than two nominal 30 Hz periods (with a 1 ns tolerance), and after the final sample, the hold lasts at most one nominal period. The integration splits at 30 s so each part is scored against its phase target. Each regional time integral is divided by the covered duration, excluding gaps beyond the hold and time before the first sample. The two halves receive equal weight within each rollout, and the ten rollouts receive equal weight within each group.
|
| 15 |
+
|
| 16 |
+
Relative reduction is `100 * (1 - trained_policy_mean / no_control_mean)`. Each of 100,000 bootstrap replicates independently draws ten complete rollouts with replacement from each group and recomputes both group means and the reduction. The script uses NumPy's PCG64 generator with seed 0, drawing trained-policy indices first and no-control indices second in ascending episode order within each group. Both metrics use the same draws. The 95% intervals use the 2.5th and 97.5th percentiles with linear interpolation. They quantify rollout-to-rollout uncertainty for the fixed trained policy.
|
| 17 |
+
|
| 18 |
+
### Run the reproduction
|
| 19 |
+
|
| 20 |
+
Save [`reproduce_pinball_control.py`](../scripts/reproduce_pinball_control.py) in your working directory and run the following with Python 3.11 and Bash. The download uses the pinned [Hugging Face dataset revision](https://huggingface.co/datasets/activefluidcontrol/fluidic-pinball-flow-tracking/tree/0251f2e407545a2c37d47da884172f0d7d718362).
|
| 21 |
+
|
| 22 |
+
```bash
|
| 23 |
+
set -e
|
| 24 |
+
python -m venv .venv
|
| 25 |
+
source .venv/bin/activate
|
| 26 |
+
python -m pip install numpy==2.2.6 pyarrow==23.0.1
|
| 27 |
+
revision=0251f2e407545a2c37d47da884172f0d7d718362
|
| 28 |
+
base="https://huggingface.co/datasets/activefluidcontrol/fluidic-pinball-flow-tracking/resolve/$revision"
|
| 29 |
+
mkdir -p pinball-data/analysis/flow_fields
|
| 30 |
+
curl -fLsS --retry 2 "$base/analysis/calibration.json" -o pinball-data/analysis/calibration.json
|
| 31 |
+
for group in eval no-control; do
|
| 32 |
+
for episode in {01..10}; do
|
| 33 |
+
path="analysis/flow_fields/$group-episode-$episode.parquet"
|
| 34 |
+
curl -fLsS --retry 2 "$base/$path" -o "pinball-data/$path"
|
| 35 |
+
done
|
| 36 |
+
done
|
| 37 |
+
python reproduce_pinball_control.py pinball-data > control-comparison.json
|
| 38 |
+
```
|
| 39 |
+
|
| 40 |
+
The script reads the 20 analysis-ready feedback tables and the calibration JSON. Its output includes the two metrics for every rollout, the group means and their intervals, and the relative reductions and their intervals.
|
scripts/reproduce_pinball_control.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Reproduce the control comparison from a Hugging Face analysis snapshot."""
|
| 2 |
+
import argparse
|
| 3 |
+
import json
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
import numpy as np
|
| 7 |
+
import pyarrow.parquet as pq
|
| 8 |
+
|
| 9 |
+
# Match the experiment's nominal schedule and the original integration rule.
|
| 10 |
+
DURATION = 60
|
| 11 |
+
SWITCH = 30
|
| 12 |
+
PERIOD = 1 / 30
|
| 13 |
+
LEFTWARD = -2.0 # pixels per pair
|
| 14 |
+
METRICS = ("mean_horizontal_absolute_error", "mean_pointwise_error")
|
| 15 |
+
RESAMPLES = 100_000
|
| 16 |
+
SEED = 0
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def rollout_errors(path, group, episode, velocity_scale):
|
| 20 |
+
data = pq.read_table(path, columns=["time_s", "u", "v", "run", "episode"]).to_pydict()
|
| 21 |
+
times = np.asarray(data["time_s"], dtype=float)
|
| 22 |
+
if (not len(times) or not np.isfinite(times).all()
|
| 23 |
+
or times[0] < 0 or times[-1] >= DURATION or np.any(np.diff(times) <= 0)):
|
| 24 |
+
raise ValueError(f"Invalid timestamps in {path}")
|
| 25 |
+
if set(data["run"]) != {group} or set(data["episode"]) != {episode - 1}:
|
| 26 |
+
raise ValueError(f"Unexpected rollout identity in {path}")
|
| 27 |
+
u = np.asarray(data["u"], dtype=float).reshape(len(times), 16, 16)
|
| 28 |
+
v = np.asarray(data["v"], dtype=float).reshape(len(times), 16, 16)
|
| 29 |
+
if not np.isfinite(u).all() or not np.isfinite(v).all():
|
| 30 |
+
raise ValueError(f"Nonfinite flow field in {path}")
|
| 31 |
+
|
| 32 |
+
following = np.r_[times[1:], DURATION]
|
| 33 |
+
ends = np.where(following - times <= 2 * PERIOD + 1e-9, following, times + PERIOD)
|
| 34 |
+
ends[-1] = min(times[-1] + PERIOD, DURATION)
|
| 35 |
+
ends = np.minimum(ends, DURATION)
|
| 36 |
+
weights = np.stack([
|
| 37 |
+
np.maximum(0, np.minimum(ends, SWITCH) - times),
|
| 38 |
+
np.maximum(0, ends - np.maximum(times, SWITCH)),
|
| 39 |
+
], axis=1)
|
| 40 |
+
covered = weights.sum()
|
| 41 |
+
regional_errors = []
|
| 42 |
+
for rows, targets in ((slice(8, 16), [LEFTWARD, 0.0]),
|
| 43 |
+
(slice(0, 8), [LEFTWARD, LEFTWARD])):
|
| 44 |
+
target = np.asarray(targets)
|
| 45 |
+
region_u, region_v = u[:, rows, :], v[:, rows, :]
|
| 46 |
+
horizontal = np.abs(region_u.mean(axis=(1, 2))[:, None] - target)
|
| 47 |
+
pointwise = np.hypot(
|
| 48 |
+
region_u[:, None] * velocity_scale - target[None, :, None, None] * velocity_scale,
|
| 49 |
+
region_v[:, None] * velocity_scale,
|
| 50 |
+
).mean(axis=(2, 3))
|
| 51 |
+
regional_errors.append([
|
| 52 |
+
(weights * horizontal).sum() / covered * velocity_scale,
|
| 53 |
+
(weights * pointwise).sum() / covered,
|
| 54 |
+
])
|
| 55 |
+
return np.mean(regional_errors, axis=0)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def compare(controlled, uncontrolled):
|
| 59 |
+
rng = np.random.default_rng(SEED)
|
| 60 |
+
controlled_draws = controlled[rng.integers(len(controlled), size=(RESAMPLES, len(controlled)))].mean(axis=1)
|
| 61 |
+
uncontrolled_draws = uncontrolled[
|
| 62 |
+
rng.integers(len(uncontrolled), size=(RESAMPLES, len(uncontrolled)))
|
| 63 |
+
].mean(axis=1)
|
| 64 |
+
if np.any(uncontrolled_draws <= 0):
|
| 65 |
+
raise ValueError("Bootstrap baseline must be positive")
|
| 66 |
+
reductions = 100 * (1 - controlled_draws / uncontrolled_draws)
|
| 67 |
+
return {
|
| 68 |
+
metric: {
|
| 69 |
+
"controlled_error_ms": float(controlled[:, i].mean()),
|
| 70 |
+
"no_control_error_ms": float(uncontrolled[:, i].mean()),
|
| 71 |
+
"error_reduction_pct": float(100 * (1 - controlled[:, i].mean() / uncontrolled[:, i].mean())),
|
| 72 |
+
"controlled_error_ci95_ms": np.quantile(controlled_draws[:, i], [0.025, 0.975]).tolist(),
|
| 73 |
+
"no_control_error_ci95_ms": np.quantile(uncontrolled_draws[:, i], [0.025, 0.975]).tolist(),
|
| 74 |
+
"error_reduction_ci95_pct": np.quantile(reductions[:, i], [0.025, 0.975]).tolist(),
|
| 75 |
+
}
|
| 76 |
+
for i, metric in enumerate(METRICS)
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def main():
|
| 81 |
+
parser = argparse.ArgumentParser(description=__doc__)
|
| 82 |
+
parser.add_argument("dataset_root", type=Path, help="Downloaded Hugging Face dataset root")
|
| 83 |
+
analysis = parser.parse_args().dataset_root / "analysis"
|
| 84 |
+
calibration = json.loads((analysis / "calibration.json").read_text())
|
| 85 |
+
velocity_scale = calibration["spatial_scale_m_per_pixel"] / (calibration["laser_interpulse_ms"] / 1000)
|
| 86 |
+
rollouts = {
|
| 87 |
+
group: np.array([
|
| 88 |
+
rollout_errors(analysis / "flow_fields" / f"{group}-episode-{i:02d}.parquet",
|
| 89 |
+
group, i, velocity_scale)
|
| 90 |
+
for i in range(1, 11)
|
| 91 |
+
])
|
| 92 |
+
for group in ("eval", "no-control")
|
| 93 |
+
}
|
| 94 |
+
print(json.dumps({
|
| 95 |
+
"metrics": compare(rollouts["eval"], rollouts["no-control"]),
|
| 96 |
+
"rollout_errors_ms": {
|
| 97 |
+
group: {str(i): dict(zip(METRICS, values.tolist()))
|
| 98 |
+
for i, values in enumerate(errors, start=1)}
|
| 99 |
+
for group, errors in rollouts.items()
|
| 100 |
+
},
|
| 101 |
+
"bootstrap": {"method": "percentile", "confidence_level": 0.95,
|
| 102 |
+
"resamples": RESAMPLES, "seed": SEED, "unit": "rollout", "paired": False},
|
| 103 |
+
}, indent=2))
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
if __name__ == "__main__":
|
| 107 |
+
main()
|