File size: 2,393 Bytes
84379f0 | 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 | from __future__ import annotations
from typing import List, Tuple, Sequence, Dict, Any, Optional
import uuid
import numpy as np
from ..contracts.graft_manifest import GraftSpec, GraftType
from ..substrate.neural_ports import NeuralPort
class CandidateGenerator:
"""Generates typed, budgeted neural graft candidate specifications."""
def __init__(
self,
default_rank: int = 8,
default_max_gain: float = 0.10,
ramp_steps: int = 50,
recurrent_state_dim: int = 16,
seed: int = 42,
):
self.default_rank = int(default_rank)
self.default_max_gain = float(default_max_gain)
self.ramp_steps = int(ramp_steps)
self.recurrent_state_dim = int(recurrent_state_dim)
self.rng = np.random.default_rng(seed)
def generate_candidates(
self,
port_pairs: Sequence[Tuple[NeuralPort, NeuralPort, float]],
parent_digest: str,
allowed_types: Tuple[GraftType, ...] = ("low_rank", "recurrent", "biological"),
max_candidates: int = 64,
) -> List[GraftSpec]:
"""Generates candidate GraftSpec objects across allowed graft architectures."""
candidates: List[GraftSpec] = []
if len(port_pairs) == 0:
return candidates
for idx, (src_port, dst_port, priority) in enumerate(port_pairs):
if len(candidates) >= max_candidates:
break
for g_type in allowed_types:
if len(candidates) >= max_candidates:
break
cid = f"graft_{g_type}_{uuid.uuid4().hex[:8]}"
spec = GraftSpec(
graft_id=cid,
graft_type=g_type,
source_neurons=src_port.indices,
target_neurons=dst_port.indices,
rank=self.default_rank,
max_gain=self.default_max_gain,
ramp_steps=self.ramp_steps,
parent_digest=parent_digest,
recurrent_state_dim=self.recurrent_state_dim,
metadata={
"source_port": src_port.port_id,
"target_port": dst_port.port_id,
"priority_score": float(priority),
},
)
candidates.append(spec)
return candidates
|