AntHocNet 2.6.1
Paper-faithful ant-colony ad hoc routing: the shared core and its adapters
Loading...
Searching...
No Matches
link_metric.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT OR GPL-2.0-only
2// Copyright (C) 2026 Daniel Henrique Joppi
3
4/**
5 * ILinkMetric: the strategy that turns a backward ant's path observation into a
6 * pheromone value (Eq.2). Extracting it from advanceBackAnt makes the pure core
7 * the natural home for both the canonical metric and pluggable research metrics
8 * (fuzzy / energy / QoS), selected by config, without touching the state machine
9 * (item 16, ADR-0003).
10 *
11 * The metric must be pure: it reads only the observation, never the simulator,
12 * clock, or RNG. Adapter-supplied signals (energy, SNR, queue occupancy) enter
13 * through LinkObservation, keeping the core free of simulator dependencies.
14 */
15#ifndef ANTHOCNET_CORE_LINK_METRIC_H
16#define ANTHOCNET_CORE_LINK_METRIC_H
17
18#include <cmath>
19#include <limits>
20
21namespace anthocnet {
22namespace core {
23
24/// What a backward ant knows about the path from this node to the destination.
25/// Carries path aggregates and node-local signals only — never per-hop
26/// downstream vectors (the wire carries one scalar cost per hop, ADR-0009).
28 double hops = 0.0; ///< hops to the destination; fractional once smoothed (#185).
29 double pathTime = 0.0; ///< accumulated time estimate (seconds, item 02).
30 double hopTime = 0.0; ///< config T_hop (seconds).
31};
32
33/// What a node knows about one of its own links when a link-quality metric
34/// prices it (#181, #145). Unknown signals are NaN, and every metric must read
35/// NaN as "no evidence against this link".
36struct HopSignals {
37 double sinrDb = std::numeric_limits<double>::quiet_NaN(); ///< ILinkQuality
38 double residualEnergy = std::numeric_limits<double>::quiet_NaN(); ///< IEnergy, 0..1
39 double sinrCriticalDb = 17.0; ///< Config::sinrCriticalDb
40 double sinrPenaltyCost = 3.0; ///< Config::sinrPenaltyCost
41 double energyFloor = 0.01; ///< Config::energyCostFloor
42};
43
44/// Maps an observation to a pheromone (goodness; higher == better).
46 virtual ~ILinkMetric() = default;
47 virtual double pheromone(const LinkObservation& obs) const = 0;
48 /// True when the metric's per-hop cost is a link-quality cost (#181) rather
49 /// than a time. The router then has each node put its own estimate of the
50 /// link it received a backward ant over into that hop of the retraced path
51 /// (thesis §4.2.2: "the reactive backward ant reads at each node i the local
52 /// estimate c_i^n of the cost to go from i to the next hop n"), and uses
53 /// the same link cost wherever it adds one hop to a path.
54 virtual bool usesLinkQuality() const { return false; }
55 /// The cost of one of this node's own links, for a metric that
56 /// usesLinkQuality(): the per-hop value a node writes into the hop a
57 /// backward ant came over, and adds wherever it extends a path by one hop.
58 /// Never called for a metric that does not use link quality.
59 virtual double hopCost(const HopSignals& /*s*/) const { return 1.0; }
60};
61
62/// The canonical AntHocNet metric (Eq.2): blend the path-time estimate with the
63/// hop-count estimate and invert. Identical to the post-item-02 inline formula.
65 double pheromone(const LinkObservation& o) const override {
66 return std::pow((o.pathTime + o.hops * o.hopTime) / 2.0, -1.0);
67 }
68};
69
70/// The SINR metric (#181), the one the 2007 thesis used for every headline
71/// result: "we normally use the signal-to-interference-and-noise ratio metric,
72/// as this gave the best results" (§4.2.6). Each link costs 1 when its SINR is
73/// above SINR_c and c_const otherwise (eq. 4.11, sinrLinkCost); a route costs
74/// the sum over its links, carried as the hops' per-hop values; and the
75/// pheromone is the inverse of that cost (eq. 4.3).
77 double pheromone(const LinkObservation& o) const override {
78 // Every hop costs at least 1, so the cost is never below the hop count.
79 const double cost = o.pathTime > o.hops ? o.pathTime : o.hops;
80 return cost > 0.0 ? 1.0 / cost : 0.0;
81 }
82 bool usesLinkQuality() const override { return true; }
83 double hopCost(const HopSignals& s) const override;
84};
85
86/// Eq. 4.11: 1 for a link whose SINR is above `criticalDb`, `penalty` for one
87/// at or below it. No sample (NaN) counts as a good link (ILinkQuality).
88inline double sinrLinkCost(double sinrDb, double criticalDb, double penalty) {
89 return (std::isnan(sinrDb) || sinrDb > criticalDb) ? 1.0 : penalty;
90}
91
92inline double SinrMetric::hopCost(const HopSignals& s) const {
94}
95
96/// The energy-aware metric (#145, the BeeAdHoc / EEABR line): a hop costs
97/// 1 / e, where e is the residual energy fraction of the node that forwards
98/// over it, floored so an almost-empty node costs a large but finite amount.
99/// A route costs the sum over its relays, and the pheromone is the inverse,
100/// so traffic shifts away from drained nodes before they die. Each node
101/// prices only its own hop, from its own battery, so nothing new travels on
102/// the wire (the per-hop scalar of ADR-0009, as for SINR, ADR-0024).
103/// Unknown energy (NaN) costs 1: the metric then degrades to hop count,
104/// never to a wrong ranking.
106 double pheromone(const LinkObservation& o) const override {
107 const double cost = o.pathTime > o.hops ? o.pathTime : o.hops;
108 return cost > 0.0 ? 1.0 / cost : 0.0;
109 }
110 bool usesLinkQuality() const override { return true; }
111 double hopCost(const HopSignals& s) const override {
112 if (std::isnan(s.residualEnergy)) return 1.0;
113 const double e = s.residualEnergy < s.energyFloor ? s.energyFloor : s.residualEnergy;
114 return e >= 1.0 ? 1.0 : 1.0 / e;
115 }
116};
117
118} // namespace core
119} // namespace anthocnet
120
121#endif // ANTHOCNET_CORE_LINK_METRIC_H
double sinrLinkCost(double sinrDb, double criticalDb, double penalty)
Eq.
Definition link_metric.h:88
AntHistoryTracker: (src, seqNum) duplicate detection.
Definition ant_history.h:24
The canonical AntHocNet metric (Eq.2): blend the path-time estimate with the hop-count estimate and i...
Definition link_metric.h:64
double pheromone(const LinkObservation &o) const override
Definition link_metric.h:65
The energy-aware metric (#145, the BeeAdHoc / EEABR line): a hop costs 1 / e, where e is the residual...
double hopCost(const HopSignals &s) const override
The cost of one of this node's own links, for a metric that usesLinkQuality(): the per-hop value a no...
bool usesLinkQuality() const override
True when the metric's per-hop cost is a link-quality cost (#181) rather than a time.
double pheromone(const LinkObservation &o) const override
What a node knows about one of its own links when a link-quality metric prices it (#181,...
Definition link_metric.h:36
double residualEnergy
IEnergy, 0..1.
Definition link_metric.h:38
double sinrDb
ILinkQuality.
Definition link_metric.h:37
double sinrCriticalDb
Config::sinrCriticalDb.
Definition link_metric.h:39
double sinrPenaltyCost
Config::sinrPenaltyCost.
Definition link_metric.h:40
double energyFloor
Config::energyCostFloor.
Definition link_metric.h:41
Maps an observation to a pheromone (goodness; higher == better).
Definition link_metric.h:45
virtual double hopCost(const HopSignals &) const
The cost of one of this node's own links, for a metric that usesLinkQuality(): the per-hop value a no...
Definition link_metric.h:59
virtual ~ILinkMetric()=default
virtual double pheromone(const LinkObservation &obs) const =0
virtual bool usesLinkQuality() const
True when the metric's per-hop cost is a link-quality cost (#181) rather than a time.
Definition link_metric.h:54
What a backward ant knows about the path from this node to the destination.
Definition link_metric.h:27
double hops
hops to the destination; fractional once smoothed (#185).
Definition link_metric.h:28
double pathTime
accumulated time estimate (seconds, item 02).
Definition link_metric.h:29
double hopTime
config T_hop (seconds).
Definition link_metric.h:30
The SINR metric (#181), the one the 2007 thesis used for every headline result: "we normally use the ...
Definition link_metric.h:76
bool usesLinkQuality() const override
True when the metric's per-hop cost is a link-quality cost (#181) rather than a time.
Definition link_metric.h:82
double pheromone(const LinkObservation &o) const override
Definition link_metric.h:77
double hopCost(const HopSignals &s) const override
The cost of one of this node's own links, for a metric that usesLinkQuality(): the per-hop value a no...
Definition link_metric.h:92