Download src/infinite_players/memory_contract.py from PureOne/Infinite-Players: direct link, hf CLI and curl.
- Browser
- Download file 2.16 kB
-
https://huggingface.co/datasets/PureOne/Infinite-Players/resolve/main/src/infinite_players/memory_contract.py
- Command line
-
hf download hf://datasets/PureOne/Infinite-Players/src/infinite_players/memory_contract.py
-
curl -L -o memory_contract.py https://huggingface.co/datasets/PureOne/Infinite-Players/resolve/main/src/infinite_players/memory_contract.py
2.16 kB
| """Exact small one-shot functional-memory contracts; classical graph-coloring formulation.""" | |
| from __future__ import annotations | |
| import math | |
| import numpy as np | |
| def confusability_graph(answers, support=None): | |
| a = np.asarray(answers) | |
| if a.ndim != 2: | |
| raise ValueError("answers must be records x future transcripts") | |
| s = np.ones(a.shape,dtype=bool) if support is None else np.asarray(support,dtype=bool) | |
| if s.shape != a.shape: | |
| raise ValueError("Support shape mismatch") | |
| adjacency = np.zeros((len(a),len(a)),dtype=bool) | |
| for i in range(len(a)): | |
| for j in range(i): | |
| edge = bool(np.any(s[i]&s[j]&(a[i]!=a[j]))) | |
| adjacency[i,j]=adjacency[j,i]=edge | |
| return adjacency | |
| def exact_coloring(adjacency): | |
| """Backtracking for small research fixtures only; general coloring is exponential.""" | |
| g = np.asarray(adjacency,dtype=bool) | |
| if g.ndim != 2 or g.shape[0] != g.shape[1] or not np.array_equal(g,g.T) or np.any(np.diag(g)): | |
| raise ValueError("Require simple undirected graph") | |
| n=len(g) | |
| if n==0: return 0,[] | |
| order=sorted(range(n),key=lambda i:-int(g[i].sum())) | |
| colors=[-1]*n | |
| def assign(t,k,used): | |
| if t==n:return True | |
| v=order[t] | |
| banned={colors[j] for j in range(n) if g[v,j] and colors[j]>=0} | |
| for c in range(min(k,used+1)): | |
| if c not in banned: | |
| colors[v]=c | |
| if assign(t+1,k,max(used,c+1)):return True | |
| colors[v]=-1 | |
| return False | |
| # Clique shortcut handles the retrospective random-access witness without exponential futile search. | |
| if np.all(g | np.eye(n,dtype=bool)):return n,list(range(n)) | |
| for k in range(1,n+1): | |
| if assign(0,k,0):return k,colors.copy() | |
| raise RuntimeError("Coloring failure") | |
| def binary_entropy(error): | |
| if not 0<=error<=1: raise ValueError("error outside [0,1]") | |
| if error in (0,1):return 0. | |
| return -error*math.log2(error)-(1-error)*math.log2(1-error) | |
| def retrospective_memory_lower_bits(n,error): | |
| if n<1 or not 0<=error<=.5:raise ValueError("Require n>=1 and error in [0,.5]") | |
| return n*(1-binary_entropy(error)) | |