File size: 2,885 Bytes
c399dd1 | 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 | """Parity check: batched nominal ROM (rom_batch.py) vs the release GoalEnv loop (l2_testing.py logic) with SB3 predict.
Needs a checkout of the original repository (github.com/uwsbel/sbel-reproducibility, folder
2024/PathFollowingSim2real/hmmwv_goal_reach) and the old `gym` package that its training script imports.
Release GoalEnv is imported unchanged; its np.random.normal draws are scaled to zero (nominal ROM).
Goals: 64 seeded goals (seed 77, radius 10-40 m; not goal set A or B). Writes results/rom_parity.json.
Usage: python verify_rom_parity.py /path/to/sbel-reproducibility/2024/PathFollowingSim2real/hmmwv_goal_reach
"""
import io
import json
import sys
from contextlib import redirect_stdout
from pathlib import Path
import numpy as np
import torch
from stable_baselines3 import PPO
from common import RELEASED_DIR, RESULTS, Actor, sha256
import rom_batch
PROJECT = Path(sys.argv[1]).resolve()
sys.path.insert(0, str(PROJECT))
from rl_script.rl_train_goal_reach import GoalEnv # noqa: E402
torch.set_num_threads(1)
def release_loop(model, goal):
orig = np.random.normal
np.random.normal = lambda loc=0.0, scale=1.0, size=None: orig(loc, 0.0 * scale, size)
try:
with redirect_stdout(io.StringIO()):
env = GoalEnv()
env.goal = [goal[0], goal[1]]
obs = env.relative_state_to_goal()
for i in range(400):
a, _ = model.predict(obs, deterministic=True)
obs, _, _, _ = env.step(a)
if env.old_distance < 3.0:
return round((i + 1) * 0.1, 1)
finally:
np.random.normal = orig
return None
def main():
rng = np.random.default_rng(77)
r, b = rng.uniform(10, 40, 64), rng.uniform(-np.pi, np.pi, 64)
goals = np.array([[float(ri * np.cos(bi)), float(ri * np.sin(bi))] for ri, bi in zip(r, b)])
report = dict(goals=len(goals), policies={})
for k in range(1, 5):
path = RELEASED_DIR / f'ppo_{k}.zip'
model = PPO.load(path, device='cpu')
ref = [release_loop(model, g) for g in goals]
out = rom_batch.simulate(Actor(path), goals, {}, noise_seed=0)
mine = [round(float(t), 1) if s else None for s, t in zip(out['success'], out['time_s'])]
diff = [i for i, (a, b) in enumerate(zip(ref, mine)) if a != b]
report['policies'][f'rel{k}'] = dict(sha256=sha256(path), mismatches_vs_release_loop=len(diff),
mismatch_detail=[(i, ref[i], mine[i]) for i in diff])
print(k, 'mismatches', len(diff), [(i, ref[i], mine[i]) for i in diff][:5], flush=True)
report['note'] = 'Mismatch = different first-entry step between the release loop and the batched ROM.'
RESULTS.mkdir(exist_ok=True)
(RESULTS / 'rom_parity.json').write_text(json.dumps(report, indent=2) + '\n')
if __name__ == '__main__':
main()
|