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 / algebra / oma_select.c

294 lines · 13578 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/* MA-2 selector, RETIRED for new decisions (TURING K.6/K.7); kept only to
18 * reproduce ma2_select_receipt.json. New selections: src/turing
19 * turing_rank_min_cost / turing.decision.v1. See oma_select.h. */
20#include "algebra/oma_select.h"
21
22#include "algebra/realize_common.h"
23
24#include <math.h>
25#include <stdlib.h>
26#include <string.h>
27
28void oma_sel_init(oma_sel_table *t) { memset(t, 0, sizeof *t); }
29
30/* Flat-object key lookup on one receipt line: "key": value */
31static const char *find_key(const char *line, const char *key) {
32 char pat[64];
33 snprintf(pat, sizeof pat, "\"%s\": ", key);
34 const char *p = strstr(line, pat);
35 return p ? p + strlen(pat) : NULL;
36}
37
38static int kv_num(const char *line, const char *key, double *out) {
39 const char *p = find_key(line, key);
40 if (!p) return -1;
41 char *end;
42 double v = strtod(p, &end);
43 if (end == p) return -1;
44 *out = v;
45 return 0;
46}
47
48static int kv_bool(const char *line, const char *key, int *out) {
49 const char *p = find_key(line, key);
50 if (!p) return -1;
51 if (strncmp(p, "true", 4) == 0) { *out = 1; return 0; }
52 if (strncmp(p, "false", 5) == 0) { *out = 0; return 0; }
53 return -1;
54}
55
56static int kv_str(const char *line, const char *key, char *out, size_t cap) {
57 const char *p = find_key(line, key);
58 if (!p || *p != '"') return -1;
59 p++;
60 const char *e = strchr(p, '"');
61 if (!e || (size_t)(e - p) >= cap) return -1;
62 memcpy(out, p, (size_t)(e - p));
63 out[e - p] = 0;
64 return 0;
65}
66
67int oma_sel_load(oma_sel_table *t, const char *path) {
68 if (!t || !path) return OMA_SEL_E_IO;
69 if (t->nruns >= OMA_SEL_MAX_RUNS) return OMA_SEL_E_FULL;
70 FILE *f = fopen(path, "r");
71 if (!f) return OMA_SEL_E_IO;
72 int run = t->nruns;
73 char line[4096];
74 int in_table = 0, schema_ok = 0;
75 size_t added = 0;
76 t->run_id[run][0] = 0;
77 while (fgets(line, sizeof line, f)) {
78 /* MA3_BENCH_V1: the historical runs recorded under the earlier MA-3 label */
79 if (strstr(line, "\"schema\": \"OMEGA_MIXED_ALGEBRA_MA2_BENCH_V1\"") ||
80 strstr(line, "\"schema\": \"OMEGA_MIXED_ALGEBRA_MA3_BENCH_V1\""))
81 schema_ok = 1;
82 if (!in_table && strstr(line, "\"run_id\": ")) kv_str(line, "run_id", t->run_id[run], sizeof t->run_id[run]);
83 if (strstr(line, "\"cost_table\": [")) { in_table = 1; continue; }
84 if (!in_table) continue;
85 if (strncmp(line, " ]", 3) == 0) break;
86 if (!strstr(line, "{\"n\": ")) continue;
87 if (t->nrows >= OMA_SEL_MAX_ROWS) { fclose(f); return OMA_SEL_E_FULL; }
88 oma_sel_row *r = &t->rows[t->nrows];
89 memset(r, 0, sizeof *r);
90 double n, m, sp;
91 if (kv_num(line, "n", &n) || kv_num(line, "m", &m) || kv_num(line, "sparsity", &sp) ||
92 kv_str(line, "rz", r->rz, sizeof r->rz) || kv_bool(line, "eligible", &r->eligible)) {
93 fclose(f);
94 return OMA_SEL_E_FORMAT;
95 }
96 r->run = run;
97 r->n = (size_t)n;
98 r->m = (size_t)m;
99 r->sparsity = sp;
100 if (kv_bool(line, "verified", &r->verified)) r->verified = 0;
101 if (r->eligible) {
102 double med, mn, nz, pk, pf;
103 if (kv_num(line, "median_ps", &med) || kv_num(line, "min_ps", &mn) || kv_num(line, "noise_rel", &nz) ||
104 kv_num(line, "pack_ps", &pk) || kv_num(line, "pct_floor_best", &pf)) {
105 fclose(f);
106 return OMA_SEL_E_FORMAT;
107 }
108 r->median_ns = med / 1e3;
109 r->min_ns = mn / 1e3;
110 r->noise_rel = nz;
111 r->pack_ns = pk / 1e3;
112 r->pct_floor = pf;
113 if (kv_num(line, "pack_noise_rel", &r->pack_noise_rel)) r->pack_noise_rel = 0;
114 }
115 t->nrows++;
116 added++;
117 }
118 fclose(f);
119 if (!schema_ok) return OMA_SEL_E_FORMAT;
120 if (!added) return OMA_SEL_E_EMPTY;
121 snprintf(t->source[run], sizeof t->source[run], "%s", path);
122 t->nruns++;
123 return OMA_SEL_OK;
124}
125
126static double cell_dist(size_t n, size_t m, double s, size_t n2, size_t m2, double s2) {
127 return fabs(log2((double)n / (double)n2)) + fabs(log2((double)m / (double)m2)) + 4.0 * fabs(s - s2);
128}
129
130int oma_sel_in_tie_set(const oma_sel_decision *d, const char *id) {
131 if (strcmp(d->chosen, id) == 0) return 1;
132 for (size_t i = 0; i < d->ntie; i++)
133 if (strcmp(d->tie_set[i], id) == 0) return 1;
134 return 0;
135}
136
137int oma_sel_decide(const oma_sel_table *t, const oma_sel_query *q, oma_sel_decision *d) {
138 if (!t || !q || !d) return OMA_SEL_E_FORMAT;
139 memset(d, 0, sizeof *d);
140 d->q = *q;
141 /* nearest measured cell */
142 double best = INFINITY;
143 for (size_t i = 0; i < t->nrows; i++) {
144 const oma_sel_row *r = &t->rows[i];
145 if (q->run >= 0 && r->run != q->run) continue;
146 double dist = cell_dist(q->n, q->m, q->sparsity, r->n, r->m, r->sparsity);
147 if (dist < best - 1e-12) {
148 best = dist;
149 d->cell_n = r->n;
150 d->cell_m = r->m;
151 d->cell_sparsity = r->sparsity;
152 }
153 }
154 if (!isfinite(best)) return OMA_SEL_E_EMPTY;
155 d->exact_cell = best < 1e-9;
156 /* candidates: every registered realization */
157 for (size_t k = 0; k < oma_rz_count() && d->ncand < OMA_SEL_MAX_CAND; k++) {
158 const oma_rz_impl *im = oma_rz_get(k);
159 oma_sel_candidate *c = &d->cand[d->ncand++];
160 snprintf(c->rz, sizeof c->rz, "%s", im->id);
161 c->eligible = 1;
162 if (!im->exact) { c->eligible = 0; c->excluded_because = "not exact"; continue; }
163 if (q->n > im->max_n) { c->eligible = 0; c->excluded_because = "query n above max_n"; continue; }
164 int seen = 0, bad = 0, nall = 0;
165 double pf = 0, all_cost[OMA_SEL_MAX_RUNS];
166 for (size_t i = 0; i < t->nrows; i++) {
167 const oma_sel_row *r = &t->rows[i];
168 if (r->n != d->cell_n || r->m != d->cell_m || fabs(r->sparsity - d->cell_sparsity) > 1e-9) continue;
169 if (strcmp(r->rz, im->id) != 0) continue;
170 int used = q->run < 0 || r->run == q->run;
171 if (!r->eligible || !r->verified) { if (used) bad = 1; continue; }
172 double pack = q->pack_per_call ? r->pack_ns : 0;
173 double cost = r->median_ns + pack;
174 /* the band always sees every run of this cell (repeated-run spread) */
175 if (nall < OMA_SEL_MAX_RUNS) all_cost[nall++] = cost;
176 if (!used) continue;
177 double noise = (r->noise_rel * r->median_ns + (q->pack_per_call ? r->pack_noise_rel * pack : 0)) / cost;
178 if (c->runs_used < OMA_SEL_MAX_RUNS) c->run_cost_ns[c->runs_used] = cost;
179 c->runs_used++;
180 c->cost_ns += cost;
181 pf += r->pct_floor;
182 if (noise > c->within_noise_rel) c->within_noise_rel = noise;
183 seen = 1;
184 }
185 if (bad) { c->eligible = 0; c->excluded_because = "not verified (or not measured) in every run"; continue; }
186 if (!seen) { c->eligible = 0; c->excluded_because = "no measurement for this cell"; continue; }
187 c->cost_ns /= c->runs_used;
188 c->pct_floor = pf / c->runs_used;
189 if (nall > 1) {
190 double lo = all_cost[0], hi = lo, mean = 0;
191 for (int r = 0; r < nall; r++) {
192 if (all_cost[r] < lo) lo = all_cost[r];
193 if (all_cost[r] > hi) hi = all_cost[r];
194 mean += all_cost[r];
195 }
196 c->cross_run_rel = (hi - lo) / (mean / nall);
197 }
198 }
199 /* winner and runner-up */
200 int w = -1, u = -1;
201 for (size_t k = 0; k < d->ncand; k++) {
202 if (!d->cand[k].eligible) continue;
203 if (w < 0 || d->cand[k].cost_ns < d->cand[w].cost_ns) { u = w; w = (int)k; }
204 else if (u < 0 || d->cand[k].cost_ns < d->cand[u].cost_ns) u = (int)k;
205 }
206 if (w < 0) {
207 snprintf(d->reason, sizeof d->reason, "no eligible realization");
208 return OMA_SEL_E_EMPTY;
209 }
210 const oma_sel_candidate *W = &d->cand[w];
211 snprintf(d->chosen, sizeof d->chosen, "%s", W->rz);
212 snprintf(d->cheapest, sizeof d->cheapest, "%s", W->rz);
213 d->chosen_cost_ns = d->cheapest_cost_ns = W->cost_ns;
214 d->tie_resolution = "none";
215 if (u < 0) {
216 snprintf(d->reason, sizeof d->reason, "only eligible realization");
217 return OMA_SEL_OK;
218 }
219 const oma_sel_candidate *U = &d->cand[u];
220 snprintf(d->runner_up, sizeof d->runner_up, "%s", U->rz);
221 d->runner_up_cost_ns = U->cost_ns;
222 d->margin_rel = (U->cost_ns - W->cost_ns) / W->cost_ns;
223 double band = W->within_noise_rel;
224 if (U->within_noise_rel > band) band = U->within_noise_rel;
225 if (W->cross_run_rel > band) band = W->cross_run_rel;
226 if (U->cross_run_rel > band) band = U->cross_run_rel;
227 d->noise_band_rel = band;
228 /* tied set: every eligible candidate whose cost is within the band of the
229 * cheapest, cheapest first */
230 memcpy(d->tie_set[d->ntie++], W->rz, OMA_SEL_ID);
231 for (size_t k = 0; k < d->ncand && d->ntie < OMA_SEL_MAX_CAND; k++) {
232 const oma_sel_candidate *c = &d->cand[k];
233 if (!c->eligible || (int)k == w) continue;
234 if ((c->cost_ns - W->cost_ns) / W->cost_ns <= band) snprintf(d->tie_set[d->ntie++], OMA_SEL_ID, "%s", c->rz);
235 }
236 /* TIE when the tied set has more than one member (the runner-up is in it
237 * exactly when margin <= band) */
238 d->tie = d->ntie > 1;
239 if (!d->tie) {
240 snprintf(d->reason, sizeof d->reason,
241 "chosen: cheapest exact verified realization, %.1f%% under %s (margin outside the measured noise band)",
242 100.0 * d->margin_rel, U->rz);
243 return OMA_SEL_OK;
244 }
245 /* ADR 0019 section 9.1: resolved only inside the tied set. The incumbent
246 * stands if it is in the tied set; else the digital reference if it is in
247 * the tied set; else the cheapest member. A realization outside the tied
248 * set is never selected. */
249 const oma_sel_candidate *inc = NULL, *ref = NULL;
250 for (size_t k = 0; k < d->ncand; k++) {
251 const oma_sel_candidate *c = &d->cand[k];
252 if (!c->eligible || !oma_sel_in_tie_set(d, c->rz)) continue;
253 if (q->incumbent && q->incumbent[0] && strcmp(c->rz, q->incumbent) == 0) inc = c;
254 if (strcmp(c->rz, OMA_SEL_REFERENCE) == 0) ref = c;
255 }
256 const oma_sel_candidate *pick = W;
257 if (inc) { pick = inc; d->tie_resolution = "incumbent"; }
258 else if (ref) { pick = ref; d->tie_resolution = "reference"; }
259 else d->tie_resolution = "cheapest";
260 memcpy(d->chosen, pick->rz, sizeof d->chosen); /* both OMA_SEL_ID, NUL-terminated */
261 d->chosen_cost_ns = pick->cost_ns;
262 snprintf(d->reason, sizeof d->reason,
263 "TIE: %s %.1f%% under %s, inside the measured noise band %.1f%%; %s %s selected from the tied set of %zu "
264 "(ADR 0019 9.1)",
265 W->rz, 100.0 * d->margin_rel, U->rz, 100.0 * band,
266 inc ? "incumbent" : ref ? "digital reference" : "cheapest", pick->rz, d->ntie);
267 return OMA_SEL_OK;
268}
269
270void oma_sel_decision_json(FILE *f, const oma_sel_decision *d, const char *ind) {
271 fprintf(f, "%s{\"query\": {\"n\": %zu, \"m\": %zu, \"sparsity\": %.2f, \"pack\": \"%s\", \"runs\": \"%s\"},\n", ind,
272 d->q.n, d->q.m, d->q.sparsity, d->q.pack_per_call ? "per_call" : "once_amortized",
273 d->q.run < 0 ? "all" : (d->q.run == 0 ? "run1" : "run2"));
274 fprintf(f, "%s \"cell\": {\"n\": %zu, \"m\": %zu, \"sparsity\": %.2f, \"exact_cell\": %s},\n", ind, d->cell_n,
275 d->cell_m, d->cell_sparsity, d->exact_cell ? "true" : "false");
276 fprintf(f, "%s \"chosen\": \"%s\", \"chosen_ns\": %.1f, \"cheapest\": \"%s\", \"cheapest_ns\": %.1f, "
277 "\"runner_up\": \"%s\", \"runner_up_ns\": %.1f, \"margin_rel\": %.4f, \"noise_band_rel\": %.4f, "
278 "\"verdict\": \"%s\", \"incumbent\": \"%s\", \"tie_resolution\": \"%s\", \"tied_set\": [",
279 ind, d->chosen, d->chosen_cost_ns, d->cheapest, d->cheapest_cost_ns, d->runner_up, d->runner_up_cost_ns,
280 d->margin_rel, d->noise_band_rel, d->tie ? "TIE" : "CHOSEN",
281 (d->q.incumbent && d->q.incumbent[0]) ? d->q.incumbent : "", d->tie_resolution ? d->tie_resolution : "none");
282 for (size_t i = 0; i < d->ntie; i++) fprintf(f, "%s\"%s\"", i ? ", " : "", d->tie_set[i]);
283 fprintf(f, "],\n%s \"reason\": \"%s\",\n%s \"candidates\": [", ind, d->reason, ind);
284 for (size_t k = 0; k < d->ncand; k++) {
285 const oma_sel_candidate *c = &d->cand[k];
286 if (c->eligible)
287 fprintf(f, "%s{\"rz\": \"%s\", \"cost_ns\": %.1f, \"within_noise_rel\": %.4f, \"cross_run_rel\": %.4f, "
288 "\"pct_floor\": %.1f}",
289 k ? ", " : "", c->rz, c->cost_ns, c->within_noise_rel, c->cross_run_rel, c->pct_floor);
290 else
291 fprintf(f, "%s{\"rz\": \"%s\", \"excluded\": \"%s\"}", k ? ", " : "", c->rz, c->excluded_because);
292 }
293 fprintf(f, "]}");
294}