File size: 9,313 Bytes
d997548
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
"""
Encompassment Engine V2 — K6 as CEO (not as counter).

Fixes V1's fatal flaw: V1's centrality was 80% K1+K3+K5 duplication.
V2 uses ONLY new information:
  - spread: how many cells share this pattern
  - diversity: how many nearby cells (from plexus)
  - uniqueness: how isolated this pattern is

V2 DECIDES (not measures):
  - decide_split: K6 says yes/no (with S as secondary)
  - decide_birth: K6 says yes/no
  - guide_retrieval: K6 ranks cells for attention
"""
from collections import defaultdict
from typing import Dict, List, Optional
import math


class EncompassmentEngineV2:
    def __init__(self, max_index_size=10000):
        self.pattern_index: Dict[str, List[str]] = defaultdict(list)
        self.centrality: Dict[str, float] = {}
        self.cell_metadata: Dict[str, dict] = {}
        self.birth_history: List[dict] = []
        self.split_history: List[dict] = []
        self.max_index_size = max_index_size
        self._total_registrations = 0

    # ------------------------------------------------------------------
    # Registration
    # ------------------------------------------------------------------
    def register(self, cell_id: str, fp: str, layer_idx: int = -1):
        """Register a cell's pattern fingerprint. K1-K5 NOT used."""
        if not fp:
            return
        if cell_id in self.cell_metadata:
            old_fp = self.cell_metadata[cell_id].get("fp")
            if old_fp == fp:
                self._recompute_centrality(cell_id)
                return
            if old_fp and cell_id in self.pattern_index.get(old_fp, []):
                self.pattern_index[old_fp].remove(cell_id)

        self.pattern_index[fp].append(cell_id)
        self.cell_metadata[cell_id] = {"fp": fp, "layer_idx": layer_idx}
        self._recompute_centrality(cell_id)
        self._total_registrations += 1

        if len(self.pattern_index) > self.max_index_size:
            self._prune()

    def _count_nearby_cells(self, cell_id: str, hamming_fn, threshold: float = 0.6) -> int:
        """Count cells whose pattern is *close but not identical* to this one."""
        meta = self.cell_metadata.get(cell_id)
        if not meta:
            return 0
        my_fp = meta["fp"]
        count = 0
        for fp, members in self.pattern_index.items():
            if fp == my_fp:
                continue
            try:
                sim = hamming_fn(my_fp, fp)
            except Exception:
                continue
            if sim > threshold:
                count += len(members)
        return count

    def _recompute_centrality(self, cell_id: str,
                               hamming_fn=None, threshold: float = 0.6):
        """
        K6 = new information ONLY:
          - spread: how many cells share this exact pattern
          - diversity: how many nearby (similar but not identical) cells
        NO K1, K3, K5.
        """
        meta = self.cell_metadata.get(cell_id)
        if not meta:
            self.centrality[cell_id] = 0.0
            return

        fp = meta["fp"]
        n_sharing = len(self.pattern_index.get(fp, []))

        # spread: [0, 1]
        spread = min(1.0, math.log1p(n_sharing) / math.log1p(20))

        # diversity: size of overall pattern space (how many distinct patterns)
        n_patterns = len(self.pattern_index)
        diversity = min(1.0, math.log1p(n_patterns) / math.log1p(50))

        # centrality = CEO score (pure network info)
        c = 0.5 * spread + 0.5 * diversity
        self.centrality[cell_id] = max(0.0, min(1.0, c))

    def refresh_all(self, hamming_fn=None):
        """Recompute all centralities (call after several registrations)."""
        for cid in list(self.cell_metadata.keys()):
            self._recompute_centrality(cid, hamming_fn=hamming_fn)

    def _prune(self):
        keep = {}
        for fp, members in self.pattern_index.items():
            if len(members) >= 2:
                keep[fp] = members
            else:
                for m in members:
                    if self.centrality.get(m, 0.0) > 0.5:
                        keep[fp] = members
                        break
        self.pattern_index = defaultdict(list, keep)

    # ------------------------------------------------------------------
    # K6 read
    # ------------------------------------------------------------------
    def K6(self, cell_id: str) -> float:
        return float(self.centrality.get(cell_id, 0.0))

    def K6_array(self, cell_ids: List[str]) -> List[float]:
        return [self.K6(c) for c in cell_ids]

    # ------------------------------------------------------------------
    # CEO decisions (not measurements)
    # ------------------------------------------------------------------
    def decide_split(self, cell_id: str, S_value: float, S_star: float) -> bool:
        """
        Split decision:
          - S >= S* (saturation signal)
          - AND centrality >= 0.5 (pattern is in active region of graph)
        Centrality prevents wasting splits on isolated noise patterns.
        """
        c = self.centrality.get(cell_id, 0.0)
        return (S_value >= S_star) and (c >= 0.15)   # threshold for R7 scale

    def decide_birth(self, fp: str, hamming_fn, threshold: float = 0.55) -> bool:
        """
        Birth = pattern that is truly new.
        - Not already in index
        - Not similar to any existing pattern
        """
        if not fp:
            return False
        if fp in self.pattern_index and len(self.pattern_index[fp]) > 0:
            return False
        for existing_fp in self.pattern_index:
            try:
                sim = hamming_fn(fp, existing_fp)
            except Exception:
                continue
            if sim > threshold:
                return False
        return True

    def record_birth(self, cell_id: str, parent_id: str, fp: str, step: int):
        self.birth_history.append({
            "cell_id": cell_id, "parent_id": parent_id,
            "fp": fp, "step": step,
        })

    def record_split(self, parent_id: str, children: List[str],
                     S: float, K6: float, step: int):
        self.split_history.append({
            "parent_id": parent_id, "children": children,
            "S": round(float(S), 4), "K6": round(float(K6), 4),
            "step": step,
        })

    # ------------------------------------------------------------------
    # Retrieval guidance
    # ------------------------------------------------------------------
    def guide_retrieval(self, query_fp: str, hamming_fn,
                         top_k: int = 5) -> List[str]:
        if not query_fp:
            return []
        scores = []
        for fp, members in self.pattern_index.items():
            try:
                sim = hamming_fn(query_fp, fp)
            except Exception:
                sim = 0.0
            if sim < 0.1:
                continue
            for cid in members:
                c = self.centrality.get(cid, 0.0)
                scores.append((sim * (0.5 + 0.5 * c), cid))
        scores.sort(reverse=True)
        return [cid for _, cid in scores[:top_k]]

    # ------------------------------------------------------------------
    # Diagnostics
    # ------------------------------------------------------------------
    def summary(self) -> dict:
        n_patterns = len(self.pattern_index)
        n_cells = len(self.cell_metadata)
        if self.centrality:
            c_vals = list(self.centrality.values())
            c_min, c_max = min(c_vals), max(c_vals)
            c_mean = sum(c_vals) / len(c_vals)
        else:
            c_min = c_max = c_mean = 0.0
        return {
            "n_patterns": n_patterns,
            "n_cells_indexed": n_cells,
            "centrality_min": round(c_min, 4),
            "centrality_max": round(c_max, 4),
            "centrality_mean": round(c_mean, 4),
            "births_total": len(self.birth_history),
            "splits_total": len(self.split_history),
            "total_registrations": self._total_registrations,
        }


