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 / runtime / rx_plan_arrange.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 | /* |
| 18 | * rx_plan_arrange.c -- arrangement domain, goal shape, binding, A* planner |
| 19 | * and generaliser. See rx_plan_arrange.h. |
| 20 | */ |
| 21 | #include "rx_plan_arrange.h" |
| 22 | |
| 23 | #include "omega_native.h" |
| 24 | #include "omega_types.h" |
| 25 | #include "sha256.h" |
| 26 | |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | #define FLOOR 15u |
| 31 | #define NONE 0xFFu |
| 32 | |
| 33 | uint64_t pla_fact(RxObjRef x, const RxObjRef *t) { |
| 34 | uint64_t res = 0; |
| 35 | (void)omega2_rx_pla_fact_pack(&res, (uint64_t)x.id, (uint64_t)x.generation, |
| 36 | t ? 0ull : 1ull, |
| 37 | t ? (uint64_t)t->id : 0ull, |
| 38 | t ? (uint64_t)t->generation : 0ull); |
| 39 | return res; |
| 40 | } |
| 41 | |
| 42 | static void fact_unpack(uint64_t f, RxObjRef *x, RxObjRef *t, uint8_t *floor) { |
| 43 | uint64_t dst[5] = { 0 }, res = 0; |
| 44 | (void)omega2_rx_pla_fact_unpack(&res, f, dst, 5); |
| 45 | x->id = (uint32_t)dst[0]; |
| 46 | x->generation = (uint32_t)dst[1]; |
| 47 | *floor = (uint8_t)dst[2]; |
| 48 | t->id = (uint32_t)dst[3]; |
| 49 | t->generation = (uint32_t)dst[4]; |
| 50 | } |
| 51 | |
| 52 | static int same(RxObjRef a, RxObjRef b) { return a.id == b.id && a.generation == b.generation; } |
| 53 | |
| 54 | static const uint64_t *fields(const PlView *v, RxObjRef r) { |
| 55 | if (r.id >= v->n || !v->o[r.id].live || v->o[r.id].generation != r.generation) return NULL; |
| 56 | return v->o[r.id].field; |
| 57 | } |
| 58 | |
| 59 | /* ---- goal ---- */ |
| 60 | |
| 61 | int pla_goal_read(const PlView *v, const PlEnv *env, PlaGoal *g, PlCost *cost) { |
| 62 | memset(g, 0, sizeof *g); |
| 63 | const uint64_t *gf = fields(v, env->obj[PL_ENV_GOAL]); |
| 64 | if (!gf || v->o[env->obj[PL_ENV_GOAL].id].type != PL_OT_GOAL) return -1; |
| 65 | g->seq = gf[0]; |
| 66 | g->kind = gf[1]; |
| 67 | g->max_steps = gf[2]; |
| 68 | g->energy = gf[3]; |
| 69 | if (!gf[4]) return -1; |
| 70 | RxObjRef fr = pl_ref_unpack(gf[4]); |
| 71 | const uint64_t *ff = fields(v, fr); |
| 72 | if (!ff || v->o[fr.id].type != PL_OT_GOALFACTS) return -1; |
| 73 | for (uint32_t i = 0; i < PLA_MAX_FACTS; i++) { |
| 74 | if (cost) cost->shape_ops++; |
| 75 | if (!(ff[i] >> 63)) continue; |
| 76 | uint32_t k = g->n_facts++; |
| 77 | fact_unpack(ff[i], &g->x[k], &g->t[k], &g->floor[k]); |
| 78 | } |
| 79 | if (!g->n_facts) return -1; |
| 80 | |
| 81 | /* Nodes of the goal forest. */ |
| 82 | RxObjRef node[2 * PLA_MAX_FACTS]; |
| 83 | uint32_t nn = 0; |
| 84 | int parent[2 * PLA_MAX_FACTS], child[2 * PLA_MAX_FACTS]; |
| 85 | for (uint32_t i = 0; i < g->n_facts; i++) { |
| 86 | RxObjRef c[2] = { g->x[i], g->t[i] }; |
| 87 | for (uint32_t q = 0; q < (g->floor[i] ? 1u : 2u); q++) { |
| 88 | uint32_t k = 0; |
| 89 | while (k < nn && !same(node[k], c[q])) k++; |
| 90 | if (k == nn) node[nn++] = c[q]; |
| 91 | } |
| 92 | } |
| 93 | for (uint32_t k = 0; k < nn; k++) parent[k] = -2, child[k] = -1; /* -2 no fact, -1 floor */ |
| 94 | for (uint32_t i = 0; i < g->n_facts; i++) { |
| 95 | uint32_t a = 0, b = 0; |
| 96 | while (!same(node[a], g->x[i])) a++; |
| 97 | if (parent[a] != -2) return -2; /* two facts about one unit */ |
| 98 | if (g->floor[i]) { parent[a] = -1; continue; } |
| 99 | while (!same(node[b], g->t[i])) b++; |
| 100 | if (a == b || child[b] >= 0) return -2; /* self, or two units on one */ |
| 101 | parent[a] = (int)b; |
| 102 | child[b] = (int)a; |
| 103 | } |
| 104 | /* Chains from each bottom, then a canonical order: grounded first, longer first. */ |
| 105 | uint32_t seen = 0; |
| 106 | struct { uint32_t len, grounded; RxObjRef o[2 * PLA_MAX_FACTS]; } ch[2 * PLA_MAX_FACTS]; |
| 107 | uint32_t nc = 0; |
| 108 | for (uint32_t k = 0; k < nn; k++) { |
| 109 | if (parent[k] >= 0) continue; |
| 110 | ch[nc].len = 0; |
| 111 | ch[nc].grounded = parent[k] == -1; |
| 112 | for (int u = (int)k; u >= 0; u = child[u]) { |
| 113 | ch[nc].o[ch[nc].len++] = node[u]; |
| 114 | seen++; |
| 115 | if (cost) cost->shape_ops++; |
| 116 | } |
| 117 | nc++; |
| 118 | } |
| 119 | if (seen != nn) return -3; /* a cycle */ |
| 120 | for (uint32_t i = 1; i < nc; i++) |
| 121 | for (uint32_t j = i; j > 0; j--) { |
| 122 | int swap = ch[j].grounded > ch[j - 1].grounded || |
| 123 | (ch[j].grounded == ch[j - 1].grounded && ch[j].len > ch[j - 1].len); |
| 124 | if (!swap) break; |
| 125 | __typeof__(ch[0]) tmp = ch[j]; |
| 126 | ch[j] = ch[j - 1]; |
| 127 | ch[j - 1] = tmp; |
| 128 | if (cost) cost->shape_ops++; |
| 129 | } |
| 130 | sha256_ctx c; |
| 131 | sha256_init(&c); |
| 132 | sha256_update(&c, (const uint8_t *)"OMEGA_PLAN_SHAPE_ARRANGE_V1", 27); |
| 133 | uint8_t b[8] = { (uint8_t)g->kind, (uint8_t)nc }; |
| 134 | sha256_update(&c, b, 2); |
| 135 | for (uint32_t i = 0; i < nc; i++) { |
| 136 | g->chain[i].start = g->n_obj; |
| 137 | g->chain[i].len = ch[i].len; |
| 138 | g->chain[i].grounded = ch[i].grounded; |
| 139 | for (uint32_t j = 0; j < ch[i].len; j++) g->obj[g->n_obj++] = ch[i].o[j]; |
| 140 | b[0] = (uint8_t)ch[i].grounded; |
| 141 | b[1] = (uint8_t)ch[i].len; |
| 142 | sha256_update(&c, b, 2); |
| 143 | } |
| 144 | g->n_chains = nc; |
| 145 | sha256_final(&c, g->shape); |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | /* ---- binding ---- */ |
| 150 | |
| 151 | static uint32_t fact_n(uint32_t n) { uint32_t f = 1; while (n > 1) f *= n--; return f; } |
| 152 | |
| 153 | int pla_bind(const PlanTemplate *t, const PlView *v, const PlaGoal *g, uint32_t attempt, |
| 154 | RxObjRef slots[PL_MAX_SLOTS], PlCost *cost) { |
| 155 | if (t->n_goal_slots != g->n_obj || t->n_slots > PL_MAX_SLOTS) return 0; |
| 156 | /* Groups of interchangeable chains; attempt = mixed-radix permutation index. */ |
| 157 | uint32_t order[2 * PLA_MAX_FACTS]; |
| 158 | uint32_t rest = attempt; |
| 159 | uint64_t total = 1; |
| 160 | for (uint32_t i = 0; i < g->n_chains;) { |
| 161 | uint32_t j = i; |
| 162 | while (j < g->n_chains && g->chain[j].len == g->chain[i].len && |
| 163 | g->chain[j].grounded == g->chain[i].grounded) j++; |
| 164 | uint32_t m = j - i, radix = fact_n(m); |
| 165 | total *= radix; |
| 166 | uint32_t code = rest % radix; |
| 167 | rest /= radix; |
| 168 | uint32_t pool[2 * PLA_MAX_FACTS], np = m; |
| 169 | for (uint32_t k = 0; k < m; k++) pool[k] = i + k; |
| 170 | for (uint32_t k = 0; k < m; k++) { |
| 171 | uint32_t f = fact_n(m - 1 - k), pick = code / f; |
| 172 | code %= f; |
| 173 | order[i + k] = pool[pick]; |
| 174 | for (uint32_t q = pick; q + 1 < np; q++) pool[q] = pool[q + 1]; |
| 175 | np--; |
| 176 | } |
| 177 | i = j; |
| 178 | } |
| 179 | if ((uint64_t)attempt >= total || total > 720) return 0; |
| 180 | if (cost) cost->bind_attempts++; |
| 181 | uint32_t s = 0; |
| 182 | for (uint32_t i = 0; i < g->n_chains; i++) { |
| 183 | uint32_t c = order[i]; |
| 184 | for (uint32_t k = 0; k < g->chain[c].len; k++) slots[s++] = g->obj[g->chain[c].start + k]; |
| 185 | } |
| 186 | for (; s < t->n_slots; s++) { |
| 187 | const PlSlot *sl = &t->slots[s]; |
| 188 | if (sl->bind_rule != PL_BIND_RESTS_ON || sl->bind_from >= s) return -1; |
| 189 | uint64_t want = pl_ref_pack(slots[sl->bind_from]); |
| 190 | int found = -1; |
| 191 | for (uint32_t i = 0; i < v->n; i++) { |
| 192 | if (!v->o[i].live || v->o[i].type != sl->type) continue; |
| 193 | if (v->o[i].field[sl->bind_field & (RX_MAX_FIELDS - 1)] != want) continue; |
| 194 | if (found >= 0) return -1; /* not unique */ |
| 195 | found = (int)i; |
| 196 | } |
| 197 | if (cost) cost->preds++; |
| 198 | if (found < 0) return -1; |
| 199 | slots[s] = (RxObjRef){ (uint32_t)found, v->o[found].generation }; |
| 200 | } |
| 201 | return 1; |
| 202 | } |
| 203 | |
| 204 | /* ---- targets ---- */ |
| 205 | |
| 206 | void pla_target_from_goal(const PlaGoal *g, PlaTarget *tg) { |
| 207 | memset(tg, 0, sizeof *tg); |
| 208 | for (uint32_t i = 0; i < g->n_facts; i++) { |
| 209 | tg->x[tg->n_facts] = g->x[i]; |
| 210 | tg->t[tg->n_facts] = g->t[i]; |
| 211 | tg->floor[tg->n_facts++] = g->floor[i]; |
| 212 | } |
| 213 | for (uint32_t i = 0; i < g->n_obj; i++) tg->insider[tg->n_insiders++] = g->obj[i]; |
| 214 | } |
| 215 | |
| 216 | int pla_target_from_template(const PlanTemplate *t, const RxObjRef *slots, PlaTarget *tg) { |
| 217 | memset(tg, 0, sizeof *tg); |
| 218 | for (uint32_t i = 0; i < t->state.n; i++) { |
| 219 | const PlPred *p = &t->state.p[i]; |
| 220 | if (p->field != PLA_F_SUPPORT) continue; |
| 221 | if (p->kind == PL_P_NOT_SLOT) return -1; |
| 222 | if (p->kind == PL_P_EQ_CONST && p->value == 0) { |
| 223 | tg->x[tg->n_facts] = slots[p->slot]; |
| 224 | tg->floor[tg->n_facts++] = 1; |
| 225 | } else if (p->kind == PL_P_EQ_SLOT) { |
| 226 | tg->x[tg->n_facts] = slots[p->slot]; |
| 227 | tg->t[tg->n_facts++] = slots[p->slot2]; |
| 228 | } |
| 229 | } |
| 230 | for (uint32_t i = 0; i < t->invariants.n; i++) |
| 231 | if (t->invariants.p[i].kind == PL_P_UNREFERENCED) |
| 232 | tg->protect[tg->n_protect++] = slots[t->invariants.p[i].slot]; |
| 233 | for (uint32_t s = 0; s < t->n_slots; s++) tg->insider[tg->n_insiders++] = slots[s]; |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | /* ---- A* ---- */ |
| 238 | |
| 239 | typedef struct { uint64_t state; uint32_t g, parent; uint8_t mover, dest; } SNode; |
| 240 | typedef struct { uint32_t f, h, node; uint64_t seq; } HeapE; |
| 241 | |
| 242 | typedef struct { |
| 243 | uint32_t n; |
| 244 | uint8_t want[PLA_MAX_UNITS]; /* target index, FLOOR, or NONE */ |
| 245 | uint16_t insider, protect; |
| 246 | } Model; |
| 247 | |
| 248 | static inline uint32_t sup(uint64_t s, uint32_t u) { return (uint32_t)((s >> (4 * u)) & 0xfu); } |
| 249 | static inline uint64_t set_sup(uint64_t s, uint32_t u, uint32_t v) { |
| 250 | return (s & ~(0xfull << (4 * u))) | ((uint64_t)v << (4 * u)); |
| 251 | } |
| 252 | |
| 253 | static uint32_t heur(const Model *m, uint64_t s) { |
| 254 | uint8_t wp[PLA_MAX_UNITS]; |
| 255 | memset(wp, 2, sizeof wp); /* 2 = unknown */ |
| 256 | uint32_t h = 0; |
| 257 | for (uint32_t u = 0; u < m->n; u++) { |
| 258 | if (m->want[u] == NONE) continue; |
| 259 | /* Walk down while the fact holds; well placed if it holds all the way. */ |
| 260 | uint32_t path[PLA_MAX_UNITS], np = 0, x = u; |
| 261 | int ok = 1; |
| 262 | for (;;) { |
| 263 | if (wp[x] != 2) { ok = wp[x]; break; } |
| 264 | path[np++] = x; |
| 265 | if (sup(s, x) != m->want[x]) { ok = 0; break; } |
| 266 | if (m->want[x] == FLOOR) break; |
| 267 | x = m->want[x]; |
| 268 | if (m->want[x] == NONE) break; |
| 269 | if (np > m->n) { ok = 0; break; } |
| 270 | } |
| 271 | for (uint32_t i = 0; i < np; i++) wp[path[i]] = (uint8_t)ok; |
| 272 | h += !wp[u]; |
| 273 | } |
| 274 | for (uint32_t u = 0; u < m->n; u++) { |
| 275 | if (m->insider & (1u << u)) continue; |
| 276 | uint32_t d = sup(s, u); |
| 277 | if (d != FLOOR && (m->protect & (1u << d))) h++; |
| 278 | } |
| 279 | return h; |
| 280 | } |
| 281 | |
| 282 | static void heap_push(HeapE *h, uint32_t *n, HeapE e) { |
| 283 | uint32_t i = (*n)++; |
| 284 | h[i] = e; |
| 285 | while (i) { |
| 286 | uint32_t p = (i - 1) / 2; |
| 287 | int less = h[i].f < h[p].f || (h[i].f == h[p].f && (h[i].h < h[p].h || |
| 288 | (h[i].h == h[p].h && h[i].seq < h[p].seq))); |
| 289 | if (!less) break; |
| 290 | HeapE t = h[i]; h[i] = h[p]; h[p] = t; |
| 291 | i = p; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | static HeapE heap_pop(HeapE *h, uint32_t *n) { |
| 296 | HeapE top = h[0]; |
| 297 | h[0] = h[--(*n)]; |
| 298 | uint32_t i = 0; |
| 299 | for (;;) { |
| 300 | uint32_t l = 2 * i + 1, r = l + 1, m = i; |
| 301 | #define LESS(a, b) (h[a].f < h[b].f || (h[a].f == h[b].f && (h[a].h < h[b].h || \ |
| 302 | (h[a].h == h[b].h && h[a].seq < h[b].seq)))) |
| 303 | if (l < *n && LESS(l, m)) m = l; |
| 304 | if (r < *n && LESS(r, m)) m = r; |
| 305 | #undef LESS |
| 306 | if (m == i) break; |
| 307 | HeapE t = h[i]; h[i] = h[m]; h[m] = t; |
| 308 | i = m; |
| 309 | } |
| 310 | return top; |
| 311 | } |
| 312 | |
| 313 | static inline uint64_t hmix(uint64_t x) { |
| 314 | x ^= x >> 33; x *= 0xff51afd7ed558ccdull; x ^= x >> 33; x *= 0xc4ceb9fe1a85ec53ull; |
| 315 | return x ^ (x >> 33); |
| 316 | } |
| 317 | |
| 318 | int pla_plan(const PlView *v, const PlaGoal *g, const PlaTarget *tg, uint64_t max_expand, |
| 319 | PlaPlan *out, PlCost *cost) { |
| 320 | (void)g; |
| 321 | memset(out, 0, sizeof *out); |
| 322 | /* Units: insiders first in the target's order, then the rest by id. */ |
| 323 | RxObjRef unit[PLA_MAX_UNITS]; |
| 324 | uint32_t n = 0; |
| 325 | for (uint32_t i = 0; i < tg->n_insiders; i++) { |
| 326 | if (!fields(v, tg->insider[i]) || v->o[tg->insider[i].id].type != PL_OT_UNIT) return -1; |
| 327 | int dup = 0; |
| 328 | for (uint32_t k = 0; k < n; k++) dup |= same(unit[k], tg->insider[i]); |
| 329 | if (dup) continue; |
| 330 | if (n >= PLA_MAX_UNITS) return -2; |
| 331 | unit[n++] = tg->insider[i]; |
| 332 | } |
| 333 | uint32_t n_in = n; |
| 334 | for (uint32_t i = 0; i < v->n; i++) { |
| 335 | if (!v->o[i].live || v->o[i].type != PL_OT_UNIT) continue; |
| 336 | RxObjRef r = { i, v->o[i].generation }; |
| 337 | int dup = 0; |
| 338 | for (uint32_t k = 0; k < n_in; k++) dup |= same(unit[k], r); |
| 339 | if (dup) continue; |
| 340 | if (n >= PLA_MAX_UNITS) return -2; |
| 341 | unit[n++] = r; |
| 342 | } |
| 343 | Model m; |
| 344 | memset(&m, 0, sizeof m); |
| 345 | m.n = n; |
| 346 | memset(m.want, NONE, sizeof m.want); |
| 347 | for (uint32_t k = 0; k < n_in; k++) m.insider |= (uint16_t)(1u << k); |
| 348 | #define IDX(r, out_) do { out_ = NONE; for (uint32_t q_ = 0; q_ < n; q_++) \ |
| 349 | if (same(unit[q_], (r))) { out_ = q_; break; } } while (0) |
| 350 | for (uint32_t i = 0; i < tg->n_facts; i++) { |
| 351 | uint32_t a, b = FLOOR; |
| 352 | IDX(tg->x[i], a); |
| 353 | if (a == NONE) return -1; |
| 354 | if (!tg->floor[i]) { IDX(tg->t[i], b); if (b == NONE) return -1; } |
| 355 | m.want[a] = (uint8_t)b; |
| 356 | } |
| 357 | for (uint32_t i = 0; i < tg->n_protect; i++) { |
| 358 | uint32_t a; |
| 359 | IDX(tg->protect[i], a); |
| 360 | if (a == NONE) return -1; |
| 361 | m.protect |= (uint16_t)(1u << a); |
| 362 | } |
| 363 | uint64_t s0 = 0; |
| 364 | for (uint32_t u = 0; u < n; u++) { |
| 365 | uint64_t f = v->o[unit[u].id].field[PLA_F_SUPPORT]; |
| 366 | uint32_t d = FLOOR; |
| 367 | if (f) { IDX(pl_ref_unpack(f), d); if (d == NONE) return -3; } |
| 368 | s0 = set_sup(s0, u, d); |
| 369 | } |
| 370 | for (uint32_t u = n; u < 16; u++) s0 = set_sup(s0, u, 0); |
| 371 | #undef IDX |
| 372 | |
| 373 | /* Tables start small and double as the search grows. */ |
| 374 | uint64_t cap_nodes = 1024, cap_heap = 1024, hcap = 2048; |
| 375 | SNode *nodes = malloc(cap_nodes * sizeof *nodes); |
| 376 | HeapE *heap = malloc(cap_heap * sizeof *heap); |
| 377 | uint32_t *table = malloc(hcap * sizeof *table); |
| 378 | if (!nodes || !heap || !table) { free(nodes); free(heap); free(table); return -4; } |
| 379 | memset(table, 0xff, hcap * sizeof *table); |
| 380 | uint32_t nn = 0, nh = 0; |
| 381 | uint64_t seq = 0; |
| 382 | int rc = -5; |
| 383 | |
| 384 | nodes[nn] = (SNode){ s0, 0, UINT32_MAX, 0, 0 }; |
| 385 | table[hmix(s0) & (hcap - 1)] = nn; |
| 386 | uint32_t h0 = heur(&m, s0); |
| 387 | if (cost) cost->heuristic++; |
| 388 | heap_push(heap, &nh, (HeapE){ h0, h0, nn, seq++ }); |
| 389 | nn++; |
| 390 | uint64_t expanded = 0; |
| 391 | while (nh) { |
| 392 | HeapE e = heap_pop(heap, &nh); |
| 393 | SNode cur = nodes[e.node]; |
| 394 | if (e.f != cur.g + e.h) continue; /* superseded entry */ |
| 395 | if (e.h == 0) { |
| 396 | /* Goal test (h == 0 is exact here: every fact well placed, no outsider on a protected unit). */ |
| 397 | uint32_t len = cur.g, at = e.node; |
| 398 | if (len > PLA_MAX_MOVES) { rc = -6; break; } |
| 399 | out->n = len; |
| 400 | for (uint32_t k = len; k > 0; k--) { |
| 401 | const SNode *x = &nodes[at]; |
| 402 | out->mover[k - 1] = unit[x->mover]; |
| 403 | out->to_floor[k - 1] = x->dest == FLOOR; |
| 404 | if (x->dest != FLOOR) out->dest[k - 1] = unit[x->dest]; |
| 405 | at = x->parent; |
| 406 | } |
| 407 | rc = 0; |
| 408 | break; |
| 409 | } |
| 410 | if (++expanded > max_expand) { rc = -5; break; } |
| 411 | if (cost) cost->expanded++; |
| 412 | uint16_t covered = 0; |
| 413 | for (uint32_t u = 0; u < n; u++) { |
| 414 | uint32_t d = sup(cur.state, u); |
| 415 | if (d != FLOOR) covered |= (uint16_t)(1u << d); |
| 416 | } |
| 417 | for (uint32_t u = 0; u < n; u++) { |
| 418 | if (covered & (1u << u)) continue; |
| 419 | uint32_t from = sup(cur.state, u); |
| 420 | for (uint32_t dd = 0; dd <= n; dd++) { |
| 421 | uint32_t d = dd == n ? FLOOR : dd; |
| 422 | if (d == from || d == u) continue; |
| 423 | if (d != FLOOR) { |
| 424 | if (!(m.insider & (1u << d)) || (covered & (1u << d))) continue; |
| 425 | if (!(m.insider & (1u << u))) continue; /* outsiders only go to the floor */ |
| 426 | } |
| 427 | uint64_t ns = set_sup(cur.state, u, d); |
| 428 | if (cost) cost->generated++; |
| 429 | uint64_t hi = hmix(ns) & (hcap - 1); |
| 430 | uint32_t found = UINT32_MAX; |
| 431 | while (table[hi] != UINT32_MAX) { |
| 432 | if (nodes[table[hi]].state == ns) { found = table[hi]; break; } |
| 433 | hi = (hi + 1) & (hcap - 1); |
| 434 | } |
| 435 | uint32_t ng = cur.g + 1; |
| 436 | if (found != UINT32_MAX && nodes[found].g <= ng) continue; |
| 437 | if (nn + 1 >= cap_nodes) { |
| 438 | SNode *g2 = realloc(nodes, 2 * cap_nodes * sizeof *nodes); |
| 439 | if (!g2) { rc = -4; goto done; } |
| 440 | nodes = g2; |
| 441 | cap_nodes *= 2; |
| 442 | } |
| 443 | if (nh + 1 >= cap_heap) { |
| 444 | HeapE *h2 = realloc(heap, 2 * cap_heap * sizeof *heap); |
| 445 | if (!h2) { rc = -4; goto done; } |
| 446 | heap = h2; |
| 447 | cap_heap *= 2; |
| 448 | } |
| 449 | if (2 * (nn + 1) > hcap) { |
| 450 | /* Rehash at half load. */ |
| 451 | uint32_t *t2 = malloc(2 * hcap * sizeof *t2); |
| 452 | if (!t2) { rc = -4; goto done; } |
| 453 | memset(t2, 0xff, 2 * hcap * sizeof *t2); |
| 454 | for (uint32_t q = 0; q < nn; q++) { |
| 455 | uint64_t p = hmix(nodes[q].state) & (2 * hcap - 1); |
| 456 | while (t2[p] != UINT32_MAX) p = (p + 1) & (2 * hcap - 1); |
| 457 | t2[p] = q; |
| 458 | } |
| 459 | free(table); |
| 460 | table = t2; |
| 461 | hcap *= 2; |
| 462 | if (found == UINT32_MAX) { |
| 463 | hi = hmix(ns) & (hcap - 1); |
| 464 | while (table[hi] != UINT32_MAX) hi = (hi + 1) & (hcap - 1); |
| 465 | } |
| 466 | } |
| 467 | uint32_t id = found; |
| 468 | if (found == UINT32_MAX) { |
| 469 | id = nn++; |
| 470 | table[hi] = id; |
| 471 | } |
| 472 | nodes[id] = (SNode){ ns, ng, e.node, (uint8_t)u, (uint8_t)d }; |
| 473 | uint32_t h = heur(&m, ns); |
| 474 | if (cost) cost->heuristic++; |
| 475 | heap_push(heap, &nh, (HeapE){ ng + h, h, id, seq++ }); |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | done: |
| 480 | free(nodes); |
| 481 | free(heap); |
| 482 | free(table); |
| 483 | return rc; |
| 484 | } |
| 485 | |
| 486 | int pla_apply(PlView *v, const PlaPlan *p) { |
| 487 | for (uint32_t k = 0; k < p->n; k++) { |
| 488 | uint64_t *x = (uint64_t *)fields(v, p->mover[k]); |
| 489 | if (!x) return -1; |
| 490 | uint64_t me = pl_ref_pack(p->mover[k]); |
| 491 | uint64_t to = p->to_floor[k] ? 0 : pl_ref_pack(p->dest[k]); |
| 492 | if (!p->to_floor[k] && !fields(v, p->dest[k])) return -1; |
| 493 | for (uint32_t i = 0; i < v->n; i++) { |
| 494 | if (!v->o[i].live || v->o[i].type != PL_OT_UNIT) continue; |
| 495 | if (v->o[i].field[PLA_F_SUPPORT] == me) return -2; /* mover not clear */ |
| 496 | if (to && v->o[i].field[PLA_F_SUPPORT] == to) return -3; /* destination not clear */ |
| 497 | } |
| 498 | x[PLA_F_SUPPORT] = to; |
| 499 | } |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | int pla_legal(const PlView *v) { |
| 504 | for (uint32_t i = 0; i < v->n; i++) { |
| 505 | if (!v->o[i].live || v->o[i].type != PL_OT_UNIT) continue; |
| 506 | RxObjRef me = { i, v->o[i].generation }; |
| 507 | if (v->o[i].field[PLA_F_SELF] != pl_ref_pack(me)) return 0; |
| 508 | uint64_t s = v->o[i].field[PLA_F_SUPPORT]; |
| 509 | uint32_t steps = 0; |
| 510 | while (s) { |
| 511 | const uint64_t *f = fields(v, pl_ref_unpack(s)); |
| 512 | if (!f || v->o[pl_ref_unpack(s).id].type != PL_OT_UNIT) return 0; |
| 513 | if (++steps > v->n) return 0; /* cycle */ |
| 514 | s = f[PLA_F_SUPPORT]; |
| 515 | } |
| 516 | for (uint32_t j = i + 1; j < v->n; j++) |
| 517 | if (v->o[j].live && v->o[j].type == PL_OT_UNIT && v->o[i].field[PLA_F_SUPPORT] && |
| 518 | v->o[j].field[PLA_F_SUPPORT] == v->o[i].field[PLA_F_SUPPORT]) |
| 519 | return 0; /* one unit carries two */ |
| 520 | } |
| 521 | return 1; |
| 522 | } |
| 523 | |
| 524 | /* ---- generalisation ---- */ |
| 525 | |
| 526 | static int slot_of(const RxObjRef *slots, uint32_t n, RxObjRef r) { |
| 527 | for (uint32_t s = 0; s < n; s++) |
| 528 | if (same(slots[s], r)) return (int)s; |
| 529 | return -1; |
| 530 | } |
| 531 | |
| 532 | static void add(PlPredList *l, PlPred p) { if (l->n < PL_MAX_PREDS) l->p[l->n++] = p; } |
| 533 | |
| 534 | int pla_generalize(const PlView *v0, const PlaGoal *g, const PlaPlan *p, uint32_t origin, |
| 535 | const uint8_t *parent, uint64_t cog_gen, PlanTemplate *t, |
| 536 | RxObjRef slots[PL_MAX_SLOTS]) { |
| 537 | memset(t, 0, sizeof *t); |
| 538 | t->goal_kind = PLA_GOAL_ARRANGE; |
| 539 | memcpy(t->goal_shape, g->shape, 32); |
| 540 | if (g->n_obj > PL_MAX_SLOTS) return -1; |
| 541 | uint32_t n = 0; |
| 542 | for (uint32_t i = 0; i < g->n_obj; i++) { |
| 543 | slots[n] = g->obj[i]; |
| 544 | t->slots[n++] = (PlSlot){ PL_OT_UNIT, PL_ROLE_GOAL, 0, PL_BIND_FROM_GOAL, 0, 0 }; |
| 545 | } |
| 546 | t->n_goal_slots = n; |
| 547 | /* Closure: units resting on a slot at the start. */ |
| 548 | for (uint32_t s = 0; s < n; s++) { |
| 549 | uint64_t me = pl_ref_pack(slots[s]); |
| 550 | for (uint32_t i = 0; i < v0->n; i++) { |
| 551 | if (!v0->o[i].live || v0->o[i].type != PL_OT_UNIT) continue; |
| 552 | if (v0->o[i].field[PLA_F_SUPPORT] != me) continue; |
| 553 | RxObjRef r = { i, v0->o[i].generation }; |
| 554 | if (slot_of(slots, n, r) >= 0) continue; |
| 555 | if (n >= PL_MAX_SLOTS) return -1; |
| 556 | slots[n] = r; |
| 557 | t->slots[n++] = (PlSlot){ PL_OT_UNIT, PL_ROLE_INVOLVED, 0, PL_BIND_RESTS_ON, s, |
| 558 | PLA_F_SUPPORT }; |
| 559 | } |
| 560 | } |
| 561 | t->n_slots = n; |
| 562 | for (uint32_t k = 0; k < p->n; k++) |
| 563 | if (slot_of(slots, n, p->mover[k]) < 0 || |
| 564 | (!p->to_floor[k] && slot_of(slots, n, p->dest[k]) < 0)) |
| 565 | return -2; |
| 566 | |
| 567 | /* Required state and invariants. */ |
| 568 | for (uint32_t s = 0; s < n; s++) { |
| 569 | uint64_t f = v0->o[slots[s].id].field[PLA_F_SUPPORT]; |
| 570 | int j = f ? slot_of(slots, n, pl_ref_unpack(f)) : -1; |
| 571 | if (!f) add(&t->state, (PlPred){ PL_P_EQ_CONST, s, PLA_F_SUPPORT, 0, 0, 0 }); |
| 572 | else if (j >= 0) add(&t->state, (PlPred){ PL_P_EQ_SLOT, s, PLA_F_SUPPORT, (uint32_t)j, 0, 0 }); |
| 573 | else add(&t->state, (PlPred){ PL_P_NOT_SLOT, s, PLA_F_SUPPORT, 0, 0, 0 }); |
| 574 | add(&t->invariants, (PlPred){ PL_P_UNREFERENCED, s, PLA_F_SUPPORT, 0, PL_OT_UNIT, 0 }); |
| 575 | add(&t->invariants, (PlPred){ PL_P_SELF_REF, s, PLA_F_SELF, 0, 0, 0 }); |
| 576 | } |
| 577 | t->n_steps = p->n; |
| 578 | t->step_need.energy_cost = PLA_MOVE_ENERGY; |
| 579 | t->energy_total = (uint64_t)p->n * PLA_MOVE_ENERGY; |
| 580 | add(&t->pre, (PlPred){ PL_P_ENV_EQ, PL_ENV_GOAL, 1, 0, 0, PLA_GOAL_ARRANGE }); |
| 581 | add(&t->pre, (PlPred){ PL_P_ENV_GE, PL_ENV_GOAL, 2, 0, 0, p->n }); |
| 582 | add(&t->pre, (PlPred){ PL_P_ENV_GE, PL_ENV_GOAL, 3, 0, 0, t->energy_total }); |
| 583 | add(&t->env, (PlPred){ PL_P_ENV_EQ, PL_ENV_STATION, 0, 0, 0, 1 }); |
| 584 | for (uint32_t i = 0; i < g->n_facts; i++) { |
| 585 | int a = slot_of(slots, n, g->x[i]); |
| 586 | int b = g->floor[i] ? -1 : slot_of(slots, n, g->t[i]); |
| 587 | if (a < 0 || (!g->floor[i] && b < 0)) return -3; |
| 588 | if (g->floor[i]) add(&t->success, (PlPred){ PL_P_EQ_CONST, (uint32_t)a, PLA_F_SUPPORT, 0, 0, 0 }); |
| 589 | else add(&t->success, (PlPred){ PL_P_EQ_SLOT, (uint32_t)a, PLA_F_SUPPORT, (uint32_t)b, 0, 0 }); |
| 590 | } |
| 591 | t->fail_flags = PL_FAIL_GRAPH | PL_FAIL_INVARIANT | PL_FAIL_EVIDENCE | PL_FAIL_CRUMBS; |
| 592 | |
| 593 | /* The action graph. Parameters are slot + 1. */ |
| 594 | AgGraph *G = &t->graph; |
| 595 | rx_graph_init(G, 0); |
| 596 | int prev = -1; |
| 597 | #define NODE(k, ty) ({ int n_ = rx_graph_node(G, (k), (ty)); if (n_ < 0) return -4; n_; }) |
| 598 | for (uint32_t k = 0; k < p->n; k++) { |
| 599 | uint32_t xs = (uint32_t)slot_of(slots, n, p->mover[k]); |
| 600 | int src; |
| 601 | if (p->to_floor[k]) { |
| 602 | src = NODE(AG_CONST, AG_T_U64); |
| 603 | G->nodes[src].imm = 0; |
| 604 | } else { |
| 605 | src = NODE(AG_WORLD_READ, AG_T_U64); |
| 606 | G->nodes[src].param = (uint32_t)slot_of(slots, n, p->dest[k]) + 1; |
| 607 | G->nodes[src].field = PLA_F_SELF; |
| 608 | t->slots[G->nodes[src].param - 1].rights |= RX_RIGHT_READ; |
| 609 | if (prev >= 0) rx_graph_order(G, (uint32_t)prev, (uint32_t)src); |
| 610 | } |
| 611 | int pub = NODE(AG_WORLD_PUBLISH, AG_T_RECEIPT); |
| 612 | G->nodes[pub].param = xs + 1; |
| 613 | G->nodes[pub].field = PLA_F_SUPPORT; |
| 614 | t->slots[xs].rights |= RX_RIGHT_WRITE; |
| 615 | rx_graph_data(G, (uint32_t)src, (uint32_t)pub, 0, AG_EDGE_DATA); |
| 616 | if (prev >= 0) rx_graph_order(G, (uint32_t)prev, (uint32_t)pub); |
| 617 | rx_graph_need_resource(G, (uint32_t)pub, &t->step_need); |
| 618 | prev = pub; |
| 619 | } |
| 620 | for (uint32_t i = 0; i < g->n_facts; i++) { |
| 621 | uint32_t a = (uint32_t)slot_of(slots, n, g->x[i]); |
| 622 | int rx = NODE(AG_WORLD_READ, AG_T_U64); |
| 623 | G->nodes[rx].param = a + 1; |
| 624 | G->nodes[rx].field = PLA_F_SUPPORT; |
| 625 | t->slots[a].rights |= RX_RIGHT_READ; |
| 626 | if (prev >= 0) rx_graph_order(G, (uint32_t)prev, (uint32_t)rx); |
| 627 | int ver = NODE(AG_VERIFY, AG_T_VERDICT); |
| 628 | if (g->floor[i]) { |
| 629 | rx_graph_data(G, (uint32_t)rx, (uint32_t)ver, 0, AG_EDGE_DATA); |
| 630 | G->nodes[ver].imm = 0; |
| 631 | G->nodes[ver].imm2 = 0; |
| 632 | } else { |
| 633 | uint32_t b = (uint32_t)slot_of(slots, n, g->t[i]); |
| 634 | int rt = NODE(AG_WORLD_READ, AG_T_U64); |
| 635 | G->nodes[rt].param = b + 1; |
| 636 | G->nodes[rt].field = PLA_F_SELF; |
| 637 | t->slots[b].rights |= RX_RIGHT_READ; |
| 638 | if (prev >= 0) rx_graph_order(G, (uint32_t)prev, (uint32_t)rt); |
| 639 | int eq = NODE(AG_PURE, AG_T_U64); |
| 640 | G->nodes[eq].op = OP_EQUAL; |
| 641 | rx_graph_data(G, (uint32_t)rx, (uint32_t)eq, 0, AG_EDGE_DATA); |
| 642 | rx_graph_data(G, (uint32_t)rt, (uint32_t)eq, 1, AG_EDGE_DATA); |
| 643 | rx_graph_data(G, (uint32_t)eq, (uint32_t)ver, 0, AG_EDGE_DATA); |
| 644 | G->nodes[ver].imm = 1; |
| 645 | G->nodes[ver].imm2 = 1; |
| 646 | } |
| 647 | rx_graph_success(G, (uint32_t)ver, AG_OK); |
| 648 | } |
| 649 | #undef NODE |
| 650 | t->n_evidence = p->n + g->n_facts; |
| 651 | t->graph_kind = PL_GRAPH_RX_GRAPH; |
| 652 | t->ancestry.origin = origin; |
| 653 | t->ancestry.has_parent = parent != NULL; |
| 654 | if (parent) memcpy(t->ancestry.parent, parent, 32); |
| 655 | t->ancestry.goal_seq = g->seq; |
| 656 | t->ancestry.cog_gen = cog_gen; |
| 657 | rx_plan_identify(t); |
| 658 | return 0; |
| 659 | } |
| 660 | |
| 661 | int pla_template_moves(const PlanTemplate *t, const RxObjRef *slots, PlaPlan *out) { |
| 662 | memset(out, 0, sizeof *out); |
| 663 | const AgGraph *G = &t->graph; |
| 664 | for (uint32_t n = 0; n < G->n_nodes; n++) { |
| 665 | const AgNode *x = &G->nodes[n]; |
| 666 | if (!x->alive || x->kind != AG_WORLD_PUBLISH || !x->param) continue; |
| 667 | int src = -1; |
| 668 | for (uint32_t e = 0; e < G->n_data; e++) |
| 669 | if (G->data[e].to == n) src = G->data[e].from; |
| 670 | if (src < 0 || out->n >= PLA_MAX_MOVES) return -1; |
| 671 | const AgNode *s = &G->nodes[src]; |
| 672 | out->mover[out->n] = slots[x->param - 1]; |
| 673 | if (s->kind == AG_CONST && s->imm == 0) out->to_floor[out->n] = 1; |
| 674 | else if (s->kind == AG_WORLD_READ && s->param) out->dest[out->n] = slots[s->param - 1]; |
| 675 | else return -1; |
| 676 | out->n++; |
| 677 | } |
| 678 | return 0; |
| 679 | } |