dsh-eval-guard 0.7.4 → 0.7.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. package/ISSUES.md +2 -2
  2. package/README.md +1 -1
  3. package/assets/neural-pendulum/SPEC.md +2 -0
  4. package/assets/neural-pendulum/TASK.md +3 -1
  5. package/assets/neural-pendulum/sim.cpp +25 -3
  6. package/assets/neural-pendulum/sim_agent +0 -0
  7. package/assets/neural-pendulum/sim_eval +0 -0
  8. package/assets/neural-pendulum/workspace.zip +0 -0
  9. package/neural-pendulum-eval/README.md +4 -2
  10. package/neural-pendulum-eval/run_eval.py +3 -1
  11. package/neural-pendulum-eval/sim_eval +0 -0
  12. package/package.json +1 -1
  13. package/assets/neural-pendulum/rl-feasibility/README.md +0 -60
  14. package/assets/neural-pendulum/rl-feasibility/RESULTS.md +0 -370
  15. package/assets/neural-pendulum/rl-feasibility/evaluate.py +0 -107
  16. package/assets/neural-pendulum/rl-feasibility/finalize_run.py +0 -168
  17. package/assets/neural-pendulum/rl-feasibility/models.py +0 -124
  18. package/assets/neural-pendulum/rl-feasibility/obs.py +0 -205
  19. package/assets/neural-pendulum/rl-feasibility/plot_selected.py +0 -55
  20. package/assets/neural-pendulum/rl-feasibility/reference_controller.py +0 -112
  21. package/assets/neural-pendulum/rl-feasibility/results/curves_selected.png +0 -0
  22. package/assets/neural-pendulum/rl-feasibility/results/reference_controller.json +0 -417
  23. package/assets/neural-pendulum/rl-feasibility/results/summary.json +0 -1599
  24. package/assets/neural-pendulum/rl-feasibility/results/table.md +0 -39
  25. package/assets/neural-pendulum/rl-feasibility/sim.py +0 -324
  26. package/assets/neural-pendulum/rl-feasibility/summarize.py +0 -175
  27. package/assets/neural-pendulum/rl-feasibility/task.py +0 -342
  28. package/assets/neural-pendulum/rl-feasibility/test_contract.py +0 -188
  29. package/assets/neural-pendulum/rl-feasibility/train_ppo.py +0 -403
  30. package/assets/neural-pendulum/rl-feasibility/train_sac.py +0 -406
package/ISSUES.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # dsh-eval-guard 问题清单与修复记录
2
2
 
3
- > 版本:0.7.4 · 审计基线:本仓库工作树(含 0.7.4 修复与 Neural-Pendulum 修正环境)
3
+ > 版本:0.7.5 · 审计基线:本仓库工作树(含 0.7.5 修复与 Neural-Pendulum 仿真器分离)
4
4
  > 对照 DSH:0.2.1-alpha.1 源码(并核对 git tag 0.1.2 / 0.1.5 / 0.1.7 / 0.2.0-rc.1 / rc.2 / 0.2.1-alpha.1)
5
5
  > 本文件先记录问题,再记录修复状态。状态:✅ 已修复 / 🟡 部分修复 / ⛔ 保留(有明确原因或需要架构级改动)
6
6
  >
@@ -8,7 +8,7 @@
8
8
  >
9
9
  > **2026-10-23 会话回归修复(未发版)**:修复 Neural-Pendulum 仿真器 Coriolis 项多乘 cos 导致高转速下能量注入并产生 NaN;修正关节 3 在绝对角坐标下的广义力矩映射,由 (tau1,0,tau3) 改为 (tau1,-tau3,tau3);TASK/SPEC/prompt 明确 theta 约定并说明工作区无 PyTorch/numpy;网络 syscall 改为 AF_INET/AF_INET6 的 EPERM 软拒绝,避免 pip/NSS 本地检查被误杀。
10
10
 
11
- > **0.7.4**:把上述修复连同固定物理的 RL 可行性环境一起发布。开发参考环境在 `assets/neural-pendulum/rl-feasibility/`(Python + CPU PyTorch,仅开发参考,不进入候选工作区);其中报告/汇总/曲线已验证 full 36D + base reward + random init 可学、去掉手动差分 da 或 H=1 会失败、PPO/SAC 应交付 best checkpoint。
11
+ > **0.7.5**:把 0.7.4 误带的开发用 RL 可行性代码从插件中撤除,只保留修正后的 simulator;同时把 agent 训练仿真器与评测仿真器拆开:`assets/neural-pendulum/sim_agent` 只做随机近向下 reset,评测用 `neural-pendulum-eval/sim_eval` 只做固定初态 reset,两者不共享 reset 分支。工作区仍只交付二进制,不提供仿真源码。
12
12
  > **下文仍有若干条目标记为 ✅ 但实际未完成,不要再按旧状态理解**:arena legacy 的 `std::thread` 静态规则未移除;`cifar_cache.py` 解压仍无 filter、缓存路径仍用 `Path.home()/.dsh`;CIFAR 隐藏标签仍以文件形式落盘;FP32“30 分钟整轮”仍是 op 间检查;无 baseline 时评测仍 fail-open。其余任务相关修复按用户要求暂不动。
13
13
 
14
14
  ## 0. 修复状态总览
package/README.md CHANGED
@@ -59,7 +59,7 @@ CIFAR-10 下载缓存固定在 DSH home 下(默认 `~/.dsh/eval-guard/cache/ci
59
59
 
60
60
  Neural-Pendulum 要求从零实现纯 C++17、仅 CPU、fp32 的神经网络基础设施,并用它训练控制器驱动预编译三节摆从接近全向下起摆、保持全向上 10 秒。后端评测与 Neural-FP32/Neural-CIFAR 使用同一套算子/阈值(单次调用 60 秒)。算子评测全部通过后才驱动 build/control,在固定初始状态上统计连续全向上时间 t_can。评测脚本在独立目录 `dsh-eval-guard/neural-pendulum-eval/`;宿主侧提交与测试工具为 eval_submit / eval_test,工具的注册依赖会话级 run 绑定。
61
61
 
62
- **开发参考环境**:修正后的 RL 可行性环境(Python + CPU PyTorch,仅开发参考,不进入候选工作区、不是提交依赖)放在 `assets/neural-pendulum/rl-feasibility/`。其 `sim.py` 与评测用 `sim_eval` 使用相同物理和物理关节力矩映射 `(tau1, -tau3, tau3)`;报告、汇总表和曲线在 `RESULTS.md` / `results/` 中。15 组实验确认:full 36D(cos/sin + 手动差分 da/5,H=4)+ base reward + random init 可学;去掉 da 或把 H 缩到 1 会失败;PPO/SAC 都应交付 best checkpoint。
62
+ **仿真器分离**:工作区 `tools/sim` 是只接受随机近向下 reset 的**训练/自测**仿真器,不包含固定评测初态;评测使用独立 `neural-pendulum-eval/sim_eval`,它只做固定初态 reset。工作区不提供仿真器源码。
63
63
 
64
64
  **分数**:eval_test 返回 stage / t_can / completed / error;时间积分 `final_score` 写入宿主侧事件与完成标记(`~/.dsh/eval-guard/neural-pendulum/<workspace>/done`),用于赛后排行,不返回给 agent。分数在**测试请求时刻**切换,当前测试结果也计入本次积分;宿主按事件重新计算最终值,而不是使用“评测完成时刻”的旧值。起算时间取首次提交写入的 meta.json。
65
65
 
@@ -132,6 +132,8 @@ score = -1
132
132
  ## 7. 训练仿真接口
133
133
 
134
134
  工作区提供预编译 tools/sim,不提供源码和内部参数。
135
+ `tools/sim` 是**训练/自测专用**:每次 reset 只生成随机近向下初始状态,不包含固定评测初态,也不提供切换评测初态的参数。
136
+ 评测使用独立程序 sim_eval;它每次 reset 固定为评测初始状态,不提供随机训练 reset。工作区不包含 sim_eval,也不包含任何固定评测初态参数。
135
137
  环境只提供状态转移,不提供 reward、loss、cost 或 done 信号。训练信号由 agent 自行构造。
136
138
 
137
139
  优化目标:
@@ -22,6 +22,8 @@ tools/sim
22
22
 
23
23
  不提供仿真器源码和物理参数。
24
24
 
25
+ 工具说明:工作区的 `tools/sim` 是训练/自测专用仿真器,每次 `reset` 只会生成随机近向下初始状态;它不是评测器,也没有切换到固定评测初态的参数。评测使用工作区之外的独立固定初态仿真程序。
26
+
25
27
  ### 初始状态
26
28
 
27
29
  训练时每次 reset:
@@ -134,7 +136,7 @@ tools/sim 使用 JSONL 协议。
134
136
  此时不返回新观测,也不会继续推进仿真。
135
137
  必须 reset 后才能开始新 episode;reset 前的 step 都返回该错误。
136
138
 
137
- 可以反复 reset / step 进行数据采集、模型训练和自测。
139
+ 可以反复 reset / step 进行数据采集、模型训练和自测。该程序只做随机训练 reset,不要依赖它获得评测固定初态;评测器是独立程序。
138
140
 
139
141
  环境只返回状态,不提供 reward、loss、cost 或 done 信号。
140
142
  训练信号由你自行构造。
@@ -89,13 +89,35 @@ static void deriv(const State& s, const std::array<double,N>& tau, State& out) {
89
89
  } // namespace
90
90
 
91
91
  int main(int argc, char** argv) {
92
- bool eval=false; int seed=0;
93
- for(int i=1;i<argc;i++){ std::string a=argv[i]; if(a=="--eval") eval=true; else if(a=="--seed" && i+1<argc) seed=std::atoi(argv[++i]); }
92
+ #ifdef DSH_EVAL_SIM
93
+ /* Evaluator-only binary: every reset MUST use the fixed evaluation state.
94
+ * This build deliberately contains no random training reset path. */
95
+ (void)argc;
96
+ (void)argv;
97
+ #else
98
+ /* Agent/training binary: every reset is a random near-down state. This
99
+ * build deliberately contains no fixed evaluation state and no --eval flag,
100
+ * so the agent cannot query the evaluation initial condition from it. */
101
+ int seed=0;
102
+ for(int i=1;i<argc;i++){
103
+ std::string a=argv[i];
104
+ if(a=="--seed" && i+1<argc) seed=std::atoi(argv[++i]);
105
+ }
94
106
  std::mt19937 rng(seed);
95
107
  std::uniform_real_distribution<double> u(-1.0,1.0);
108
+ #endif
96
109
  State s;
97
110
  int steps=0; bool ready=false;
98
- auto reset=[&](){ steps=0; ready=true; if(eval){ s.theta={0.013,-0.008,0.021}; s.omega={0,0,0}; } else { s.theta={u(rng)*0.05,u(rng)*0.05,u(rng)*0.05}; s.omega={u(rng)*0.02,u(rng)*0.02,u(rng)*0.02}; } };
111
+ auto reset=[&](){
112
+ steps=0; ready=true;
113
+ #ifdef DSH_EVAL_SIM
114
+ s.theta={0.013,-0.008,0.021};
115
+ s.omega={0,0,0};
116
+ #else
117
+ s.theta={u(rng)*0.05,u(rng)*0.05,u(rng)*0.05};
118
+ s.omega={u(rng)*0.02,u(rng)*0.02,u(rng)*0.02};
119
+ #endif
120
+ };
99
121
  // zero state
100
122
  s.theta={0,0,0}; s.omega={0,0,0};
101
123
  std::string line;
Binary file
Binary file
@@ -13,8 +13,10 @@ plugin UI.
13
13
  - `ops_eval.py` — operator oracle: PyTorch references computed in-process,
14
14
  weighted mean relative error compared with per-operation thresholds. Same
15
15
  operations, cases, seeds and thresholds as Neural-FP32 / Neural-CIFAR.
16
- - `sim_eval` — trusted simulator binary for the evaluation initial state.
17
- Identical to the workspace `tools/sim`.
16
+ - `sim_eval` — trusted evaluator-only simulator binary. Every `reset` is the
17
+ fixed evaluation initial state; it has no random training reset and no
18
+ `--eval` switch. The workspace `tools/sim` is a separate agent/training
19
+ binary that only does random near-down resets and is not shipped here.
18
20
 
19
21
  ## Requirements
20
22
 
@@ -266,7 +266,9 @@ def main() -> int:
266
266
  return emit(fail(ops_eval.infrastructure_error(ops), 'infra', workspace, events, ops=ops))
267
267
 
268
268
  env = child_env()
269
- sim = subprocess.Popen([str(SIM), '--eval'], stdin=subprocess.PIPE,
269
+ # sim_eval is the evaluator-only binary: reset is always the fixed
270
+ # evaluation initial state; no flag or random mode exists there.
271
+ sim = subprocess.Popen([str(SIM)], stdin=subprocess.PIPE,
270
272
  stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, env=env)
271
273
  control = subprocess.Popen([str(control_path)], cwd=str(source),
272
274
  stdin=subprocess.PIPE, stdout=subprocess.PIPE,
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-eval-guard",
3
- "version": "0.7.4",
3
+ "version": "0.7.5",
4
4
  "type": "module",
5
5
  "description": "Evaluation guard for DSH: workspace-only filesystem, no network/subagents, wall-clock limits, and task installation for meow-ai-arena and benchmark tasks.",
6
6
  "license": "MIT",
@@ -1,60 +0,0 @@
1
- # Neural-Pendulum RL feasibility harness (dev-only reference)
2
-
3
- This directory is a **development/reference** copy of the reinforcement-learning
4
- environment used to confirm that the corrected Neural-Pendulum dynamics are
5
- learnable. It is **not** part of the candidate runtime, is not copied into the
6
- task workspace, and is not required by the evaluator.
7
-
8
- ## Fixed physics
9
-
10
- The Python simulator here uses the same physical model as
11
- `assets/neural-pendulum/sim.cpp` / `sim_eval`:
12
-
13
- - three-link planar pendulum, 100 Hz ZOH, RK4 with 4 substeps;
14
- - physical joint-motor mapping in absolute-angle coordinates:
15
- `(tau1, -tau3, tau3)` (joint 3 is the internal motor between links 2 and 3,
16
- joint 2 is passive);
17
- - corrected Coriolis/centrifugal term
18
- `C_i = sum_j a_ij sin(theta_i-theta_j) omega_j^2`;
19
- - 30 s episodes, success tolerance 0.05 rad, t_tgt = 10 s.
20
-
21
- The Python simulator was cross-checked against `../../sim_eval` on the fixed
22
- initial state and identical action sequences; maximum observation error was
23
- about 5e-6 (simulator output has 6 significant digits).
24
-
25
- ## What was run
26
-
27
- 15 formal runs (PPO/SAC, observation ablations, reward ablations, fixed vs
28
- random init; 3 PPO seeds) in the corrected environment. Full report:
29
- `RESULTS.md`; machine table: `results/table.md`; aggregate:
30
- `results/summary.json`; selected curves: `results/curves_selected.png`.
31
-
32
- Core result:
33
-
34
- - PPO full 36D (cos/sin + manual-difference da/5, H=4), base reward, random
35
- init: first success 11.33M env steps / 16 min; best t_can 26.85 s.
36
- - SAC full 36D, base reward, random init: first success 0.70M env steps;
37
- best t_can 28.75 s.
38
- - Angle-only (no da) and H=1 observations fail; sparse/energy rewards fail in
39
- this budget; PPO needs random init, SAC can also learn from fixed init.
40
- - A non-RL pole-placement reference with 0.5 low-pass holds 29.99 s from the
41
- fixed upright offset and succeeds on 32/32 random upright offsets, proving
42
- the hold is physically feasible.
43
-
44
- ## Run
45
-
46
- Requires Python 3 with CPU PyTorch on the development host. This is **not**
47
- available to the candidate sandbox and is not a submission dependency.
48
-
49
- ```bash
50
- cd assets/neural-pendulum/rl-feasibility
51
- TORCH_THREADS=4 python3 test_contract.py
52
- TORCH_THREADS=4 python3 train_ppo.py --exp local_ppo --init rand --obs full \
53
- --reward base --envs 64 --minutes 30 --tb-root tb --ckpt-dir checkpoints --out results
54
- tensorboard --logdir tb
55
- ```
56
-
57
- TensorBoard event files are written live during training. The experiment
58
- matrix and per-run JSON/TB/log files from the full sweep are kept outside the
59
- npm package; only the source, report, summary, table and selected curve are
60
- bundled here to keep the plugin package small.
@@ -1,370 +0,0 @@
1
- # RESULTS — fixed-physics 3-link pendulum RL feasibility
2
-
3
- Work dir: `feasibility/neural-pendulum-fixedenv`.
4
- Started 2026-10-23 01:13 CST; experiment matrix completed 06:35 CST.
5
- All numbers below are new runs in the corrected environment; no old
6
- `(tau1, 0, tau3)` result is reused.
7
-
8
- ## TL;DR
9
-
10
- * **PPO and SAC both learn the corrected dynamics from the mandated random
11
- near-down resets and reach t_can >= 10 s at the fixed evaluation state.**
12
- * PPO (64 env, full 36D, base reward): first success at **11.33M env steps /
13
- 16.0 min**; best **26.85 s**; final 26.61 s; best-checkpoint robustness
14
- **8/8**.
15
- * SAC (32 env, full 36D, base reward): first success at **0.70M env steps /
16
- 24.7 min**; best **28.75 s**; final 28.21 s; best-checkpoint robustness
17
- **6/8** (final 8/8).
18
- * **The learnability boundary is set by observation information, not by the
19
- algorithm alone.**
20
- * `full` H=4 (36D: cos/sin + da/5) works for both PPO and SAC.
21
- * `angle` H=4 (24D, no da): PPO best 0.97 s, SAC best 0.04 s — fails.
22
- * `full_h1` (9D) and `angle_h1` (6D): fail completely (best <= 0.06 s);
23
- even a 50% upright curriculum does not rescue H=1.
24
- * `angle_h8` (48D): best 1.31 s — longer history helps a little but does
25
- not replace `da/5`.
26
- * **Reward boundary:** base works; sparse and strong energy shaping fail.
27
- * `sparse`: 19.12M steps, best t_can 0, policy converges to tiny actions.
28
- * `energy`: two designs fail; energy chasing with manual `da` has a
29
- degenerate high-kinetic-energy spin optimum.
30
- * **Init boundary is algorithm-dependent.** Random init is required for PPO
31
- (fixed-init PPO: best 1.19 s, robust 0/8). SAC fixed-init *does* succeed
32
- (best 28.86 s, robust 8/8) because its replay covers the state space; PPO's
33
- on-policy batch of 64 identical states does not.
34
- * Three PPO seeds all reach >26 s at their best checkpoint (26.85 / 27.17 /
35
- 28.46 s) but the final checkpoint is unreliable (seed 2 final 0.00 s).
36
- Use `_best.pt`, not the last model.
37
- * A non-RL pole-placement stabiliser with a 0.5 low-pass torque filter holds
38
- **29.99 s** from the fixed upright offset and succeeds on 32/32 random
39
- upright offsets. The hold is physically feasible with the corrected
40
- mapping; it is not an RL result (privileged omega).
41
-
42
- ## 1. Environment and contract
43
-
44
- * Simulator returns angles only: `reset(theta,omega)->theta`,
45
- `step(u)->theta`. No reward, done, info, omega or tip is exposed.
46
- * Corrected physical torque mapping:
47
- `generalized_torque = (tau1, -tau3, tau3)` (joint 3 is the internal motor
48
- between links 2 and 3; joint 2 is passive). `sim.generalized_torque` and
49
- `sim.make_torch_consts` both implement it. Do not revert to
50
- `(tau1, 0, tau3)`.
51
- * 100 Hz ZOH, RK4 with 4 substeps, 30 s episodes (3000 steps),
52
- tau_max = 18 N m, damping 0.01, success tolerance 0.05 rad, t_tgt = 10 s.
53
- * `test_contract.py` passes; the Python sim was cross-validated against
54
- `assets/neural-pendulum/sim_eval` by the Lead (~5e-6).
55
- * Training reset (random arm): theta_i ~ U(-0.05,0.05),
56
- omega_i ~ U(-0.02,0.02), independent, no fixed bias.
57
- Fixed arm: theta = [0.013,-0.008,0.021], omega = 0.
58
- Curriculum arm: 50% near down, 50% theta = pi + U(-0.05,0.05).
59
- * Evaluation: fixed theta = [0.013,-0.008,0.021], omega = 0, deterministic
60
- actions, full 3000 steps. t_can is the longest continuous interval with
61
- max_i |wrap(theta_i - pi)| < 0.05 rad. Eval always uses the **same** obs
62
- mode and reward preset as training; no reward switch at eval.
63
-
64
- ## 2. Code changes in this directory
65
-
66
- 1. `obs.py`: switchable modes
67
- `full` 36D, `angle` 24D, `full_h1` 9D, `angle_h1` 6D,
68
- `full_h2` 18D, `angle_h2` 12D, plus `angle_h8` 48D.
69
- `da` is always computed internally because the reward uses it;
70
- `diff=False` only drops it from the policy input.
71
- 2. `task.py`: reward presets `base`, `sparse`, `energy` (exact weights
72
- below), bounded energy error, and `--init mix` curriculum support.
73
- 3. `train_ppo.py` / `train_sac.py`: `--obs`, `--reward`, `--init`,
74
- `--up-frac`; eval uses the same mode/reward; live TB flush every
75
- iteration/log tick; 8 extra deterministic rollouts from the training
76
- reset distribution for final and best checkpoints.
77
- 4. `evaluate.py`: obs-mode aware, plus `evaluate_starts`.
78
- 5. `reference_controller.py`: non-RL local stabilisability proof.
79
- 6. `finalize_run.py`: rebuild a results JSON from checkpoints + log when a
80
- trainer crashes after saving checkpoints (used once for PPO seed 0).
81
- 7. `summarize.py`, `plot_selected.py`: aggregate table/JSON/curves.
82
-
83
- ## 3. Observation modes
84
-
85
- | --obs | frame | H | dim | contents |
86
- |---|---|---|---|---|
87
- | full | [cos a_i, sin a_i, da_i/5] | 4 | 36 | baseline |
88
- | angle | [cos a_i, sin a_i] | 4 | 24 | no explicit velocity |
89
- | full_h1 | [cos a_i, sin a_i, da_i/5] | 1 | 9 | single frame |
90
- | angle_h1 | [cos a_i, sin a_i] | 1 | 6 | single frame, no velocity |
91
- | full_h2 | [cos a_i, sin a_i, da_i/5] | 2 | 18 | optional |
92
- | angle_h2 | [cos a_i, sin a_i] | 2 | 12 | optional |
93
- | angle_h8 | [cos a_i, sin a_i] | 8 | 48 | longer history follow-up |
94
-
95
- `a_i = wrap_to_pi(theta_i - pi)`, `da_i = wrap_to_pi(a_i(t)-a_i(t-1))/dt`,
96
- `da(0)=0`. Theta is in radians, never divided by pi. History is oldest
97
- first, reset repeats the first frame. The eval builder is the same class and
98
- mode as training.
99
-
100
- ## 4. Reward presets (exact)
101
-
102
- ```
103
- r = exp * exp(-2 s2)
104
- + sharp * exp(-50 s2)
105
- - ang * s2
106
- - vel * sum da^2
107
- - act * sum u^2
108
- + up * 1[max_i |a_i| < 0.15]
109
- + tip * (tip_y + L)/(2L)
110
- - energy * |E - E_up| / (E_up - E_down)
111
- ```
112
-
113
- All terms are trainer-side functions of theta, the manual difference da and
114
- the action u; no simulator omega is read. `E` uses da as velocity. The
115
- energy error is clamped at 3 to keep the reward bounded at high spin.
116
-
117
- ```json
118
- base = {"exp":1.0,"sharp":2.0,"ang":0.05,"vel":0.001,"act":0.01,"up":3.0,"tip":0.3,"energy":0.01}
119
- sparse = {"exp":0.2,"sharp":0.0,"ang":0.0,"vel":0.001,"act":0.01,"up":3.0,"tip":0.0,"energy":0.0}
120
- energy = {"exp":1.0,"sharp":2.0,"ang":0.05,"vel":0.0,"act":0.005,"up":3.0,"tip":0.0,"energy":0.5}
121
- ```
122
-
123
- `sparse`: at the near-down reset the only dense term is ~1e-26, so it is an
124
- intentionally hard exploration test. `energy`: base angle terms + strong
125
- energy shaping + no velocity penalty; an earlier energy-dominant design
126
- (`exp=.2, sharp=.5, ang=.01, vel=.0002`) is archived under
127
- `failed/energy_unbounded/`.
128
-
129
- ## 5. Training protocol
130
-
131
- PPO: 2x128 tanh actor/critic, 64 envs, horizon 600, 8 epochs,
132
- minibatch 2048, lr 3e-4 linearly decayed to 20%, gamma .99, lambda .95,
133
- clip .2, entropy .003, vf .5, target KL .03, grad clip .5.
134
-
135
- SAC: 2x256 tanh squashed-Gaussian actor, shared-trunk twin Q, 32 envs,
136
- batch 256, replay 300k, auto-alpha (target entropy -2), gamma .99,
137
- tau .005, lr 3e-4, UTD 0.25 (8 gradient updates per 32-env vector step).
138
-
139
- Both: CPU PyTorch, 4 threads per process, at most 2 training processes
140
- concurrently. Eval every 5 PPO iterations (192k steps) or every 20k SAC
141
- steps. Best checkpoint by eval t_can (ties by return). Final checkpoint at
142
- the time budget. TensorBoard flushed during training (not only at the end).
143
- At the end, 8 deterministic rollouts from the training distribution are run
144
- for the final and best checkpoints.
145
-
146
- ## 6. Complete experiment matrix (fixed-initial deterministic eval)
147
-
148
- `best trob` = success rate / mean t_can of the best checkpoint on the 8
149
- training-distribution starts. `first succ` = first eval snapshot with
150
- t_can >= 10 s.
151
-
152
- | exp | algo | obs | reward | init | env steps | wall | best t_can | final t_can | ever_up | best trob | first succ |
153
- |---|---|---|---|---|---|---|---|---|---|---|---|
154
- | ppo_full_base_rand | PPO | full 36 | base | rand | 35.98M | 45.1 min | **26.85 s** | 26.61 s | yes | 8/8, 26.86 s | **11.33M** |
155
- | ppo_full_base_rand_s1 | PPO | full 36 | base | rand | 29.03M | 40.8 min | **27.17 s** | 0.23 s | yes | 8/8, 26.78 s | 13.63M |
156
- | ppo_full_base_rand_s2 | PPO | full 36 | base | rand | 34.21M | 40.3 min | **28.46 s** | 0.00 s | no | 8/8, 28.03 s | 14.40M |
157
- | sac_full_base_rand | SAC | full 36 | base | rand | 1.42M | 50.6 min | **28.75 s** | 28.21 s | yes | 6/8, 20.89 s | **0.70M** |
158
- | ppo_angle_base_rand | PPO | angle 24 | base | rand | 31.76M | 40.6 min | 0.97 s | 0.04 s | yes | 0/8 | - |
159
- | sac_angle_base_rand | SAC | angle 24 | base | rand | 1.42M | 50.6 min | 0.04 s | 0.00 s | no | 0/8 | - |
160
- | ppo_full_h1_rand | PPO | full_h1 9 | base | rand | 25.42M | 35.3 min | 0.06 s | 0.00 s | no | 0/8 | - |
161
- | ppo_angle_h1_rand | PPO | angle_h1 6 | base | rand | 20.97M | 30.4 min | 0.00 s | 0.00 s | no | 0/8 | - |
162
- | ppo_angle_h8_rand | PPO | angle_h8 48 | base | rand | 30.11M | 40.4 min | 1.31 s | 0.05 s | yes | 0/8 | - |
163
- | ppo_full_h1_mix | PPO | full_h1 9 | base | mix 50% up | 21.39M | 30.5 min | 0.02 s | 0.00 s | no | 0/8 | - |
164
- | ppo_angle_mix | PPO | angle 24 | base | mix 50% up | 23.50M | 30.5 min | 0.25 s | 0.00 s | yes | 0/8 | - |
165
- | ppo_full_sparse_rand | PPO | full 36 | sparse | rand | 19.12M | 30.5 min | 0.00 s | 0.00 s | no | 0/8 | - |
166
- | ppo_full_energy_rand | PPO | full 36 | energy | rand | 24.50M | 35.5 min | 0.00 s | 0.00 s | no | 0/8 | - |
167
- | ppo_full_base_fixed | PPO | full 36 | base | fixed | 21.93M | 30.5 min | 1.19 s | 0.00 s | no | 0/8 | - |
168
- | sac_full_base_fixed | SAC | full 36 | base | fixed | 1.41M | 50.4 min | **28.86 s** | 27.62 s | yes | 8/8, 23.45 s | **0.52M** |
169
-
170
- Raw machine-readable table: `results/table.md`; per-run JSON:
171
- `results/<exp>.json`; plots: `results/curves_selected.png`.
172
-
173
- ## 7. Results by question
174
-
175
- ### Q1. Are PPO and SAC learnable? Steps/time to t_can >= 10 s
176
-
177
- **Yes, both, with full 36D observations and base reward.**
178
-
179
- | algo | envs | first success | wall to first success | steps/s | budget | best | final | best-checkpoint robustness |
180
- |---|---|---|---|---|---|---|---|---|
181
- | PPO | 64 | 11.33M | 16.0 min | ~13.3k | 45 min / 35.98M | 26.85 s | 26.61 s | 8/8 (mean 26.86 s) |
182
- | SAC | 32 | 0.70M | 24.7 min | ~470 | 50 min / 1.42M | 28.75 s | 28.21 s | 6/8 (mean 20.89 s) |
183
-
184
- The first successful snapshots were 26.65 s (PPO at 11.33M) and 16.74 s
185
- (SAC at 0.70M). SAC needs ~16x fewer environment steps but ~28x more
186
- wall-clock per step, so the time to first success is comparable (16 vs
187
- 25 min). PPO is more stable
188
- at the final snapshot; SAC's final policy is also strong (8/8, 28.21 s).
189
- PPO eval t_can is intermittent during learning (0 <-> 26 s between successive
190
- 192k-step evals), so a single eval snapshot is not a reliable success
191
- criterion; both `_best.pt` and the success-snapshot count are reported.
192
-
193
- ### Q2. Angle-only vs full
194
-
195
- **Removing the explicit da/5 channel is a hard failure in this budget.**
196
-
197
- | exp | algo | obs | best t_can | final t_can | best robust |
198
- |---|---|---|---|---|---|
199
- | ppo_full_base_rand | PPO | full 36 | **26.85 s** | 26.61 s | 8/8 |
200
- | ppo_angle_base_rand | PPO | angle 24 | 0.97 s | 0.04 s | 0/8 |
201
- | sac_full_base_rand | SAC | full 36 | **28.75 s** | 28.21 s | 6/8 |
202
- | sac_angle_base_rand | SAC | angle 24 | 0.04 s | 0.00 s | 0/8 |
203
-
204
- PPO angle-only can swing up (ever_up=1, first up ~1.2 s) but cannot hold;
205
- its best up_frac is 3.8%. SAC angle-only only briefly touches upright
206
- (best t_can 0.04 s, a single 0.04 s window at ~9.23 s; final ever_up 0) and
207
- its final return is negative. H=4 cos/sin history does not substitute for
208
- the manually differentiated velocity. This is a budget
209
- statement, not an information-theoretic impossibility, but 40-50 min and
210
- 20-32M steps is a generous budget in this study.
211
-
212
- ### Q3. H=4 -> H=1 and reduced observations
213
-
214
- **H=1 destroys learnability even with explicit da; longer history only
215
- partially substitutes for da.**
216
-
217
- | exp | obs | dim | best t_can | final t_can | ever_up | best robust |
218
- |---|---|---|---|---|---|---|
219
- | ppo_full_base_rand | full H=4 | 36 | **26.85 s** | 26.61 s | yes | 8/8 |
220
- | ppo_full_h1_rand | full H=1 | 9 | 0.06 s | 0.00 s | no | 0/8 |
221
- | ppo_angle_base_rand | angle H=4 | 24 | 0.97 s | 0.04 s | yes | 0/8 |
222
- | ppo_angle_h8_rand | angle H=8 | 48 | 1.31 s | 0.05 s | yes | 0/8 |
223
- | ppo_angle_h1_rand | angle H=1 | 6 | 0.00 s | 0.00 s | no | 0/8 |
224
- | ppo_full_h1_mix | full H=1 + 50% upright | 9 | 0.02 s | 0.00 s | no | 0/8 |
225
-
226
- The 9D full_h1 observation has the instantaneous full state (cos/sin+da/5)
227
- but no temporal window. It never reaches upright in 25.4M steps. A
228
- curriculum with half the resets already near upright also fails (21.4M
229
- steps), so the H=1 failure is representational (no phase/acceleration
230
- history), not only exploration. Angle H=8 improves the best t_can from 0.97
231
- to 1.31 s but is still far from success; explicit da + H=4 is the only
232
- working combination.
233
-
234
- ### Q4. Reward preset
235
-
236
- | exp | reward | env steps | best t_can | final t_can | ever_up | note |
237
- |---|---|---|---|---|---|---|
238
- | ppo_full_base_rand | base | 35.98M | **26.85 s** | 26.61 s | yes | works |
239
- | ppo_full_sparse_rand | sparse | 19.12M | 0.00 s | 0.00 s | no | policy -> tiny actions |
240
- | ppo_full_energy_rand | energy v2 | 24.50M | 0.00 s | 0.00 s | no | spinning optimum |
241
- | (failed v1) | energy unbounded | 6.7M | 0.00 s | 0.00 s | no | value loss ~5e7 |
242
-
243
- **base** is the only working reward. **sparse** has no usable gradient from
244
- the near-down reset: `0.2 exp(-2 s2)` is ~1e-26 at s2~30, and the 0.15 rad
245
- bonus is never reached by exploration; the policy converges to mean |u|~0.01
246
- and return -0.39. This is an exploration failure, not a crash.
247
-
248
- **energy** failed in two designs. v1 used an unbounded energy error and
249
- blew up the value loss; v2 clamped it and kept the base angle terms but still
250
- never reached upright in 24.5M steps (0/24.5M, final return -1948). The
251
- reason is degenerate: total mechanical energy `E = T + V` can be driven to
252
- `E_up` by spinning fast at the wrong angles, and `T` computed from the
253
- manual `da` is noisy/large exactly there. A strong energy term rewards that
254
- spin instead of swing-up. The base reward keeps energy weight at 0.01;
255
- raising it is not a shortcut.
256
-
257
- ### Q5. Fixed-init vs random-init (algorithm-dependent)
258
-
259
- | exp | algo | init | env steps | best t_can | final t_can | best-checkpoint robustness |
260
- |---|---|---|---|---|---|---|
261
- | ppo_full_base_rand | PPO | random | 35.98M | **26.85 s** | 26.61 s | 8/8 (26.86 s) |
262
- | ppo_full_base_fixed | PPO | fixed | 21.93M | **1.19 s** | 0.00 s | 0/8 (mean return -99,380) |
263
- | sac_full_base_rand | SAC | random | 1.42M | **28.75 s** | 28.21 s | 6/8 (20.89 s) |
264
- | sac_full_base_fixed | SAC | fixed | 1.41M | **28.86 s** | 27.62 s | 8/8 (23.45 s) |
265
-
266
- PPO fixed-init **fails**, even at the exact evaluation state (best 1.19 s),
267
- and is catastrophic on the 8 distribution starts (mean return -99k). PPO's
268
- on-policy batch contains 64 copies of one state, so it never sees the
269
- balance-recovery states it needs.
270
-
271
- SAC fixed-init **succeeds** (best 28.86 s, final 27.62 s, 8/8 on the
272
- distribution starts, first success at 0.52M). SAC's replay buffer is filled
273
- with trajectories generated by its stochastic policy from that one start, so
274
- it does see off-state recovery data. This is the opposite of the old
275
- `(tau1,0,tau3)` round, where fixed-init SAC failed and fixed-init PPO looked
276
- better at the fixed state. The corrected internal-motor dynamics changes
277
- which algorithm can exploit a single start.
278
-
279
- ### Seed variance (PPO full/base/rand)
280
-
281
- | seed | env steps | first success | best t_can | final t_can | best-checkpoint robustness |
282
- |---|---|---|---|---|---|
283
- | 0 | 35.98M | 11.33M | 26.85 s | 26.61 s | 8/8 (26.86 s) |
284
- | 1 | 29.03M | 13.63M | 27.17 s | 0.23 s | 8/8 (26.78 s) |
285
- | 2 | 34.21M | 14.40M | 28.46 s | 0.00 s | 8/8 (28.03 s) |
286
-
287
- All three seeds solve the task at their best checkpoint (8/8 robust), but the
288
- final checkpoint is unreliable (seed 2 final 0.00 s). First success ranges
289
- 11.3-14.4M steps (16-20 min). **Always evaluate and ship `_best.pt`; the
290
- last model is not the policy.**
291
-
292
- ## 8. Non-RL feasibility reference
293
-
294
- `reference_controller.py` → `results/reference_controller.json`:
295
-
296
- * Linearise at upright, pole placement with poles -5..-10, full state
297
- (theta + simulator omega), torque command low-pass filtered with alpha=0.5.
298
- * Fixed start theta = pi + [0.013,-0.008,0.021], omega=0:
299
- **t_can = 29.99 s**, final angle error ~2e-14, saturation 0%.
300
- * 32 random upright offsets U(-0.05,0.05): **32/32 success**, mean t_can
301
- 24.79 s.
302
- * Without the low-pass filter the same controller chatters and holds only
303
- ~1.1 s; smooth torque commands matter for the 0.05 rad tolerance.
304
-
305
- This proves the upright hold is physically reachable with
306
- `(tau1,-tau3,tau3)` and |u|<=1. It uses privileged omega and starts near
307
- upright, so it is not comparable to the RL numbers; it is an existence check
308
- and a hint that action smoothness matters.
309
-
310
- ## 9. Failures / implementation notes (honest list)
311
-
312
- * PPO seed 0's results JSON was reconstructed by `finalize_run.py`. The
313
- trainer crashed **after** saving checkpoints and TB, in the post-training
314
- robustness code (passed `ActorCritic` instead of `ActorCritic.actor` to
315
- `make_ppo_policy`). Training was unaffected. The bug is fixed in
316
- `train_ppo.py`; all later PPO runs wrote JSON directly.
317
- * The first energy preset produced unbounded rewards and a value-loss blow-up
318
- (~5e7). It was killed at 6.7M steps and archived under
319
- `failed/energy_unbounded/`. The second energy design is the one in the
320
- matrix; it fails for a genuine reward-design reason, not a crash.
321
- * Sparse reward's no-op policy is a real exploration failure, not a logging
322
- artefact: final mean |u| ~0.01, return -0.39, ever_up 0.
323
- * Some runs show value-loss spikes (PPO fixed, PPO angle) without NaNs; the
324
- policies still train/plateau. These are recorded in TB.
325
- * No simulator physics was changed. The torque mapping was not reverted.
326
-
327
- ## 10. Artifacts
328
-
329
- * TensorBoard: `tb/<exp>/` (live event files, all 15 runs).
330
- * Logs: `logs/<exp>.log` (stdout, contains eval lines and DONE summary).
331
- * Results: `results/<exp>.json`, aggregate `results/summary.json`,
332
- `results/table.md`, `results/curves.png`, `results/curves_selected.png`.
333
- * Checkpoints: `checkpoints/<exp>_{best,last,final}.pt`.
334
- * Smoke tests: `smoke_archive/`; 6-min probe: `probes/`.
335
- * Failed/abandoned runs: `failed/energy_unbounded/`, `failed/energy_collapse/`.
336
- * Reference controller: `results/reference_controller.json`.
337
-
338
- ## 11. Reproduction
339
-
340
- ```bash
341
- cd feasibility/neural-pendulum-fixedenv
342
- export OMP_NUM_THREADS=4 MKL_NUM_THREADS=4 TORCH_THREADS=4
343
- python3 test_contract.py
344
- python3 train_ppo.py --exp ppo_full_base_rand --obs full --reward base \
345
- --init rand --envs 64 --horizon 600 --minutes 45 --eval-every 5 --seed 0
346
- python3 train_sac.py --exp sac_full_base_rand --obs full --reward base \
347
- --init rand --envs 32 --minutes 50 --eval-every 20000 --log-every 10 --seed 0
348
- python3 summarize.py
349
- python3 plot_selected.py
350
- ```
351
-
352
- ## 12. Recommendations / next steps
353
-
354
- 1. **For the actual C++ task, the controller should estimate velocity from
355
- angles and use a 4-frame history (or an explicit recurrent state).** The
356
- 3D observation is not enough by itself; the agent must reconstruct phase.
357
- `full` 36D is the strongest configuration in this study.
358
- 2. **Train from random near-down resets, not one fixed state.** For PPO this
359
- is the difference between success and failure; even for SAC, random init
360
- is more robust across the distribution.
361
- 3. **Use base reward; do not chase sparse or strong energy shaping.** If a
362
- sparse objective is required, add a curriculum or demonstration warm start;
363
- pure sparse PPO does nothing in 19M steps.
364
- 4. **Select checkpoints by eval t_can and ship the best, not the last.**
365
- Final models collapse in 2 of 3 PPO seeds.
366
- 5. **If more compute is available**, try (a) a recurrent/GRU policy on the 3D
367
- angle stream, (b) an action low-pass filter or action-rate penalty, and
368
- (c) a two-stage curriculum (balance near upright, then swing-up) with
369
- warm-started PPO. These target the two observed bottlenecks: velocity
370
- estimation and balance-phase coverage.