File size: 3,007 Bytes
52fc221
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
81
"""A fixed interpreter for a bounded, nonperiodic passive network family.

Gene inheritance and frame/counter copying are IDEAL in this demonstrator.
JSON capsule size is reported separately from this charged interpreter.
Physical positions are a fixed nearest-neighbour embedding, not Brownian growth.
"""
import json
import numpy as np


def capsule(n, dim):
    return {'version': 2, 'extent': n, 'dimension': dim,
            'motif': 'parity_conductance', 'ports': 2,
            'eta': .08, 'seal_bound': .01, 'reserve_ratio': .9}


def canonical(g):
    return json.dumps(g, sort_keys=True, separators=(',', ':')).encode()


def parity(x):
    return int(x).bit_count() % 2


def role(g, inherited_coordinate, axis):
    # Thue-Morse parity produces a deterministic nonperiodic size family.
    v = sum((j+1)*int(x) for j, x in enumerate(inherited_coordinate))
    return .7 + .2*parity(v) + .15*axis


def compile_grid(g):
    n, dim = g['extent'], g['dimension']
    shape = (n,)*dim
    xyz = np.array(list(np.ndindex(shape)))
    index = {tuple(x): i for i, x in enumerate(xyz)}
    parent = np.full(len(xyz), -1, int)
    children = [[] for _ in xyz]
    # Parent is the neighbour obtained by reducing the first nonzero coordinate.
    # Each node can discover exactly these children using its local counters.
    for i, x in enumerate(xyz):
        nz = np.flatnonzero(x)
        if len(nz):
            y=x.copy(); y[nz[0]]-=1
            parent[i]=index[tuple(y)]; children[parent[i]].append(i)
    edges, target, owner = [], [], []
    for i, x in enumerate(xyz):
        for ax in range(dim):
            if x[ax] > 0:
                y=x.copy(); y[ax]-=1
                edges.append([index[tuple(y)], i]); owner.append(i)
                target.append(role(g, x, ax))
    return {'coordinates': xyz, 'parent': parent, 'children': children,
            'edges': np.array(edges), 'target': np.array(target),
            'owner': np.array(owner), 'depth': xyz.sum(axis=1),
            'left': np.flatnonzero(xyz[:,0]==0),
            'right': np.flatnonzero(xyz[:,0]==n-1)}


def access_from_root(parent, sealed):
    """Local acknowledgements on the known acyclic service tree.

    A node may receive service until it seals; sealing a parent removes service
    from descendants. The root inlet is kept open until final completion.
    """
    access = np.zeros(len(parent), bool); access[0] = not sealed[0]
    for i in range(1,len(parent)):
        access[i] = access[parent[i]] and not sealed[i]
    return access


def seal_ready(own_ready, sealed, children, preserve_descendants=True):
    result=sealed.copy()
    # One hop of ACK propagation per round, never an instantaneous global oracle.
    for i in range(1,len(sealed)):
        if own_ready[i] and not sealed[i]:
            if not preserve_descendants or all(sealed[j] for j in children[i]):
                result[i]=True
    if own_ready[0] and all(sealed[j] for j in children[0]):
        result[0]=True
    return result