File size: 13,840 Bytes
c3ed182
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
"""Autonomous Neural Evolution: Transaction-Safe Graph-Topology Mutation.

Enables SYNAPSE to dynamically grow, remove, split, duplicate, and reorganise
neural circuits based on task performance and metabolic constraints.
All mutations are transaction-safe with atomic preview, parent digest verification,
and rollback receipts.
"""
from __future__ import annotations
import copy
import math
import uuid
from dataclasses import dataclass, field
import numpy as np
import torch
import torch.nn as nn
from .stateio import digest

SCHEMA = 'synapse-topology-mutation-v1'

@dataclass
class MutationReceipt:
    mutation_id: str
    mutation_type: str
    parent_digest: str
    resulting_digest: str
    details: dict
    undo_state: dict

    def to_dict(self) -> dict:
        return {
            'mutation_id': self.mutation_id,
            'mutation_type': self.mutation_type,
            'parent_digest': self.parent_digest,
            'resulting_digest': self.resulting_digest,
            'details': self.details,
        }

class TopologyEvolutionEngine:
    """Manages structural mutations on an IntegratedAgent's connectome."""

    def __init__(self, metabolic_penalty_per_neuron: float = 0.05, metabolic_penalty_per_synapse: float = 0.01):
        self.metabolic_penalty_per_neuron = metabolic_penalty_per_neuron
        self.metabolic_penalty_per_synapse = metabolic_penalty_per_synapse
        self.mutation_history: list[dict] = []
        self._undo_stack: dict[str, dict] = {}

    def add_neuron(self, agent, neuropil: int = 0) -> MutationReceipt:
        """Add a new neuron to the specified neuropil with bounded connectivity."""
        parent_digest = digest(agent.export_state())
        undo_state = copy.deepcopy(agent.export_state())
        undo_graph = {
            'role': agent.graph.role.copy(),
            'origin': agent.graph.origin.copy(),
            'cell_type': agent.graph.cell_type.copy(),
            'names': list(agent.graph.names),
            'graft_pre': agent.graph.graft_pre.copy(),
            'graft_post': agent.graph.graft_post.copy(),
            'brain_n': agent.brain.n,
        }
        mutation_id = uuid.uuid4().hex

        new_idx = agent.brain.n
        agent.brain.n += 1
        agent.graph.role = np.append(agent.graph.role, 'central')
        agent.graph.origin = np.append(agent.graph.origin, neuropil)
        agent.graph.cell_type = np.append(agent.graph.cell_type, 'interneuron')
        agent.graph.names.append(f'new_{new_idx}')

        # Expand rate and state vectors
        with torch.no_grad():
            agent.brain.r = torch.cat([agent.brain.r, torch.zeros(1)])
            agent.brain.activity_ema = torch.cat([agent.brain.activity_ema, torch.zeros(1)])
            agent.brain.elig = torch.cat([agent.brain.elig, torch.zeros(1)])
            agent.brain.heb = torch.cat([agent.brain.heb, torch.zeros(1)])
            agent.evoked = torch.cat([agent.evoked, torch.zeros(1)])

            # Connect new neuron with 2 random input and 2 random output graft edges
            rng = np.random.default_rng()
            possible_peers = list(range(max(0, new_idx - 32), new_idx))
            if len(possible_peers) >= 2:
                in_peers = rng.choice(possible_peers, size=2, replace=False)
                out_peers = rng.choice(possible_peers, size=2, replace=False)

                new_pres = list(agent.graph.graft_pre)
                new_posts = list(agent.graph.graft_post)
                new_weights = list(agent.brain.graft_w.detach().cpu().numpy())

                for p in in_peers:
                    new_pres.append(int(p))
                    new_posts.append(new_idx)
                    new_weights.append(float(rng.normal(0.0, 0.1)))
                for p in out_peers:
                    new_pres.append(new_idx)
                    new_posts.append(int(p))
                    new_weights.append(float(rng.normal(0.0, 0.1)))

                agent.graph.graft_pre = np.array(new_pres, dtype=np.int64)
                agent.graph.graft_post = np.array(new_posts, dtype=np.int64)
                agent.brain.graft_w = nn.Parameter(torch.tensor(new_weights, dtype=torch.float32))

        resulting_digest = digest(agent.export_state())
        details = {
            'new_neuron_index': new_idx,
            'neuropil': neuropil,
            'total_neurons': agent.brain.n,
            'new_synapses': 4,
            'metabolic_delta': self.metabolic_penalty_per_neuron + 4 * self.metabolic_penalty_per_synapse
        }
        receipt = MutationReceipt(mutation_id, 'add_neuron', parent_digest, resulting_digest, details, undo_state)
        self._undo_stack[mutation_id] = {'agent_state': undo_state, 'graph_state': undo_graph}
        self.mutation_history.append(receipt.to_dict())
        return receipt

    def split_neuron(self, agent, node_id: int) -> MutationReceipt:
        """Split a bottleneck neuron into two specialized sister neurons."""
        if not (0 <= node_id < agent.brain.n):
            raise ValueError(f'Invalid node_id {node_id}')
        parent_digest = digest(agent.export_state())
        undo_state = copy.deepcopy(agent.export_state())
        mutation_id = uuid.uuid4().hex

        neuropil = int(agent.graph.origin[node_id]) if node_id < len(agent.graph.origin) else 0
        sister_id = agent.brain.n
        agent.brain.n += 1
        agent.graph.role = np.append(agent.graph.role, str(agent.graph.role[node_id]))
        agent.graph.origin = np.append(agent.graph.origin, neuropil)
        agent.graph.cell_type = np.append(agent.graph.cell_type, str(agent.graph.cell_type[node_id]))
        agent.graph.names.append(f'sister_{node_id}')

        with torch.no_grad():
            agent.brain.r = torch.cat([agent.brain.r, torch.zeros(1)])
            agent.brain.activity_ema = torch.cat([agent.brain.activity_ema, torch.zeros(1)])
            agent.brain.elig = torch.cat([agent.brain.elig, torch.zeros(1)])
            agent.brain.heb = torch.cat([agent.brain.heb, torch.zeros(1)])
            agent.evoked = torch.cat([agent.evoked, torch.zeros(1)])

            # Split incoming edges between original and sister; duplicate outgoing edges
            new_pres = list(agent.graph.graft_pre)
            new_posts = list(agent.graph.graft_post)
            new_weights = list(agent.brain.graft_w.detach().cpu().numpy())

            for i in range(len(agent.graph.graft_pre)):
                pre = int(agent.graph.graft_pre[i])
                post = int(agent.graph.graft_post[i])
                w = float(new_weights[i])

                if post == node_id and i % 2 == 1:
                    # Divert half of incoming connections to sister
                    new_posts[i] = sister_id
                elif pre == node_id:
                    # Duplicate outgoing connection for sister
                    new_pres.append(sister_id)
                    new_posts.append(post)
                    new_weights.append(w * 0.5)

            agent.graph.graft_pre = np.array(new_pres, dtype=np.int64)
            agent.graph.graft_post = np.array(new_posts, dtype=np.int64)
            agent.brain.graft_w = nn.Parameter(torch.tensor(new_weights, dtype=torch.float32))

        resulting_digest = digest(agent.export_state())
        details = {
            'original_node': node_id,
            'sister_node': sister_id,
            'total_neurons': agent.brain.n,
            'total_grafts': len(agent.graph.graft_pre)
        }
        receipt = MutationReceipt(mutation_id, 'split_neuron', parent_digest, resulting_digest, details, undo_state)
        self._undo_stack[mutation_id] = undo_state
        self.mutation_history.append(receipt.to_dict())
        return receipt

    def duplicate_subcircuit(self, agent, node_ids: list[int]) -> MutationReceipt:
        """Duplicate a specialist subnetwork to create parallel processing channels."""
        if not node_ids or any(n < 0 or n >= agent.brain.n for n in node_ids):
            raise ValueError('Invalid node_ids for subcircuit duplication')
        parent_digest = digest(agent.export_state())
        undo_state = copy.deepcopy(agent.export_state())
        mutation_id = uuid.uuid4().hex

        id_map = {}
        for old_id in node_ids:
            new_id = agent.brain.n
            id_map[old_id] = new_id
            agent.brain.n += 1
            agent.graph.role = np.append(agent.graph.role, str(agent.graph.role[old_id]))
            agent.graph.origin = np.append(agent.graph.origin, int(agent.graph.origin[old_id]))
            agent.graph.cell_type = np.append(agent.graph.cell_type, str(agent.graph.cell_type[old_id]))
            agent.graph.names.append(f'dup_{old_id}')

        with torch.no_grad():
            pad_len = len(node_ids)
            agent.brain.r = torch.cat([agent.brain.r, torch.zeros(pad_len)])
            agent.brain.activity_ema = torch.cat([agent.brain.activity_ema, torch.zeros(pad_len)])
            agent.brain.elig = torch.cat([agent.brain.elig, torch.zeros(pad_len)])
            agent.brain.heb = torch.cat([agent.brain.heb, torch.zeros(pad_len)])
            agent.evoked = torch.cat([agent.evoked, torch.zeros(pad_len)])

            new_pres = list(agent.graph.graft_pre)
            new_posts = list(agent.graph.graft_post)
            new_weights = list(agent.brain.graft_w.detach().cpu().numpy())

            node_set = set(node_ids)
            for i in range(len(agent.graph.graft_pre)):
                pre = int(agent.graph.graft_pre[i])
                post = int(agent.graph.graft_post[i])
                w = float(new_weights[i])

                if pre in node_set and post in node_set:
                    # Internal edge duplicated
                    new_pres.append(id_map[pre])
                    new_posts.append(id_map[post])
                    new_weights.append(w)
                elif pre in node_set:
                    # Outgoing edge duplicated
                    new_pres.append(id_map[pre])
                    new_posts.append(post)
                    new_weights.append(w * 0.5)
                elif post in node_set:
                    # Incoming edge duplicated
                    new_pres.append(pre)
                    new_posts.append(id_map[post])
                    new_weights.append(w * 0.5)

            agent.graph.graft_pre = np.array(new_pres, dtype=np.int64)
            agent.graph.graft_post = np.array(new_posts, dtype=np.int64)
            agent.brain.graft_w = nn.Parameter(torch.tensor(new_weights, dtype=torch.float32))

        resulting_digest = digest(agent.export_state())
        details = {
            'duplicated_nodes': len(node_ids),
            'id_map': id_map,
            'total_neurons': agent.brain.n,
            'total_grafts': len(agent.graph.graft_pre)
        }
        receipt = MutationReceipt(mutation_id, 'duplicate_subcircuit', parent_digest, resulting_digest, details, undo_state)
        self._undo_stack[mutation_id] = undo_state
        self.mutation_history.append(receipt.to_dict())
        return receipt

    def prune_synapses(self, agent, threshold: float = 0.02) -> MutationReceipt:
        """Prune functionally dormant synapses below activity threshold with metabolic credit."""
        parent_digest = digest(agent.export_state())
        undo_state = copy.deepcopy(agent.export_state())
        mutation_id = uuid.uuid4().hex

        with torch.no_grad():
            w = agent.brain.graft_w.detach().cpu().numpy()
            keep_mask = np.abs(w) >= threshold

            if not np.any(keep_mask):
                # Always keep at least the top 10% strongest connections
                top_k = max(1, len(w) // 10)
                keep_indices = np.argsort(np.abs(w))[-top_k:]
                keep_mask = np.zeros(len(w), dtype=bool)
                keep_mask[keep_indices] = True

            pruned_count = int(np.sum(~keep_mask))
            agent.graph.graft_pre = agent.graph.graft_pre[keep_mask]
            agent.graph.graft_post = agent.graph.graft_post[keep_mask]
            agent.brain.graft_w = nn.Parameter(torch.tensor(w[keep_mask], dtype=torch.float32))

        resulting_digest = digest(agent.export_state())
        details = {
            'pruned_synapses': pruned_count,
            'retained_synapses': int(np.sum(keep_mask)),
            'threshold': threshold,
            'metabolic_credit': pruned_count * self.metabolic_penalty_per_synapse
        }
        receipt = MutationReceipt(mutation_id, 'prune_synapses', parent_digest, resulting_digest, details, undo_state)
        self._undo_stack[mutation_id] = undo_state
        self.mutation_history.append(receipt.to_dict())
        return receipt

    def rollback(self, agent, mutation_id: str) -> dict:
        """Rollback a structural mutation using its saved undo state."""
        if mutation_id not in self._undo_stack:
            raise KeyError(f'Mutation {mutation_id} not found in undo stack')
        saved = self._undo_stack.pop(mutation_id)
        if isinstance(saved, dict) and 'graph_state' in saved:
            undo_state = saved['agent_state']
            gs = saved['graph_state']
            agent.graph.role = gs['role'].copy()
            agent.graph.origin = gs['origin'].copy()
            agent.graph.cell_type = gs['cell_type'].copy()
            agent.graph.names = list(gs['names'])
            agent.graph.graft_pre = gs['graft_pre'].copy()
            agent.graph.graft_post = gs['graft_post'].copy()
            agent.brain.n = gs['brain_n']
        else:
            undo_state = saved

        agent.import_state(undo_state)
        return {
            'status': 'ok',
            'rolled_back': mutation_id,
            'restored_digest': digest(agent.export_state()),
            'scope': 'transaction-safe graph topology mutation rollback'
        }