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
omega-mv / tests / physics0 / learner_v2 / test_pd0_learner2.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* Unit tests for the PD-0 learner (Direction 4). Synthetic worlds are written |
| 18 | * here from the test's own equations (test-only, like tests/physics0/verify/ |
| 19 | * truth.h); the learner library never sees them. */ |
| 20 | #include "pd0_learner.h" |
| 21 | #include "pd0_codes.h" |
| 22 | #include "pd0_controls.h" |
| 23 | #include "pd0_score.h" |
| 24 | #include "pd0_rng.h" |
| 25 | #include <math.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | static int fails = 0, checks = 0; |
| 31 | #define CHECK(c, msg) do { checks++; if (!(c)) { fails++; printf("FAIL: %s (%s:%d)\n", msg, __FILE__, __LINE__); } } while (0) |
| 32 | |
| 33 | 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; } |
| 34 | /* synthetic cubic map: s0 += dt s1 ; s1 += dt(-k s0 - b s0^3 + u) (micro arithmetic, same evaluator family as the checker) */ |
| 35 | 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); } |
| 36 | static int64_t uni(pd0_rng *g, int64_t lo, int64_t hi) { return lo + pd0_mul(hi - lo, pd0_rng_unit(g)); } |
| 37 | /* feed n_ep episodes of 20 steps; tags FIT except every 4th SELECT; also fills held-out transitions from a separate stream */ |
| 38 | 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) |
| 39 | { |
| 40 | pd0_rng g; pd0_rng_stream(&g, seed, "feed"); pd0_rng nz; pd0_rng_stream(&nz, seed, "null"); uint32_t h = 0; |
| 41 | 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]; |
| 42 | uint8_t tag = (e % 4 == 3) ? TAG_SELECT : TAG_FIT; int holdout = e >= n_ep; if (!holdout) pd0_learner_observe(L, &r, tag); |
| 43 | 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); |
| 44 | if (llabs(nx[0]) > 10000000 || llabs(nx[1]) > 10000000) break; |
| 45 | 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]; |
| 46 | 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); |
| 47 | st[0] = nx[0]; st[1] = nx[1]; } } |
| 48 | if (n_held) *n_held = h; |
| 49 | } |
| 50 | static int64_t heldout_nrmse(pd0_rollout_fn roll, pd0_predict_fn pred, void *ctx, const pd0_transition *t, uint32_t n) |
| 51 | { (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; } } |
| 52 | 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); } |
| 53 | |
| 54 | int main(void) |
| 55 | { |
| 56 | pd0l_desc d; desc2(&d); static pd0_transition held[4096]; uint32_t nh; |
| 57 | /* 1. determinism: two learners, same records, byte-identical serialisation and report */ |
| 58 | { 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); |
| 59 | CHECK(pd0_learner_n_candidates(A) == pd0_learner_n_candidates(B), "same candidate count"); |
| 60 | 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"); } |
| 61 | 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"); |
| 62 | 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"); |
| 63 | /* 2. serialisation round trip through PDLAW1 */ |
| 64 | 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"); |
| 65 | 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"); |
| 66 | { uint8_t buf2[8192]; size_t n2 = pd0_rel_write(&r1, buf2, sizeof buf2); CHECK(n2 == n && !memcmp(buf, buf2, n), "re-serialisation identical"); } |
| 67 | /* 3. the compact theory beats a memoriser of the same records on held-out transitions */ |
| 68 | 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)"); |
| 69 | pd0_rel rc = best->rel; int64_t theory = heldout_nrmse(pd0_rel_rollout_fn, pd0_rel_predict_fn, &rc, held, nh); |
| 70 | static pd0_transition fitt[4096]; uint32_t nf = 0; { pd0_learner *C = pd0_learner_new(&d, 7); (void)C; pd0_learner_free(C); } |
| 71 | /* memoriser over the same FIT episodes, rebuilt from the same stream */ |
| 72 | { 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]; } } } |
| 73 | pd0_memoriser m = { fitt, nf, 2 }; int64_t memo = heldout_nrmse(pd0_memoriser_rollout, pd0_memoriser_predict, &m, held, nh); |
| 74 | 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)); |
| 75 | CHECK(theory < 20000, "theory within the in-box bound on held-out"); |
| 76 | CHECK(theory * 4 < memo, "memoriser loses to the compact theory on held-out"); |
| 77 | /* 4. deterministic seeded experiments and a discriminating proposal between candidate and null */ |
| 78 | { 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"); |
| 79 | 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"); |
| 80 | 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"); |
| 81 | 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"); } |
| 82 | pd0_learner_free(A); pd0_learner_free(B); } |
| 83 | /* 5. withholding: HOLDOUT/TRIAL/REP records are refused by the learner */ |
| 84 | { 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); } |
| 85 | /* 6. null world: no candidate ranks above the null */ |
| 86 | { 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); |
| 87 | /* i.i.d. draws: "pull to the mean" (next = 0) predicts better than "no change", so MDL may rank it above the |
| 88 | * empty relation; what must hold is that no candidate predicts within the law bounds (the ladder T3 then |
| 89 | * refuses it, NC-1). The strongest correlation is regression to the mean (var_a == var_b), which T2 accepts. */ |
| 90 | 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)); |
| 91 | 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"); |
| 92 | 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); } |
| 93 | /* v2 additions (rollout ranking, flag, prior, clamp) */ |
| 94 | { pd0_learner *A = pd0_learner_new(&d, 7); feed(A, 11, 60, 0, held, &nh, 4096); pd0_learner_fit(A); |
| 95 | const pd0l_candidate *b0 = pd0_learner_candidate(A, 0); |
| 96 | CHECK(!b0->is_null && !b0->hidden_state_suspected && !b0->hidden_by_rollout, "v2: no hidden-state flag on the fully observed cubic world"); |
| 97 | CHECK(b0->select_roll_nrmse_micro < 20000, "v2: best candidate rolls out 20 steps inside the in-box bound on SELECT"); |
| 98 | CHECK(b0->rank_bits >= b0->mdl_bits, "v2: rank_bits adds the rollout code length to mdl_bits"); |
| 99 | for (int i = 1; i < pd0_learner_n_candidates(A); i++) CHECK(pd0_learner_candidate(A, i - 1)->rank_bits <= pd0_learner_candidate(A, i)->rank_bits, "v2: candidates sorted by rank_bits"); |
| 100 | /* a deliberately wrong prior (largest coefficient sign-flipped) is added, flagged, and ranked below the fitted best */ |
| 101 | pd0_rel w = b0->rel; int bm = 0, be = 0, bt = 0; for (int e = 0; e < w.n_equations; e++) for (int t = 0; t < w.eq[e].n_terms; t++) if (llabs(w.eq[e].coef[t]) > bm) { bm = (int)llabs(w.eq[e].coef[t]); be = e; bt = t; } |
| 102 | w.eq[be].coef[bt] = -w.eq[be].coef[bt]; |
| 103 | pd0_learner_set_prior(A, &w); pd0_learner_fit(A); int seen = 0; for (int i = 0; i < pd0_learner_n_candidates(A); i++) if (pd0_learner_candidate(A, i)->is_prior) { seen = 1; CHECK(i > 0, "v2: the wrong prior does not outrank the fitted best"); } |
| 104 | CHECK(seen, "v2: the injected prior is a ranked candidate"); CHECK(pd0_learner_candidate(A, 0)->is_null == 0 && !pd0_learner_candidate(A, 0)->is_prior, "v2: fitted best stays rank 0 with a wrong prior present"); |
| 105 | pd0_learner_set_prior(A, NULL); pd0_learner_fit(A); seen = 0; for (int i = 0; i < pd0_learner_n_candidates(A); i++) seen += pd0_learner_candidate(A, i)->is_prior; CHECK(seen == 0, "v2: set_prior(NULL) clears the prior"); |
| 106 | /* a diverging relation (s0 += 50 * s0) cannot overflow: the rollout clamps */ |
| 107 | pd0_rel dv; memset(&dv, 0, sizeof dv); dv.n_vars = 2; dv.n_channels = 1; dv.n_equations = 1; dv.eq[0].target = 0; dv.eq[0].n_terms = 1; dv.eq[0].coef[0] = 50000000; dv.eq[0].expo[0][0] = 3; |
| 108 | int64_t init[2] = { 2000000, 0 }, out[40]; pd0_step st[20]; for (int s = 0; s < 20; s++) { st[s].channel = 0; st[s].value = 0; } pd0_learner_rollout(&dv, init, st, 20, out); |
| 109 | int inb = 1; for (int s = 0; s < 40; s++) if (llabs(out[s]) > PD0L_ROLL_CLAMP) inb = 0; CHECK(inb, "v2: a diverging rollout is clamped to the declared bound"); |
| 110 | pd0_learner_free(A); } |
| 111 | printf("checks %d, failures %d\n", checks, fails); printf("PHYSICS0_LEARNER2_UNIT: %s\n", fails ? "FAIL" : "PASS"); return fails ? 1 : 0; |
| 112 | } |