Skip to content

sim: ADAPTIVE flood routing with density-based relay selection and TX power saving#2466

Open
Heilo27 wants to merge 1 commit intomeshcore-dev:mainfrom
Heilo27:feature/adaptive-routing-sim
Open

sim: ADAPTIVE flood routing with density-based relay selection and TX power saving#2466
Heilo27 wants to merge 1 commit intomeshcore-dev:mainfrom
Heilo27:feature/adaptive-routing-sim

Conversation

@Heilo27
Copy link
Copy Markdown

@Heilo27 Heilo27 commented May 2, 2026

Summary

This PR adds a full native-C++ simulation harness for MeshCore, along with a new ADAPTIVE routing strategy that has been empirically validated across multiple topologies. Everything in the sim/ directory is simulation-only — no firmware code is touched.

What's new

ADAPTIVE routing strategy (sim/src/RoutingStrategies.h)

Hash-based relay gate selects a deterministic subset of nodes to relay each flood, with the subset size adapted to local neighbour density:

Tier Neighbours Relay % Rationale
SPARSE ≤ 4 100% Every relay counts; no redundancy to spare
MEDIUM 5–14 25% Sufficient redundancy, reduced collisions
DENSE ≥ 15 15% 7–8 relayers covers full mesh; -70% collisions

Selection: hash(packet_seed XOR node_seed) % 100 < relay_pct

  • Zero coordination — each node independently makes the same decision for a given packet
  • Load balanced — different packets select different relay subsets
  • Relay suppression — a node cancels its queued TX if it hears another node already relaying the same flood (hash-matched)

DensityEstimator (sim/src/DensityEstimator.h)

Passive neighbour density measurement from normal traffic — no extra protocol messages. Counts unique direct senders (hop_count == 1) over a 60-second sliding window. Thresholds are compile-time overridable.

Adaptive TX power saving (sim/src/SimNode.h)

Opt-in feature (power_save_enabled). ADAPTIVE-selected relay nodes reduce TX power in dense areas:

  • DENSE tier: configurable, default 10 dBm (vs 20 dBm full power) — saves ~75% TX current
  • MEDIUM tier: configurable, default 14 dBm — saves ~60% TX current
  • SPARSE tier: always full power — marginal links need every dB

Motivation: at a high-density event (festival, conference), nodes running at full power 100% of the time waste battery and raise the local RF noise floor. Reducing power when you have 15+ neighbours: (a) saves battery, (b) shrinks interference radius via the LoRa capture effect, (c) doesn't hurt local delivery since nearby nodes still receive well above sensitivity.

Validated: 100% delivery maintained at MODERATE (-10 dBm DENSE) across all topologies tested.

Simulation infrastructure

File Role
sim/src/SimBus.h Virtual RF medium — packet delivery, collision model, channel models
sim/src/SimNode.h Per-node MeshCore instance with metrics and routing hooks
sim/src/SimRadio.h Radio abstraction — airtime estimation, LoRa capture effect (6 dB threshold)
sim/src/SimMetrics.h Flood delivery rate, latency, per-flood aggregation
sim/src/SimRuntime.h Deterministic clock and RNG for reproducible runs

Channel models available: FullMeshModel, ChainModel, GridModel, PositionalModel.

Scenario suite

Scenario What it validates
scenario_adaptive ADAPTIVE vs DEFAULT vs PATH_SNR_HYBRID across FM50/Chain/Grid
scenario_concurrent 2–8 simultaneous flood sources
scenario_longchain 20-hop chains at marginal SNR (6 dB)
scenario_mixed ADAPTIVE + legacy DEFAULT firmware interoperability
scenario_dutycycle EU 1% duty cycle enforcement
scenario_relay_pct_sweep Grid sweep to find optimal DENSE%/MEDIUM% operating point
scenario_txpower TX power saving vs delivery rate vs energy — festival use case

Key empirical results

  • FM50, ADAPTIVE vs DEFAULT: equal delivery rate, -65% airtime, -70% collisions
  • Chain-20: ADAPTIVE stays at SPARSE tier throughout — identical behaviour to DEFAULT. No regression on sparse topologies.
  • TX power MODERATE (-10 dBm DENSE, -6 dBm MEDIUM): 100% delivery maintained, ~35% energy reduction
  • Legacy interop: zero delivery regression when ADAPTIVE and DEFAULT nodes coexist
  • Duty cycle: ADAPTIVE uses ~40% fewer TX budget slots than DEFAULT at the same load

