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 / learner_v2 / pd0_learner.h
| 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 learner v2 (adaptive-intelligence milestone 3, gap G3). A NEW version beside the |
| 18 | * seal-1 learner (src/physics0/learner is frozen and untouched). Differences from v1: |
| 19 | * (1) candidates carry a 20-step rollout error on the SELECT episodes and the ranking |
| 20 | * score adds its code length; (2) the hidden-state flag also fires on rollout/one-step |
| 21 | * inconsistency; (3) an optional prior relation can be injected as a ranked candidate. |
| 22 | * Everything else is the v1 text. |
| 23 | * |
| 24 | * PD-0 learner (Physics-0 Discovery Engine, Direction 4): the first genuine |
| 25 | * learner. It sees only the describe record and PD0REC1 observation records |
| 26 | * the harness forwards (FIT and SELECT episodes), fits compact one-step |
| 27 | * relations over a bounded monomial vocabulary, keeps several candidates, |
| 28 | * and emits evidence for the independent ladder checker. It never declares |
| 29 | * a ladder state, never reads HOLDOUT/TRIAL/REP raw records, holds no file, |
| 30 | * no generator symbol, no level identity. Formats come from pd0_fmt.h |
| 31 | * (byte layouts only). Floating point is used only inside fitting; every |
| 32 | * serialised number is a micro-unit integer. */ |
| 33 | #ifndef PD0_LEARNER_H |
| 34 | #define PD0_LEARNER_H |
| 35 | #include "pd0_fmt.h" |
| 36 | |
| 37 | #define PD0L_MAX_TRANS 65536u |
| 38 | #define PD0L_MAX_LIB 128u |
| 39 | #define PD0L_MAX_CAND 8 |
| 40 | #define PD0L_MAX_SEL 10u /* terms per equation the search may select */ |
| 41 | #define PD0L_MAX_FORBID 4096u |
| 42 | #define PD0L_ROLL_H 20u /* rollout horizon on SELECT episodes (the ladder uses 20-step rollouts too) */ |
| 43 | #define PD0L_ROLL_CLAMP 100000000 /* 100 units: a diverging rollout is clamped here so integer arithmetic cannot overflow */ |
| 44 | #ifndef PD0L_DET_BASE_EP |
| 45 | #define PD0L_DET_BASE_EP 32u /* episodes in the detector's reference block */ |
| 46 | #endif |
| 47 | #ifndef PD0L_DET_SLACK |
| 48 | #define PD0L_DET_SLACK 0.3 /* CUSUM allowance (nats): an episode must be about 1.35x worse than the reference error to push the sum up; tuned on dev seeds 101-150 and 201-250, never the evaluation seeds 1-25 */ |
| 49 | #endif |
| 50 | #ifndef PD0L_DET_CLIP |
| 51 | #define PD0L_DET_CLIP 5.0 /* per-episode log-ratio cap: one episode alone can never raise an alarm */ |
| 52 | #endif |
| 53 | #ifndef PD0L_DET_H |
| 54 | #define PD0L_DET_H 8.0 /* CUSUM alarm threshold */ |
| 55 | #endif |
| 56 | #ifndef PD0L_DET_FLOOR |
| 57 | #define PD0L_DET_FLOOR 0.0001 /* units: floor of the reference residual scale, so a noise-free exact model has a finite scale */ |
| 58 | #endif |
| 59 | |
| 60 | typedef struct { |
| 61 | uint8_t n_obs, n_channels; |
| 62 | int64_t dt_micro; |
| 63 | int64_t chan_min[PD0_MAX_CHAN], chan_max[PD0_MAX_CHAN]; |
| 64 | int64_t reset_min[PD0_MAX_OBS], reset_max[PD0_MAX_OBS]; /* per variable (PD0DESC2); a one-pair describe is broadcast by the harness */ |
| 65 | uint32_t episode_max_steps, budget_steps, budget_episodes; |
| 66 | } pd0l_desc; |
| 67 | |
| 68 | typedef struct { |
| 69 | pd0_rel rel; /* serialisable relation (coefficients in micro) */ |
| 70 | uint32_t bits; /* description_bits (spec 6.2 rule) */ |
| 71 | uint32_t size; /* |R| */ |
| 72 | int64_t fit_nrmse_micro; /* one-step NRMSE on FIT (integer evaluator) */ |
| 73 | int64_t select_nrmse_micro; /* one-step NRMSE on SELECT (integer evaluator) */ |
| 74 | double mdl_bits; /* description bits + residual code length on SELECT */ |
| 75 | double lag1_autocorr[PD0_MAX_OBS]; /* residual lag-1 autocorrelation on FIT per variable */ |
| 76 | int64_t select_roll_nrmse_micro; /* v2: 20-step rollout NRMSE over the SELECT episodes (latent starts at 0) */ |
| 77 | double roll_bits; /* v2: residual code length of those rollouts (added to mdl_bits to give rank_bits) */ |
| 78 | double rank_bits; /* v2: mdl_bits + roll_bits, the ranking score */ |
| 79 | int hidden_by_rollout; /* v2: rollout error is large and out of proportion to the one-step error */ |
| 80 | int hidden_state_suspected; /* residuals carry memory the vocabulary cannot express (v1 rule OR v2 rollout rule) */ |
| 81 | int is_prior; /* v2: the injected prior relation */ |
| 82 | int is_old; /* v2.1: the model held before a detected change, kept as a rival candidate */ |
| 83 | int is_null; |
| 84 | } pd0l_candidate; |
| 85 | |
| 86 | typedef struct pd0_learner pd0_learner; |
| 87 | |
| 88 | pd0_learner *pd0_learner_new(const pd0l_desc *d, uint64_t seed); |
| 89 | void pd0_learner_free(pd0_learner *L); |
| 90 | /* a FIT or SELECT record (tag TAG_FIT / TAG_SELECT); others are refused (-1) */ |
| 91 | int pd0_learner_observe(pd0_learner *L, const pd0_rec *r, uint8_t tag); |
| 92 | /* the records of an episode that was TRIAL/REP and is now FIT after a refutation */ |
| 93 | uint32_t pd0_learner_n_transitions(const pd0_learner *L, uint8_t tag); |
| 94 | /* exploration: seeded random reset + schedule inside the describe bounds */ |
| 95 | void pd0_learner_explore(pd0_learner *L, uint32_t episode_index, int64_t *reset, pd0_step *steps, uint32_t n_steps); |
| 96 | /* fit: rebuilds the candidate set from FIT, ranked by MDL on SELECT. returns count (>= 1: the null is always present) */ |
| 97 | /* v2: inject (rel != NULL) or clear (NULL) a prior relation; fit() ranks it with the fitted candidates on the same SELECT data */ |
| 98 | void pd0_learner_set_prior(pd0_learner *L, const pd0_rel *rel); |
| 99 | /* v2.1: change detection and bounded forgetting from admissible observations only. When on, fit() first runs a residual |
| 100 | * detector (CUSUM on the per-episode one-step prediction error of a reference model fitted on the first PD0L_DET_BASE_EP |
| 101 | * episodes, scaled by that model's error on those episodes' SELECT data). On an alarm the fit window starts at the estimated change |
| 102 | * episode (older episodes are forgotten) and the model held before the change stays in the ranking as a rival candidate. */ |
| 103 | typedef struct { |
| 104 | int enabled, alarm; |
| 105 | uint32_t n_ep, base_ep; /* episodes seen; episodes in the reference block (0 if the detector had too little data) */ |
| 106 | uint32_t alarm_ep, change_ep; /* 0-based episode index of the alarm and of the estimated first changed episode (valid if alarm) */ |
| 107 | double cusum_max; /* largest CUSUM value reached over the monitored episodes */ |
| 108 | int old_kept; /* the pre-change model was non-null and entered the ranking */ |
| 109 | } pd0l_detect; |
| 110 | void pd0_learner_set_forgetting(pd0_learner *L, int on); |
| 111 | const pd0l_detect *pd0_learner_detection(const pd0_learner *L); |
| 112 | int pd0_learner_fit(pd0_learner *L); |
| 113 | int pd0_learner_n_candidates(const pd0_learner *L); |
| 114 | const pd0l_candidate *pd0_learner_candidate(const pd0_learner *L, int i); /* 0 = best */ |
| 115 | /* the strongest (variable or intervention, one-step change) correlation over all visible OK step |
| 116 | * records, with a permutation test (recorded seed). returns 0 or -1 (too few records). */ |
| 117 | int pd0_learner_correlation(const pd0_learner *L, uint64_t shuffle_seed, uint32_t n_shuffles, pd0_corr *out); |
| 118 | /* planner stand-in (Direction 5 owns the real one): among n_random seeded schedules inside the |
| 119 | * bounds, not in the forbidden hash list, pick the one where the hypotheses disagree most. |
| 120 | * hyps[0] is the candidate. returns divergence in micro (D * 1e6), or -1 if none (NO_DISCRIMINATING_EXPERIMENT). */ |
| 121 | int64_t pd0_learner_propose(const pd0_learner *L, const pd0_rel *hyps, int n_hyp, uint64_t planner_seed, uint32_t n_random, |
| 122 | const uint8_t (*forbidden)[PD0_HASH], uint32_t n_forbidden, pd0_exp *out); |
| 123 | /* a random schedule from a named stream (used for the random half of replication) */ |
| 124 | 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); |
| 125 | /* prediction interface over a relation (observed variables in and out; latent starts at 0) */ |
| 126 | void pd0_learner_rollout(const pd0_rel *rel, const int64_t *init, const pd0_step *steps, uint32_t n, int64_t *out); |
| 127 | /* one-step NRMSE of rel over the learner's records of a tag (integer evaluator, latent carried per episode) */ |
| 128 | int64_t pd0_learner_onestep_nrmse(const pd0_learner *L, const pd0_rel *rel, uint8_t tag); |
| 129 | /* human report (JSON-ish, non-canonical) */ |
| 130 | size_t pd0_learner_report(const pd0_learner *L, char *buf, size_t cap); |
| 131 | #endif |