micro_routing
Dynamic routing and context management for micro-neural networks
The micro_routing
crate provides intelligent routing mechanisms for directing inputs to the most appropriate micro-networks in the Semantic Cartan Matrix system. It implements context-aware decision making and adaptive routing strategies.
๐ Features
- 4 Routing Strategies: Rule-based, learned, context-aware, and hybrid routing
- Context Management: Stateful routing with historical success tracking
- Gating Networks: Neural network-based routing decisions
- Execution Planning: Parallel and sequential micro-net coordination
- Performance Optimization: Sub-millisecond routing decisions
- no_std Compatible: Works in embedded and WebAssembly environments
๐ฆ Installation
Add this to your Cargo.toml
:
[]
= "0.1.0"
= "0.1.0"
# Optional features
= { = "0.1.0", = ["std", "neural-gating"] }
๐๏ธ Architecture
Router System
The core Router
provides multiple routing strategies:
use ;
use ;
// Create a router with hybrid strategy
let mut router = new;
// Add micro-nets to the routing table
router.register_agent;
router.register_agent;
router.register_agent;
// Route input to appropriate agents
let input = from_slice;
let plan = router.route;
match plan
๐ง Routing Strategies
1. Rule-Based Routing
Uses predefined rules and pattern matching:
use ;
let mut router = new;
// Add routing rules
router.add_rule;
router.add_rule;
// Default fallback
router.set_default;
2. Learned Gating Network
Neural network-based routing decisions:
use ;
let config = GatingConfig ;
let mut gating_net = new;
// Train the gating network (optional)
gating_net.train;
// Get routing probabilities
let input = from_slice;
let probabilities = gating_net.forward;
// Route to highest probability agent
let best_agent = probabilities.argmax;
3. Context-Aware Routing
Incorporates historical context and success tracking:
use ;
let mut router = new;
let mut context = new;
// Update context with recent interactions
context.add_interaction; // Success score
context.add_interaction;
// Route considering context
let plan = router.route_with_context;
// Context influences future routing decisions
// Successful agents get higher probability
4. Hybrid Routing
Combines multiple strategies with adaptive weighting:
use ;
let mut router = new;
// Configure strategy weights
router.set_weights;
// Adaptive weighting based on performance
router.enable_adaptive_weighting;
// Router automatically adjusts strategy weights
// based on routing success rates
โก Execution Planning
Execution Modes
The router generates different execution plans:
use ExecutionPlan;
// Single agent execution
let plan = Single;
// Parallel execution for consensus
let plan = Parallel;
// Sequential pipeline
let plan = Sequential;
// Execute the plan
let results = executor.execute_plan;
Load Balancing
Automatic load balancing across agents:
use ;
let mut balancer = new;
// Monitor agent loads
let metrics = LoadMetrics ;
balancer.update_metrics;
// Route considering load
let plan = balancer.route_with_load_balancing;
๐ Context Management
Context State
Track interaction history and agent performance:
use ;
let mut context = new;
// Record successful interaction
let record = InteractionRecord ;
context.add_record;
// Get agent success rate
let success_rate = context.get_success_rate;
// Get contextual similarity
let similarity = context.similarity_to_previous;
Memory Management
Efficient context storage with LRU eviction:
use ;
let config = MemoryConfig ;
let mut memory = new;
// Automatically manages memory usage
// Evicts old entries when capacity reached
๐ฏ Performance
Benchmarks
Operation | Latency (ฮผs) | Throughput (ops/sec) |
---|---|---|
Rule-Based Routing | 5.2 | 192,000 |
Neural Gating | 45.8 | 21,800 |
Context Lookup | 2.1 | 476,000 |
Plan Generation | 12.3 | 81,300 |
Memory Usage
- Context Entry: 64 bytes per interaction record
- Gating Network: ~50KB for 8 agents, 64 hidden units
- Rule Table: ~1KB per 100 rules
๐ง Configuration
Feature Flags
[]
= ["rule-based"]
= ["dep:std"]
= ["dep:candle-core"]
= ["dep:lz4"]
= ["dep:prometheus"]
Routing Configuration
use ;
let config = RouterConfig ;
let router = from_config;
๐งช Testing
Run the comprehensive test suite:
# Unit tests
# Integration tests with micro_core
# Benchmarks
# Test all routing strategies
๐ Examples
See the examples/
directory for:
- Basic Routing: Simple agent selection
- Context Management: Stateful routing examples
- Neural Gating: Training and using gating networks
- Load Balancing: Distributed routing strategies
- Hybrid Systems: Combining multiple approaches
๐ค Integration
With micro_core
use ;
use ;
// Seamless integration with micro_core agents
let router = new;
// Router automatically works with any MicroNet implementation
With micro_swarm
use SwarmOrchestrator;
use Router;
// Router is used by orchestrator for agent selection
let orchestrator = with_router;
๐ ๏ธ Development
Building
# Standard build
# With all features
# For WebAssembly
Testing Neural Gating
# Requires neural-gating feature
๐ License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
๐ Related Crates
micro_core
: Core types and neural network traitsmicro_cartan_attn
: Attention mechanisms for agent coordinationmicro_metrics
: Performance monitoring and analyticsmicro_swarm
: High-level orchestration and coordination
Part of the rUv-FANN Semantic Cartan Matrix system ๐ง ๐