File size: 4,153 Bytes
0a3edb6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | """How much of each human training window works in our engine."""
import glob, random, sys, collections
import numpy as np, torch
import factorion_rs
import corpus.convert as cv
from build_tasks import records, MACH, SRC, SNK, BELTISH, SPLITTER, DELTA, C
from compare import frame as bp_frame
from factorion import Channel, Footprint, count_dangling_inserters, count_unpowered, refresh_power, str2ent, str2item
INS = {str2ent(n).value for n in ("inserter", "long_handed_inserter")}
rng = random.Random(0)
c = collections.Counter()
for rec in records(sorted(glob.glob(sys.argv[1]))):
if c["windows"] >= int(sys.argv[2]):
break
if not rec.get("lanes"):
continue
bp = next(cv.designs(rec["string"])); bp["unbuildable"] = rec.get("unbuildable") or []
r = cv.rasterise(bp)
if r is None:
continue
mx, my = bp_frame(bp)
lab = {}
for x, y, _, its in rec["lanes"]:
lab.setdefault((x - mx, y - my), set()).update(i.replace("-", "_") for i in its.split(","))
f = cv.frame(r, 32, rng)
w = None if f is None else cv.place(r, f, 32, region=True)
if w is None:
continue
c["windows"] += 1
ent = w[Channel.ENTITIES.value]
build = w[Channel.FOOTPRINT.value] == Footprint.BUILDABLE.value
n_mach = len({tuple(p) for p in np.argwhere(np.isin(ent, list(MACH)))}) // 9 + 0 # approx (3x3 / 2x2 mix)
c["inserters"] += int(np.isin(ent, list(INS)).sum())
d_in, d_out = count_dangling_inserters(torch.from_numpy(w))
c["inserters nothing feeds"] += d_in; c["inserters feeding nothing"] += d_out
c["unpowered machines+inserters"] += count_unpowered(torch.from_numpy(w))
c["machine+inserter tiles in build region"] += int((build & np.isin(ent, list(MACH | INS))).sum())
# Flow test on the build region: sources/sinks where labelled belts cross its edge.
x0, y0, bw, bh = f.x + f.bx, f.y + f.by, f.bw, f.bh
inside = lambda x, y: x0 <= x < x0 + bw and y0 <= y < y0 + bh # noqa: E731
W, H = r.ent.shape
g = np.zeros((len(Channel), bw + 2, bh + 2), np.int64)
inst = r.inst[x0:x0 + bw, y0:y0 + bh]
inner = np.bincount(inst[inst > 0], minlength=len(r.tiles))
whole = (inst > 0) & (inner == r.tiles)[np.maximum(inst, 0)]
for (i, j) in np.argwhere(whole):
for cc, a in zip(C, (r.ent, r.dir, r.item, r.misc)):
g[cc, i + 1, j + 1] = a[x0 + i, y0 + j]
g[Channel.FOOTPRINT.value] = Footprint.UNBUILDABLE.value
n_src = n_snk = 0
for x in range(x0, x0 + bw):
for y in range(y0, y0 + bh):
if (x not in (x0, x0 + bw - 1) and y not in (y0, y0 + bh - 1)) or r.ent[x, y] not in BELTISH or int(r.dir[x, y]) not in DELTA:
continue
its = lab.get((x, y), set())
if len(its) != 1 or str2item(next(iter(its))) is None:
continue
dx, dy = DELTA[int(r.dir[x, y])]
for (ux, uy), kind in (((x - dx, y - dy), SRC), ((x + dx, y + dy), SNK)):
if not inside(ux, uy) and 0 <= ux < W and 0 <= uy < H and r.ent[ux, uy] in BELTISH | {SPLITTER}:
gx, gy = ux - x0 + 1, uy - y0 + 1
if 0 <= gx < bw + 2 and 0 <= gy < bh + 2:
g[C[0], gx, gy], g[C[1], gx, gy], g[C[2], gx, gy] = kind, int(r.dir[x, y]), str2item(next(iter(its))).value
n_src += kind == SRC; n_snk += kind == SNK
has_mach = np.isin(g[C[0]], list(MACH)).any()
c["build regions with a machine"] += has_mach
if n_src and n_snk:
c["...with labelled sources and sinks"] += 1
refresh_power(torch.from_numpy(g))
thput = factorion_rs.simulate_throughput(np.ascontiguousarray(g.transpose(1, 2, 0)))[0]
c["...that deliver anything"] += thput > 0
if has_mach:
c["...with a machine, labelled ends"] += 1
bare = g.copy(); bare[:4, np.isin(bare[C[0]], list(MACH))] = 0
through = factorion_rs.simulate_throughput(np.ascontiguousarray(bare.transpose(1, 2, 0)))[0]
c["...with a machine, and the machines add output"] += thput > through
for k, v in c.items():
print(f"{k:50s} {v}")
|