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 / src / physics0 / pd0_gen.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 | /* PD-0 hidden generators: spec section 4.1 (physics main @ 5bd2b04, revision 3) |
| 18 | * verbatim in integer micro-units. Discrete maps: the law is the map written |
| 19 | * here, not the differential equation it resembles. |
| 20 | * Build with -DPD0_MUTANT_SIGN to flip the sign of the spring term in L1 |
| 21 | * (NC-3 style mutation: the oracle check must then FAIL). -DPD0_MUTANT_LATENT_SIGN flips the sign of m*h in L6. */ |
| 22 | #include "physics0/pd0_gen.h" |
| 23 | #include "physics0/pd0_guard.h" |
| 24 | |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | |
| 28 | #define U(x) ((int64_t)((x) * 1000000.0)) |
| 29 | |
| 30 | static const char *NAMES[PD0_LEVELS] = {"L0", "L1", "L2", "L3", "L4", "L5", "L6", "NULL"}; |
| 31 | const char *pd0_gen_level_name(int level) { return (level >= 0 && level < PD0_LEVELS) ? NAMES[level] : "?"; } |
| 32 | |
| 33 | /* THE one table of declared constant ranges (spec 4.1), in DRAW ORDER. The |
| 34 | * generator draws from it and the oracle's declared-range check reads it. */ |
| 35 | static const pd0_crange T_L1[] = {{"k", U(1.0), U(4.0)}}; |
| 36 | static const pd0_crange T_L2[] = {{"k", U(1.0), U(9.0)}, {"c", U(0.2), U(2.0)}}; |
| 37 | static const pd0_crange T_L3[] = {{"k", U(1.0), U(4.0)}, {"g", U(0.5), U(2.0)}}; |
| 38 | static const pd0_crange T_L4[] = {{"k", U(1.0), U(4.0)}, {"b", U(0.5), U(2.0)}}; |
| 39 | /* rev 3: order k, w, m, q */ |
| 40 | static const pd0_crange T_L6[] = {{"k", U(3.0), U(5.0)}, {"w", U(0.5), U(1.0)}, {"m", U(1.0), U(2.0)}, {"q", U(1.0), U(2.0)}}; |
| 41 | |
| 42 | int pd0_gen_const_table(int level, const pd0_crange **t) { |
| 43 | switch (level) { |
| 44 | case PD0_L1: *t = T_L1; return 1; |
| 45 | case PD0_L2: case PD0_L5: *t = T_L2; return 2; |
| 46 | case PD0_L3: *t = T_L3; return 2; |
| 47 | case PD0_L4: *t = T_L4; return 2; |
| 48 | case PD0_L6: *t = T_L6; return 4; |
| 49 | default: *t = NULL; return 0; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /* reset box per observed variable (spec 4 table notes, rev 3): [-2, 2] except |
| 54 | * L0 s1 [-0.2, 0.2] and L4 both [-1, 1]. */ |
| 55 | void pd0_gen_reset_box(int level, int var, int64_t *lo, int64_t *hi) { |
| 56 | int64_t b = U(2.0); |
| 57 | if (level == PD0_L0 && var == 1) b = U(0.2); |
| 58 | if (level == PD0_L4) b = U(1.0); |
| 59 | *lo = -b; *hi = b; |
| 60 | } |
| 61 | |
| 62 | /* scoring boxes (spec 6.1, rev 3): in-box = the reset box, extrapolation = 1.5 x it */ |
| 63 | void pd0_gen_score_box(int level, int var, int extrap, int64_t *lo, int64_t *hi) { |
| 64 | pd0_gen_reset_box(level, var, lo, hi); |
| 65 | if (extrap) { *lo = (*lo * 3) / 2; *hi = (*hi * 3) / 2; } |
| 66 | } |
| 67 | |
| 68 | int pd0_gen_desc(int level, pd0_desc *d) { |
| 69 | memset(d, 0, sizeof *d); |
| 70 | d->n_obs = 2; d->n_channels = 1; |
| 71 | for (int v = 0; v < PD0_MAX_OBS; v++) pd0_gen_reset_box(level, v, &d->reset_min[v], &d->reset_max[v]); /* trimmed to n_obs below */ |
| 72 | d->episode_max_steps = PD0_EPISODE_STEPS; |
| 73 | d->budget_steps = 3000; d->budget_episodes = 300; |
| 74 | d->dt_micro = U(0.05); |
| 75 | d->chan_min[0] = -U(2.0); d->chan_max[0] = U(2.0); |
| 76 | switch (level) { |
| 77 | case PD0_L0: /* rev 3: 20 steps per episode, 3000 steps = 150 episodes, pushes [-0.1, 0.1] */ |
| 78 | d->dt_micro = U(1.0); d->chan_min[0] = -U(0.1); d->chan_max[0] = U(0.1); |
| 79 | d->episode_max_steps = 20; d->budget_episodes = 150; |
| 80 | break; |
| 81 | case PD0_L1: case PD0_L2: case PD0_L4: break; |
| 82 | case PD0_L3: d->n_obs = 4; d->n_channels = 2; d->chan_min[1] = -U(2.0); d->chan_max[1] = U(2.0); break; |
| 83 | case PD0_L5: case PD0_L6: d->budget_steps = 8000; break; |
| 84 | case PD0_LEVEL_NULL: break; /* same interface and budget as L2 (NC-1) */ |
| 85 | default: return -1; |
| 86 | } |
| 87 | for (int v = d->n_obs; v < PD0_MAX_OBS; v++) d->reset_min[v] = d->reset_max[v] = 0; /* unused slots stay zero (round trip) */ |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | int pd0_gen_init_cap(pd0_gen *g, int level, uint64_t seed, int redraw_cap) { |
| 92 | memset(g, 0, sizeof *g); |
| 93 | g->level = level; |
| 94 | pd0_rng c; |
| 95 | pd0_stream(&c, seed, "const"); |
| 96 | const pd0_crange *t; |
| 97 | int n = pd0_gen_const_table(level, &t); |
| 98 | for (int i = 0; i < n; i++) g->k[i] = pd0_const(&c, t[i].lo, t[i].hi); /* draw order = table order */ |
| 99 | switch (level) { |
| 100 | case PD0_L0: case PD0_L1: case PD0_L2: case PD0_L3: case PD0_L4: break; |
| 101 | case PD0_L5: g->sigma_obs = U(0.02); break; |
| 102 | case PD0_L6: |
| 103 | g->n_hidden = 1; |
| 104 | /* spec 4.1 rev 3: while m*q/w > 0.7*k redraw m then q from the same stream. |
| 105 | * Exact integer test (no rounding): m*q/w > 0.7*k <=> 10*M*Q > 7*K*W in micro units. */ |
| 106 | while (10 * g->k[2] * g->k[3] > 7 * g->k[0] * g->k[1]) { |
| 107 | if (g->redraws >= redraw_cap) { g->invalid = 1; return -1; } /* reached the cap: invalid seed */ |
| 108 | g->k[2] = pd0_const(&c, t[2].lo, t[2].hi); |
| 109 | g->k[3] = pd0_const(&c, t[3].lo, t[3].hi); |
| 110 | g->redraws++; |
| 111 | } |
| 112 | break; |
| 113 | case PD0_LEVEL_NULL: g->sigma_null = U(0.5); break; |
| 114 | default: return -1; |
| 115 | } |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | int pd0_gen_init(pd0_gen *g, int level, uint64_t seed) { return pd0_gen_init_cap(g, level, seed, PD0_L6_REDRAW_CAP); } |
| 120 | |
| 121 | static int oob(int64_t v) { return !pd0_guard_in_box(v); } |
| 122 | |
| 123 | int pd0_gen_step(const pd0_gen *g, const int64_t *s, int64_t h, const int64_t *u, int64_t dt, |
| 124 | int64_t *o, int64_t *h_out, int64_t *obs, pd0_rng *noise, pd0_rng *null) { |
| 125 | const int64_t k = g->k[0], c = g->k[1]; |
| 126 | int64_t hn = h; |
| 127 | int n = 2; |
| 128 | switch (g->level) { |
| 129 | case PD0_L0: |
| 130 | o[0] = s[0] + s[1]; |
| 131 | o[1] = s[1] + u[0]; |
| 132 | break; |
| 133 | case PD0_L1: { |
| 134 | #ifdef PD0_MUTANT_SIGN |
| 135 | int64_t spring = pd0_mul(k, s[0]); |
| 136 | #else |
| 137 | int64_t spring = -pd0_mul(k, s[0]); |
| 138 | #endif |
| 139 | o[0] = s[0] + pd0_mul(s[1], dt); |
| 140 | o[1] = s[1] + pd0_mul(spring + u[0], dt); |
| 141 | break; |
| 142 | } |
| 143 | case PD0_L2: case PD0_L5: |
| 144 | o[0] = s[0] + pd0_mul(s[1], dt); |
| 145 | o[1] = s[1] + pd0_mul(-pd0_mul(k, s[0]) - pd0_mul(c, s[1]) + u[0], dt); |
| 146 | break; |
| 147 | case PD0_L3: { |
| 148 | const int64_t gg = g->k[1]; |
| 149 | n = 4; |
| 150 | o[0] = s[0] + pd0_mul(s[1], dt); |
| 151 | o[1] = s[1] + pd0_mul(-pd0_mul(k, s[0]) + pd0_mul(gg, s[2] - s[0]) + u[0], dt); |
| 152 | o[2] = s[2] + pd0_mul(s[3], dt); |
| 153 | o[3] = s[3] + pd0_mul(-pd0_mul(k, s[2]) - pd0_mul(gg, s[2] - s[0]) + u[1], dt); |
| 154 | break; |
| 155 | } |
| 156 | case PD0_L4: { |
| 157 | const int64_t b = g->k[1]; |
| 158 | int64_t cube = pd0_mul(pd0_mul(s[0], s[0]), s[0]); |
| 159 | o[0] = s[0] + pd0_mul(s[1], dt); |
| 160 | o[1] = s[1] + pd0_mul(-pd0_mul(k, s[0]) - pd0_mul(b, cube) + u[0], dt); |
| 161 | break; |
| 162 | } |
| 163 | case PD0_L6: { |
| 164 | const int64_t w = g->k[1], m = g->k[2], q = g->k[3]; /* draw order k, w, m, q */ |
| 165 | o[0] = s[0] + pd0_mul(s[1], dt); |
| 166 | #ifdef PD0_MUTANT_LATENT_SIGN |
| 167 | const int64_t lat = -pd0_mul(m, h); /* wrong latent sign: the spec says +m*h */ |
| 168 | #else |
| 169 | const int64_t lat = pd0_mul(m, h); |
| 170 | #endif |
| 171 | o[1] = s[1] + pd0_mul(-pd0_mul(k, s[0]) + lat + u[0], dt); |
| 172 | hn = h + pd0_mul(-pd0_mul(w, h) + pd0_mul(q, s[0]), dt); |
| 173 | break; |
| 174 | } |
| 175 | case PD0_LEVEL_NULL: |
| 176 | /* nothing depends on anything: fresh draws, state and intervention ignored */ |
| 177 | o[0] = pd0_noise(null, g->sigma_null); |
| 178 | o[1] = pd0_noise(null, g->sigma_null); |
| 179 | break; |
| 180 | default: return -1; |
| 181 | } |
| 182 | *h_out = hn; |
| 183 | int bad = oob(hn); |
| 184 | for (int i = 0; i < n; i++) bad |= oob(o[i]); |
| 185 | for (int i = 0; i < n; i++) obs[i] = o[i] + (g->sigma_obs ? pd0_noise(noise, g->sigma_obs) : 0); |
| 186 | return bad; |
| 187 | } |
| 188 | |
| 189 | void pd0_gen_observe_reset(const pd0_gen *g, const int64_t *s, int64_t *obs, pd0_rng *noise) { |
| 190 | int n = g->level == PD0_L3 ? 4 : 2; |
| 191 | for (int i = 0; i < n; i++) obs[i] = s[i] + (g->sigma_obs ? pd0_noise(noise, g->sigma_obs) : 0); |
| 192 | } |
| 193 | |
| 194 | /* term helpers: variable index v (observed then latent), channel index ch */ |
| 195 | static void add_var(pd0_eq *e, int64_t coef, int v) { pd0_term t = {coef, {0}}; t.ex[v] = 1; e->t[e->n_terms++] = t; } |
| 196 | static void add_ch(pd0_eq *e, int64_t coef, int ch, int n_vars) { (void)n_vars; pd0_term t = {coef, {0}}; t.ex[PD0_MAX_VARS + ch] = 1; e->t[e->n_terms++] = t; } |
| 197 | |
| 198 | int pd0_gen_true_relation(const pd0_gen *g, int64_t dt, pd0_relation *r) { |
| 199 | memset(r, 0, sizeof *r); |
| 200 | r->n_channels = g->level == PD0_L3 ? 2 : 1; |
| 201 | r->n_vars = g->level == PD0_L3 ? 4 : 2; |
| 202 | if (g->level == PD0_L6) { r->n_vars = 3; r->n_latent = 1; } |
| 203 | if (g->level == PD0_LEVEL_NULL) { r->n_eq = 0; return 0; } |
| 204 | const int nv = r->n_vars; |
| 205 | const int64_t k = g->k[0]; |
| 206 | pd0_eq *e0 = &r->eq[0], *e1 = &r->eq[1]; |
| 207 | e0->target = 0; e1->target = 1; |
| 208 | r->n_eq = 2; |
| 209 | switch (g->level) { |
| 210 | case PD0_L0: |
| 211 | add_var(e0, PD0_MICRO, 1); |
| 212 | add_ch(e1, PD0_MICRO, 0, nv); |
| 213 | break; |
| 214 | case PD0_L1: |
| 215 | add_var(e0, dt, 1); |
| 216 | #ifdef PD0_MUTANT_SIGN |
| 217 | add_var(e1, -pd0_mul(k, dt), 0); /* the reference law stays the spec's: that is the point */ |
| 218 | #else |
| 219 | add_var(e1, -pd0_mul(k, dt), 0); |
| 220 | #endif |
| 221 | add_ch(e1, dt, 0, nv); |
| 222 | break; |
| 223 | case PD0_L2: case PD0_L5: |
| 224 | add_var(e0, dt, 1); |
| 225 | add_var(e1, -pd0_mul(k, dt), 0); add_var(e1, -pd0_mul(g->k[1], dt), 1); add_ch(e1, dt, 0, nv); |
| 226 | break; |
| 227 | case PD0_L3: { |
| 228 | const int64_t gg = g->k[1]; |
| 229 | pd0_eq *e2 = &r->eq[2], *e3 = &r->eq[3]; |
| 230 | e2->target = 2; e3->target = 3; r->n_eq = 4; |
| 231 | add_var(e0, dt, 1); |
| 232 | add_var(e1, -pd0_mul(k + gg, dt), 0); add_var(e1, pd0_mul(gg, dt), 2); add_ch(e1, dt, 0, nv); |
| 233 | add_var(e2, dt, 3); |
| 234 | add_var(e3, pd0_mul(gg, dt), 0); add_var(e3, -pd0_mul(k + gg, dt), 2); add_ch(e3, dt, 1, nv); |
| 235 | break; |
| 236 | } |
| 237 | case PD0_L4: { |
| 238 | add_var(e0, dt, 1); |
| 239 | add_var(e1, -pd0_mul(k, dt), 0); |
| 240 | pd0_term cube = {-pd0_mul(g->k[1], dt), {0}}; cube.ex[0] = 3; e1->t[e1->n_terms++] = cube; |
| 241 | add_ch(e1, dt, 0, nv); |
| 242 | break; |
| 243 | } |
| 244 | case PD0_L6: { |
| 245 | pd0_eq *eh = &r->eq[2]; |
| 246 | eh->target = 2; r->n_eq = 3; |
| 247 | add_var(e0, dt, 1); |
| 248 | add_var(e1, -pd0_mul(k, dt), 0); add_var(e1, pd0_mul(g->k[2], dt), 2); add_ch(e1, dt, 0, nv); /* m = k[2] */ |
| 249 | add_var(eh, -pd0_mul(g->k[1], dt), 2); add_var(eh, pd0_mul(g->k[3], dt), 0); /* w = k[1], q = k[3] */ |
| 250 | break; |
| 251 | } |
| 252 | default: return -1; |
| 253 | } |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | int pd0_gen_true_size(int level) { |
| 258 | static const int S[PD0_LEVELS] = {2, 3, 4, 8, 4, 4, 7, 0}; |
| 259 | return (level >= 0 && level < PD0_LEVELS) ? S[level] : -1; |
| 260 | } |