interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / src / physics0 / controls / pd0_controls.c

83 lines · 5647 bytes Raw Blame History
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#include "pd0_controls.h"
18#include "pd0_rng.h"
19#include <string.h>
20
21uint32_t pd0_null_world(uint64_t seed, uint8_t n_obs, uint32_t n_episodes, uint32_t steps, int64_t reset_min, int64_t reset_max, int64_t chan_min, int64_t chan_max, pd0_rec *out, uint32_t cap)
22{
23 pd0_rng nz, sch; pd0_rng_stream(&nz, seed, "null"); pd0_rng_stream(&sch, seed, "score");
24 uint32_t n = 0; uint64_t seq = 0; uint8_t prev[PD0_HASH] = { 0 }; uint8_t buf[512];
25 for (uint32_t e = 0; e < n_episodes; e++) {
26 int64_t st[PD0_MAX_OBS];
27 for (int j = 0; j < n_obs; j++) st[j] = reset_min + ((reset_max - reset_min) * pd0_rng_unit(&sch)) / PD0_MICRO;
28 for (uint32_t s = 0; s <= steps && n < cap; s++) {
29 pd0_rec *r = &out[n]; memset(r, 0, sizeof *r); r->n_obs = n_obs; r->seq = seq++; r->episode = e; r->step_in_episode = s; r->time_micro = (int64_t)s * 50000;
30 memcpy(r->prev_hash, prev, PD0_HASH);
31 if (s == 0) { r->kind = PD0_KIND_RESET; r->channel = PD0_CHAN_NONE; for (int j = 0; j < n_obs; j++) r->before[j] = r->after[j] = st[j]; }
32 else { r->kind = PD0_KIND_STEP; r->channel = 0; r->requested = r->applied = chan_min + ((chan_max - chan_min) * pd0_rng_unit(&sch)) / PD0_MICRO;
33 for (int j = 0; j < n_obs; j++) { r->before[j] = st[j]; st[j] = pd0_rng_noise(&nz, 500000); r->after[j] = st[j]; }
34 if (s == steps) r->status = PD0_ST_EPISODE_END; }
35 pd0_rec_write(r, buf, sizeof buf); memcpy(prev, r->record_hash, PD0_HASH); n++; }
36 }
37 return n;
38}
39void pd0_shuffle_outcomes(pd0_rec *recs, uint32_t n, uint64_t seed)
40{
41 pd0_rng g; pd0_rng_stream(&g, seed, "shuffle"); uint32_t idx[65536], m = 0;
42 for (uint32_t i = 0; i < n && m < 65536; i++) if (recs[i].kind == PD0_KIND_STEP && recs[i].status == PD0_ST_OK) idx[m++] = i;
43 for (uint32_t i = m; i > 1; i--) { uint32_t j = (uint32_t)(pd0_rng_next(&g) % i); int64_t tmp[PD0_MAX_OBS];
44 memcpy(tmp, recs[idx[i - 1]].after, sizeof tmp); memcpy(recs[idx[i - 1]].after, recs[idx[j]].after, sizeof tmp); memcpy(recs[idx[j]].after, tmp, sizeof tmp); }
45 /* keep before = previous after inside each episode so the record stream is still well formed */
46 for (uint32_t i = 1; i < n; i++) if (recs[i].kind == PD0_KIND_STEP && recs[i].episode == recs[i - 1].episode) memcpy(recs[i].before, recs[i - 1].after, sizeof recs[i].before);
47}
48void pd0_rechain(pd0_rec *recs, uint32_t n)
49{
50 uint8_t prev[PD0_HASH] = { 0 }, buf[512];
51 for (uint32_t i = 0; i < n; i++) { memcpy(recs[i].prev_hash, prev, PD0_HASH); pd0_rec_write(&recs[i], buf, sizeof buf); memcpy(prev, recs[i].record_hash, PD0_HASH); }
52}
53void pd0_mut_scale_coef(pd0_rel *r, uint8_t target, const uint8_t *expo, int64_t num, int64_t den)
54{
55 unsigned ne = r->n_vars + r->n_channels;
56 for (int e = 0; e < r->n_equations; e++) if (r->eq[e].target == target) for (int k = 0; k < r->eq[e].n_terms; k++) if (!memcmp(r->eq[e].expo[k], expo, ne)) r->eq[e].coef[k] = (r->eq[e].coef[k] * num) / den;
57 r->description_bits = pd0_rel_bits(r);
58}
59void pd0_mut_pad(pd0_rel *r, uint32_t n_extra, int64_t tiny)
60{
61 unsigned ne = r->n_vars + r->n_channels; uint32_t added = 0;
62 for (int e = 0; e < r->n_equations && added < n_extra; e++) { pd0_eq *q = &r->eq[e];
63 for (unsigned i = 0; i < ne && added < n_extra && q->n_terms < PD0_MAX_TERMS; i++) for (unsigned j = i; j < ne && added < n_extra && q->n_terms < PD0_MAX_TERMS; j++) {
64 uint8_t ex[PD0_MAX_VARS + PD0_MAX_CHAN]; memset(ex, 0, sizeof ex); ex[i]++; ex[j]++; int dup = 0;
65 for (int k = 0; k < q->n_terms; k++) if (!memcmp(q->expo[k], ex, ne)) dup = 1; if (dup) continue;
66 q->coef[q->n_terms] = tiny; memcpy(q->expo[q->n_terms], ex, sizeof ex); q->n_terms++; added++; } }
67 r->description_bits = pd0_rel_bits(r);
68}
69int pd0_memoriser_predict(void *ctx, const int64_t *state, uint8_t chan, int64_t value, int64_t *next)
70{
71 const pd0_memoriser *m = ctx; double best = 1e300; uint32_t bi = 0;
72 for (uint32_t i = 0; i < m->n; i++) { double d = 0; for (int j = 0; j < m->n_obs; j++) { double x = (double)(state[j] - m->t[i].before[j]); d += x * x; }
73 double dv = (double)(value - (m->t[i].chan == chan ? m->t[i].value : 0)); d += dv * dv; if (m->t[i].chan != chan) d += 1e12; if (d < best) { best = d; bi = i; } }
74 for (int j = 0; j < m->n_obs; j++) next[j] = state[j] + (m->t[bi].after[j] - m->t[bi].before[j]);
75 return 0;
76}
77int pd0_memoriser_rollout(void *ctx, const int64_t *init, const pd0_step *steps, uint32_t n, int64_t *out)
78{
79 const pd0_memoriser *m = ctx; int64_t st[PD0_MAX_OBS], nx[PD0_MAX_OBS]; memcpy(st, init, sizeof(int64_t) * m->n_obs);
80 for (uint32_t s = 0; s < n; s++) { pd0_memoriser_predict(ctx, st, steps[s].channel, steps[s].value, nx); memcpy(out + s * m->n_obs, nx, sizeof(int64_t) * m->n_obs); memcpy(st, nx, sizeof nx); }
81 return 0;
82}
83uint32_t pd0_memoriser_stored_numbers(const pd0_memoriser *m) { return m->n * (2u * m->n_obs + 2u); }