File size: 2,166 Bytes
1354b33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""apply_patch.py <repo root>: install magicsim/Collect/perturb.py and hook it into SyncRobotEnv.step / reset_idx (collect path only; the L3 replay / eval paths are untouched)."""
import shutil, sys
from pathlib import Path
root = Path(sys.argv[1]); here = Path(__file__).resolve().parent
shutil.copy(here / "perturb.py", root / "src/magicsim/Collect/perturb.py")
p = root / "src/magicsim/Env/Environment/SyncRobotEnv.py"; s = p.read_text()
a = "        self.direct_robot_action = False\n"
a2 = a + "        self._l3_perturb = None  # Collect.perturb.L3Perturber (MMB_COLLECT_PERTURB=1), built on the first collect-path step\n        self._l3_perturb_checked = False\n"
b = """        step_info = self.robot_manager.step(
            action=action_info["robot_action"], env_ids=env_ids
        )"""
b2 = """        exec_action = action_info["robot_action"]
        if not self.direct_robot_action:
            # perturbed collection (MMB_COLLECT_PERTURB=1): the recorded L3 stays the expert's; the robot executes it plus a temporary offset (Collect/perturb.py)
            if not self._l3_perturb_checked:
                self._l3_perturb_checked = True
                from magicsim.Collect.perturb import L3Perturber
                rm = self.robot_manager
                hint = ",".join([str(k) for k in (getattr(rm, "robots", None) or {})] + [type(v).__name__ for v in (getattr(rm, "robots", None) or {}).values()])
                self._l3_perturb = L3Perturber.from_env(self.num_envs, int(getattr(rm, "total_action_dim", exec_action.shape[-1])), hint)
            if self._l3_perturb is not None:
                exec_action = self._l3_perturb.apply(exec_action, env_ids)
        step_info = self.robot_manager.step(
            action=exec_action, env_ids=env_ids
        )"""
c = "        super().reset_idx(env_ids=env_id_list, seed=seed, options=options)\n"
c2 = c + "        if getattr(self, \"_l3_perturb\", None) is not None:\n            self._l3_perturb.reset(env_id_list)\n"
for x in (a, b, c):
    assert s.count(x) == 1, ("pattern count", s.count(x), x[:60])
s = s.replace(a, a2).replace(b, b2).replace(c, c2); p.write_text(s); print("patched", p)