File size: 3,084 Bytes
860030d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""A small runnable accounting compiler, not a physical sequence/CAD generator."""
import json,math,argparse
from pathlib import Path

def gf4mul(a,b):
    v=0
    while b:
        if b&1:v^=a
        b>>=1;a<<=1
        if a&4:a^=7
    return v

def affine_code():
    return [[gf4mul(a,x)^b for x in range(4)] for a in range(4) for b in range(4)]

def compile_conflicts(spec):
    if not spec.get('retirement_verified',False):
        raise ValueError('Palette reuse requires an explicit qualified-retirement assumption')
    code=affine_code();output=[]
    for stage in spec['stages']:
        vertices=stage['classes'];neighbors={v:set() for v in vertices}
        for a,b in stage['conflicts']:
            if a not in neighbors or b not in neighbors or a==b:raise ValueError('Invalid edge')
            neighbors[a].add(b);neighbors[b].add(a)
        color={}
        for v in sorted(vertices,key=lambda v:(-len(neighbors[v]),v)):
            used={color[n] for n in neighbors[v] if n in color}
            c=next(c for c in range(len(code)) if c not in used)
            color[v]=c
        assert all(color[a]!=color[b] for a,b in stage['conflicts'])
        output.append(dict(stage=stage['stage'],palette_size=max(color.values())+1,
            assignment={v:code[c] for v,c in color.items()},
            risk_bound=stage['joins']*stage['join_error_bound']+stage['retirements']*stage['retirement_error_bound']))
    return dict(target=spec['target'],status='logical accounting only; physical assumptions unverified',
        stages=output,maximum_active_palette=max(x['palette_size'] for x in output),
        total_union_risk_bound=min(1,sum(x['risk_bound'] for x in output)),
        elementary_recognition_pairs=4,slots_per_port=4,
        warnings=['No DNA sequences generated','No collision graph inferred from geometry',
                  'Retirement flag is a user-supplied contract, not proof'])

def adaptive_pitch(sensitivity,epsilon,a=1,d=3,hmin=.001,hmax=1.):
    # Unit-volume equal-volume cells; sensitivity supplied per volume.
    n=len(sensitivity)
    def pitch(lam):
        return [hmax if s<=0 else min(hmax,max(hmin,(d/(lam*a*s))**(1/(a+d)))) for s in sensitivity]
    def err(h):return sum(s*x**a for s,x in zip(sensitivity,h))/n
    if err([hmin]*n)>epsilon:raise ValueError('Infeasible error budget at minimum pitch')
    lo=1e-20;hi=1.
    while err(pitch(hi))>epsilon:hi*=2
    for _ in range(100):
        mid=(lo+hi)/2
        if err(pitch(mid))>epsilon:lo=mid
        else:hi=mid
    h=pitch(hi)
    return dict(pitches=h,bounded_error=err(h),count_density=sum(x**(-d) for x in h)/n)

if __name__=='__main__':
    ap=argparse.ArgumentParser();ap.add_argument('input',type=Path);ap.add_argument('output',type=Path);args=ap.parse_args()
    result=compile_conflicts(json.loads(args.input.read_text()))
    result['adaptive_example']=adaptive_pitch([1.,1.,.001,.001],.1,hmin=.01,hmax=1.)
    args.output.write_text(json.dumps(result,indent=2))
    print('Compiled',len(result['stages']),'stages; active palette',result['maximum_active_palette'])