if __name__ == "__main__":
    def fake_hamming(a, b):
        if not a or not b:
            return 0.0
        n = min(len(a), len(b))
        return sum(1 for i in range(n) if a[i] == b[i]) / n

    e = EncompassmentEngineV2()
    # Unique pattern
    e.register("cell-1", "aaaaaaaaaa", layer_idx=0)
    # Same pattern (spread)
    e.register("cell-2", "aaaaaaaaaa", layer_idx=0)
    e.register("cell-3", "aaaaaaaaaa", layer_idx=0)
    # Similar but not identical (diversity)
    e.register("cell-4", "aaaaaaaabb", layer_idx=0)
    # Far away
    e.register("cell-5", "ffffffffff", layer_idx=0)

    e.refresh_all(hamming_fn=fake_hamming)

    print("=== K6 V2 (CEO) ===")
    print(f"cell-1 (with 2 sharing + nearby): K6 = {e.K6('cell-1'):.4f}")
    print(f"cell-5 (isolated): K6 = {e.K6('cell-5'):.4f}")
    print(f"summary: {e.summary()}")
    print()
    print("=== Decisions ===")
    print(f"decide_birth(new pattern): {e.decide_birth('zzzzzzzzzz', fake_hamming)}")
    print(f"decide_birth(existing): {e.decide_birth('aaaaaaaaaa', fake_hamming)}")
    print(f"decide_split(cell-1, S=0.9, S*=0.85): {e.decide_split('cell-1', 0.9, 0.85)}")
    print(f"decide_split(cell-5, S=0.9, S*=0.85): {e.decide_split('cell-5', 0.9, 0.85)}")