The emerge package implements goal-directed synchronization - distributed systems that pursue target coordination states through adaptive strategies. Inspired by how biological systems achieve morphological goals through multiple pathways, emerge maintains synchronization targets as invariants and switches strategies when convergence stalls.
Agents maintain a phase (0 to 2π) representing their position in an oscillation cycle. The system pursues target phase alignment through multiple strategies, adapting when the default approach fails to achieve the synchronization goal.
The Kuramoto model provides the synchronization dynamics (see Algorithm for details):
dθᵢ/dt = ωᵢ + (K/N) × Σⱼ sin(θⱼ - θᵢ)
Where:
- θᵢ = phase of agent i
- ωᵢ = natural frequency of agent i
- K = coupling strength (adaptively adjusted)
- N = number of neighbors
Goal-directedness adds adaptive strategy switching when this default dynamics fails to achieve targets (see Goal-Directed).
Agents have limited energy for adjustments, preventing oscillation and ensuring stable convergence. Energy depletes with actions and recovers over time.
emerge/
├── agent/ # Core agent implementation with optimizations
├── swarm/ # Swarm coordination and goal-directed convergence
│ └── goal_directed.go # Adaptive strategy switching (key file)
├── core/ # Fundamental types and interfaces
├── strategy/ # Multiple pathways to goals
├── completion/ # Pattern completion for gap filling
├── convergence/ # Convergence monitoring
├── goal/ # Goal management and blending
├── monitoring/ # System monitoring and metrics
├── decision/ # Decision engines
├── scale/ # Scaling utilities
└── trait/ # Agent traitsEach agent maintains:
- Phase (0 to 2π) - Position in oscillation cycle
- Frequency - Oscillation speed
- Energy - Available action resources
- LocalGoal - Individual preferences (see Goals)
- Influence - How much agent affects neighbors (0.0 to 1.0)
- Stubbornness - Resistance to external influence (0.0 to 1.0)
- CouplingStrength - Connection strength to neighbors
Agents automatically optimize based on swarm size:
Small swarms (≤100 agents)
- sync.Map for neighbor storage
- Standard atomic fields
- Simple iteration patterns
Large swarms (>100 agents)
- Fixed-size arrays for neighbors
- Grouped atomic fields to reduce cache bouncing
- Pre-allocated storage pools
- Goal setting - Define target synchronization state
- Strategy selection - Choose initial approach
- Local sensing - Agents observe neighbor states
- Adaptive adjustment - Apply current strategy
- Progress monitoring - Check convergence toward goal
- Strategy switching - Change approach if stuck
- Goal achievement - Continue until target reached
Coherence measures synchronization using the Kuramoto order parameter:
R = |Σ(e^(iθ))| / N- 0.0 = No synchronization (chaos)
- 0.5 = Partial synchronization
- 1.0 = Perfect synchronization
Swarms maintain target states as invariants, finding alternative paths when blocked (see Disruption):
- Phase - Target alignment point (maintained despite disruptions)
- Frequency - Goal oscillation rate (achieved through multiple strategies)
- Coherence - Target synchronization level (pursued adaptively)
See Strategies for detailed descriptions.
PhaseNudge (Gentle approach)
- Small incremental phase adjustments
- Minimal energy consumption
- First strategy tried for efficiency
FrequencyLock (Frequency-first approach)
- Aligns frequencies before phases
- Alternative path when phase adjustment alone fails
- Effective for disparate natural frequencies
PulseCoupling (Strong synchronization)
- Powerful synchronization pulses
- Used when gentle approaches stall
- Higher energy cost but faster convergence
EnergyAware (Resource-conscious)
- Balances goal achievement with resource limits
- Adapts strategy based on available energy
- Ensures sustainable convergence
Implement the DecisionMaker interface:
type DecisionMaker interface {
Decide(context DecisionContext) Decision
}
type DecisionContext struct {
Current State
Target State
Neighbors []NeighborState
Energy float64
}| Swarm size | Convergence time | Memory/agent | CPU usage |
|---|---|---|---|
| 10-100 | ~800ms | ~5KB | Minimal |
| 100-1000 | ~300ms/agent | ~3KB | Moderate |
| 1000-5000 | Sub-linear | ~2KB | Optimized |
- Swarm size > 100 → Array storage
- Update rate > 1000/sec → Atomic grouping
- Neighbors > 20 → Fixed neighbor arrays
Coordinate microservices to batch API calls:
import (
"github.com/carlisia/bio-adapt/client/emerge"
"github.com/carlisia/bio-adapt/emerge/swarm/scale"
)
// Simple: Use client API
client := emerge.MinimizeAPICalls(scale.Small) // 50 agents
client.Start(ctx) // Pursues goal adaptively
// Advanced: Direct swarm access if needed
import "github.com/carlisia/bio-adapt/emerge"
cfg := swarm.For(goal.MinimizeAPICalls)
swarm, _ := swarm.New(50, targetState, swarm.WithGoalConfig(cfg))
swarm.Run(ctx)Prevent thundering herd in scheduled tasks (see Use Cases):
// Use load distribution for anti-synchronization
client := emerge.DistributeLoad(scale.Small) // Automatically targets low coherence
client.Start(ctx)Natural load distribution:
// Custom configuration for moderate clustering
client := emerge.Custom().
WithGoal(goal.DistributeLoad).
WithScale(scale.Medium). // 200 agents
WithTargetCoherence(0.5). // Moderate clustering
Build()
client.Start(ctx)See Disruption for detailed coverage.
Agent failures
- Neighbors detect missing agents
- Automatic topology reconfiguration
- Graceful coherence degradation
Network partitions
- Local coherence within partitions
- Automatic re-merge when healed
- No split-brain issues
Byzantine agents
- Energy limits prevent unlimited disruption
- Stubbornness limits influence spread
- Statistical convergence despite bad actors
See Security for security considerations.
- Algorithm - Mathematical foundation
- Protocol - Synchronization protocol
- Goal-Directed - Goal pursuit mechanisms
- Disruption - Failure handling
- Decentralization - No central control
- Concurrency - Go implementation patterns
- Optimization - Performance enhancements
- Security - Security considerations
- Agents - Fundamental units
- Swarm - Agent collections
- Synchronization - Coordination
- Coherence - Measurement
- Phase - Oscillation position
- Frequency - Rate of change
- Energy - Resource constraints
- Goals - Objectives
- Strategies - Approaches