| # sim3 Simulator — 设计、产物与用法 |
|
|
| ## 1. 目标 |
|
|
| 测试一个 LLM 能否**通过主动实验恢复一个公式**(symbolic regression + active |
| experimentation 范式),而不是测 OOD 泛化。 |
|
|
| 每个 task 是一个"平行世界 oracle":被测 agent **主动请求带噪声的数据点**,从中 |
| 反推背后的解析公式。 |
|
|
| 关键设计 —— **memorization-proof 变体**:每个 task 的 ground truth 不是教科书 |
| 原式,而是一个 **LLM 提出的变体公式**:形状被改过(物理上仍合理、对真实数据拟合 |
| 得差不多),所以 agent **不能靠背公式过关**,必须真正从实验数据里把变了形的形状 |
| 恢复出来。 |
|
|
| 覆盖 **118 个 task**(typeI 66 + typeII 52)。 |
|
|
| - **Type I**:单一全局公式 `predict(X)`。 |
| - **Type II**:统一的函数 *形式* + 每个 group 的局部参数(agent 要恢复的是 |
| universal FORM)。 |
|
|
| --- |
|
|
| ## 2. 产物与目录结构 |
|
|
| ### 2.1 每个 task 的 simulator |
| ``` |
| tasks/<type>/<task>/simulator/ |
| state.joblib # 这个 task 的"世界"(公式 + 噪声模型 + 范围 + 预算) |
| sample.csv # 一份固定的带噪声样本(免费看,不耗预算) |
| formula.py # GRADER-ONLY:变体公式明文(= state.formula_source),grader 直接 import 取真值 |
| ``` |
| > simulator 目录里**不含** agent 用的 wrapper 代码 —— 引擎集中在 `harness/`(见 2.2)。 |
| > `formula.py` 是答案,只给 grader 用;runtime/agent 不读它(runtime 走 state 里的 formula_source)。 |
| |
| ### 2.2 共用引擎(本目录 `harness/`) |
| ``` |
| harness/ |
| simulator.py # 统一入口 load(sim_dir),自动辨别 typeI/typeII |
| sim_runtime/ |
| __init__.py # 注册 unpickle 别名,使 state.joblib 脱离 data_aug 可加载 |
| runtime.py # Type I 引擎(build_simulator) |
| typeII_runtime.py # Type II 引擎(多 group) |
| bootstrap.py # k-NN 残差噪声模型(ResidualBootstrap) |
| ``` |
| |
| ### 2.3 构建侧产物(`data_aug/sim3/`,开发用,非发布必需) |
| - `task_list.json` — 118 task 清单(name / tier / base) |
| - `base_functions_all.csv` — 每个 task 的 base function |
| - `quality_report.csv` — 每个 task 的 b% / r2_deb / 是否通过质量门槛 |
| - `formula_complexity.csv` — 公式复杂度 |
| - `emit_all_results.json` — 批量 emit 结果(含每 task 的 winner formula id) |
|
|
| --- |
|
|
| ## 3. state.joblib 内容 |
|
|
| `state.joblib` 是一个 dict,字段分两类。 |
|
|
| ### 引擎需要的(非答案,生成数据用) |
| | 字段 | 说明 | |
| |---|---| |
| | `schema_version` | `sim3.typeI.1` 或 `sim3.typeII.1` | |
| | `task` / `target` / `metric` | 标识、预测目标、评分 metric | |
| | `used_inputs` | 公式真正用到的输入列 | |
| | `all_input_cols` | 全部输入列(含干扰列,逼 agent 自己找哪个变量重要) | |
| | `x_axes` / `residual_space` | 采样轴(linear/log10)、残差空间(linear/log) | |
| | `support` | 各输入的合法范围 `{col: {min, max}}` | |
| | `noise_scale` | 噪声缩放(降到 target R²=0.9;干净 task = 1.0) | |
| | `bootstrap` | `ResidualBootstrap` 实例(k-NN 真实残差噪声模型) | |
| | `real_rows` | fetch_where 的查询池(保留多输入联合分布) | |
| | `fetch_budget_rows` | 主动实验总预算 | |
| | Type II 额外:`groups` | `{gid: {params, bootstrap, support, real_rows, n, noise_scale}}` | |
| |
| ### GRADER-ONLY(= 答案,绝不暴露给 agent) |
| | 字段 | 说明 | |
| |---|---| |
| | `formula_source` | 变体公式的**完整 Python 源码**(含 docstring/常量/predict) | |
| | `law_constants` | refit 后的公式系数(Type II 在 `groups[*].params`) | |
| | `formula_doc` / `equation_loc` / `paper_ref` | 公式说明、出处 | |
|
|
| --- |
|
|
| ## 4. 用法(主动实验) |
|
|
| agent 通过 `load()` 拿到一个 bundle,只暴露去泄漏的 fetch 接口,**看不到公式**。 |
|
|
| ```python |
| import sys; sys.path.insert(0, "<.../hf_realsr_benchmark_v3/harness>") |
| from simulator import load |
| |
| # ---- Type I ---- |
| sim = load(".../tasks/typeI/<task>/simulator") |
| sim.fetch_data(C1=[0.12, 0.16], n_samples=2, seed=0) # 在指定点取带噪声数据 |
| sim.fetch_where(query="C1 > 0.15", limit=20) # 查询合成池 |
| sim.load_sample() # 固定样本(免费) |
| sim.budget_status() # 剩余预算 |
| sim.TOOL_DESCRIPTION # 给 agent 的工具说明 |
| |
| # ---- Type II ---- |
| sim = load(".../tasks/typeII/<task>/simulator") |
| g = sim.list_groups() # {n_groups, used_inputs, groups{...}} |
| gid = next(iter(g["groups"])) |
| sim.fetch_data(group_id=gid, gamma=[0.1], n_samples=1, seed=0) |
| ``` |
|
|
| **生成规则**:每次 fetch 都是新一次抽样 |
| ``` |
| y = formula(X) + bootstrap.sample(X) × noise_scale |
| # log 空间:y = 10 ** ( log10(formula(X)) + 残差 ) |
| ``` |
| - `fetch_data` / `fetch_where` 都计入有限预算 `fetch_budget_rows`;`load_sample` 免费。 |
| - `noise_scale` 固定在 build 时校准值,**不可被调用方控制**(否则 agent 设 ns=0 就读到无噪声真值)。 |
| - 返回结构:`{source, n_returned, n_clipped, rows:[{...}], budget:{used, remaining}}`。 |
|
|
| **去泄漏**:bundle 不暴露 `formula_source` / `predict` / `law_constants` / `state`。 |
| 当前部署假设 agent 在 **fetch-only 沙箱**(只能调接口,读不到 `state.joblib` 文件 |
| 内容)。 |
|
|
| --- |
|
|
| ## 5. 噪声模型与质量框架 |
|
|
| ### 噪声 = k-NN 残差重采样(`bootstrap.py`) |
| 不拟合高斯,而是从 query 点**附近的真实残差**里重采样,保留异方差 / 非高斯 / |
| 偏态重尾。`local_mean` 估系统偏差(b),`local_std` 估测量噪声幅度(a)。 |
|
|
| ### (a)/(b) 残差分解 |
| 残差 = 测量噪声 (a) + 系统性模型偏差 (b)。emit 时用 k-NN 局部均值估 (b) 并**减掉**, |
| **只注入 (a)**,所以 simulator 的噪声是干净的测量噪声,不掺公式失配。 |
|
|
| ### 质量三条件(emit 前检查) |
| 1. **b% 小** —— 形状对(系统偏差占比低); |
| 2. **r2_deb ≥ 0.6** —— 去掉 b 后真实信号够强(降噪少); |
| 3. **aug-R² = 0.9** —— `noise_scale` 把 simulator 调到 R²=0.9(可恢复)。 |
| |
| ### refit |
| emit 前先在真实数据上重拟合 `LAW_CONSTANTS`(免费,修掉大部分系统偏差 —— 高 b% |
| 通常是没优化的发表系数,而非形状错)。 |
| |
| ### 全范围生成 |
| - 单输入:在 support 范围网格采样(log 轴 log-uniform); |
| - 多输入:重采样真实行(保留相关输入如 ρ(T) 的联合分布,避免独立采样撑大方差)。 |
| |
| --- |
| |
| ## 6. 构建流程(如何造出 simulator) |
| |
| 构建代码在 `data_aug/sim3/`(开发用,不随发布包)。流程:fan-out → 竞争 → emit。 |
| |
| ``` |
| (0) [仅 Type II] 保留 multi-group(每 group 一套局部参数,FORM 统一;不塌缩) |
| (1) gather task → context bundle(给 propose agent 读) |
| (2) propose × N 并行 N 个 agent,从不同角度各提一个候选变体公式 |
| (3) validate 每个候选:能跑 / fitR²≥0.50 / 满足 rubric / 反作弊 |
| (4) compete judge 打分选出唯一 winner(cand_variant) |
| (5) emit winner + 噪声模型 → 去泄漏带噪 simulator(见第 5 章) |
| ``` |
| |
| ### (1) gather — context bundle(`gather.py`) |
| 给 propose agent 的 markdown,内容(`gather.to_prompt`): |
| - **task 标识**:task_id / domain / context / target(名 + 单位 + 描述) |
| - **metric** + best baseline |
| - **inputs**:每个输入的名 / 单位 / range / 描述(USED_INPUTS 顺序) |
| - **真实数据**:train+test 合并后的逐列统计(count/mean/std/min/max)+ CSV 路径(agent 可自己 `pd.read_csv` 探查全量数据) |
| - **validity_rubrics**:物理约束(新公式必须**全部**满足) |
| - **baseline 公式**:每个 `formulas/*.py` 源码 + 实测 metric(让 agent 看到要从哪发展、要超越什么) |
| - **reference paper**:`reference/summary_*.md`(物理推导) |
|
|
| ### (2) propose × N — 给 agent 的 prompt |
| N 个 agent 并行(各从不同角度发展),把候选写到 `candidates/cand_variant.py`, |
| StructuredOutput 返回 `form / candidate_path / b_pct / passed / note`。**核心要求: |
| 变体必须改形状,不能只改系数**(emit 的 refit 会把纯系数变化重新优化回 baseline)。 |
| prompt 末尾让 agent 自测 `quality.py` / `typeII_validate.py` 确认改形状后仍 distinct。 |
|
|
| 下面 `<...>` 是按 task 填入的模板变量(`<base>`=baseline id,`<b_pct>`=当前系统偏差, |
| `<task_dir>`/`<out>`=路径)。 |
|
|
| **Type I prompt**(逐字): |
| ```text |
| Generate a MEMORIZATION-PROOF VARIANT formula (sim3 Type I) so a model that |
| memorized the textbook law cannot recognize it and must recover yours from the |
| simulator's noisy data. |
| TASK DIR: <task_dir> |
| Develop FROM baseline: <base> (current systematic-misfit b%=<b_pct>). |
| READ: metadata.yaml; formulas/<base>.py and the other formulas (copy the |
| contract: USED_INPUTS, LAW_CONSTANTS, def predict(X,**LAW_CONSTANTS)); |
| reference/summary_*.md; data/{train,test}.csv (probe with python). |
| PRODUCE a physically-plausible VARIANT that: |
| 1. DIFFERS IN FUNCTIONAL SHAPE from the baseline/textbook form — NOT just |
| coefficients (a downstream refit re-optimizes all coeffs, so a pure coeff |
| change collapses back to baseline). Change the SHAPE: add a small physical |
| correction term, change the parameterization, or use an equivalent-but- |
| distinct form. It must stay DISTINCT from the textbook form AFTER refit. |
| 2. Still FITS about as well (b% not much worse than <b_pct>). |
| 3. Respects the validity_rubrics / physics. |
| 4. Stays SIMPLE (a couple terms at most; not very complex). |
| Contract: USED_INPUTS same as baseline, LAW_CONSTANTS={your fitted coeffs}, |
| def predict(X,**LAW_CONSTANTS) pure-numpy vectorised. Fit coeffs yourself (scipy). |
| WRITE the module to: <out> |
| SELF-TEST: python data_aug/sim3/quality.py <task_dir> <out> |
| It reports b_pct, r2_deb, passed (gate: b%<0.15 AND r2_deb>=0.6). Confirm it runs |
| and the shape stays distinct after refit. |
| Return: form (one line), candidate_path=<out>, b_pct, passed, note. |
| ``` |
|
|
| **Type II prompt**(逐字,多 group): |
| ```text |
| Generate a MEMORIZATION-PROOF VARIANT form (sim3 Type II, MULTI-GROUP) so a model |
| that memorized the textbook law cannot recognize it. Each group_id has its own |
| local params; the FORM is universal. |
| TASK DIR: <task_dir> |
| Develop FROM baseline: <base> (current cross-group median b%=<b_pct>). |
| READ: metadata.yaml; formulas/<base>.py and others (copy the contract: |
| USED_INPUTS, LOCAL_FITTABLE, def fit(X_fit,y_fit)->dict, def predict(X,**params)); |
| reference/summary_*.md; data/{train,test_fit,test_test}.csv (group_id column; |
| probe per group). |
| PRODUCE a physically-plausible VARIANT that: |
| 1. DIFFERS IN FUNCTIONAL SHAPE from the baseline/textbook form (not just coeffs). |
| Add a small physical term / change parameterization / equivalent-distinct |
| form. Distinct after per-group fit. |
| 2. Cross-group median b% not much worse than <b_pct>. |
| 3. Respects validity_rubrics. |
| 4. Simple; fit() robust across ALL groups. |
| Contract: USED_INPUTS, LOCAL_FITTABLE, def fit(X_fit,y_fit)->dict, |
| def predict(X,**params) pure numpy + scipy. |
| WRITE the module to: <out> |
| SELF-TEST: python data_aug/sim3/typeII_validate.py <task_dir> <out> --baseline <base> |
| Confirm passed=true and report candidate median_b_pct vs baseline. |
| Return: form (one line), candidate_path=<out>, b_pct=median_b_pct, passed, note. |
| ``` |
|
|
| (批量编排:workflow `sim3-variants`,每个 task 一个 propose agent。) |
|
|
| ### (3) validate — 机械门槛(`validate.py` / `typeII_validate.py`) |
| - **能跑**:隔离 import,`predict` 在真实 X 上返回有限 `(n,)`;遵守 contract;纯 numpy(无 I/O、无网络)。 |
| - **fit ≥ 50%**:残差空间 R² ≥ 0.50(够解释主要结构即可,不追求完美拟合)。 |
| - **物理有效**:judge 子 agent 逐条打分 `validity_rubrics`,全过。 |
| - **反作弊**:无逐行查表 / 数据回显;系数数量合理;`predict` 是输入的闭式函数。 |
|
|
| ### (4) compete — 选 winner |
| 存活候选按 physics(rubric 全过 + 机制合理)/ simplicity(常数少、复杂度低)/ |
| fit adequacy(R²≥0.50 是**门槛非最大化目标**,避免奖励过拟合)/ develops-from-baseline |
| 评分,judge 选 top1 为 winner(平局看简洁度)。 |
|
|
| ### (5) emit — 见第 5 章 |
| refit + (a)/(b) 分解 + target-R²=0.9 + 全范围生成 → `state.joblib` + `sample.csv`。 |
|
|
| ### 重新生成 |
| ``` |
| python data_aug/sim3/run_emit_all.py # 批量 emit 到 data_aug/sim3/out[_typeII]/ |
| # 再把 winner 三件套拷入 tasks/<type>/<task>/simulator/ |
| # (emit 已把 formula_source 写进 state.joblib,故发布只需 state.joblib + sample.csv) |
| ``` |
|
|
| --- |
|
|
| ## 7. 现状与局限 |
|
|
| - **118/118 已就位并 standalone 验证通过**(脱离 `data_aug`,加载 + fetch + 去泄漏正常)。 |
| - 质量:~106/118 b% < 0.15。 |
| - **Hard tier**(b% 0.3–0.75,变体没完全抓全形状):afm / exfor / solar / fluid / |
| sex_mortality / life。 |
| - **信号弱**(~11 个,ns < 0.5,降噪到 R²=0.9):如 mincer / bird / binary_pulsar。 |
| - `qg_turbulence` 不做(空间场)。 |
| - **打分方案待设计**(grader 尚未确定)。 |
| - `state.joblib` / `formula.py` 含明文答案(`formula.py` 供 grader 直接 import),依赖 fetch-only 沙箱保密。 |
|
|