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 / planner / pd0_planner.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 experiment planner, see pd0_planner.h. Integer arithmetic only. */ |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include "pd0_planner.h" |
| 21 | #include "pd0_rng.h" |
| 22 | |
| 23 | __extension__ typedef __int128 p_i128; |
| 24 | |
| 25 | #define P_MAGIC "PD0PLN1" |
| 26 | |
| 27 | typedef struct { |
| 28 | pd0_exp exp; |
| 29 | int64_t d_micro, info_micro, cost_micro; |
| 30 | int eligible; /* 0 if the hash is already in the prior set */ |
| 31 | } p_cand; |
| 32 | |
| 33 | /* ---- helpers ---- */ |
| 34 | |
| 35 | static int64_t p_unit_in(pd0_rng *r, int64_t lo, int64_t hi) { |
| 36 | int64_t u = pd0_rng_unit(r); |
| 37 | p_i128 span = (p_i128)(hi - lo) * u; |
| 38 | return lo + (int64_t)(span / 1000000); |
| 39 | } |
| 40 | |
| 41 | static int p_bounds_ok(const pd0p_bounds *b) { |
| 42 | if (b->n_obs == 0 || b->n_obs > PD0_MAX_OBS) return 0; |
| 43 | if (b->n_channels == 0 || b->n_channels > PD0_MAX_CHAN) return 0; |
| 44 | for (int i = 0; i < b->n_obs; i++) if (b->reset_min[i] > b->reset_max[i]) return 0; |
| 45 | for (int i = 0; i < b->n_channels; i++) if (b->chan_min[i] > b->chan_max[i]) return 0; |
| 46 | return 1; |
| 47 | } |
| 48 | |
| 49 | static int p_sched_in_bounds(const pd0p_bounds *b, const pd0_exp *e) { |
| 50 | if (e->n_obs != b->n_obs || e->n_steps == 0 || e->n_steps > PD0_MAX_STEPS) return 0; |
| 51 | if (b->episode_max_steps && e->n_steps > b->episode_max_steps) return 0; |
| 52 | for (int i = 0; i < e->n_obs; i++) |
| 53 | if (e->reset[i] < b->reset_min[i] || e->reset[i] > b->reset_max[i]) return 0; |
| 54 | for (int s = 0; s < e->n_steps; s++) { |
| 55 | uint8_t c = e->steps[s].channel; |
| 56 | if (c >= b->n_channels) return 0; /* passive step (255) is not emitted by the planner */ |
| 57 | if (e->steps[s].value < b->chan_min[c] || e->steps[s].value > b->chan_max[c]) return 0; |
| 58 | } |
| 59 | return 1; |
| 60 | } |
| 61 | |
| 62 | static uint8_t p_steps_for(const pd0p_bounds *b, uint32_t budget_steps) { |
| 63 | uint32_t n = PD0_MAX_STEPS; |
| 64 | if (b->episode_max_steps && b->episode_max_steps < n) n = b->episode_max_steps; |
| 65 | if (budget_steps < n) n = budget_steps; |
| 66 | return (uint8_t)n; |
| 67 | } |
| 68 | |
| 69 | static void p_random_sched(pd0_rng *r, const pd0p_bounds *b, uint8_t n_steps, pd0_exp *e) { |
| 70 | memset(e, 0, sizeof *e); |
| 71 | e->n_obs = b->n_obs; e->n_steps = n_steps; |
| 72 | for (int i = 0; i < b->n_obs; i++) e->reset[i] = p_unit_in(r, b->reset_min[i], b->reset_max[i]); |
| 73 | for (int s = 0; s < n_steps; s++) { |
| 74 | uint8_t c = (uint8_t)(pd0_rng_next(r) % b->n_channels); |
| 75 | e->steps[s].channel = c; |
| 76 | e->steps[s].value = p_unit_in(r, b->chan_min[c], b->chan_max[c]); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /* rollout of every hypothesis; D, info proxy, cost; schedule hash */ |
| 81 | static void p_evaluate(const pd0p_hyps *h, pd0_exp *e, int64_t *d_out, int64_t *info_out, int64_t *cost_out) { |
| 82 | int64_t r = h->residual_sd_micro; |
| 83 | int64_t maxdiff = 0, cost = 0; |
| 84 | p_i128 info = 0; |
| 85 | e->n_hyp = h->n_hyp; |
| 86 | for (int k = 0; k < h->n_hyp; k++) { |
| 87 | const pd0_rel *rel = h->hyp[k]; |
| 88 | int64_t state[PD0_MAX_VARS], next[PD0_MAX_VARS]; |
| 89 | memset(state, 0, sizeof state); |
| 90 | for (int i = 0; i < e->n_obs; i++) state[i] = e->reset[i]; |
| 91 | for (int s = 0; s < e->n_steps; s++) { |
| 92 | pd0_rel_step(rel, state, e->steps[s].channel, e->steps[s].value, next); |
| 93 | for (int i = 0; i < e->n_obs; i++) e->expected[k][s][i] = next[i]; |
| 94 | memcpy(state, next, sizeof state); |
| 95 | } |
| 96 | } |
| 97 | for (int s = 0; s < e->n_steps; s++) { |
| 98 | int64_t stepmax = 0; |
| 99 | for (int a = 0; a < h->n_hyp; a++) |
| 100 | for (int b2 = a + 1; b2 < h->n_hyp; b2++) |
| 101 | for (int i = 0; i < e->n_obs; i++) { |
| 102 | int64_t d = e->expected[a][s][i] - e->expected[b2][s][i]; |
| 103 | if (d < 0) d = -d; |
| 104 | if (d > stepmax) stepmax = d; |
| 105 | } |
| 106 | if (stepmax > maxdiff) maxdiff = stepmax; |
| 107 | p_i128 q = ((p_i128)stepmax * 1000000) / r; /* stepmax / r in micro */ |
| 108 | info += (q * q) / 1000000; |
| 109 | cost += e->steps[s].value < 0 ? -e->steps[s].value : e->steps[s].value; |
| 110 | } |
| 111 | *d_out = (int64_t)(((p_i128)maxdiff * 1000000) / r); |
| 112 | e->divergence_micro = *d_out; |
| 113 | if (info > INT64_MAX) info = INT64_MAX; |
| 114 | *info_out = (int64_t)info; |
| 115 | *cost_out = cost; |
| 116 | pd0_schedule_hash(e->n_obs, e->reset, e->n_steps, e->steps, e->schedule_hash); |
| 117 | } |
| 118 | |
| 119 | /* spec 8 ordering: higher D, then lower cost, then lexicographic hash */ |
| 120 | static int p_better(const p_cand *a, const p_cand *b) { |
| 121 | if (a->eligible != b->eligible) return a->eligible > b->eligible; |
| 122 | if (a->d_micro != b->d_micro) return a->d_micro > b->d_micro; |
| 123 | if (a->cost_micro != b->cost_micro) return a->cost_micro < b->cost_micro; |
| 124 | return memcmp(a->exp.schedule_hash, b->exp.schedule_hash, PD0_HASH) < 0; |
| 125 | } |
| 126 | |
| 127 | static void p_sort(p_cand *c, uint32_t n) { /* insertion sort: n is small and the order must be total */ |
| 128 | for (uint32_t i = 1; i < n; i++) { |
| 129 | p_cand tmp; memcpy(&tmp, &c[i], sizeof tmp); |
| 130 | uint32_t j = i; |
| 131 | while (j > 0 && p_better(&tmp, &c[j - 1])) { memcpy(&c[j], &c[j - 1], sizeof tmp); j--; } |
| 132 | memcpy(&c[j], &tmp, sizeof tmp); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | static uint8_t p_outside_middle(const pd0p_domain *dom, const pd0_exp *e) { |
| 137 | for (int i = 0; i < e->n_obs; i++) { |
| 138 | int64_t lo = dom->var_min[i], hi = dom->var_max[i]; |
| 139 | if (hi <= lo) continue; |
| 140 | int64_t q = (hi - lo) / 4, mlo = lo + q, mhi = hi - q; |
| 141 | if (e->reset[i] < mlo || e->reset[i] > mhi) return 1; |
| 142 | for (int s = 0; s < e->n_steps; s++) |
| 143 | if (e->expected[0][s][i] < mlo || e->expected[0][s][i] > mhi) return 1; |
| 144 | } |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | /* ---- public ---- */ |
| 149 | |
| 150 | void pd0p_prior_add(pd0p_prior *p, const uint8_t hash[PD0_HASH]) { |
| 151 | if (p->n < PD0P_MAX_PRIOR) memcpy(p->hash[p->n++], hash, PD0_HASH); |
| 152 | } |
| 153 | |
| 154 | int pd0p_prior_has(const pd0p_prior *p, const uint8_t hash[PD0_HASH]) { |
| 155 | if (!p) return 0; |
| 156 | for (uint32_t i = 0; i < p->n; i++) if (memcmp(p->hash[i], hash, PD0_HASH) == 0) return 1; |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | int pd0p_validate(const pd0p_bounds *b, const pd0p_prior *prior, const pd0_exp *e) { |
| 161 | uint8_t h[PD0_HASH]; |
| 162 | if (!p_bounds_ok(b) || e->n_obs != b->n_obs) return PD0P_ERR_INPUT; |
| 163 | pd0_schedule_hash(e->n_obs, e->reset, e->n_steps, e->steps, h); |
| 164 | if (memcmp(h, e->schedule_hash, PD0_HASH) != 0) return PD0P_ERR_INPUT; |
| 165 | if (!p_sched_in_bounds(b, e)) return PD0P_ERR_BOUNDS; |
| 166 | if (pd0p_prior_has(prior, e->schedule_hash)) return PD0P_ERR_DUPLICATE; |
| 167 | return PD0P_OK; |
| 168 | } |
| 169 | |
| 170 | int pd0p_propose(const pd0p_bounds *b, const pd0p_domain *dom, const pd0p_hyps *h, |
| 171 | uint32_t budget_steps, const pd0p_prior *prior, uint64_t seed, pd0p_proposal *out) { |
| 172 | memset(out, 0, sizeof *out); |
| 173 | out->seed = seed; |
| 174 | if (!p_bounds_ok(b) || !h || h->n_hyp < 2 || h->n_hyp > PD0_MAX_HYP || h->residual_sd_micro <= 0) |
| 175 | return out->status = PD0P_ERR_INPUT; |
| 176 | for (int k = 0; k < h->n_hyp; k++) |
| 177 | if (!h->hyp[k] || h->hyp[k]->n_vars < b->n_obs || h->hyp[k]->n_channels < b->n_channels) |
| 178 | return out->status = PD0P_ERR_INPUT; |
| 179 | if (budget_steps < 2) return out->status = PD0P_ERR_BUDGET; |
| 180 | uint8_t n_steps = p_steps_for(b, budget_steps); |
| 181 | |
| 182 | p_cand *c = calloc(PD0P_CANDIDATES, sizeof *c); |
| 183 | if (!c) return out->status = PD0P_ERR_INPUT; |
| 184 | pd0_rng rng; pd0_rng_stream(&rng, seed, "plan"); |
| 185 | uint32_t skipped = 0; |
| 186 | for (uint32_t i = 0; i < PD0P_CANDIDATES; i++) { |
| 187 | p_random_sched(&rng, b, n_steps, &c[i].exp); |
| 188 | p_evaluate(h, &c[i].exp, &c[i].d_micro, &c[i].info_micro, &c[i].cost_micro); |
| 189 | c[i].eligible = !pd0p_prior_has(prior, c[i].exp.schedule_hash); |
| 190 | if (!c[i].eligible) skipped++; |
| 191 | } |
| 192 | p_sort(c, PD0P_CANDIDATES); |
| 193 | uint32_t refined = 0; |
| 194 | for (uint32_t t = 0; t < PD0P_REFINE_TOP && t < PD0P_CANDIDATES; t++) { |
| 195 | for (uint32_t round = 0; round < PD0P_REFINE_ROUNDS; round++) { |
| 196 | p_cand m; memcpy(&m, &c[t], sizeof m); |
| 197 | uint32_t pos = (uint32_t)(pd0_rng_next(&rng) % (uint32_t)(b->n_obs + n_steps)); |
| 198 | if (pos < b->n_obs) m.exp.reset[pos] = p_unit_in(&rng, b->reset_min[pos], b->reset_max[pos]); |
| 199 | else { |
| 200 | uint32_t s = pos - b->n_obs; |
| 201 | uint8_t ch = (uint8_t)(pd0_rng_next(&rng) % b->n_channels); |
| 202 | m.exp.steps[s].channel = ch; |
| 203 | m.exp.steps[s].value = p_unit_in(&rng, b->chan_min[ch], b->chan_max[ch]); |
| 204 | } |
| 205 | p_evaluate(h, &m.exp, &m.d_micro, &m.info_micro, &m.cost_micro); |
| 206 | m.eligible = !pd0p_prior_has(prior, m.exp.schedule_hash); |
| 207 | if (!m.eligible) { skipped++; continue; } |
| 208 | if (p_better(&m, &c[t])) { memcpy(&c[t], &m, sizeof m); refined++; } |
| 209 | } |
| 210 | } |
| 211 | p_sort(c, PD0P_REFINE_TOP); |
| 212 | out->n_candidates = PD0P_CANDIDATES; |
| 213 | out->n_refined = refined; |
| 214 | out->n_skipped_duplicate = skipped; |
| 215 | if (!c[0].eligible) { free(c); return out->status = PD0P_ERR_DUPLICATE; } |
| 216 | memcpy(&out->exp, &c[0].exp, sizeof out->exp); |
| 217 | out->divergence_micro = c[0].d_micro; |
| 218 | out->info_proxy_micro = c[0].info_micro; |
| 219 | out->cost_micro = c[0].cost_micro; |
| 220 | out->outside_middle = dom ? p_outside_middle(dom, &out->exp) : 0; |
| 221 | free(c); |
| 222 | if (!p_sched_in_bounds(b, &out->exp)) return out->status = PD0P_ERR_BOUNDS; /* cannot happen; defence */ |
| 223 | if (out->divergence_micro < PD0P_MIN_D_MICRO) return out->status = PD0P_NO_DISCRIMINATING_EXPERIMENT; |
| 224 | return out->status = PD0P_OK; |
| 225 | } |
| 226 | |
| 227 | size_t pd0p_prereg_bytes(const pd0p_proposal *p, uint8_t *out, size_t cap) { |
| 228 | pd0_exp e; |
| 229 | if (p->status != PD0P_OK) return 0; |
| 230 | memcpy(&e, &p->exp, sizeof e); |
| 231 | return pd0_exp_write(&e, out, cap); |
| 232 | } |
| 233 | |
| 234 | static void p_put_i64(uint8_t *o, int64_t v) { pd0_put_u64(o, (uint64_t)v); } |
| 235 | |
| 236 | size_t pd0p_plan_write(const pd0p_proposal *p, uint8_t out[PD0P_PLAN_SIZE]) { |
| 237 | uint8_t *o = out; |
| 238 | memcpy(o, P_MAGIC, 7); o[7] = 0; o += 8; |
| 239 | o[0] = 1; o[1] = 0; o += 2; |
| 240 | *o++ = (uint8_t)(p->status & 0xff); |
| 241 | pd0_put_u32(o, p->n_candidates); o += 4; |
| 242 | pd0_put_u32(o, p->n_refined); o += 4; |
| 243 | pd0_put_u64(o, p->seed); o += 8; |
| 244 | p_put_i64(o, p->divergence_micro); o += 8; |
| 245 | p_put_i64(o, p->info_proxy_micro); o += 8; |
| 246 | p_put_i64(o, p->cost_micro); o += 8; |
| 247 | *o++ = p->outside_middle; |
| 248 | memcpy(o, p->exp.schedule_hash, PD0_HASH); o += PD0_HASH; |
| 249 | return (size_t)(o - out); |
| 250 | } |
| 251 | |
| 252 | int pd0p_plan_parse(const uint8_t *in, size_t len, pd0p_proposal *p) { |
| 253 | if (len != PD0P_PLAN_SIZE || memcmp(in, P_MAGIC "\0", 8) != 0) return -1; |
| 254 | if (in[8] != 1 || in[9] != 0) return -1; /* unknown version refused */ |
| 255 | memset(p, 0, sizeof *p); |
| 256 | p->status = (int8_t)in[10]; |
| 257 | p->n_candidates = pd0_get_u32(in + 11); |
| 258 | p->n_refined = pd0_get_u32(in + 15); |
| 259 | p->seed = pd0_get_u64(in + 19); |
| 260 | p->divergence_micro = (int64_t)pd0_get_u64(in + 27); |
| 261 | p->info_proxy_micro = (int64_t)pd0_get_u64(in + 35); |
| 262 | p->cost_micro = (int64_t)pd0_get_u64(in + 43); |
| 263 | p->outside_middle = in[51]; |
| 264 | memcpy(p->exp.schedule_hash, in + 52, PD0_HASH); |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | int pd0p_passive(const pd0p_bounds *b, uint32_t budget_steps, uint64_t stream_seed, uint32_t index, pd0_exp *out) { |
| 269 | if (!p_bounds_ok(b)) return PD0P_ERR_INPUT; |
| 270 | if (budget_steps < 2) return PD0P_ERR_BUDGET; |
| 271 | pd0_rng r; pd0_rng_stream(&r, stream_seed, "passive"); |
| 272 | r.s ^= (uint64_t)index * 0x9E3779B97F4A7C15ull; /* index-th schedule of the stream, deterministic */ |
| 273 | p_random_sched(&r, b, p_steps_for(b, budget_steps), out); |
| 274 | out->n_hyp = 0; |
| 275 | pd0_schedule_hash(out->n_obs, out->reset, out->n_steps, out->steps, out->schedule_hash); |
| 276 | return PD0P_OK; |
| 277 | } |
| 278 | |
| 279 | uint32_t pd0p_outside_fraction_ppm(const uint8_t *flags, uint32_t n) { |
| 280 | uint64_t k = 0; |
| 281 | if (n == 0) return 0; |
| 282 | for (uint32_t i = 0; i < n; i++) k += flags[i] ? 1u : 0u; |
| 283 | return (uint32_t)((k * 1000000ull) / n); |
| 284 | } |