Skip to content

Routing Algorithm

Raul Montoya Cardenas edited this page Jul 29, 2026 · 2 revisions

Routing Algorithm

Wiki hero

Generated with Grok Build: Grok 4.5 · xAI Imagine (/imagine)

Per-tick pipeline in update_routing! (src/region_router.jl, main @ c9ff99c).

Scoring rule

For each region i:

score_i = α · density_i + β · surprise_i + γ · momentum_i
Symbol Default Meaning
α (ALPHA) 0.50 Normalized spike / activity rate
β (BETA) 0.35 Manifold surprise vs EMA
γ (GAMMA) 0.15 Routing-weight momentum
EMA_DECAY 0.05 EMA blend: 5% new estimate per tick
MIN_SCORE 0.01 Soft floor before/during normalization
EPSILON 1e-6 Stability floor

Stages

1. Spike density

spike_density[i] = regions[i].last_spike_rate

2. Readout EMA

ema[i,:] ← (1 − EMA_DECAY) · ema[i,:] + EMA_DECAY · regions[i].output

3. Manifold surprise

surprise[i] = ‖output − ema‖ / (‖ema‖ + ε)

High surprise ⇒ readout moved relative to recent history (regime / attractor change).

4. Momentum

momentum[i] = |routing_weights[i] − prev_routing_weights[i]|

On main, after each successful tick the code snapshots:

copyto!(router.prev_routing_weights, router.routing_weights)

so post-update prev_routing_weights matches the just-written weights (interop notes this). Scoring still multiplies by γ; treat momentum semantics carefully when interpreting diagnostics across ticks.

5. Cross-region inhibition

For each destination region dst:

inhibited[dst] = max(
  raw[dst] − Σ_src 1[adjacency[src,dst] > 0] · inhibition[src,dst] · raw[src],
  MIN_SCORE
)

adjacency_matrix is a binary-style mask in the hot path (magnitude not used as a continuous weight).

6. Softmax-like normalization

  1. Subtract max for numerical stability
  2. exp and divide by (sum + ε)
  3. Clamp entries below MIN_SCORE, re-normalize

Result: routing_weights length n_regions, sum ≈ 1.

Inhibition defaults

n_regions Default matrix
≤ 4 Top-left slice of historical asymmetric INHIBIT (4×4 table)
> 4 Zero diagonal; off-diagonal `0.08 /

Override with RegionRouter(; inhibition_matrix=M).

Design goals

  • In-place updates on pre-allocated buffers
  • No heap alloc on the hot path after construction
  • Float32 throughout
  • Tick counter increments every call

What is not configurable on main

α/β/γ, EMA decay, floors — module constants only. Per-router scoring config is not on this tip (tracked separately; e.g. open work around LIM-230 / GH#24).


Last updated: July 29, 2026 Updated by: Grok Build: Grok 4.5 Package tip reference: c9ff99c (main)

Clone this wiki locally