Bug fixes (found during development)

  • DensityEstimator: replaced seen[64] fixed array with std::unordered_set — was silently capping unique neighbour count at 64
  • SimBus::onTransmit: added len > 255 guard before memcpy into 255-byte buffer
  • SimNode p_forward gate: >=> — was silently suppressing relays at exactly p_forward=1.0

Building

cd sim
mkdir build && cd build
cmake ..
make -j4
./scenario_adaptive
./scenario_txpower

Requires a C++17 compiler. No dependencies beyond what's already in the repo (uses lib/ed25519, deps/arduinolibs/Crypto).

Not in this PR

This PR is simulation-only. The ADAPTIVE strategy would need firmware integration work (replacing the node_seed with a pub-key-prefix hash, hooking getRetransmitDelay()) before it could run on hardware. That's a separate conversation — wanted to get the algorithm validated first.

…aving

Adds a complete simulation harness for the MeshCore radio platform, including:

### ADAPTIVE routing strategy (sim/src/RoutingStrategies.h)
Hash-based relay gate selects a deterministic subset of nodes to forward each
flood, with relay percentage adapted to local neighbour density:
  - SPARSE  (<=4 neighbours): 100% relay -- no redundancy to spare
  - MEDIUM  (5-14):           25% relay
  - DENSE   (>=15):           15% relay

Selection uses hash(packet_seed XOR node_seed) with no coordination required.
Different packets select different subsets for stochastic load balancing.

Relay suppression: nodes cancel queued outbound TX if they overhear another
node already relaying the same flood (matched by packet hash).

### DensityEstimator (sim/src/DensityEstimator.h)
Passive sliding-window neighbour count from normal traffic -- no extra messages.
Only counts direct senders (hop_count==1) over a configurable window (default 60s).
Uses std::unordered_set for unique counting; no fixed-array cap.

### Adaptive TX power saving (sim/src/SimNode.h)
power_save_enabled reduces TX power in DENSE/MEDIUM tiers:
  DENSE:  configurable (default 10 dBm, saves 75% TX current vs 20 dBm)
  MEDIUM: configurable (default 14 dBm, saves 60% TX current)
  SPARSE: always full power -- every hop counts on marginal links

Validated: 100% delivery maintained at -10dBm DENSE reduction. Energy savings
~35% radio total. TX power reduction also lowers area RF noise floor.

### Scenario suite (sim/scenarios/)
  scenario_adaptive:        ADAPTIVE vs DEFAULT vs PATH_SNR_HYBRID comparison
  scenario_concurrent:      2-8 simultaneous flood sources
  scenario_longchain:       20-hop chains at marginal SNR
  scenario_mixed:           ADAPTIVE + legacy DEFAULT interoperability
  scenario_dutycycle:       EU 1% duty cycle enforcement validation
  scenario_relay_pct_sweep: grid sweep to find optimal DENSE%/MEDIUM% operating point
  scenario_txpower:         TX power saving vs delivery rate vs energy trade-off

### Key empirical results
  FM50 ADAPTIVE vs DEFAULT:  equal delivery rate, -65% airtime, -70% collisions
  CH20 (chain):              ADAPTIVE = SPARSE throughout, identical to DEFAULT
  TX power MODERATE (-10dB): 100% delivery, -35% energy across all topologies
  Legacy interop:            zero delivery regression with mixed firmware

### Bug fixes
  - DensityEstimator: replace seen[64] array with unordered_set (was capping at 64 neighbours)
  - SimBus::onTransmit: add len > 255 guard before memcpy into 255-byte buffer
  - SimNode p_forward gate: >= -> > (was silently dropping relays at p_forward=1.0)
  - SimNode TX power reset: remove redundant !power_save_enabled clause
  - scenario_concurrent: fix stale ConcurrentResult* pointer after vector reallocation
  - scenario_relay_pct_sweep: update TxCallback from 4-arg to 5-arg (tx_power_dbm)
  - scenario_adaptive: remove unused variable last_suppressed

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant