Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/patches/7f4ee48345bbc3ff # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers ## GitHub aien-dev/omega-mv#249 (code only) closed derived `7f4ee48345bbc3ff` #249 Imported from GitHub aien-dev/omega-mv#249 (code only) GitHub's metadata for this pull request is unavailable: its state is DERIVED from the commits (merged: the head is reachable from this project's main; open: GitHub kept a merge ref; else closed). Base `8da7f77194a3fae0c3dd561a76d1fe0e23971867` · Head `85f8b54398b99a3f2c7a41536d2808ec7d9a5e23` ### Apply it ``` git checkout -b patch-7f4ee48345bbc3ff 8da7f77194a3fae0c3dd561a76d1fe0e23971867 curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/7f4ee48345bbc3ff.patch | git am ``` Raw patch (mbox) · Diff ### Provenance This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record: ``` github-import 1 aien-dev/omega-mv 249 85f8b54398b99a3f2c7a41536d2808ec7d9a5e23 ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":249,"url":"https://github.com/aien-dev/omega-mv/pull/249","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:36:58Z","metadata":"unavailable","state_derived":true} ``` ### Changes ``` diff --git a/docs/physics0/LEARNER.md b/docs/physics0/LEARNER.md new file mode 100644 index 000000000..6204d3361 --- /dev/null +++ b/docs/physics0/LEARNER.md @@ -0,0 +1,140 @@ +# PD-0 learner (Physics-0 Discovery Engine, Direction 4) + +The first genuine Physics-0 learner. It never sees the generators, the world +source, the oracle, the constants or the evidence files. It receives the +describe record and the PD0REC1 observation records of FIT and SELECT +episodes only (the harness withholds HOLDOUT, TRIAL and REP records, and the +learner refuses them if offered), and it emits evidence for the independent +ladder checker of `src/physics0/ladder`. It never declares a ladder state. + +Spec: aien-dev/physics `docs/PD0_HIDDEN_EQUATION_BENCHMARK.md` rev 2 +(2f881b1); world parameters follow rev 3 (omega #248, merged); the two rev 5 +rules (requested-reset preregistration hash, no T2 self pairs) are +implemented in the checker and the learner ahead of the spec text. + +## Layout + +| path | role | +|---|---| +| `src/physics0/learner/pd0_learner.{h,c}` | the learner library (C11, no file I/O, no process calls) | +| `src/physics0/learner/pd0_learner_main.c` | `pd0-learner`: deterministic offline fit over a raw PD0REC1 stream | +| `tests/physics0/learner/pd0_harness.c` | development harness: drives `pd0-world`, tags the split, builds the PD0LEDG1 ledger, runs the checker and the scorer | +| `tests/physics0/learner/pd0_truth_main.c` | test-only truth process (the only binary here that links `pd0_gen.c`) | +| `tests/physics0/learner/test_pd0_learner.c` | unit tests | +| `mk/physics0-learner.mk` | `test-physics0-learner`, `physics0-learner-dev`, `physics0-learner-evidence`, `p0l-isolation` | + +The substrate headers (`src/physics0/pd0_wire.h` family) and the verifier +headers (`src/physics0/ladder/pd0_fmt.h`) define the same names with +different limits, so one binary cannot include both. The learner and the +harness are built on the verifier side only; the world and the truth run as +separate processes over pipes. The harness reads the describe record itself +(PD0DESC2 with per-variable reset bounds; PD0DESC1 is refused). + +## Mechanisms + +1. **Numerical prediction.** One-step change of every observed variable is + regressed on a monomial vocabulary of total degree at most 3 over the + observed variables and the intervention channels (constant included, 20 + monomials for two variables and one channel). Per target: forward + selection with backward pruning under a description-length score, least + squares by normal equations in doubles on unit values. Floats live only + here; coefficients are rounded to micro-units and every evaluation after + that uses the checker's integer `pd0_rel_step`. +2. **Symbolic sparse search and description length.** Score per equation = + residual code length `n * log2(max(rmse_micro, 1))` plus the spec 6.2 + term bits `8 * (n_vars + n_channels) + 24` per term. The candidate set is + the MDL-best relation, plus one alternative per target (its runner-up + equation), re-ranked on SELECT by residual bits plus description bits. + Candidates serialise as PDLAW1 with `description_bits` from the spec rule. +3. **Model comparison.** Up to 8 candidates, always including the explicit + null (no change) and at least one rival. The harness registers the best as + CANDIDATE, the next two as RIVAL, the null as NULL, and the falsifier names + them all. A lag-1 residual autocorrelation per variable flags suspected + hidden state (`hidden_state_suspected`). + +Also in the library: the T2 correlation evidence (strongest pair over +visible OK step records with a seeded 2000-shuffle permutation test, +Bonferroni over all pairs), seeded exploration schedules, a planner stand-in +(Direction 5 owns the real one: among 256 seeded schedules inside the bounds +and not in the forbidden hash list, the one where the hypotheses disagree +most in units of pooled residual sd; `-1` below 3 sd), and `rollout` / +`onestep_nrmse` prediction interfaces. Everything is deterministic from the +seed; the determinism test compares bytes. + +## Harness protocol (development, seeds 1..20 allowed, 1..5 run) + +Episodes are 20 steps. Exploration tags per five episodes: FIT FIT FIT +SELECT HOLDOUT (60/20/20). After exploration: CORR, RELATION entries, +FALSIFIER (eps = the level's in-box bound, 5 trials), five PREREG + TRIAL +episodes, then three BATCH entries of ten REP episodes (planner and random +halves alternate). On a refutation the trial records become FIT and the +learner refits (at most three attempts). The checker runs after each stage; +the harness never proceeds to trials without a HYPOTHESIS state. The scorer +runs on 20 noise-free truth episodes (10 in the reset box, 10 at 1.5x). + +## Development results (seeds 1..5, rev 3 world, rev 5 checker rules) + +Learner never saw HOLDOUT/TRIAL/REP. "state" is the checker's verdict; +"scorer" is `pd0_score` against the truth. The previous receipt (rev 2 +world, DESC1) is kept beside the new one. + +| level | state reached (seeds) | scorer | size / bound | bits | notes | +|---|---|---|---|---|---| +| L0 | PREDICTED 5/5 | PASS 5/5 (exact) | 2 / 4 | 96 | T6 "trial missing": the rev 3 L0 episode is 20 steps, the 20th record is EPISODE_END not OK, so no trial has 20 OK steps; see spec item 2 | +| L1 | PROVISIONAL_LAW 5/5 | PASS 5/5 | 3 / 5 | 144 | exact, one-step error 0 | +| L2 | PROVISIONAL_LAW 5/5 | PASS 5/5 | 4 / 6 | 192 | exact | +| L3 | PROVISIONAL_LAW 5/5 | PASS 5/5 | 8 / 10 | 576 | four variables, two channels, exact | +| L4 | PROVISIONAL_LAW 5/5 | PASS 5/5 (in-box 0.008 to 0.023 on the rev 3 box) | 4 / 6 | 192 | cubic term found | +| L5 | INTERVENED 1/5, CANDIDATE 4/5 (1 to 3 refutations) | PASS 5/5 (in-box 0.004 to 0.015, constants within 10%) | 4 / 6 | 192 | preregistration now matches (rev 5 rule); trials fail on in-trial NRMSE, see spec item 1 | +| L6 | CANDIDATE 5/5 (3 refutations each) | FAIL 5/5 (307 latent missing, 308 reference too good) | 5 to 6 / 9 | 240 to 288 | reported, not claimed: no latent mechanism; rev 3 constants make the hidden variable matter more (in-box 0.05 to 0.08) | +| null | OBSERVATION 5/5 | not scored | 2 / 6 | 96 | no correlation evidence once self pairs are excluded; no candidate admitted; NC-1 holds | + +Numbers per instance: `evidence/physics0/learner/pd0-learner-dev-.txt`. + +## Failure analysis + +- **L5 (noise).** The relation is right (scorer PASS on all seeds, constants + within 10%) and trials now match their preregistration. They fail T6 because + the trial error is an NRMSE normalised by the within-trial spread of the + observed trajectory: a quiet 20-step trial has a small spread, so 0.02 + observation noise alone exceeds the 0.05 bound. Same mechanism refutes + REP episodes (code 266). Not a learner defect; see spec item 1. +- **L6 (hidden variable).** Candidates are latent-free fits; on 1 of 5 + seeds the ladder still replicates one (the checker has no latent + requirement; the scorer does and rejects all five). On the other seeds the + trials or batches exceed eps, which is the honest outcome for a wrong + model class. `hidden_state_suspected` did not fire on L6 because the + latent-free fit's one-step residuals are small (0.001 to 0.005); the flag + needs a rollout-based test, left for the next cut. +- **L0.** Exact relation on every seed; blocked at T6 because a 20-step + episode yields 19 OK records plus one EPISODE_END record (spec item 2). +- **Null world.** With self pairs excluded (rev 5) no correlation evidence + exists and the ladder stays at OBSERVATION. The MDL ranking still puts + "pull to the mean" above the empty relation, which is the correct + predictor of an i.i.d. world; its one-step error is 1.0 sd, far above eps. + +## Spec items that proved unimplementable or ambiguous as written + +1. **Noisy-level trial error.** T6/T7 normalise the trial error by the + within-trial spread of the observed trajectory. On L5 (sigma 0.02) any + trial whose trajectory spread is below about 0.4 units fails the 0.05 + bound even for the true relation. The spec should normalise by the + declared reset-box scale, or set the noisy eps relative to sigma. Spec + owner decision needed; the learner claims nothing on L5. +2. **L0 episode length.** With rev 3 the L0 episode is exactly 20 steps and + the world marks the 20th step EPISODE_END, so no episode has the 20 OK + steps T5/T6/T7 require. Either the world returns OK on step 20 and + refuses step 21, or the checker counts an EPISODE_END step as observed. +3. **L6 pass rule** (factor-3 reference margin) is still open with Drake; + the learner reports L6 and claims nothing. +4. **Requested reset on noisy levels** (rev 5 rule as implemented): the + ledger record does not carry the requested reset vector, so on noisy + levels the checker binds the reset through the PD0EXP1 entry and the + steps only; the trial error bound covers a mismatched reset. A reset + record that carries the requested values would close this. + +## Not implemented + +Latent-variable fitting, local refinement in the planner (Direction 5), +multi-step consistency in candidate ranking, PD0DESC2 parsing beyond the +assumed per-variable layout. diff --git a/evidence/physics0/learner/.crumb b/evidence/physics0/learner/.crumb new file mode 100644 index 000000000..e4f33e7fb --- /dev/null +++ b/evidence/physics0/learner/.crumb @@ -0,0 +1,6 @@ +{ + "schema_version": "1.0.0", + "name": "evidence/physics0/learner", + "layer": "evidence", + "purpose": "Immutable receipts of learner development runs (seeds 1..5 per level, HOLDOUT/TRIAL/REP withheld by the harness). New files per cut, never rewritten." +} diff --git a/evidence/physics0/learner/pd0-learner-dev-4eae52d11089.txt b/evidence/physics0/learner/pd0-learner-dev-4eae52d11089.txt new file mode 100644 index 000000000..c3c0ad961 --- /dev/null +++ b/evidence/physics0/learner/pd0-learner-dev-4eae52d11089.txt @@ -0,0 +1,48 @@ +receipt: PD0_LEARNER_DEV +commit: 4eae52d1108921560b5859e14e30a93af736eeed +date_utc: 2026-10-04T00:30:35Z +levels: 0 1 2 3 4 5 6 null +seeds: 1 2 3 4 5 (development seeds; HOLDOUT/TRIAL/REP withheld by the harness) +--- isolation +PHYSICS0_ISOLATION: PASS (3 learner-side objects clean) +--- results +PD0L level=L0 seed=1 desc=v2 episodes=115 steps=2300 fit=1254 select=418 state=PREDICTED code=250 stall=250 refutations=0 attempts=1 cand_size=2 cand_bits=96 size_bound=4 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=0 extrap=0 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L0 seed=2 desc=v2 episodes=115 steps=2298 fit=1252 select=418 state=PREDICTED code=250 stall=250 refutations=0 attempts=1 cand_size=2 cand_bits=96 size_bound=4 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=0 extrap=0 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=1 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L0 seed=3 desc=v2 episodes=115 steps=2299 fit=1254 select=418 state=PREDICTED code=250 stall=250 refutations=0 attempts=1 cand_size=2 cand_bits=96 size_bound=4 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=0 extrap=0 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=1 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L0 seed=4 desc=v2 episodes=115 steps=2299 fit=1254 select=417 state=PREDICTED code=250 stall=250 refutations=0 attempts=1 cand_size=2 cand_bits=96 size_bound=4 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=0 extrap=0 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=2 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L0 seed=5 desc=v2 episodes=115 steps=2300 fit=1254 select=418 state=PREDICTED code=250 stall=250 refutations=0 attempts=1 cand_size=2 cand_bits=96 size_bound=4 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=0 extrap=0 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L1 seed=1 desc=v2 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=3 cand_bits=144 size_bound=5 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=1 extrap=1 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L1 seed=2 desc=v2 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=3 cand_bits=144 size_bound=5 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=2 extrap=2 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L1 seed=3 desc=v2 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=3 cand_bits=144 size_bound=5 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=6 extrap=6 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L1 seed=4 desc=v2 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=3 cand_bits=144 size_bound=5 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=1 extrap=1 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L1 seed=5 desc=v2 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=3 cand_bits=144 size_bound=5 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=8 extrap=4 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L2 seed=1 desc=v2 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=4 cand_bits=192 size_bound=6 fit_nrmse=0 select_nrmse=1 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=2 extrap=2 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L2 seed=2 desc=v2 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=4 cand_bits=192 size_bound=6 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=1 extrap=1 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L2 seed=3 desc=v2 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=4 cand_bits=192 size_bound=6 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=1 extrap=2 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L2 seed=4 desc=v2 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=4 cand_bits=192 size_bound=6 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=4 extrap=3 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L2 seed=5 desc=v2 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=4 cand_bits=192 size_bound=6 fit_nrmse=1 select_nrmse=1 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=5 extrap=3 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L3 seed=1 desc=v2 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=8 cand_bits=576 size_bound=10 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=4 extrap=3 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L3 seed=2 desc=v2 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=8 cand_bits=576 size_bound=10 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=1 extrap=1 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L3 seed=3 desc=v2 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=8 cand_bits=576 size_bound=10 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=3 extrap=4 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L3 seed=4 desc=v2 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=8 cand_bits=576 size_bound=10 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=4 extrap=3 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L3 seed=5 desc=v2 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=8 cand_bits=576 size_bound=10 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=3 extrap=2 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L4 seed=1 desc=v2 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=4 cand_bits=192 size_bound=6 fit_nrmse=1 select_nrmse=2 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=23 extrap=28 onestep=2 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L4 seed=2 desc=v2 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=4 cand_bits=192 size_bound=6 fit_nrmse=1 select_nrmse=1 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=9 extrap=8 onestep=1 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L4 seed=3 desc=v2 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=4 cand_bits=192 size_bound=6 fit_nrmse=1 select_nrmse=1 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=8 extrap=6 onestep=1 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L4 seed=4 desc=v2 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=4 cand_bits=192 size_bound=6 fit_nrmse=1 select_nrmse=1 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=9 extrap=6 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L4 seed=5 desc=v2 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=4 cand_bits=192 size_bound=6 fit_nrmse=1 select_nrmse=1 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=15 extrap=6 onestep=1 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L5 seed=1 desc=v2 episodes=275 steps=5500 fit=3420 select=1040 state=CANDIDATE code=251 stall=250 refutations=3 attempts=3 cand_size=4 cand_bits=192 size_bound=6 fit_nrmse=27760 select_nrmse=32376 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=3964 extrap=4989 onestep=420 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L5 seed=2 desc=v2 episodes=295 steps=5900 fit=3120 select=1040 state=CANDIDATE code=266 stall=261 refutations=1 attempts=1 cand_size=4 cand_bits=192 size_bound=6 fit_nrmse=38039 select_nrmse=42991 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=10846 extrap=9891 onestep=1088 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L5 seed=3 desc=v2 episodes=300 steps=5980 fit=3220 select=1040 state=INTERVENED code=261 stall=261 refutations=1 attempts=2 cand_size=4 cand_bits=192 size_bound=6 fit_nrmse=31165 select_nrmse=34968 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=14948 extrap=15107 onestep=1945 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=1 propose_fail=0 trial_random=0 +PD0L level=L5 seed=4 desc=v2 episodes=295 steps=5900 fit=3120 select=1040 state=CANDIDATE code=266 stall=261 refutations=1 attempts=1 cand_size=4 cand_bits=192 size_bound=6 fit_nrmse=33607 select_nrmse=33926 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=9881 extrap=10085 onestep=1294 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L5 seed=5 desc=v2 episodes=275 steps=5500 fit=3420 select=1040 state=CANDIDATE code=251 stall=250 refutations=3 attempts=3 cand_size=4 cand_bits=192 size_bound=6 fit_nrmse=31352 select_nrmse=32542 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=7310 extrap=7497 onestep=688 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L6 seed=1 desc=v2 episodes=275 steps=5500 fit=3420 select=1040 state=CANDIDATE code=251 stall=250 refutations=3 attempts=3 cand_size=6 cand_bits=288 size_bound=9 fit_nrmse=10622 select_nrmse=10781 hidden_state=1 score=FAIL score_code=307 mask=0x183 inbox=83238 extrap=170182 onestep=0 ref=66472 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L6 seed=2 desc=v2 episodes=275 steps=5500 fit=3420 select=1040 state=CANDIDATE code=251 stall=250 refutations=3 attempts=3 cand_size=5 cand_bits=240 size_bound=9 fit_nrmse=6758 select_nrmse=6819 hidden_state=1 score=FAIL score_code=307 mask=0x181 inbox=67405 extrap=74489 onestep=0 ref=63828 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L6 seed=3 desc=v2 episodes=275 steps=5500 fit=3420 select=1040 state=CANDIDATE code=251 stall=250 refutations=3 attempts=3 cand_size=5 cand_bits=240 size_bound=9 fit_nrmse=8389 select_nrmse=8490 hidden_state=1 score=FAIL score_code=307 mask=0x181 inbox=60855 extrap=75487 onestep=0 ref=58964 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L6 seed=4 desc=v2 episodes=275 steps=5500 fit=3420 select=1040 state=CANDIDATE code=251 stall=250 refutations=3 attempts=3 cand_size=5 cand_bits=240 size_bound=9 fit_nrmse=6525 select_nrmse=6878 hidden_state=1 score=FAIL score_code=307 mask=0x181 inbox=54335 extrap=73008 onestep=0 ref=59366 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L6 seed=5 desc=v2 episodes=275 steps=5500 fit=3420 select=1040 state=CANDIDATE code=251 stall=250 refutations=3 attempts=3 cand_size=5 cand_bits=240 size_bound=9 fit_nrmse=7792 select_nrmse=7646 hidden_state=1 score=FAIL score_code=307 mask=0x181 inbox=72836 extrap=69814 onestep=0 ref=68627 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=null seed=1 desc=v2 episodes=110 steps=2200 fit=1320 select=440 state=OBSERVATION code=270 stall=211 refutations=0 attempts=1 cand_size=2 cand_bits=96 size_bound=6 fit_nrmse=1000700 select_nrmse=1000751 hidden_state=0 score=NOT_SCORED score_code=-1 mask=0x0 inbox=0 extrap=0 onestep=0 ref=0 score_eps=0 law=-1 checker_rc=270 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=null seed=2 desc=v2 episodes=110 steps=2200 fit=1320 select=440 state=OBSERVATION code=270 stall=211 refutations=0 attempts=1 cand_size=2 cand_bits=96 size_bound=6 fit_nrmse=1000709 select_nrmse=1001724 hidden_state=0 score=NOT_SCORED score_code=-1 mask=0x0 inbox=0 extrap=0 onestep=0 ref=0 score_eps=0 law=-1 checker_rc=270 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=null seed=3 desc=v2 episodes=110 steps=2200 fit=1320 select=440 state=OBSERVATION code=270 stall=211 refutations=0 attempts=1 cand_size=2 cand_bits=96 size_bound=6 fit_nrmse=999896 select_nrmse=1007210 hidden_state=0 score=NOT_SCORED score_code=-1 mask=0x0 inbox=0 extrap=0 onestep=0 ref=0 score_eps=0 law=-1 checker_rc=270 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=null seed=4 desc=v2 episodes=110 steps=2200 fit=1320 select=440 state=OBSERVATION code=270 stall=211 refutations=0 attempts=1 cand_size=2 cand_bits=96 size_bound=6 fit_nrmse=1000521 select_nrmse=1000384 hidden_state=0 score=NOT_SCORED score_code=-1 mask=0x0 inbox=0 extrap=0 onestep=0 ref=0 score_eps=0 law=-1 checker_rc=270 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=null seed=5 desc=v2 episodes=110 steps=2200 fit=1320 select=440 state=OBSERVATION code=270 stall=211 refutations=0 attempts=1 cand_size=2 cand_bits=96 size_bound=6 fit_nrmse=1000243 select_nrmse=1000843 hidden_state=0 score=NOT_SCORED score_code=-1 mask=0x0 inbox=0 extrap=0 onestep=0 ref=0 score_eps=0 law=-1 checker_rc=270 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 diff --git a/evidence/physics0/learner/pd0-learner-dev-9f1d059928f6.txt b/evidence/physics0/learner/pd0-learner-dev-9f1d059928f6.txt new file mode 100644 index 000000000..71a0409e7 --- /dev/null +++ b/evidence/physics0/learner/pd0-learner-dev-9f1d059928f6.txt @@ -0,0 +1,48 @@ +receipt: PD0_LEARNER_DEV +commit: 9f1d059928f6221d5367a12054ffac6bec656fa1 +date_utc: 2026-10-04T00:25:39Z +levels: 0 1 2 3 4 5 6 null +seeds: 1 2 3 4 5 (development seeds; HOLDOUT/TRIAL/REP withheld by the harness) +--- isolation +PHYSICS0_ISOLATION: PASS (3 learner-side objects clean) +--- results +PD0L level=L0 seed=1 desc=v1 episodes=55 steps=922 fit=186 select=69 state=INTERVENED code=261 stall=261 refutations=0 attempts=1 cand_size=2 cand_bits=96 size_bound=4 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=0 extrap=0 onestep=0 ref=0 score_eps=18 law=0 checker_rc=0 oob=18 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L0 seed=2 desc=v1 episodes=55 steps=862 fit=142 select=51 state=INTERVENED code=261 stall=261 refutations=0 attempts=1 cand_size=2 cand_bits=96 size_bound=4 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=0 extrap=0 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=25 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L0 seed=3 desc=v1 episodes=55 steps=798 fit=117 select=54 state=INTERVENED code=261 stall=261 refutations=0 attempts=1 cand_size=2 cand_bits=96 size_bound=4 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=0 extrap=0 onestep=0 ref=0 score_eps=19 law=0 checker_rc=0 oob=31 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L0 seed=4 desc=v1 episodes=55 steps=783 fit=114 select=26 state=INTERVENED code=261 stall=261 refutations=0 attempts=1 cand_size=2 cand_bits=96 size_bound=4 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=0 extrap=0 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=29 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L0 seed=5 desc=v1 episodes=55 steps=878 fit=160 select=44 state=INTERVENED code=261 stall=261 refutations=0 attempts=1 cand_size=2 cand_bits=96 size_bound=4 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=0 extrap=0 onestep=0 ref=0 score_eps=19 law=0 checker_rc=0 oob=24 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L1 seed=1 desc=v1 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=3 cand_bits=144 size_bound=5 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=1 extrap=1 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L1 seed=2 desc=v1 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=3 cand_bits=144 size_bound=5 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=2 extrap=2 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L1 seed=3 desc=v1 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=3 cand_bits=144 size_bound=5 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=6 extrap=6 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L1 seed=4 desc=v1 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=3 cand_bits=144 size_bound=5 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=1 extrap=1 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L1 seed=5 desc=v1 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=3 cand_bits=144 size_bound=5 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=8 extrap=4 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L2 seed=1 desc=v1 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=4 cand_bits=192 size_bound=6 fit_nrmse=0 select_nrmse=1 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=2 extrap=2 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L2 seed=2 desc=v1 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=4 cand_bits=192 size_bound=6 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=1 extrap=1 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L2 seed=3 desc=v1 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=4 cand_bits=192 size_bound=6 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=1 extrap=2 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L2 seed=4 desc=v1 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=4 cand_bits=192 size_bound=6 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=4 extrap=3 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L2 seed=5 desc=v1 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=4 cand_bits=192 size_bound=6 fit_nrmse=1 select_nrmse=1 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=5 extrap=3 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L3 seed=1 desc=v1 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=8 cand_bits=576 size_bound=10 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=4 extrap=3 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L3 seed=2 desc=v1 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=8 cand_bits=576 size_bound=10 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=1 extrap=1 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L3 seed=3 desc=v1 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=8 cand_bits=576 size_bound=10 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=3 extrap=4 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L3 seed=4 desc=v1 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=8 cand_bits=576 size_bound=10 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=4 extrap=3 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L3 seed=5 desc=v1 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=8 cand_bits=576 size_bound=10 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=3 extrap=2 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L4 seed=1 desc=v1 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=4 cand_bits=192 size_bound=6 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=2 extrap=2 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L4 seed=2 desc=v1 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=4 cand_bits=192 size_bound=6 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=2 extrap=1 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L4 seed=3 desc=v1 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=4 cand_bits=192 size_bound=6 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=4 extrap=2 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L4 seed=4 desc=v1 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=4 cand_bits=192 size_bound=6 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=4 extrap=2 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L4 seed=5 desc=v1 episodes=145 steps=2900 fit=1320 select=440 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=4 cand_bits=192 size_bound=6 fit_nrmse=0 select_nrmse=0 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=5 extrap=3 onestep=0 ref=0 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L5 seed=1 desc=v1 episodes=265 steps=5300 fit=3120 select=1040 state=HYPOTHESIS code=252 stall=240 refutations=0 attempts=1 cand_size=4 cand_bits=192 size_bound=6 fit_nrmse=30248 select_nrmse=32375 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=3903 extrap=4164 onestep=497 ref=0 score_eps=20 law=0 checker_rc=252 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L5 seed=2 desc=v1 episodes=265 steps=5300 fit=3120 select=1040 state=HYPOTHESIS code=252 stall=240 refutations=0 attempts=1 cand_size=4 cand_bits=192 size_bound=6 fit_nrmse=38039 select_nrmse=42991 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=10846 extrap=9891 onestep=1088 ref=0 score_eps=20 law=0 checker_rc=252 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L5 seed=3 desc=v1 episodes=265 steps=5300 fit=3120 select=1040 state=HYPOTHESIS code=252 stall=240 refutations=0 attempts=1 cand_size=4 cand_bits=192 size_bound=6 fit_nrmse=32341 select_nrmse=34964 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=17789 extrap=18118 onestep=2176 ref=0 score_eps=20 law=0 checker_rc=252 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L5 seed=4 desc=v1 episodes=265 steps=5300 fit=3120 select=1040 state=HYPOTHESIS code=252 stall=240 refutations=0 attempts=1 cand_size=4 cand_bits=192 size_bound=6 fit_nrmse=33607 select_nrmse=33926 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=9881 extrap=10085 onestep=1294 ref=0 score_eps=20 law=0 checker_rc=252 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L5 seed=5 desc=v1 episodes=265 steps=5300 fit=3120 select=1040 state=HYPOTHESIS code=252 stall=240 refutations=0 attempts=1 cand_size=4 cand_bits=192 size_bound=6 fit_nrmse=31997 select_nrmse=32532 hidden_state=0 score=PASS score_code=0 mask=0x0 inbox=6186 extrap=6864 onestep=685 ref=0 score_eps=20 law=0 checker_rc=252 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L6 seed=1 desc=v1 episodes=295 steps=5900 fit=3120 select=1040 state=CANDIDATE code=266 stall=261 refutations=1 attempts=1 cand_size=6 cand_bits=288 size_bound=9 fit_nrmse=2412 select_nrmse=2434 hidden_state=0 score=FAIL score_code=307 mask=0x180 inbox=18965 extrap=38772 onestep=0 ref=17289 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L6 seed=2 desc=v1 episodes=300 steps=5980 fit=3220 select=1040 state=INTERVENED code=261 stall=261 refutations=1 attempts=2 cand_size=5 cand_bits=240 size_bound=9 fit_nrmse=2790 select_nrmse=2748 hidden_state=0 score=FAIL score_code=307 mask=0x180 inbox=28010 extrap=27638 onestep=0 ref=26891 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=1 propose_fail=0 trial_random=0 +PD0L level=L6 seed=3 desc=v1 episodes=300 steps=5980 fit=3220 select=1040 state=INTERVENED code=261 stall=261 refutations=1 attempts=2 cand_size=5 cand_bits=240 size_bound=9 fit_nrmse=1710 select_nrmse=1696 hidden_state=0 score=FAIL score_code=307 mask=0x180 inbox=13063 extrap=20396 onestep=0 ref=12912 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=1 propose_fail=0 trial_random=0 +PD0L level=L6 seed=4 desc=v1 episodes=295 steps=5900 fit=3120 select=1040 state=REPLICATED code=0 stall=0 refutations=0 attempts=1 cand_size=5 cand_bits=240 size_bound=9 fit_nrmse=1266 select_nrmse=1269 hidden_state=0 score=FAIL score_code=307 mask=0x180 inbox=12365 extrap=15069 onestep=0 ref=12365 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=L6 seed=5 desc=v1 episodes=275 steps=5500 fit=3420 select=1040 state=CANDIDATE code=251 stall=250 refutations=3 attempts=3 cand_size=4 cand_bits=192 size_bound=9 fit_nrmse=4976 select_nrmse=5001 hidden_state=0 score=FAIL score_code=307 mask=0x181 inbox=44967 extrap=40561 onestep=0 ref=42755 score_eps=20 law=0 checker_rc=0 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=null seed=1 desc=v1 episodes=110 steps=2200 fit=1320 select=440 state=CORRELATION code=270 stall=222 refutations=0 attempts=1 cand_size=2 cand_bits=96 size_bound=6 fit_nrmse=1000700 select_nrmse=1000751 hidden_state=0 score=NOT_SCORED score_code=-1 mask=0x0 inbox=0 extrap=0 onestep=0 ref=0 score_eps=0 law=-1 checker_rc=270 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=null seed=2 desc=v1 episodes=110 steps=2200 fit=1320 select=440 state=CORRELATION code=270 stall=222 refutations=0 attempts=1 cand_size=2 cand_bits=96 size_bound=6 fit_nrmse=1000709 select_nrmse=1001724 hidden_state=0 score=NOT_SCORED score_code=-1 mask=0x0 inbox=0 extrap=0 onestep=0 ref=0 score_eps=0 law=-1 checker_rc=270 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=null seed=3 desc=v1 episodes=110 steps=2200 fit=1320 select=440 state=CORRELATION code=270 stall=222 refutations=0 attempts=1 cand_size=2 cand_bits=96 size_bound=6 fit_nrmse=999896 select_nrmse=1007210 hidden_state=0 score=NOT_SCORED score_code=-1 mask=0x0 inbox=0 extrap=0 onestep=0 ref=0 score_eps=0 law=-1 checker_rc=270 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=null seed=4 desc=v1 episodes=110 steps=2200 fit=1320 select=440 state=CORRELATION code=270 stall=222 refutations=0 attempts=1 cand_size=2 cand_bits=96 size_bound=6 fit_nrmse=1000521 select_nrmse=1000384 hidden_state=0 score=NOT_SCORED score_code=-1 mask=0x0 inbox=0 extrap=0 onestep=0 ref=0 score_eps=0 law=-1 checker_rc=270 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 +PD0L level=null seed=5 desc=v1 episodes=110 steps=2200 fit=1320 select=440 state=CORRELATION code=270 stall=222 refutations=0 attempts=1 cand_size=2 cand_bits=96 size_bound=6 fit_nrmse=1000243 select_nrmse=1000843 hidden_state=0 score=NOT_SCORED score_code=-1 mask=0x0 inbox=0 extrap=0 onestep=0 ref=0 score_eps=0 law=-1 checker_rc=270 oob=0 refused=0 budget_hits=0 propose_fail=0 trial_random=0 diff --git a/mk/physics0-learner.mk b/mk/physics0-learner.mk new file mode 100644 index 000000000..34ac00c52 --- /dev/null +++ b/mk/physics0-learner.mk @@ -0,0 +1,74 @@ +# PD-0 learner (Physics-0 Discovery Engine, Direction 4): src/physics0/learner, +# tests/physics0/learner. The learner links only the verifier's byte formats +# (ladder/pd0_fmt.c, header-only pd0_rng.h) and sha256; never the generators, +# the world, or any file I/O (isolation check below). The harness drives the +# world process and a test-only truth process (links the generators) over pipes. +# +# test-physics0-learner isolation + unit tests (plain + ASan/UBSan) +# physics0-learner-dev development run: levels L0..L6 + null, seeds 1..5 +# physics0-learner-evidence copy new receipts into evidence/physics0/learner/ (cp -n) +ifndef PHYSICS0_LEARNER_MK +PHYSICS0_LEARNER_MK := 1 +.PHONY: test-physics0-learner physics0-learner-dev physics0-learner-evidence p0l-isolation +P0L_DIR = $(OUT_DIR)/physics0-learner +P0L_CFLAGS = -std=c11 -Wall -Wextra -Werror -pedantic -Wno-misleading-indentation -O2 -D_POSIX_C_SOURCE=200809L -ffp-contract=off -fno-fast-math \ + -Isrc -Isrc/physics0/ladder -Isrc/physics0/score -Isrc/physics0/controls -Isrc/physics0/learner +P0L_ASAN = -O1 -g -fsanitize=address,undefined -fno-sanitize-recover=all +P0L_LEARNER_SRCS = src/physics0/learner/pd0_learner.c src/physics0/ladder/pd0_fmt.c +P0L_VERIFIER_SRCS = src/physics0/ladder/pd0_codes.c src/physics0/ladder/pd0_ladder.c src/physics0/score/pd0_score.c src/physics0/controls/pd0_controls.c +P0L_HDRS = src/physics0/learner/pd0_learner.h src/physics0/ladder/pd0_fmt.h src/physics0/ladder/pd0_rng.h src/physics0/ladder/pd0_ladder.h src/physics0/ladder/pd0_codes.h src/physics0/score/pd0_score.h src/physics0/controls/pd0_controls.h +# substrate side (generators): the truth process only +P0L_TRUTH_SRCS = src/physics0/pd0_chan.c src/physics0/pd0_wire.c src/physics0/pd0_rng.c src/physics0/pd0_relation.c src/physics0/pd0_gen.c src/physics0/pd0_world.c src/physics0/pd0_guard.c src/sha256.c +P0L_LEVELS ?= 0 1 2 3 4 5 6 null +P0L_SEEDS ?= 1 2 3 4 5 + +$(P0L_DIR)/obj/%.o: src/physics0/learner/%.c $(P0L_HDRS) + @mkdir -p $(P0L_DIR)/obj + $(CC) $(P0L_CFLAGS) -c -o $@ $< +$(P0L_DIR)/obj/pd0_fmt.o: src/physics0/ladder/pd0_fmt.c $(P0L_HDRS) + @mkdir -p $(P0L_DIR)/obj + $(CC) $(P0L_CFLAGS) -c -o $@ $< +$(P0L_DIR)/obj/pd0_gen.o: src/physics0/pd0_gen.c + @mkdir -p $(P0L_DIR)/obj + $(CC) -std=c11 -Wall -Wextra -Werror -pedantic -O2 -D_POSIX_C_SOURCE=200809L -Isrc -c -o $@ $< +P0L_OBJS = $(P0L_DIR)/obj/pd0_learner.o $(P0L_DIR)/obj/pd0_learner_main.o $(P0L_DIR)/obj/pd0_fmt.o +# G5 isolation (spec I10) on the learner objects, plus: no truth/oracle symbol, no process or socket call +p0l-isolation: $(P0L_OBJS) $(P0L_DIR)/obj/pd0_gen.o + PD0_GEN_OBJ=$(P0L_DIR)/obj/pd0_gen.o sh tests/physics0/isolation.sh $(P0L_OBJS) | tee $(P0L_DIR)/isolation.out | tail -1; grep -q '^PHYSICS0_ISOLATION: PASS' $(P0L_DIR)/isolation.out + @if nm $(P0L_OBJS) | grep -E 'truth_|oracle|rx_|aienos_|argus_|forge_|mmap|fork|exec|socket|getenv|system'; then echo "learner object references forbidden symbol"; exit 1; fi + @echo "p0l-isolation: learner objects carry no generator, world, file, process or authority symbol" + +$(P0L_DIR)/pd0-learner: src/physics0/learner/pd0_learner_main.c $(P0L_LEARNER_SRCS) src/sha256.c $(P0L_HDRS) + @mkdir -p $(P0L_DIR) + $(CC) $(P0L_CFLAGS) -o $@ src/physics0/learner/pd0_learner_main.c $(P0L_LEARNER_SRCS) src/sha256.c -lm +$(P0L_DIR)/test_pd0_learner: tests/physics0/learner/test_pd0_learner.c $(P0L_LEARNER_SRCS) $(P0L_VERIFIER_SRCS) src/sha256.c $(P0L_HDRS) + @mkdir -p $(P0L_DIR) + $(CC) $(P0L_CFLAGS) -o $@ $< $(P0L_LEARNER_SRCS) $(P0L_VERIFIER_SRCS) src/sha256.c -lm +$(P0L_DIR)/test_pd0_learner_asan: tests/physics0/learner/test_pd0_learner.c $(P0L_LEARNER_SRCS) $(P0L_VERIFIER_SRCS) src/sha256.c $(P0L_HDRS) + @mkdir -p $(P0L_DIR) + $(CC) $(P0L_CFLAGS) $(P0L_ASAN) -o $@ $< $(P0L_LEARNER_SRCS) $(P0L_VERIFIER_SRCS) src/sha256.c -lm +$(P0L_DIR)/pd0-harness: tests/physics0/learner/pd0_harness.c $(P0L_LEARNER_SRCS) $(P0L_VERIFIER_SRCS) src/sha256.c $(P0L_HDRS) + @mkdir -p $(P0L_DIR) + $(CC) $(P0L_CFLAGS) -o $@ $< $(P0L_LEARNER_SRCS) $(P0L_VERIFIER_SRCS) src/sha256.c -lm +$(P0L_DIR)/pd0-truth: tests/physics0/learner/pd0_truth_main.c $(P0L_TRUTH_SRCS) $(wildcard src/physics0/*.h) + @mkdir -p $(P0L_DIR) + $(CC) -std=c11 -Wall -Wextra -Werror -pedantic -O2 -D_POSIX_C_SOURCE=200809L -Isrc -o $@ $< $(P0L_TRUTH_SRCS) -lm + +test-physics0-learner: p0l-isolation $(P0L_DIR)/test_pd0_learner $(P0L_DIR)/test_pd0_learner_asan $(P0L_DIR)/pd0-learner + ./$(P0L_DIR)/test_pd0_learner + ./$(P0L_DIR)/test_pd0_learner_asan + @echo "test-physics0-learner: PASS (isolation, unit tests plain + ASan/UBSan)" + +# development run on the world process; results in $(P0L_DIR)/dev/ (one line per instance + ledger/law/json per instance) +physics0-learner-dev: $(P0L_DIR)/pd0-harness $(P0L_DIR)/pd0-truth $(OUT_DIR)/physics0/pd0-world + @mkdir -p $(P0L_DIR)/dev + @: > $(P0L_DIR)/dev/results.txt + for l in $(P0L_LEVELS); do for s in $(P0L_SEEDS); do ./$(P0L_DIR)/pd0-harness $(OUT_DIR)/physics0/pd0-world $(P0L_DIR)/pd0-truth $$l $$s $(P0L_DIR)/dev | tee -a $(P0L_DIR)/dev/results.txt; done; done + @echo "physics0-learner-dev: done, $$(wc -l < $(P0L_DIR)/dev/results.txt) instances in $(P0L_DIR)/dev/results.txt" + +# receipts are immutable: new files only (cp -n) +physics0-learner-evidence: physics0-learner-dev test-physics0-learner + @mkdir -p evidence/physics0/learner + { echo "receipt: PD0_LEARNER_DEV"; echo "commit: $$(git rev-parse HEAD)"; echo "date_utc: $$(date -u +%Y-%m-%dT%H:%M:%SZ)"; echo "levels: $(P0L_LEVELS)"; echo "seeds: $(P0L_SEEDS) (development seeds; HOLDOUT/TRIAL/REP withheld by the harness)"; echo "--- isolation"; cat $(P0L_DIR)/isolation.out; echo "--- results"; cat $(P0L_DIR)/dev/results.txt; } > $(P0L_DIR)/dev/receipt.txt + cp -n $(P0L_DIR)/dev/receipt.txt evidence/physics0/learner/pd0-learner-dev-$$(git rev-parse --short=12 HEAD).txt +endif diff --git a/src/physics0/ladder/pd0_ladder.c b/src/physics0/ladder/pd0_ladder.c index b28d9dc32..43b4a48d1 100644 --- a/src/physics0/ladder/pd0_ladder.c +++ b/src/physics0/ladder/pd0_ladder.c @@ -114,6 +114,21 @@ static int ep_schedule_hash(const ctx *c, const episode *e, uint8_t n_steps, uin pd0_schedule_hash(r0->n_obs, r0->after, n_steps, st, out); return 1; } +/* spec rev 5: a preregistered schedule binds the REQUESTED reset values and the applied (channel, value) + * steps, never the observed reset (which carries noise on noisy levels). The requested reset is the one + * in the PD0EXP1 entry; the episode matches when the hash of that reset with the episode's applied steps + * equals the preregistered hash and, on noise-free levels, the observed reset equals the requested one. + * On noisy levels the observed reset is not compared (limit: the trial error bound covers it). */ +static int ep_matches_prereg(const ctx *c, const episode *ep, const pd0_exp *e) +{ + const pd0_rec *r0 = &c->rec[ep->first]; if (r0->kind != PD0_KIND_RESET || r0->n_obs != e->n_obs) return 0; + pd0_step st[PD0_MAX_STEPS]; uint8_t k = 0; + for (uint32_t i = 1; i < ep->n && k < e->n_steps; i++) { const pd0_rec *r = &c->rec[ep->first + i]; if (r->kind == PD0_KIND_STEP && r->status == PD0_ST_OK) { st[k].channel = r->channel; st[k].value = r->applied; k++; } } + if (k < e->n_steps) return 0; + uint8_t h[PD0_HASH]; pd0_schedule_hash(e->n_obs, e->reset, e->n_steps, st, h); if (memcmp(h, e->schedule_hash, PD0_HASH)) return 0; + if (!c->P->noisy) for (int j = 0; j < e->n_obs; j++) if (r0->after[j] != e->reset[j]) return 0; + return 1; +} static void set_code(ctx *c, int t, int code) { c->R->transition = t; c->R->code = code; if (code == PD0V_OK) c->R->stall_code = 0; else if (!c->R->stall_code) c->R->stall_code = code; } static void refute(ctx *c, const pd0_rec *bad, int64_t predicted, int64_t observed) { @@ -161,6 +176,7 @@ static void try_t1(ctx *c) static int try_t2(ctx *c, const pd0_corr *k) { uint8_t no = c->P->n_obs; if (k->var_b >= no || (k->var_a >= no && k->var_a != PD0_CHAN_NONE)) return PD0V_BAD_FIELD; + if (k->var_a == k->var_b) { set_code(c, 2, PD0V_T2_NO_EVIDENCE); return 0; } /* spec rev 5: a variable against its own one-step change is regression to the mean, not evidence */ if (k->n_shuffles < 2000) { set_code(c, 2, PD0V_T2_TOO_FEW_SHUFFLES); return 0; } double *x = malloc(sizeof(double) * MAX_REC), *y = malloc(sizeof(double) * MAX_REC); uint32_t n = 0; if (!x || !y) { free(x); free(y); return PD0V_TOO_LARGE; } @@ -227,9 +243,8 @@ static int try_t5(ctx *c, const pd0_exp *e, const uint8_t *entry_hash) if (e->n_hyp != 1u + c->fals.n_rivals) { set_code(c, 5, PD0V_T5_MISSING_RIVAL_PREDICTION); return PD0V_T5_MISSING_RIVAL_PREDICTION; } for (int j = 0; j < e->n_obs; j++) if (e->reset[j] < P->reset_min || e->reset[j] > P->reset_max) { set_code(c, 5, PD0V_T5_OUT_OF_BOUNDS); return PD0V_T5_OUT_OF_BOUNDS; } for (int s = 0; s < e->n_steps; s++) { uint8_t ch = e->steps[s].channel; if (ch != PD0_CHAN_NONE && (ch >= P->n_channels || e->steps[s].value < P->chan_min[ch] || e->steps[s].value > P->chan_max[ch])) { set_code(c, 5, PD0V_T5_OUT_OF_BOUNDS); return PD0V_T5_OUT_OF_BOUNDS; } } - uint8_t h[PD0_HASH]; for (uint32_t i = 0; i < c->nep; i++) { const episode *ep = &c->ep[i]; if (!ep->has_tag || ep->tag > TAG_HOLDOUT || ep->n == 0) continue; - if (ep_schedule_hash(c, ep, e->n_steps, h) && !memcmp(h, e->schedule_hash, PD0_HASH)) { set_code(c, 5, PD0V_T5_SCHEDULE_REUSED); return PD0V_T5_SCHEDULE_REUSED; } } + if (ep_matches_prereg(c, ep, e)) { set_code(c, 5, PD0V_T5_SCHEDULE_REUSED); return PD0V_T5_SCHEDULE_REUSED; } } for (uint32_t i = 0; i < c->npre; i++) if (!memcmp(c->pre[i].e.schedule_hash, e->schedule_hash, PD0_HASH)) { set_code(c, 5, PD0V_T5_SCHEDULE_REUSED); return PD0V_T5_SCHEDULE_REUSED; } if (c->npre >= MAX_PREREG) return PD0V_TOO_LARGE; prereg *p = &c->pre[c->npre++]; p->e = *e; memcpy(p->hash, entry_hash, PD0_HASH); p->entry = c->entry_idx; p->matched_ep = -1; @@ -240,12 +255,12 @@ static int try_t5(ctx *c, const pd0_exp *e, const uint8_t *entry_hash) /* ---------- T6 ---------- */ static int try_t6(ctx *c) { - const pd0_ladder_params *P = c->P; uint8_t no = P->n_obs, h[PD0_HASH]; uint32_t n = P->rollout_steps; + const pd0_ladder_params *P = c->P; uint8_t no = P->n_obs; uint32_t n = P->rollout_steps; /* match complete TRIAL episodes to preregistrations */ for (uint32_t i = 0; i < c->nep; i++) { episode *ep = &c->ep[i]; if (!ep->has_tag || ep->tag != TAG_TRIAL || ep->n == 0) continue; - if (ep_ok_steps(c, ep) < n) continue; if (!ep_schedule_hash(c, ep, (uint8_t)n, h)) continue; + if (ep_ok_steps(c, ep) < n) continue; int found = 0; - for (uint32_t k = 0; k < c->npre; k++) if (!memcmp(c->pre[k].e.schedule_hash, h, PD0_HASH) && c->pre[k].entry < ep->first_entry) { c->pre[k].matched_ep = (int)i; found = 1; break; } + for (uint32_t k = 0; k < c->npre; k++) if (c->pre[k].entry < ep->first_entry && ep_matches_prereg(c, ep, &c->pre[k].e)) { c->pre[k].matched_ep = (int)i; found = 1; break; } if (!found) { set_code(c, 6, PD0V_T6_TRIAL_NOT_PREREGISTERED); return PD0V_T6_TRIAL_NOT_PREREGISTERED; } } for (uint32_t k = 0; k < c->npre; k++) if (c->pre[k].matched_ep < 0) { set_code(c, 6, PD0V_T6_TRIAL_MISSING); return 0; } /* evaluate every trial against the committed prediction (hypothesis index 0) */ @@ -331,7 +346,7 @@ int pd0_ladder_check(const uint8_t *ledger, size_t len, const pd0_ladder_params if (ep->first == UINT32_MAX) { ep->first = c->nrec; ep->first_entry = c->entry_idx; if (r->kind != PD0_KIND_RESET) { rc = PD0V_BAD_FIELD; break; } } else if (ep->first + ep->n != c->nrec) { rc = PD0V_BAD_FIELD; break; } /* episodes are contiguous */ ep->n++; c->nrec++; R->n_records++; - if (ep->has_tag && ep->tag == TAG_TRIAL && ep_ok_steps(c, ep) == P->rollout_steps) { uint8_t sh[PD0_HASH]; int found = 0; if (ep_schedule_hash(c, ep, (uint8_t)P->rollout_steps, sh)) for (uint32_t k = 0; k < c->npre; k++) if (!memcmp(c->pre[k].e.schedule_hash, sh, PD0_HASH) && c->pre[k].entry < ep->first_entry) found = 1; + if (ep->has_tag && ep->tag == TAG_TRIAL && ep_ok_steps(c, ep) == P->rollout_steps) { int found = 0; for (uint32_t k = 0; k < c->npre; k++) if (c->pre[k].entry < ep->first_entry && ep_matches_prereg(c, ep, &c->pre[k].e)) found = 1; if (!found) { rc = PD0V_T6_TRIAL_NOT_PREREGISTERED; set_code(c, 6, rc); break; } } if (ep->has_tag && (ep->tag == TAG_TRIAL || ep->tag == TAG_REP) && ep->tag_entry > ep->first_entry) { rc = PD0V_T6_TRIAL_NOT_PREREGISTERED; break; } if (R->state == LS_START) try_t1(c); diff --git a/src/physics0/learner/.crumb b/src/physics0/learner/.crumb new file mode 100644 index 000000000..7084ad757 --- /dev/null +++ b/src/physics0/learner/.crumb @@ -0,0 +1,6 @@ +{ + "schema_version": "1.0.0", + "name": "src/physics0/learner", + "layer": "src", + "purpose": "PD-0 learner (Physics-0 Discovery Engine D4): fits compact one-step relations over a bounded monomial vocabulary from FIT/SELECT records only; emits candidates, correlation evidence and preregistered experiments for the independent ladder checker. Links only ladder/pd0_fmt.c and sha256; never the generators, the world, file I/O or a level identity (p0l-isolation)." +} diff --git a/src/physics0/learner/pd0_learner.c b/src/physics0/learner/pd0_learner.c new file mode 100644 index 000000000..43e4a4a2a --- /dev/null +++ b/src/physics0/learner/pd0_learner.c @@ -0,0 +1,284 @@ +/* PD-0 learner implementation. See pd0_learner.h. No generator knowledge, + * no file I/O, no level identity. */ +#include "pd0_learner.h" +#include "pd0_rng.h" +#include +#include +#include +#include + +typedef struct { int64_t before[PD0_MAX_OBS], after[PD0_MAX_OBS]; uint8_t chan; int64_t value; uint8_t tag; uint32_t episode; uint8_t is_reset; } trans; + +struct pd0_learner { + pd0l_desc d; + uint64_t seed; + trans *t; uint32_t nt; + unsigned ni, nl; /* inputs (obs + channels), library size */ + uint8_t lib[PD0L_MAX_LIB][PD0_MAX_VARS + PD0_MAX_CHAN]; + int ncand; pd0l_candidate cand[PD0L_MAX_CAND]; +}; + +/* ---- vocabulary: every monomial of total degree <= 3 over the inputs (constant included) ---- */ +static unsigned build_library(unsigned ni, uint8_t lib[PD0L_MAX_LIB][PD0_MAX_VARS + PD0_MAX_CHAN]) +{ + unsigned n = 0; uint8_t e[PD0_MAX_VARS + PD0_MAX_CHAN]; + for (unsigned a = 0; a <= ni; a++) for (unsigned b = a; b <= ni; b++) for (unsigned c = b; c <= ni; c++) { + memset(e, 0, sizeof e); if (a < ni) e[a]++; if (b < ni) e[b]++; if (c < ni) e[c]++; + int dup = 0; for (unsigned k = 0; k < n; k++) if (!memcmp(lib[k], e, ni)) { dup = 1; break; } + if (!dup && n < PD0L_MAX_LIB) memcpy(lib[n++], e, sizeof e); } + return n; +} + +pd0_learner *pd0_learner_new(const pd0l_desc *d, uint64_t seed) +{ + if (!d || d->n_obs == 0 || d->n_obs > PD0_MAX_OBS || d->n_channels > PD0_MAX_CHAN) return NULL; + pd0_learner *L = calloc(1, sizeof *L); if (!L) return NULL; + L->t = malloc(sizeof(trans) * PD0L_MAX_TRANS); if (!L->t) { free(L); return NULL; } + L->d = *d; L->seed = seed; L->ni = d->n_obs + d->n_channels; L->nl = build_library(L->ni, L->lib); + return L; +} +void pd0_learner_free(pd0_learner *L) { if (L) { free(L->t); free(L); } } + +int pd0_learner_observe(pd0_learner *L, const pd0_rec *r, uint8_t tag) +{ + if (tag != TAG_FIT && tag != TAG_SELECT) return -1; /* I10: nothing else may reach the learner */ + if (r->n_obs != L->d.n_obs || L->nt >= PD0L_MAX_TRANS) return -1; + if (r->kind == PD0_KIND_STEP && r->status != PD0_ST_OK) return 0; /* refused / ended steps carry no transition */ + trans *t = &L->t[L->nt++]; memset(t, 0, sizeof *t); + memcpy(t->before, r->before, sizeof(int64_t) * r->n_obs); memcpy(t->after, r->after, sizeof(int64_t) * r->n_obs); + t->chan = r->channel; t->value = r->applied; t->tag = tag; t->episode = r->episode; t->is_reset = r->kind == PD0_KIND_RESET; + return 0; +} +uint32_t pd0_learner_n_transitions(const pd0_learner *L, uint8_t tag) { uint32_t n = 0; for (uint32_t i = 0; i < L->nt; i++) if (L->t[i].tag == tag && !L->t[i].is_reset) n++; return n; } + +/* ---- schedules ---- */ +static int64_t draw_in(pd0_rng *g, int64_t lo, int64_t hi) { int64_t u = pd0_rng_unit(g); return lo + pd0_mul(hi - lo, u); } +void pd0_learner_random_schedule(const pd0l_desc *d, uint64_t seed, const char *tag, uint32_t idx, int64_t *reset, pd0_step *steps, uint32_t n) +{ + pd0_rng g; pd0_rng_stream(&g, seed ^ ((uint64_t)idx * 0x9E3779B97F4A7C15ull), tag); + for (int j = 0; j < d->n_obs; j++) reset[j] = draw_in(&g, d->reset_min[j], d->reset_max[j]); + for (uint32_t s = 0; s < n; s++) { + uint8_t ch = d->n_channels ? (uint8_t)(pd0_rng_next(&g) % d->n_channels) : PD0_CHAN_NONE; + steps[s].channel = ch; steps[s].value = ch == PD0_CHAN_NONE ? 0 : draw_in(&g, d->chan_min[ch], d->chan_max[ch]); } +} +void pd0_learner_explore(pd0_learner *L, uint32_t episode_index, int64_t *reset, pd0_step *steps, uint32_t n_steps) +{ pd0_learner_random_schedule(&L->d, L->seed, "explore", episode_index, reset, steps, n_steps); } + +/* ---- prediction over a relation (integer evaluator shared with the checker: pd0_rel_step) ---- */ +void pd0_learner_rollout(const pd0_rel *rel, const int64_t *init, const pd0_step *steps, uint32_t n, int64_t *out) +{ + int64_t st[PD0_MAX_VARS], nx[PD0_MAX_VARS]; uint8_t no = (uint8_t)(rel->n_vars - rel->n_latent); + memset(st, 0, sizeof st); for (int j = 0; j < no; j++) st[j] = init[j]; + for (uint32_t s = 0; s < n; s++) { pd0_rel_step(rel, st, steps[s].channel, steps[s].value, nx); for (int j = 0; j < no; j++) out[s * no + j] = nx[j]; memcpy(st, nx, sizeof st); } +} + +typedef struct { double se[PD0_MAX_OBS], s[PD0_MAX_OBS], ss[PD0_MAX_OBS]; uint32_t n; } acc; +static void acc_add(acc *a, const int64_t *p, const int64_t *t, uint8_t no) { for (int j = 0; j < no; j++) { double e = (double)(p[j] - t[j]) / 1e6, v = (double)t[j] / 1e6; a->se[j] += e * e; a->s[j] += v; a->ss[j] += v * v; } a->n++; } +static int64_t acc_nrmse(const acc *a, uint8_t no) +{ + double w = 0; if (!a->n) return 0; + for (int j = 0; j < no; j++) { double rmse = sqrt(a->se[j] / a->n), m = a->s[j] / a->n, var = a->ss[j] / a->n - m * m, sd = var > 0 ? sqrt(var) : 0, v = sd > 1e-9 ? rmse / sd : rmse; if (v > w) w = v; } + return w > 9e12 ? INT64_MAX : (int64_t)(w * 1e6); +} +/* one-step NRMSE on the records of one tag; latent carried along each episode (as the checker's T3 does) */ +static int64_t onestep_eval(const pd0_learner *L, const pd0_rel *rel, uint8_t tag, double *lag1) +{ + uint8_t no = L->d.n_obs; acc a; memset(&a, 0, sizeof a); int64_t st[PD0_MAX_VARS], nx[PD0_MAX_VARS]; memset(st, 0, sizeof st); + double num[PD0_MAX_OBS] = { 0 }, den[PD0_MAX_OBS] = { 0 }, prev[PD0_MAX_OBS] = { 0 }; int have_prev = 0; + for (uint32_t i = 0; i < L->nt; i++) { const trans *t = &L->t[i]; if (t->tag != tag) continue; + if (t->is_reset) { memset(st, 0, sizeof st); have_prev = 0; continue; } + for (int v = 0; v < no; v++) st[v] = t->before[v]; + pd0_rel_step(rel, st, t->chan, t->value, nx); acc_add(&a, nx, t->after, no); + for (int v = 0; v < no; v++) { double res = (double)(t->after[v] - nx[v]) / 1e6; if (have_prev) num[v] += res * prev[v]; den[v] += res * res; prev[v] = res; } + have_prev = 1; memcpy(st, nx, sizeof st); } + if (lag1) for (int v = 0; v < no; v++) lag1[v] = den[v] > 0 ? num[v] / den[v] : 0; + return acc_nrmse(&a, no); +} +int64_t pd0_learner_onestep_nrmse(const pd0_learner *L, const pd0_rel *rel, uint8_t tag) { return onestep_eval(L, rel, tag, NULL); } + +/* ---- least squares machinery ---- */ +static double mono(const double *x, const uint8_t *e, unsigned ni) { double v = 1; for (unsigned i = 0; i < ni; i++) for (uint8_t k = 0; k < e[i]; k++) v *= x[i]; return v; } +static int solve(double *A, double *b, unsigned n) +{ + for (unsigned i = 0; i < n; i++) { unsigned p = i; for (unsigned r = i + 1; r < n; r++) if (fabs(A[r * n + i]) > fabs(A[p * n + i])) p = r; + if (fabs(A[p * n + i]) < 1e-14) return -1; + if (p != i) { for (unsigned k = 0; k < n; k++) { double t = A[i * n + k]; A[i * n + k] = A[p * n + k]; A[p * n + k] = t; } double t = b[i]; b[i] = b[p]; b[p] = t; } + for (unsigned r = i + 1; r < n; r++) { double f = A[r * n + i] / A[i * n + i]; for (unsigned k = i; k < n; k++) A[r * n + k] -= f * A[i * n + k]; b[r] -= f * b[i]; } } + for (int i = (int)n - 1; i >= 0; i--) { double s = b[i]; for (unsigned k = (unsigned)i + 1; k < n; k++) s -= A[i * n + k] * b[k]; b[i] = s / A[i * n + i]; } + return 0; +} +typedef struct { const double *X; const double *y; uint32_t n; unsigned nl; } design; +/* fit the subset sel[0..m) by normal equations; returns RSS (INFINITY if singular) and coef */ +static double fit_subset(const design *D, const unsigned *sel, unsigned m, double *coef) +{ + double A[PD0L_MAX_SEL * PD0L_MAX_SEL] = { 0 }, b[PD0L_MAX_SEL] = { 0 }; + for (uint32_t i = 0; i < D->n; i++) for (unsigned p = 0; p < m; p++) { double xp = D->X[i * D->nl + sel[p]]; b[p] += xp * D->y[i]; for (unsigned q = 0; q < m; q++) A[p * m + q] += xp * D->X[i * D->nl + sel[q]]; } + for (unsigned p = 0; p < m; p++) A[p * m + p] += 1e-12; + if (m && solve(A, b, m)) return INFINITY; + double rss = 0; for (uint32_t i = 0; i < D->n; i++) { double pr = 0; for (unsigned p = 0; p < m; p++) pr += b[p] * D->X[i * D->nl + sel[p]]; rss += (pr - D->y[i]) * (pr - D->y[i]); } + memcpy(coef, b, sizeof(double) * m); return rss; +} +/* description-length score in bits: residual code length (n * log2 of the rmse in micro units, + * floor 1 micro) plus the spec 6.2 description bits of the terms */ +static double term_bits(unsigned ni) { return 8.0 * ni + 24.0; } +static double resid_bits(double rss, uint32_t n) { double rmse_micro = n ? sqrt(rss / n) * 1e6 : 0; if (rmse_micro < 1) rmse_micro = 1; return n * log2(rmse_micro); } + +typedef struct { unsigned sel[PD0L_MAX_SEL]; unsigned m; double coef[PD0L_MAX_SEL]; double rss, mdl; } eqfit; + +/* forward selection with backward pruning for one target; fills best (MDL-optimal) and alt (the + * runner-up subset: one term fewer if it exists, else one term more). deterministic order. */ +static void select_terms(const design *D, unsigned ni, unsigned max_terms, eqfit *best, eqfit *alt) +{ + eqfit path[PD0L_MAX_SEL + 1]; memset(path, 0, sizeof path); + path[0].m = 0; path[0].rss = fit_subset(D, path[0].sel, 0, path[0].coef); path[0].mdl = resid_bits(path[0].rss, D->n); + unsigned steps = 0; + for (unsigned round = 1; round <= max_terms; round++) { + const eqfit *prev = &path[round - 1]; double best_rss = INFINITY; unsigned bk = D->nl; double bc[PD0L_MAX_SEL]; + for (unsigned k = 0; k < D->nl; k++) { int used = 0; for (unsigned s = 0; s < prev->m; s++) if (prev->sel[s] == k) used = 1; if (used) continue; + unsigned cand[PD0L_MAX_SEL]; memcpy(cand, prev->sel, sizeof(unsigned) * prev->m); cand[prev->m] = k; double c[PD0L_MAX_SEL]; + double rss = fit_subset(D, cand, prev->m + 1, c); + if (rss < best_rss * (1 - 1e-12)) { best_rss = rss; bk = k; memcpy(bc, c, sizeof c); } } + if (bk == D->nl) break; + eqfit *e = &path[round]; *e = *prev; e->sel[e->m++] = bk; memcpy(e->coef, bc, sizeof bc); e->rss = best_rss; e->mdl = resid_bits(best_rss, D->n) + term_bits(ni) * e->m; steps = round; + } + unsigned bi = 0; for (unsigned r = 1; r <= steps; r++) if (path[r].mdl < path[bi].mdl - 1e-9) bi = r; + eqfit b = path[bi]; + /* backward pruning: drop any term whose removal does not worsen MDL */ + int changed = 1; + while (changed && b.m > 0) { changed = 0; + for (unsigned drop = 0; drop < b.m; drop++) { eqfit c = b; for (unsigned s = drop; s + 1 < c.m; s++) c.sel[s] = c.sel[s + 1]; c.m--; + c.rss = fit_subset(D, c.sel, c.m, c.coef); c.mdl = resid_bits(c.rss, D->n) + term_bits(ni) * c.m; + if (c.mdl <= b.mdl + 1e-9) { b = c; changed = 1; break; } } } + *best = b; + /* runner-up: one fewer (weakest by refit) if possible, else one more from the forward path */ + if (b.m > 0) { eqfit bestdrop; bestdrop.mdl = INFINITY; bestdrop.m = 0; + for (unsigned drop = 0; drop < b.m; drop++) { eqfit c = b; for (unsigned s = drop; s + 1 < c.m; s++) c.sel[s] = c.sel[s + 1]; c.m--; c.rss = fit_subset(D, c.sel, c.m, c.coef); c.mdl = resid_bits(c.rss, D->n) + term_bits(ni) * c.m; if (c.mdl < bestdrop.mdl) bestdrop = c; } + *alt = bestdrop; } + else if (steps >= 1) *alt = path[1]; + else *alt = b; +} + +static void quantise(const pd0_learner *L, const eqfit *f, uint8_t target, pd0_eq *q) +{ + memset(q, 0, sizeof *q); q->target = target; unsigned m = 0; + for (unsigned s = 0; s < f->m; s++) { int64_t c = (int64_t)llround(f->coef[s] * 1e6); if (c == 0) continue; q->coef[m] = c; memcpy(q->expo[m], L->lib[f->sel[s]], L->ni); m++; } + q->n_terms = (uint16_t)m; +} +static void finish_candidate(const pd0_learner *L, pd0l_candidate *c) +{ + c->rel.description_bits = pd0_rel_bits(&c->rel); c->bits = c->rel.description_bits; c->size = pd0_rel_size(&c->rel); + c->fit_nrmse_micro = onestep_eval(L, &c->rel, TAG_FIT, c->lag1_autocorr); + c->select_nrmse_micro = onestep_eval(L, &c->rel, TAG_SELECT, NULL); + /* MDL on SELECT with the integer evaluator: residual bits per variable + description bits */ + uint8_t no = L->d.n_obs; double rb = 0; uint32_t n = 0; double se[PD0_MAX_OBS] = { 0 }; int64_t st[PD0_MAX_VARS], nx[PD0_MAX_VARS]; memset(st, 0, sizeof st); + for (uint32_t i = 0; i < L->nt; i++) { const trans *t = &L->t[i]; if (t->tag != TAG_SELECT) continue; if (t->is_reset) { memset(st, 0, sizeof st); continue; } + for (int v = 0; v < no; v++) st[v] = t->before[v]; + pd0_rel_step(&c->rel, st, t->chan, t->value, nx); + for (int v = 0; v < no; v++) { double e = (double)(t->after[v] - nx[v]) / 1e6; se[v] += e * e; } n++; memcpy(st, nx, sizeof st); } + for (int v = 0; v < no; v++) rb += resid_bits(se[v], n); + c->mdl_bits = rb + c->bits; + c->hidden_state_suspected = 0; for (int v = 0; v < no; v++) if (fabs(c->lag1_autocorr[v]) > 0.3 && c->fit_nrmse_micro > 5000) c->hidden_state_suspected = 1; +} +static int cand_cmp(const void *a, const void *b) { const pd0l_candidate *x = a, *y = b; if (x->mdl_bits < y->mdl_bits) return -1; if (x->mdl_bits > y->mdl_bits) return 1; return (int)x->size - (int)y->size; } +static int same_structure(const pd0_rel *a, const pd0_rel *b) { return pd0_rel_size(a) == pd0_rel_size(b) && pd0_rel_bits(a) == pd0_rel_bits(b) && memcmp(a->eq, b->eq, sizeof a->eq) == 0; } + +int pd0_learner_fit(pd0_learner *L) +{ + uint8_t no = L->d.n_obs; unsigned ni = L->ni, nl = L->nl; L->ncand = 0; + uint32_t n = 0; for (uint32_t i = 0; i < L->nt; i++) if (L->t[i].tag == TAG_FIT && !L->t[i].is_reset) n++; + pd0l_candidate *nullc = &L->cand[L->ncand++]; memset(nullc, 0, sizeof *nullc); nullc->is_null = 1; + nullc->rel.n_vars = no; nullc->rel.n_channels = L->d.n_channels; nullc->rel.n_equations = 0; finish_candidate(L, nullc); + if (n < 8) return L->ncand; + double *X = malloc(sizeof(double) * n * nl), *Y = malloc(sizeof(double) * n * no); if (!X || !Y) { free(X); free(Y); return L->ncand; } + uint32_t r = 0; + for (uint32_t i = 0; i < L->nt; i++) { const trans *t = &L->t[i]; if (t->tag != TAG_FIT || t->is_reset) continue; + double x[PD0_MAX_VARS + PD0_MAX_CHAN]; for (int j = 0; j < no; j++) x[j] = (double)t->before[j] / 1e6; for (int c = 0; c < L->d.n_channels; c++) x[no + c] = t->chan == c ? (double)t->value / 1e6 : 0; + for (unsigned k = 0; k < nl; k++) X[r * nl + k] = mono(x, L->lib[k], ni); + for (int j = 0; j < no; j++) Y[r * no + j] = (double)(t->after[j] - t->before[j]) / 1e6; + r++; } + double *y = malloc(sizeof(double) * n); eqfit best[PD0_MAX_OBS], alt[PD0_MAX_OBS]; + for (int tg = 0; tg < no; tg++) { for (uint32_t i = 0; i < n; i++) y[i] = Y[i * no + tg]; design D = { X, y, n, nl }; select_terms(&D, ni, PD0L_MAX_SEL, &best[tg], &alt[tg]); } + /* candidate 1: MDL-best per target; alternatives: swap one target's equation for its runner-up */ + pd0l_candidate tmp[PD0L_MAX_CAND]; int nt = 0; + for (int which = -1; which < (int)no && nt < PD0L_MAX_CAND - 1; which++) { + pd0l_candidate *c = &tmp[nt]; memset(c, 0, sizeof *c); c->rel.n_vars = no; c->rel.n_channels = L->d.n_channels; unsigned ne = 0; + for (int tg = 0; tg < no; tg++) { const eqfit *f = (tg == which) ? &alt[tg] : &best[tg]; pd0_eq q; quantise(L, f, (uint8_t)tg, &q); if (q.n_terms) c->rel.eq[ne++] = q; } + c->rel.n_equations = (uint8_t)ne; finish_candidate(L, c); + int dup = 0; for (int k = 0; k < nt; k++) if (same_structure(&tmp[k].rel, &c->rel)) dup = 1; if (!dup && (ne || which < 0)) nt++; } + for (int k = 0; k < nt && L->ncand < PD0L_MAX_CAND; k++) if (!same_structure(&tmp[k].rel, &nullc->rel)) L->cand[L->ncand++] = tmp[k]; + qsort(L->cand, (size_t)L->ncand, sizeof L->cand[0], cand_cmp); + free(X); free(Y); free(y); + return L->ncand; +} +int pd0_learner_n_candidates(const pd0_learner *L) { return L->ncand; } +const pd0l_candidate *pd0_learner_candidate(const pd0_learner *L, int i) { return (i >= 0 && i < L->ncand) ? &L->cand[i] : NULL; } + +/* ---- correlation evidence (spec T2) ---- */ +static double pearson(const double *x, const double *y, uint32_t n) +{ + double mx = 0, my = 0; for (uint32_t i = 0; i < n; i++) { mx += x[i]; my += y[i]; } mx /= n; my /= n; + double sxy = 0, sxx = 0, syy = 0; for (uint32_t i = 0; i < n; i++) { sxy += (x[i] - mx) * (y[i] - my); sxx += (x[i] - mx) * (x[i] - mx); syy += (y[i] - my) * (y[i] - my); } + return (sxx > 0 && syy > 0) ? sxy / sqrt(sxx * syy) : 0; +} +int pd0_learner_correlation(const pd0_learner *L, uint64_t shuffle_seed, uint32_t n_shuffles, pd0_corr *out) +{ + uint8_t no = L->d.n_obs; uint32_t n = 0; for (uint32_t i = 0; i < L->nt; i++) if (!L->t[i].is_reset) n++; + if (n < 100) return -1; + double *x = malloc(sizeof(double) * n), *y = malloc(sizeof(double) * n), *yp = malloc(sizeof(double) * n); if (!x || !y || !yp) { free(x); free(y); free(yp); return -1; } + double best_r = 0; int ba = -1, bb = -1; uint32_t n_pairs = (uint32_t)no * no; + for (int a = -1; a < (int)no; a++) for (int b = 0; b < no; b++) { uint32_t k = 0; if (a == b) continue; /* spec rev 5: no self pairs */ + for (uint32_t i = 0; i < L->nt; i++) { const trans *t = &L->t[i]; if (t->is_reset) continue; x[k] = a < 0 ? (double)t->value : (double)t->before[a]; y[k] = (double)(t->after[b] - t->before[b]); k++; } + double r = pearson(x, y, n); if (fabs(r) > fabs(best_r)) { best_r = r; ba = a; bb = b; } } + if (bb < 0) { free(x); free(y); free(yp); return -1; } + uint32_t k = 0; for (uint32_t i = 0; i < L->nt; i++) { const trans *t = &L->t[i]; if (t->is_reset) continue; x[k] = ba < 0 ? (double)t->value : (double)t->before[ba]; y[k] = (double)(t->after[bb] - t->before[bb]); k++; } + pd0_rng g; pd0_rng_stream(&g, shuffle_seed, "perm"); uint32_t hits = 0; double r0 = fabs(best_r); + for (uint32_t s = 0; s < n_shuffles; s++) { memcpy(yp, y, sizeof(double) * n); for (uint32_t i = n - 1; i > 0; i--) { uint32_t j = (uint32_t)(pd0_rng_next(&g) % (i + 1)); double t = yp[i]; yp[i] = yp[j]; yp[j] = t; } if (fabs(pearson(x, yp, n)) >= r0) hits++; } + double p = (hits + 1.0) / (n_shuffles + 1.0) * n_pairs; if (p > 1) p = 1; + memset(out, 0, sizeof *out); out->var_a = ba < 0 ? PD0_CHAN_NONE : (uint8_t)ba; out->var_b = (uint8_t)bb; out->n = n; out->r_micro = (int64_t)(best_r * 1e6); out->p_micro = (int64_t)(p * 1e6); out->n_pairs = n_pairs; out->n_shuffles = n_shuffles; out->shuffle_seed = shuffle_seed; + free(x); free(y); free(yp); return 0; +} + +/* ---- planner stand-in (spec section 8 selection rule, without local refinement) ---- */ +int64_t pd0_learner_propose(const pd0_learner *L, const pd0_rel *hyps, int n_hyp, uint64_t planner_seed, uint32_t n_random, + const uint8_t (*forbidden)[PD0_HASH], uint32_t n_forbidden, pd0_exp *out) +{ + uint8_t no = L->d.n_obs; uint32_t n = PD0_MAX_STEPS; if (n_hyp < 2 || n_hyp > PD0_MAX_HYP) return -1; + /* pooled residual sd of the hypotheses on SELECT (in units); floor 1e-4 */ + double r = 0; int cnt = 0; for (int h = 0; h < n_hyp; h++) { int64_t e = pd0_learner_onestep_nrmse(L, &hyps[h], TAG_SELECT); if (e > 0 && e < INT64_MAX) { r += (double)e / 1e6; cnt++; } } + r = cnt ? r / cnt : 0; if (r < 1e-4) r = 1e-4; + double bestD = -1; int64_t best_cost = INT64_MAX; pd0_exp best; memset(&best, 0, sizeof best); uint8_t best_hash[PD0_HASH] = { 0 }; + int64_t pred[PD0_MAX_HYP][PD0_MAX_STEPS * PD0_MAX_OBS]; + for (uint32_t idx = 0; idx < n_random; idx++) { + pd0_exp e; memset(&e, 0, sizeof e); e.n_obs = no; e.n_hyp = (uint8_t)n_hyp; e.n_steps = (uint8_t)n; + pd0_learner_random_schedule(&L->d, planner_seed, "plan", idx, e.reset, e.steps, n); + uint8_t h[PD0_HASH]; pd0_schedule_hash(no, e.reset, (uint8_t)n, e.steps, h); int bad = 0; + for (uint32_t f = 0; f < n_forbidden; f++) if (!memcmp(h, forbidden[f], PD0_HASH)) { bad = 1; break; } + if (bad) continue; + for (int hy = 0; hy < n_hyp; hy++) pd0_learner_rollout(&hyps[hy], e.reset, e.steps, n, pred[hy]); + /* safety: the candidate's own prediction must stay well inside the world box */ + for (uint32_t s = 0; s < n && !bad; s++) for (int j = 0; j < no; j++) if (llabs(pred[0][s * no + j]) > 8000000) { bad = 1; break; } + if (bad) continue; + double D = 0; for (int a = 0; a < n_hyp; a++) for (int b = a + 1; b < n_hyp; b++) for (uint32_t s = 0; s < n; s++) for (int j = 0; j < no; j++) { double d = fabs((double)(pred[a][s * no + j] - pred[b][s * no + j])) / 1e6 / r; if (d > D) D = d; } + int64_t cost = 0; for (uint32_t s = 0; s < n; s++) cost += llabs(e.steps[s].value); + int better = D > bestD + 1e-12 || (fabs(D - bestD) <= 1e-12 && (cost < best_cost || (cost == best_cost && memcmp(h, best_hash, PD0_HASH) < 0))); + if (better) { bestD = D; best_cost = cost; best = e; memcpy(best_hash, h, PD0_HASH); for (int hy = 0; hy < n_hyp; hy++) for (uint32_t s = 0; s < n; s++) for (int j = 0; j < no; j++) best.expected[hy][s][j] = pred[hy][s * no + j]; } + } + if (bestD < 3.0) return -1; /* NO_DISCRIMINATING_EXPERIMENT */ + best.divergence_micro = (int64_t)(bestD * 1e6); memcpy(best.schedule_hash, best_hash, PD0_HASH); *out = best; return best.divergence_micro; +} + +size_t pd0_learner_report(const pd0_learner *L, char *buf, size_t cap) +{ + size_t n = 0; n += (size_t)snprintf(buf + n, cap > n ? cap - n : 0, "{\"n_fit\":%u,\"n_select\":%u,\"candidates\":[", pd0_learner_n_transitions(L, TAG_FIT), pd0_learner_n_transitions(L, TAG_SELECT)); + for (int i = 0; i < L->ncand; i++) { const pd0l_candidate *c = &L->cand[i]; + n += (size_t)snprintf(buf + n, cap > n ? cap - n : 0, "%s{\"rank\":%d,\"null\":%d,\"size\":%u,\"bits\":%u,\"fit_nrmse_micro\":%lld,\"select_nrmse_micro\":%lld,\"mdl_bits\":%.1f,\"hidden_state_suspected\":%d,\"eq\":[", + i ? "," : "", i, c->is_null, c->size, c->bits, (long long)c->fit_nrmse_micro, (long long)c->select_nrmse_micro, c->mdl_bits, c->hidden_state_suspected); + for (int e = 0; e < c->rel.n_equations; e++) { const pd0_eq *q = &c->rel.eq[e]; n += (size_t)snprintf(buf + n, cap > n ? cap - n : 0, "%s{\"target\":%u,\"terms\":[", e ? "," : "", q->target); + for (int t = 0; t < q->n_terms; t++) { n += (size_t)snprintf(buf + n, cap > n ? cap - n : 0, "%s{\"coef\":%lld,\"expo\":[", t ? "," : "", (long long)q->coef[t]); + for (unsigned k = 0; k < (unsigned)(c->rel.n_vars + c->rel.n_channels); k++) n += (size_t)snprintf(buf + n, cap > n ? cap - n : 0, "%s%u", k ? "," : "", q->expo[t][k]); + n += (size_t)snprintf(buf + n, cap > n ? cap - n : 0, "]}"); } + n += (size_t)snprintf(buf + n, cap > n ? cap - n : 0, "]}"); } + n += (size_t)snprintf(buf + n, cap > n ? cap - n : 0, "]}"); } + n += (size_t)snprintf(buf + n, cap > n ? cap - n : 0, "]}"); + return n < cap ? n : cap - 1; +} diff --git a/src/physics0/learner/pd0_learner.h b/src/physics0/learner/pd0_learner.h new file mode 100644 index 000000000..835fc6e70 --- /dev/null +++ b/src/physics0/learner/pd0_learner.h @@ -0,0 +1,70 @@ +/* PD-0 learner (Physics-0 Discovery Engine, Direction 4): the first genuine + * learner. It sees only the describe record and PD0REC1 observation records + * the harness forwards (FIT and SELECT episodes), fits compact one-step + * relations over a bounded monomial vocabulary, keeps several candidates, + * and emits evidence for the independent ladder checker. It never declares + * a ladder state, never reads HOLDOUT/TRIAL/REP raw records, holds no file, + * no generator symbol, no level identity. Formats come from pd0_fmt.h + * (byte layouts only). Floating point is used only inside fitting; every + * serialised number is a micro-unit integer. */ +#ifndef PD0_LEARNER_H +#define PD0_LEARNER_H +#include "pd0_fmt.h" + +#define PD0L_MAX_TRANS 65536u +#define PD0L_MAX_LIB 128u +#define PD0L_MAX_CAND 8 +#define PD0L_MAX_SEL 10u /* terms per equation the search may select */ +#define PD0L_MAX_FORBID 4096u + +typedef struct { + uint8_t n_obs, n_channels; + int64_t dt_micro; + int64_t chan_min[PD0_MAX_CHAN], chan_max[PD0_MAX_CHAN]; + int64_t reset_min[PD0_MAX_OBS], reset_max[PD0_MAX_OBS]; /* per variable (PD0DESC2); a one-pair describe is broadcast by the harness */ + uint32_t episode_max_steps, budget_steps, budget_episodes; +} pd0l_desc; + +typedef struct { + pd0_rel rel; /* serialisable relation (coefficients in micro) */ + uint32_t bits; /* description_bits (spec 6.2 rule) */ + uint32_t size; /* |R| */ + int64_t fit_nrmse_micro; /* one-step NRMSE on FIT (integer evaluator) */ + int64_t select_nrmse_micro; /* one-step NRMSE on SELECT (integer evaluator) */ + double mdl_bits; /* description bits + residual code length on SELECT */ + double lag1_autocorr[PD0_MAX_OBS]; /* residual lag-1 autocorrelation on FIT per variable */ + int hidden_state_suspected; /* residuals carry memory the vocabulary cannot express */ + int is_null; +} pd0l_candidate; + +typedef struct pd0_learner pd0_learner; + +pd0_learner *pd0_learner_new(const pd0l_desc *d, uint64_t seed); +void pd0_learner_free(pd0_learner *L); +/* a FIT or SELECT record (tag TAG_FIT / TAG_SELECT); others are refused (-1) */ +int pd0_learner_observe(pd0_learner *L, const pd0_rec *r, uint8_t tag); +/* the records of an episode that was TRIAL/REP and is now FIT after a refutation */ +uint32_t pd0_learner_n_transitions(const pd0_learner *L, uint8_t tag); +/* exploration: seeded random reset + schedule inside the describe bounds */ +void pd0_learner_explore(pd0_learner *L, uint32_t episode_index, int64_t *reset, pd0_step *steps, uint32_t n_steps); +/* fit: rebuilds the candidate set from FIT, ranked by MDL on SELECT. returns count (>= 1: the null is always present) */ +int pd0_learner_fit(pd0_learner *L); +int pd0_learner_n_candidates(const pd0_learner *L); +const pd0l_candidate *pd0_learner_candidate(const pd0_learner *L, int i); /* 0 = best */ +/* the strongest (variable or intervention, one-step change) correlation over all visible OK step + * records, with a permutation test (recorded seed). returns 0 or -1 (too few records). */ +int pd0_learner_correlation(const pd0_learner *L, uint64_t shuffle_seed, uint32_t n_shuffles, pd0_corr *out); +/* planner stand-in (Direction 5 owns the real one): among n_random seeded schedules inside the + * bounds, not in the forbidden hash list, pick the one where the hypotheses disagree most. + * hyps[0] is the candidate. returns divergence in micro (D * 1e6), or -1 if none (NO_DISCRIMINATING_EXPERIMENT). */ +int64_t pd0_learner_propose(const pd0_learner *L, const pd0_rel *hyps, int n_hyp, uint64_t planner_seed, uint32_t n_random, + const uint8_t (*forbidden)[PD0_HASH], uint32_t n_forbidden, pd0_exp *out); +/* a random schedule from a named stream (used for the random half of replication) */ +void pd0_learner_random_schedule(const pd0l_desc *d, uint64_t seed, const char *tag, uint32_t idx, int64_t *reset, pd0_step *steps, uint32_t n); +/* prediction interface over a relation (observed variables in and out; latent starts at 0) */ +void pd0_learner_rollout(const pd0_rel *rel, const int64_t *init, const pd0_step *steps, uint32_t n, int64_t *out); +/* one-step NRMSE of rel over the learner's records of a tag (integer evaluator, latent carried per episode) */ +int64_t pd0_learner_onestep_nrmse(const pd0_learner *L, const pd0_rel *rel, uint8_t tag); +/* human report (JSON-ish, non-canonical) */ +size_t pd0_learner_report(const pd0_learner *L, char *buf, size_t cap); +#endif diff --git a/src/physics0/learner/pd0_learner_main.c b/src/physics0/learner/pd0_learner_main.c new file mode 100644 index 000000000..b6185c991 --- /dev/null +++ b/src/physics0/learner/pd0_learner_main.c @@ -0,0 +1,25 @@ +/* pd0-learner: deterministic offline fit over a raw PD0REC1 stream on stdin + * (records are treated as FIT unless an episode index appears in the optional + * SELECT list). Prints the learner report and the PDLAW1 bytes of the best + * candidate as hex. Used by the determinism test; holds no world knowledge. + * Usage: pd0-learner [select-every-k] < records */ +#include "pd0_learner.h" +#include "pd0_codes.h" +#include +#include +#include + +int main(int argc, char **argv) +{ + if (argc < 3) { fprintf(stderr, "usage: pd0-learner [select-every-k]\n"); return 2; } + pd0l_desc d; memset(&d, 0, sizeof d); d.n_obs = (uint8_t)atoi(argv[1]); d.n_channels = (uint8_t)atoi(argv[2]); int k = argc > 3 ? atoi(argv[3]) : 4; + for (int c = 0; c < d.n_channels; c++) { d.chan_min[c] = -2000000; d.chan_max[c] = 2000000; } for (int j = 0; j < d.n_obs; j++) { d.reset_min[j] = -2000000; d.reset_max[j] = 2000000; } + d.dt_micro = 50000; d.episode_max_steps = 100; d.budget_steps = 3000; d.budget_episodes = 300; + pd0_learner *L = pd0_learner_new(&d, 1); if (!L) return 2; + static uint8_t buf[1 << 22]; size_t n = fread(buf, 1, sizeof buf, stdin), off = 0; pd0_rec r; size_t used; + while (off < n && pd0_rec_parse(buf + off, n - off, &r, &used) == PD0V_OK) { pd0_learner_observe(L, &r, (k > 0 && (int)(r.episode % (uint32_t)k) == k - 1) ? TAG_SELECT : TAG_FIT); off += used; } + pd0_learner_fit(L); + static char jb[1 << 20]; pd0_learner_report(L, jb, sizeof jb); puts(jb); + const pd0l_candidate *c = pd0_learner_candidate(L, 0); if (c) { uint8_t law[8192]; pd0_rel rel = c->rel; size_t ln = pd0_rel_write(&rel, law, sizeof law); printf("PDLAW1 "); for (size_t i = 0; i < ln; i++) printf("%02x", law[i]); printf("\n"); } + pd0_learner_free(L); return 0; +} diff --git a/tests/physics0/learner/.crumb b/tests/physics0/learner/.crumb new file mode 100644 index 000000000..a79284b09 --- /dev/null +++ b/tests/physics0/learner/.crumb @@ -0,0 +1,6 @@ +{ + "schema_version": "1.0.0", + "name": "tests/physics0/learner", + "layer": "tests", + "purpose": "Learner gates: unit tests (determinism, PDLAW1 round trip, memoriser vs compact theory, null world, withholding), the development harness that drives pd0-world and the test-only truth process over pipes, tags the split, builds the PD0LEDG1 ledger and scores against the truth. The truth process is the only binary here that links the generators." +} diff --git a/tests/physics0/learner/pd0_harness.c b/tests/physics0/learner/pd0_harness.c new file mode 100644 index 000000000..c760981e7 --- /dev/null +++ b/tests/physics0/learner/pd0_harness.c @@ -0,0 +1,240 @@ +/* PD-0 development harness for the learner (spec section 11 roles: the harness + * owns the split, the tags, the ledger and the scoring; the learner only sees + * FIT/SELECT records). Verifier-side build: links pd0_fmt/pd0_ladder/pd0_score/ + * pd0_controls and the learner library, never the generators. The world and the + * truth are separate processes (build/physics0/pd0-world, pd0-truth). + * Usage: pd0-harness + * Prints one PD0L result line; writes /pd0l-L-s.{ledger,law,json}. */ +#include "pd0_learner.h" +#include "pd0_ladder.h" +#include "pd0_codes.h" +#include "pd0_score.h" +#include "pd0_controls.h" +#include "pd0_rng.h" +#include +#include +#include +#include +#include +#include + +#define EP_LEN 20u +#define LCAP (64u << 20) +#define MAX_EP 1024 + +typedef struct { pid_t pid; int in, out; FILE *rd, *wr; } proc; +static int spawn(proc *p, char *const argv[]) +{ + int a[2], b[2]; if (pipe(a) || pipe(b)) return -1; + p->pid = fork(); if (p->pid < 0) return -1; + if (p->pid == 0) { dup2(a[0], 0); dup2(b[1], 1); close(a[0]); close(a[1]); close(b[0]); close(b[1]); execv(argv[0], argv); _exit(127); } + close(a[0]); close(b[1]); p->out = a[1]; p->in = b[0]; p->rd = fdopen(p->in, "r"); p->wr = fdopen(p->out, "w"); return 0; +} +static int rd_full(int fd, uint8_t *b, size_t n) { while (n) { ssize_t k = read(fd, b, n); if (k <= 0) return -1; b += k; n -= (size_t)k; } return 0; } +static int wr_full(int fd, const uint8_t *b, size_t n) { while (n) { ssize_t k = write(fd, b, n); if (k <= 0) return -1; b += k; n -= (size_t)k; } return 0; } +/* one framed world call; returns response length or 0 */ +static size_t world_call(proc *w, const uint8_t *req, size_t n, uint8_t *resp, size_t cap) +{ + uint8_t l4[4]; pd0_put_u32(l4, (uint32_t)n); if (wr_full(w->out, l4, 4) || wr_full(w->out, req, n) || rd_full(w->in, l4, 4)) return 0; + uint32_t rn = pd0_get_u32(l4); if (rn == 0 || rn > cap || rd_full(w->in, resp, rn)) return 0; return rn; +} +typedef struct { int version; pd0l_desc d; } describe; +static int parse_describe(const uint8_t *b, size_t n, describe *D) +{ + memset(D, 0, sizeof *D); if (n < 10) return -1; + if (!memcmp(b, "PD0DESC2", 8)) D->version = 2; else return -1; /* PD0DESC1 (one reset pair) is refused */ + pd0l_desc *d = &D->d; d->n_obs = b[8]; d->n_channels = b[9]; size_t o = 10; if (d->n_obs == 0 || d->n_obs > PD0_MAX_OBS || d->n_channels > PD0_MAX_CHAN) return -1; + size_t nr = d->n_obs; if (n < o + 8 + 16u * d->n_channels + 16u * nr + 12) return -1; + d->dt_micro = (int64_t)pd0_get_u64(b + o); o += 8; + for (int c = 0; c < d->n_channels; c++, o += 8) d->chan_min[c] = (int64_t)pd0_get_u64(b + o); + for (int c = 0; c < d->n_channels; c++, o += 8) d->chan_max[c] = (int64_t)pd0_get_u64(b + o); + int64_t mn[PD0_MAX_OBS], mx[PD0_MAX_OBS]; + for (size_t j = 0; j < nr; j++, o += 8) mn[j] = (int64_t)pd0_get_u64(b + o); + for (size_t j = 0; j < nr; j++, o += 8) mx[j] = (int64_t)pd0_get_u64(b + o); + for (int j = 0; j < d->n_obs; j++) { d->reset_min[j] = mn[nr == 1 ? 0 : (size_t)j]; d->reset_max[j] = mx[nr == 1 ? 0 : (size_t)j]; } + d->episode_max_steps = pd0_get_u32(b + o); d->budget_steps = pd0_get_u32(b + o + 4); d->budget_episodes = pd0_get_u32(b + o + 8); + return 0; +} + +/* ---- run state ---- */ +typedef struct { + describe D; int level; uint64_t seed; proc world, truth; pd0_learner *L; + uint8_t *ledger; size_t llen; uint8_t last[PD0_HASH]; + pd0_ladder_params P; + uint32_t next_episode, n_episodes, steps_used, episodes_used; + /* per episode bookkeeping: tag, schedule hash, record range */ + uint8_t ep_tag[MAX_EP]; uint8_t ep_hash[MAX_EP][PD0_HASH]; uint32_t ep_first[MAX_EP], ep_n[MAX_EP]; int ep_ok[MAX_EP]; + pd0_rec *recs; uint32_t nrec; + pd0_transition *fit; uint32_t nfit; + /* truth */ + pd0_rel truth_rel; int truth_size, truth_n_latent; int have_truth; + /* counters for the report */ + uint32_t n_oob, n_refused, n_propose_fail, n_trial_random, n_attempts, n_budget; +} run; + +static void append(run *R, uint8_t kind, const uint8_t *p, uint32_t n) { R->llen = pd0_ledg_append(R->ledger, R->llen, LCAP, kind, p, n, R->last); } +static void tag(run *R, uint32_t ep, uint8_t t, uint32_t batch, uint8_t origin) { pd0_tag T = { ep, t, batch, origin }; uint8_t p[32]; append(R, LEDG_TAG, p, (uint32_t)pd0_tag_write(&T, p)); } + +/* one episode against the world: TAG first, then reset + steps; records go to the ledger, FIT/SELECT ones to the learner. + * returns the episode index (bookkeeping slot) or -1 on budget exhaustion */ +static int episode(run *R, uint8_t t, uint32_t batch, uint8_t origin, const int64_t *reset, const pd0_step *steps, uint32_t n) +{ + if (R->n_episodes >= MAX_EP) return -1; + uint32_t ep = R->next_episode; uint32_t slot = R->n_episodes; uint8_t req[128], resp[1024]; pd0_rec r; + tag(R, ep, t, batch, origin); + req[0] = 1; req[1] = R->D.d.n_obs; for (int j = 0; j < R->D.d.n_obs; j++) pd0_put_u64(req + 2 + 8 * j, (uint64_t)reset[j]); + size_t rn = world_call(&R->world, req, 2 + 8u * R->D.d.n_obs, resp, sizeof resp); if (!rn) { fprintf(stderr, "world: reset call failed\n"); exit(3); } + if (pd0_rec_parse(resp, rn, &r, NULL) != PD0V_OK) { fprintf(stderr, "world: bad reset record\n"); exit(3); } + if (r.status == PD0_ST_BUDGET_EXHAUSTED) { R->n_budget++; return -1; } + if (r.episode != ep) { fprintf(stderr, "harness: episode numbering drift (%u vs %u)\n", r.episode, ep); exit(3); } + R->next_episode = ep + 1; R->episodes_used++; + R->ep_tag[slot] = t; R->ep_first[slot] = R->nrec; R->ep_n[slot] = 0; R->ep_ok[slot] = 0; R->n_episodes++; + append(R, LEDG_OBS, resp, (uint32_t)rn); R->recs[R->nrec++] = r; R->ep_n[slot]++; + if (t == TAG_FIT || t == TAG_SELECT) pd0_learner_observe(R->L, &r, t); + if (r.status != PD0_ST_OK) { R->n_refused++; memset(R->ep_hash[slot], 0, PD0_HASH); return (int)slot; } + pd0_step applied[PD0_MAX_STEPS]; uint32_t n_ok = 0; + for (uint32_t s = 0; s < n; s++) { + req[0] = 2; req[1] = steps[s].channel; pd0_put_u64(req + 2, (uint64_t)steps[s].value); + rn = world_call(&R->world, req, 10, resp, sizeof resp); if (!rn) { fprintf(stderr, "world: step call failed\n"); exit(3); } + if (pd0_rec_parse(resp, rn, &r, NULL) != PD0V_OK) { fprintf(stderr, "world: bad step record\n"); exit(3); } + append(R, LEDG_OBS, resp, (uint32_t)rn); R->recs[R->nrec++] = r; R->ep_n[slot]++; + if (t == TAG_FIT || t == TAG_SELECT) pd0_learner_observe(R->L, &r, t); + if (r.status == PD0_ST_BUDGET_EXHAUSTED) { R->n_budget++; break; } + R->steps_used++; + if (r.status == PD0_ST_OK || r.status == PD0_ST_EPISODE_END) { if (n_ok < PD0_MAX_STEPS) { applied[n_ok].channel = r.channel; applied[n_ok].value = r.applied; } n_ok++; + if (t == TAG_FIT && R->nfit < 65536) { pd0_transition *f = &R->fit[R->nfit++]; memcpy(f->before, r.before, sizeof f->before); f->chan = r.channel; f->value = r.applied; memcpy(f->after, r.after, sizeof f->after); } } + if (r.status == PD0_ST_OUT_OF_BOUNDS) { R->n_oob++; break; } + if (r.status == PD0_ST_REFUSED_RANGE) { R->n_refused++; continue; } + if (r.status == PD0_ST_EPISODE_END) break; + } + R->ep_ok[slot] = n_ok >= EP_LEN; + pd0_schedule_hash(R->D.d.n_obs, reset, (uint8_t)(n_ok < PD0_MAX_STEPS ? n_ok : PD0_MAX_STEPS), applied, R->ep_hash[slot]); + return (int)slot; +} + +/* ---- truth process ---- */ +static int truth_rel(run *R) +{ + fprintf(R->truth.wr, "rel\n"); fflush(R->truth.wr); char line[1024]; unsigned nv, nl, nc, ne; int sz, tsz; + if (!fgets(line, sizeof line, R->truth.rd) || sscanf(line, "rel %u %u %u %u %d %d", &nv, &nl, &nc, &ne, &sz, &tsz) != 6) return -1; + pd0_rel *t = &R->truth_rel; memset(t, 0, sizeof *t); t->n_vars = (uint8_t)nv; t->n_latent = (uint8_t)nl; t->n_channels = (uint8_t)nc; R->truth_size = tsz; R->truth_n_latent = (int)nl; + while (fgets(line, sizeof line, R->truth.rd) && strncmp(line, "end", 3)) { unsigned tg; long long coef; int off; if (sscanf(line, "term %u %lld%n", &tg, &coef, &off) != 2) return -1; + int e; for (e = 0; e < t->n_equations; e++) if (t->eq[e].target == tg) break; if (e == t->n_equations) { t->eq[t->n_equations++].target = (uint8_t)tg; } + pd0_eq *q = &t->eq[e]; int k = q->n_terms++; q->coef[k] = coef; char *p = line + off; for (unsigned i = 0; i < nv + nc; i++) q->expo[k][i] = (uint8_t)strtoul(p, &p, 10); } + t->description_bits = pd0_rel_bits(t); R->have_truth = 1; return 0; +} +/* noise-free truth trajectory; returns 1 if it stayed in bounds */ +static int truth_episode(run *R, const int64_t *reset, const pd0_step *steps, uint32_t n, int64_t out[][PD0_MAX_OBS]) +{ + fprintf(R->truth.wr, "ep %u", R->D.d.n_obs); for (int j = 0; j < R->D.d.n_obs; j++) fprintf(R->truth.wr, " %lld", (long long)reset[j]); fprintf(R->truth.wr, " %u", n); + for (uint32_t s = 0; s < n; s++) fprintf(R->truth.wr, " %d %lld", steps[s].channel == PD0_CHAN_NONE ? -1 : steps[s].channel, (long long)steps[s].value); fprintf(R->truth.wr, "\n"); fflush(R->truth.wr); + char line[1024]; if (!fgets(line, sizeof line, R->truth.rd)) return 0; uint32_t got = 0; int ok = 1; + while (fgets(line, sizeof line, R->truth.rd) && strncmp(line, "end", 3)) { if (!strncmp(line, "oob", 3)) { ok = 0; continue; } char *p = line; for (int j = 0; j < R->D.d.n_obs; j++) out[got][j] = strtoll(p, &p, 10); got++; } + return ok && got == n; +} + +static const char *lvl_name(int level) { static char b[8]; if (level < 0) return "null"; snprintf(b, sizeof b, "L%d", level); return b; } + +int main(int argc, char **argv) +{ + if (argc < 6) { fprintf(stderr, "usage: pd0-harness \n"); return 2; } + static run R; memset(&R, 0, sizeof R); R.level = strcmp(argv[3], "null") == 0 ? -1 : atoi(argv[3]); R.seed = strtoull(argv[4], NULL, 0); + char *wargv[] = { argv[1], argv[3], argv[4], NULL }, *targv[] = { argv[2], argv[3], argv[4], NULL }; + if (spawn(&R.world, wargv) || spawn(&R.truth, targv)) { perror("spawn"); return 3; } + R.ledger = malloc(LCAP); R.recs = malloc(sizeof(pd0_rec) * 65536); R.fit = malloc(sizeof(pd0_transition) * 65536); + /* describe */ + uint8_t req[128], resp[1024]; req[0] = 0; size_t rn = world_call(&R.world, req, 1, resp, sizeof resp); + if (!rn || parse_describe(resp, rn, &R.D)) { fprintf(stderr, "world: bad describe\n"); return 3; } + const pd0l_desc *d = &R.D.d; + if (truth_rel(&R)) { fprintf(stderr, "truth: bad relation\n"); return 3; } + /* ladder parameters: spec section 6 bounds for the level; harness knowledge, never the learner's */ + pd0_ladder_params_default(&R.P, d->n_obs, d->n_channels); + for (int c = 0; c < d->n_channels; c++) { R.P.chan_min[c] = d->chan_min[c]; R.P.chan_max[c] = d->chan_max[c]; } + R.P.reset_min = d->reset_min[0]; R.P.reset_max = d->reset_max[0]; for (int j = 1; j < d->n_obs; j++) { if (d->reset_min[j] < R.P.reset_min) R.P.reset_min = d->reset_min[j]; if (d->reset_max[j] > R.P.reset_max) R.P.reset_max = d->reset_max[j]; } + R.P.episode_len = EP_LEN; R.P.dt_micro = d->dt_micro; R.P.size_bound = (uint32_t)(R.truth_size > 0 ? R.truth_size : 4) + 2; + R.P.eps_bound_micro = R.level == 5 ? 50000 : R.level == 6 ? 30000 : 20000; R.P.noisy = R.level == 5; + R.L = pd0_learner_new(d, R.seed); + /* 1. exploration: FIT FIT FIT SELECT HOLDOUT per five (60/20/20), 20-step episodes; HOLDOUT never reaches the learner */ + uint32_t reserve = 5 * EP_LEN + 3 * 10 * EP_LEN + 100; uint32_t n_explore = (d->budget_steps > reserve ? (d->budget_steps - reserve) / EP_LEN : 0); + if (n_explore + 40 > d->budget_episodes) n_explore = d->budget_episodes > 40 ? d->budget_episodes - 40 : 0; + int64_t reset[PD0_MAX_OBS]; pd0_step steps[PD0_MAX_STEPS]; + for (uint32_t e = 0; e < n_explore; e++) { uint8_t t = (e % 5) < 3 ? TAG_FIT : (e % 5) == 3 ? TAG_SELECT : TAG_HOLDOUT; + pd0_learner_explore(R.L, e, reset, steps, EP_LEN); if (episode(&R, t, 0, 0, reset, steps, EP_LEN) < 0) break; } + /* 2. candidate, falsifier, preregistered trials; on refutation the trial records become FIT and the learner refits */ + pd0_ladder_report rep; memset(&rep, 0, sizeof rep); int checker_rc = pd0_ladder_check(R.ledger, R.llen, &R.P, &rep); + pd0_corr corr; int have_corr = 0; const pd0l_candidate *best = NULL; uint8_t cand_hash[PD0_HASH] = { 0 }; pd0_rel hyps[PD0_MAX_HYP]; int n_hyp = 0; uint8_t pl[16384]; + static uint8_t forbidden[PD0L_MAX_FORBID][PD0_HASH]; uint32_t n_forb = 0; + for (uint32_t a = 0; a < 3; a++) { + R.n_attempts++; pd0_learner_fit(R.L); int nc = pd0_learner_n_candidates(R.L); + if (!have_corr && pd0_learner_correlation(R.L, R.seed ^ 0xC0DEu, 2000, &corr) == 0) { append(&R, LEDG_CORR, pl, (uint32_t)pd0_corr_write(&corr, pl)); have_corr = 1; } + best = NULL; for (int i = 0; i < nc; i++) if (!pd0_learner_candidate(R.L, i)->is_null) { best = pd0_learner_candidate(R.L, i); break; } + if (!best || pd0_learner_candidate(R.L, 0)->is_null) { best = NULL; break; } /* nothing beats the null: stop honestly */ + n_hyp = 0; hyps[n_hyp++] = best->rel; + pl[0] = REL_CANDIDATE; size_t n = pd0_rel_write(&hyps[0], pl + 1, sizeof pl - 1); append(&R, LEDG_RELATION, pl, (uint32_t)n + 1); memcpy(cand_hash, R.last, PD0_HASH); + pd0_falsifier f; memset(&f, 0, sizeof f); memcpy(f.candidate, cand_hash, PD0_HASH); f.eps_micro = R.P.eps_bound_micro; f.min_trials = 5; + for (int i = 0; i < nc && n_hyp < 4; i++) { const pd0l_candidate *c = pd0_learner_candidate(R.L, i); if (c == best || c->is_null) continue; + hyps[n_hyp++] = c->rel; pl[0] = REL_RIVAL; n = pd0_rel_write(&c->rel, pl + 1, sizeof pl - 1); append(&R, LEDG_RELATION, pl, (uint32_t)n + 1); memcpy(f.rival[f.n_rivals++], R.last, PD0_HASH); } + { pd0_rel nullr; memset(&nullr, 0, sizeof nullr); nullr.n_vars = d->n_obs; nullr.n_channels = d->n_channels; hyps[n_hyp++] = nullr; + pl[0] = REL_NULL; n = pd0_rel_write(&nullr, pl + 1, sizeof pl - 1); append(&R, LEDG_RELATION, pl, (uint32_t)n + 1); memcpy(f.rival[f.n_rivals++], R.last, PD0_HASH); } + append(&R, LEDG_FALSIFIER, pl, (uint32_t)pd0_fals_write(&f, pl)); + checker_rc = pd0_ladder_check(R.ledger, R.llen, &R.P, &rep); if (rep.state < LS_HYPOTHESIS) break; /* no admissible candidate: no trials */ + n_forb = 0; for (uint32_t s = 0; s < R.n_episodes && n_forb < PD0L_MAX_FORBID; s++) memcpy(forbidden[n_forb++], R.ep_hash[s], PD0_HASH); + for (int t = 0; t < 5; t++) { static pd0_exp x; memset(&x, 0, sizeof x); + int64_t div = pd0_learner_propose(R.L, hyps, n_hyp, R.seed * 1000 + a * 10 + (uint64_t)t, 256, (const uint8_t (*)[PD0_HASH])forbidden, n_forb, &x); + if (div < 0) { R.n_propose_fail++; R.n_trial_random++; x.n_obs = d->n_obs; x.n_hyp = (uint8_t)n_hyp; x.n_steps = PD0_MAX_STEPS; + pd0_learner_random_schedule(d, R.seed * 7919 + a, "trial", (uint32_t)t, x.reset, x.steps, PD0_MAX_STEPS); + for (int h = 0; h < n_hyp; h++) { int64_t tmp[PD0_MAX_STEPS * PD0_MAX_OBS]; pd0_learner_rollout(&hyps[h], x.reset, x.steps, PD0_MAX_STEPS, tmp); for (int s = 0; s < PD0_MAX_STEPS; s++) for (int j = 0; j < d->n_obs; j++) x.expected[h][s][j] = tmp[s * d->n_obs + j]; } + x.divergence_micro = 0; } + size_t xn = pd0_exp_write(&x, pl, sizeof pl); append(&R, LEDG_PREREG, pl, (uint32_t)xn); + int slot = episode(&R, TAG_TRIAL, 0, 0, x.reset, x.steps, PD0_MAX_STEPS); if (slot >= 0 && n_forb < PD0L_MAX_FORBID) memcpy(forbidden[n_forb++], R.ep_hash[slot], PD0_HASH); } + checker_rc = pd0_ladder_check(R.ledger, R.llen, &R.P, &rep); + if (rep.state >= LS_INTERVENED) break; + if (rep.code == PD0V_T6_TRIAL_FAILED) { /* refuted: the trial records are FIT from now on */ + for (uint32_t s = 0; s < R.n_episodes; s++) if (R.ep_tag[s] == TAG_TRIAL) { for (uint32_t i = 0; i < R.ep_n[s]; i++) pd0_learner_observe(R.L, &R.recs[R.ep_first[s] + i], TAG_FIT); R.ep_tag[s] = TAG_FIT; } + continue; } + break; + } + /* 3. replication: three batches of ten episodes, planner and random halves */ + if (rep.state >= LS_INTERVENED) { + for (uint32_t bt = 0; bt < 3; bt++) { pd0_batch bb = { bt + 1, R.seed * 100 + bt + 1, 10 }; append(&R, LEDG_BATCH, pl, (uint32_t)pd0_batch_write(&bb, pl)); + for (uint32_t e = 0; e < 10; e++) { uint8_t origin = (e & 1) ? ORIGIN_RANDOM : ORIGIN_PLANNER; static pd0_exp x; + if (origin == ORIGIN_PLANNER && pd0_learner_propose(R.L, hyps, n_hyp, bb.stream_seed * 31 + e, 128, (const uint8_t (*)[PD0_HASH])forbidden, n_forb, &x) >= 0) { memcpy(reset, x.reset, sizeof reset); memcpy(steps, x.steps, sizeof steps); } + else { if (origin == ORIGIN_PLANNER) R.n_propose_fail++; pd0_learner_random_schedule(d, bb.stream_seed, "rep", e, reset, steps, PD0_MAX_STEPS); } + int slot = episode(&R, TAG_REP, bb.batch_id, origin, reset, steps, PD0_MAX_STEPS); if (slot >= 0 && n_forb < PD0L_MAX_FORBID) memcpy(forbidden[n_forb++], R.ep_hash[slot], PD0_HASH); } } + checker_rc = pd0_ladder_check(R.ledger, R.llen, &R.P, &rep); + } + /* 4. law record + independent re-verification */ + static uint8_t law[65536]; size_t law_len = pd0_ladder_emit_law(&rep, &R.P, law, sizeof law); int law_rc = law_len ? pd0_ladder_verify_law(&rep, &R.P, law, law_len) : -1; + /* 5. scoring against the truth: 10 in-box + 10 extrapolation (1.5x box) episodes from the "score" stream */ + pd0_score_result S; memset(&S, 0, sizeof S); S.code = -1; const pd0_rel *scored = rep.have_candidate ? &rep.candidate : best ? &best->rel : NULL; int n_score_eps = 0; + if (scored && R.have_truth && R.level >= 0) { + static pd0_score_episode eps[20]; pd0_rng g; pd0_rng_stream(&g, R.seed + 500, "score"); pd0l_desc box = *d; + for (int i = 0; i < 20 && n_score_eps < 20; ) { pd0_score_episode *e = &eps[n_score_eps]; memset(e, 0, sizeof *e); e->in_box = i < 10; + for (int j = 0; j < d->n_obs; j++) { box.reset_min[j] = e->in_box ? d->reset_min[j] : d->reset_min[j] * 3 / 2; box.reset_max[j] = e->in_box ? d->reset_max[j] : d->reset_max[j] * 3 / 2; } + pd0_learner_random_schedule(&box, R.seed + 500, e->in_box ? "score-in" : "score-ex", (uint32_t)pd0_rng_next(&g), e->init, e->steps, PD0_MAX_STEPS); + if (truth_episode(&R, e->init, e->steps, PD0_MAX_STEPS, e->truth)) { n_score_eps++; i++; } else if (pd0_rng_next(&g) % 64 == 0) i++; } + pd0_score_params SP; memset(&SP, 0, sizeof SP); SP.n_obs = d->n_obs; SP.n_channels = d->n_channels; SP.size_bound = R.P.size_bound; + SP.inbox_bound = R.level == 6 ? 30000 : 20000; SP.extrap_bound = R.level == 6 ? 80000 : 60000; SP.onestep_bound = R.level == 6 ? 0 : 10000; + SP.score_constants = R.level != 6; SP.const_tol_ppm = R.level == 5 ? 100000 : 50000; SP.require_latent = R.level == 6; SP.latent_ref_factor = 3; + if (R.level != 6) for (int e = 0; e < R.truth_rel.n_equations; e++) for (int t = 0; t < R.truth_rel.eq[e].n_terms && SP.n_true_terms < PD0_MAX_TRUE_TERMS; t++) { pd0_true_term *tt = &SP.true_terms[SP.n_true_terms++]; tt->target = R.truth_rel.eq[e].target; memcpy(tt->expo, R.truth_rel.eq[e].expo[t], sizeof tt->expo); tt->coef = R.truth_rel.eq[e].coef[t]; } + pd0_rel rc = *scored; pd0_score(&SP, &rc, 0, pd0_rel_rollout_fn, pd0_rel_predict_fn, &rc, eps, (uint32_t)n_score_eps, R.fit, R.nfit, &S); + } + /* 6. outputs */ + char path[1024]; snprintf(path, sizeof path, "%s/pd0l-%s-s%llu.ledger", argv[5], lvl_name(R.level), (unsigned long long)R.seed); FILE *fo = fopen(path, "wb"); if (fo) { fwrite(R.ledger, 1, R.llen, fo); fclose(fo); } + if (law_len) { snprintf(path, sizeof path, "%s/pd0l-%s-s%llu.law", argv[5], lvl_name(R.level), (unsigned long long)R.seed); fo = fopen(path, "wb"); if (fo) { fwrite(law, 1, law_len, fo); fclose(fo); } } + static char jb[1 << 20]; pd0_learner_report(R.L, jb, sizeof jb); snprintf(path, sizeof path, "%s/pd0l-%s-s%llu.json", argv[5], lvl_name(R.level), (unsigned long long)R.seed); fo = fopen(path, "w"); + if (fo) { fprintf(fo, "{\"level\":\"%s\",\"seed\":%llu,\"describe_version\":%d,\"state\":\"%s\",\"code\":%d,\"refutations\":%u,\"score_code\":%d,\"score_mask\":%u,\"learner\":%s}\n", lvl_name(R.level), (unsigned long long)R.seed, R.D.version, pd0_ladder_state_name(rep.state), rep.code, rep.n_refutations, S.code, S.fail_mask, jb); fclose(fo); } + uint32_t cbits = scored ? pd0_rel_bits(scored) : 0, csize = scored ? pd0_rel_size(scored) : 0; int hidden = best ? best->hidden_state_suspected : 0; + printf("PD0L level=%s seed=%llu desc=v%d episodes=%u steps=%u fit=%u select=%u state=%s code=%d stall=%d refutations=%u attempts=%u " + "cand_size=%u cand_bits=%u size_bound=%u fit_nrmse=%lld select_nrmse=%lld hidden_state=%d " + "score=%s score_code=%d mask=0x%x inbox=%lld extrap=%lld onestep=%lld ref=%lld score_eps=%d law=%d checker_rc=%d " + "oob=%u refused=%u budget_hits=%u propose_fail=%u trial_random=%u\n", + lvl_name(R.level), (unsigned long long)R.seed, R.D.version, R.n_episodes, R.steps_used, pd0_learner_n_transitions(R.L, TAG_FIT), pd0_learner_n_transitions(R.L, TAG_SELECT), + pd0_ladder_state_name(rep.state), rep.code, rep.stall_code, rep.n_refutations, R.n_attempts, + csize, cbits, R.P.size_bound, (long long)(best ? best->fit_nrmse_micro : -1), (long long)(best ? best->select_nrmse_micro : -1), hidden, + !scored ? "NO_CANDIDATE" : S.code == 0 ? "PASS" : S.code < 0 ? "NOT_SCORED" : "FAIL", S.code, S.fail_mask, (long long)S.inbox_nrmse, (long long)S.extrap_nrmse, (long long)S.onestep_nrmse, (long long)S.ref_nrmse, n_score_eps, law_rc, checker_rc, + R.n_oob, R.n_refused, R.n_budget, R.n_propose_fail, R.n_trial_random); + fprintf(R.truth.wr, "quit\n"); fflush(R.truth.wr); fclose(R.world.wr); fclose(R.truth.wr); waitpid(R.world.pid, NULL, 0); waitpid(R.truth.pid, NULL, 0); + pd0_learner_free(R.L); free(R.ledger); free(R.recs); free(R.fit); + return 0; +} diff --git a/tests/physics0/learner/pd0_truth_main.c b/tests/physics0/learner/pd0_truth_main.c new file mode 100644 index 000000000..da6f96aa2 --- /dev/null +++ b/tests/physics0/learner/pd0_truth_main.c @@ -0,0 +1,37 @@ +/* TEST-ONLY truth process for the learner harness (substrate side: links the + * generators). Runs as a separate process so no learner or harness object ever + * links pd0_gen.c. Text protocol on stdin/stdout: + * rel -> the true relation (PDLAW1 delta form) and S* + * ep * -> noise-free trajectory of n steps (or "oob") + * Usage: pd0-truth */ +#include "physics0/pd0_gen.h" +#include "physics0/pd0_relation.h" +#include +#include +#include + +int main(int argc, char **argv) +{ + if (argc < 3) return 2; + int level = strcmp(argv[1], "null") == 0 ? PD0_LEVEL_NULL : atoi(argv[1]); + pd0_gen g; pd0_desc d; if (pd0_gen_init(&g, level, strtoull(argv[2], NULL, 0)) != 0 || pd0_gen_desc(level, &d) != 0) return 2; + pd0_rng dummy_noise, dummy_null; pd0_stream(&dummy_noise, 1, "x"); pd0_stream(&dummy_null, 1, "y"); + char line[4096]; + while (fgets(line, sizeof line, stdin)) { + if (!strncmp(line, "rel", 3)) { + pd0_relation r; memset(&r, 0, sizeof r); pd0_gen_true_relation(&g, d.dt_micro, &r); + printf("rel %u %u %u %u %d %d\n", r.n_vars, r.n_latent, r.n_channels, r.n_eq, pd0_relation_size(&r), pd0_gen_true_size(level)); + for (int e = 0; e < r.n_eq; e++) for (int t = 0; t < r.eq[e].n_terms; t++) { printf("term %u %lld", r.eq[e].target, (long long)r.eq[e].t[t].coef); for (int i = 0; i < r.n_vars; i++) printf(" %u", r.eq[e].t[t].ex[i]); for (int c = 0; c < r.n_channels; c++) printf(" %u", r.eq[e].t[t].ex[PD0_MAX_VARS + c]); printf("\n"); } + printf("end\n"); fflush(stdout); + } else if (!strncmp(line, "ep ", 3)) { + char *p = line + 3; int no = (int)strtol(p, &p, 10); int64_t s[PD0_MAX_OBS] = { 0 }, h = 0, ns[PD0_MAX_OBS], nh, obs[PD0_MAX_OBS]; + for (int j = 0; j < no; j++) s[j] = strtoll(p, &p, 10); + int n = (int)strtol(p, &p, 10); printf("traj %d\n", n); + for (int st = 0; st < n; st++) { int ch = (int)strtol(p, &p, 10); int64_t v = strtoll(p, &p, 10); int64_t u[PD0_MAX_CH] = { 0 }; if (ch >= 0 && ch < PD0_MAX_CH) u[ch] = v; + if (pd0_gen_step(&g, s, h, u, d.dt_micro, ns, &nh, obs, &dummy_noise, &dummy_null)) { printf("oob\n"); break; } + memcpy(s, ns, sizeof s); h = nh; for (int j = 0; j < no; j++) printf("%s%lld", j ? " " : "", (long long)s[j]); printf("\n"); } + printf("end\n"); fflush(stdout); + } else if (!strncmp(line, "quit", 4)) break; + } + return 0; +} diff --git a/tests/physics0/learner/test_pd0_learner.c b/tests/physics0/learner/test_pd0_learner.c new file mode 100644 index 000000000..aae4d4bf1 --- /dev/null +++ b/tests/physics0/learner/test_pd0_learner.c @@ -0,0 +1,78 @@ +/* Unit tests for the PD-0 learner (Direction 4). Synthetic worlds are written + * here from the test's own equations (test-only, like tests/physics0/verify/ + * truth.h); the learner library never sees them. */ +#include "pd0_learner.h" +#include "pd0_codes.h" +#include "pd0_controls.h" +#include "pd0_score.h" +#include "pd0_rng.h" +#include +#include +#include +#include + +static int fails = 0, checks = 0; +#define CHECK(c, msg) do { checks++; if (!(c)) { fails++; printf("FAIL: %s (%s:%d)\n", msg, __FILE__, __LINE__); } } while (0) + +static void desc2(pd0l_desc *d) { memset(d, 0, sizeof *d); d->n_obs = 2; d->n_channels = 1; d->chan_min[0] = -2000000; d->chan_max[0] = 2000000; for (int j = 0; j < 2; j++) { d->reset_min[j] = -2000000; d->reset_max[j] = 2000000; } d->dt_micro = 50000; d->episode_max_steps = 100; d->budget_steps = 3000; d->budget_episodes = 300; } +/* synthetic cubic map: s0 += dt s1 ; s1 += dt(-k s0 - b s0^3 + u) (micro arithmetic, same evaluator family as the checker) */ +static void cubic_step(const int64_t *s, int64_t u, int64_t *n) { int64_t dt = 50000, k = 2500000, b = 900000; n[0] = s[0] + pd0_mul(s[1], dt); n[1] = s[1] + pd0_mul(-pd0_mul(k, s[0]) - pd0_mul(b, pd0_mul(s[0], pd0_mul(s[0], s[0]))) + u, dt); } +static int64_t uni(pd0_rng *g, int64_t lo, int64_t hi) { return lo + pd0_mul(hi - lo, pd0_rng_unit(g)); } +/* feed n_ep episodes of 20 steps; tags FIT except every 4th SELECT; also fills held-out transitions from a separate stream */ +static void feed(pd0_learner *L, uint64_t seed, uint32_t n_ep, int null_world, pd0_transition *held, uint32_t *n_held, uint32_t cap) +{ + pd0_rng g; pd0_rng_stream(&g, seed, "feed"); pd0_rng nz; pd0_rng_stream(&nz, seed, "null"); uint32_t h = 0; + for (uint32_t e = 0; e < n_ep + 10; e++) { int64_t st[2], nx[2]; st[0] = uni(&g, -2000000, 2000000); st[1] = uni(&g, -2000000, 2000000); pd0_rec r; memset(&r, 0, sizeof r); r.n_obs = 2; r.kind = PD0_KIND_RESET; r.channel = PD0_CHAN_NONE; r.episode = e; r.before[0] = r.after[0] = st[0]; r.before[1] = r.after[1] = st[1]; + uint8_t tag = (e % 4 == 3) ? TAG_SELECT : TAG_FIT; int holdout = e >= n_ep; if (!holdout) pd0_learner_observe(L, &r, tag); + for (int s = 0; s < 20; s++) { int64_t u = uni(&g, -2000000, 2000000); if (null_world) { nx[0] = pd0_rng_noise(&nz, 500000); nx[1] = pd0_rng_noise(&nz, 500000); } else cubic_step(st, u, nx); + if (llabs(nx[0]) > 10000000 || llabs(nx[1]) > 10000000) break; + memset(&r, 0, sizeof r); r.n_obs = 2; r.kind = PD0_KIND_STEP; r.status = PD0_ST_OK; r.channel = 0; r.episode = e; r.step_in_episode = (uint32_t)s + 1; r.requested = r.applied = u; r.before[0] = st[0]; r.before[1] = st[1]; r.after[0] = nx[0]; r.after[1] = nx[1]; + if (holdout) { if (h < cap) { pd0_transition *t = &held[h++]; t->before[0] = st[0]; t->before[1] = st[1]; t->chan = 0; t->value = u; t->after[0] = nx[0]; t->after[1] = nx[1]; } } else pd0_learner_observe(L, &r, tag); + st[0] = nx[0]; st[1] = nx[1]; } } + if (n_held) *n_held = h; +} +static int64_t heldout_nrmse(pd0_rollout_fn roll, pd0_predict_fn pred, void *ctx, const pd0_transition *t, uint32_t n) +{ (void)roll; double se[2] = { 0 }, s[2] = { 0 }, ss[2] = { 0 }; for (uint32_t i = 0; i < n; i++) { int64_t nx[PD0_MAX_VARS]; int64_t st[PD0_MAX_VARS] = { t[i].before[0], t[i].before[1] }; pred(ctx, st, t[i].chan, t[i].value, nx); for (int j = 0; j < 2; j++) { double e = (double)(nx[j] - t[i].after[j]) / 1e6, v = (double)t[i].after[j] / 1e6; se[j] += e * e; s[j] += v; ss[j] += v * v; } } + double w = 0; for (int j = 0; j < 2; j++) { double rmse = sqrt(se[j] / n), m = s[j] / n, sd = sqrt(fmax(ss[j] / n - m * m, 0)); double v = sd > 1e-9 ? rmse / sd : rmse; if (v > w) w = v; } return (int64_t)(w * 1e6); } + +int main(void) +{ + pd0l_desc d; desc2(&d); static pd0_transition held[4096]; uint32_t nh; + /* 1. determinism: two learners, same records, byte-identical serialisation and report */ + { pd0_learner *A = pd0_learner_new(&d, 7), *B = pd0_learner_new(&d, 7); feed(A, 11, 60, 0, held, &nh, 4096); feed(B, 11, 60, 0, held, &nh, 4096); pd0_learner_fit(A); pd0_learner_fit(B); + CHECK(pd0_learner_n_candidates(A) == pd0_learner_n_candidates(B), "same candidate count"); + for (int i = 0; i < pd0_learner_n_candidates(A); i++) { uint8_t a[8192], b[8192]; pd0_rel ra = pd0_learner_candidate(A, i)->rel, rb = pd0_learner_candidate(B, i)->rel; size_t na = pd0_rel_write(&ra, a, sizeof a), nb = pd0_rel_write(&rb, b, sizeof b); CHECK(na == nb && na > 0 && !memcmp(a, b, na), "candidate bytes identical"); } + static char ja[1 << 18], jbuf[1 << 18]; pd0_learner_report(A, ja, sizeof ja); pd0_learner_report(B, jbuf, sizeof jbuf); CHECK(!strcmp(ja, jbuf), "report identical"); + pd0_corr ca, cb; CHECK(pd0_learner_correlation(A, 5, 500, &ca) == 0 && pd0_learner_correlation(B, 5, 500, &cb) == 0 && !memcmp(&ca, &cb, sizeof ca), "correlation identical"); + /* 2. serialisation round trip through PDLAW1 */ + pd0_rel r0 = pd0_learner_candidate(A, 0)->rel, r1; uint8_t buf[8192]; size_t n = pd0_rel_write(&r0, buf, sizeof buf); size_t used = 0; CHECK(n > 0 && pd0_rel_parse(buf, n, &r1, &used) == PD0V_OK && used == n, "PDLAW1 parses"); + CHECK(r1.n_vars == r0.n_vars && r1.n_equations == r0.n_equations && pd0_rel_size(&r1) == pd0_rel_size(&r0) && pd0_rel_bits(&r1) == pd0_rel_bits(&r0) && r1.description_bits == r0.description_bits, "round trip preserves structure and bits"); + { uint8_t buf2[8192]; size_t n2 = pd0_rel_write(&r1, buf2, sizeof buf2); CHECK(n2 == n && !memcmp(buf, buf2, n), "re-serialisation identical"); } + /* 3. the compact theory beats a memoriser of the same records on held-out transitions */ + const pd0l_candidate *best = pd0_learner_candidate(A, 0); CHECK(!best->is_null, "best is not the null on the cubic world"); CHECK(pd0_rel_size(&best->rel) <= 6, "compact (size <= 6)"); + pd0_rel rc = best->rel; int64_t theory = heldout_nrmse(pd0_rel_rollout_fn, pd0_rel_predict_fn, &rc, held, nh); + static pd0_transition fitt[4096]; uint32_t nf = 0; { pd0_learner *C = pd0_learner_new(&d, 7); (void)C; pd0_learner_free(C); } + /* memoriser over the same FIT episodes, rebuilt from the same stream */ + { pd0_rng g; pd0_rng_stream(&g, 11, "feed"); for (uint32_t e = 0; e < 60; e++) { int64_t st[2], nx[2]; st[0] = uni(&g, -2000000, 2000000); st[1] = uni(&g, -2000000, 2000000); for (int s = 0; s < 20; s++) { int64_t u = uni(&g, -2000000, 2000000); cubic_step(st, u, nx); if (llabs(nx[0]) > 10000000 || llabs(nx[1]) > 10000000) break; if (e % 4 != 3 && nf < 4096) { pd0_transition *t = &fitt[nf++]; t->before[0] = st[0]; t->before[1] = st[1]; t->chan = 0; t->value = u; t->after[0] = nx[0]; t->after[1] = nx[1]; } st[0] = nx[0]; st[1] = nx[1]; } } } + pd0_memoriser m = { fitt, nf, 2 }; int64_t memo = heldout_nrmse(pd0_memoriser_rollout, pd0_memoriser_predict, &m, held, nh); + printf("held-out one-step NRMSE: theory %lld micro (size %u, %u bits) vs memoriser %lld micro (%u stored numbers)\n", (long long)theory, pd0_rel_size(&best->rel), pd0_rel_bits(&best->rel), (long long)memo, pd0_memoriser_stored_numbers(&m)); + CHECK(theory < 20000, "theory within the in-box bound on held-out"); + CHECK(theory * 4 < memo, "memoriser loses to the compact theory on held-out"); + /* 4. deterministic seeded experiments and a discriminating proposal between candidate and null */ + { int64_t r1v[2], r2v[2]; pd0_step s1[20], s2[20]; pd0_learner_explore(A, 3, r1v, s1, 20); pd0_learner_explore(B, 3, r2v, s2, 20); CHECK(!memcmp(r1v, r2v, sizeof r1v) && !memcmp(s1, s2, sizeof s1), "explore deterministic"); + pd0_rel hy[2]; hy[0] = best->rel; memset(&hy[1], 0, sizeof hy[1]); hy[1].n_vars = 2; hy[1].n_channels = 1; pd0_exp x; int64_t dv = pd0_learner_propose(A, hy, 2, 99, 64, NULL, 0, &x); CHECK(dv >= 3000000, "proposal separates candidate from null by >= 3 sd"); + for (int j = 0; j < 2; j++) CHECK(x.reset[j] >= -2000000 && x.reset[j] <= 2000000, "proposal reset inside box"); for (int s = 0; s < 20; s++) CHECK(x.steps[s].value >= -2000000 && x.steps[s].value <= 2000000, "proposal step inside bounds"); + uint8_t forb[1][PD0_HASH]; memcpy(forb[0], x.schedule_hash, PD0_HASH); pd0_exp y; pd0_learner_propose(A, hy, 2, 99, 64, (const uint8_t (*)[PD0_HASH])forb, 1, &y); CHECK(memcmp(y.schedule_hash, x.schedule_hash, PD0_HASH) != 0, "forbidden schedule is not re-proposed"); } + pd0_learner_free(A); pd0_learner_free(B); } + /* 5. withholding: HOLDOUT/TRIAL/REP records are refused by the learner */ + { pd0_learner *A = pd0_learner_new(&d, 1); pd0_rec r; memset(&r, 0, sizeof r); r.n_obs = 2; r.kind = PD0_KIND_STEP; CHECK(pd0_learner_observe(A, &r, TAG_HOLDOUT) == -1 && pd0_learner_observe(A, &r, TAG_TRIAL) == -1 && pd0_learner_observe(A, &r, TAG_REP) == -1, "non-visible tags refused"); CHECK(pd0_learner_n_transitions(A, TAG_FIT) == 0, "nothing stored"); pd0_learner_free(A); } + /* 6. null world: no candidate ranks above the null */ + { pd0_learner *A = pd0_learner_new(&d, 3); feed(A, 21, 60, 1, held, &nh, 4096); pd0_learner_fit(A); const pd0l_candidate *c0 = pd0_learner_candidate(A, 0); + /* i.i.d. draws: "pull to the mean" (next = 0) predicts better than "no change", so MDL may rank it above the + * empty relation; what must hold is that no candidate predicts within the law bounds (the ladder T3 then + * refuses it, NC-1). The strongest correlation is regression to the mean (var_a == var_b), which T2 accepts. */ + printf("null world: best rank is %s (mdl %.0f bits, select NRMSE %lld micro), %d candidates\n", c0->is_null ? "the null" : "a relation", c0->mdl_bits, (long long)c0->select_nrmse_micro, pd0_learner_n_candidates(A)); + for (int i = 0; i < pd0_learner_n_candidates(A); i++) CHECK(pd0_learner_candidate(A, i)->select_nrmse_micro > 500000, "null world: no candidate predicts within 0.5 sd"); + pd0_corr c; int rc = pd0_learner_correlation(A, 5, 2000, &c); CHECK(rc != 0 || (c.var_a != c.var_b && c.p_micro > 10000), "null world: no significant cross-variable correlation (self pairs excluded, spec rev 5)"); pd0_learner_free(A); } + printf("checks %d, failures %d\n", checks, fails); printf("PHYSICS0_LEARNER_UNIT: %s\n", fails ? "FAIL" : "PASS"); return fails ? 1 : 0; +} ```