Skip to main content

task_allocation/acbba/
table.rs

1//! The ACBBA deconfliction tables: what a receiver does with one task's entry in a message,
2//! and what it broadcasts in response.
3//!
4//! Two published revisions of the table differ, and an agent follows one of them, chosen
5//! with [`Rules`]:
6//!
7//! - [`Rules::Gnc2010`]: Table 1 of Johnson, Ponda, Choi and How, "Improving the Efficiency
8//!   of a Decentralized Tasking Algorithm for UAV Teams with Asynchronous Communications",
9//!   AIAA GNC 2010 (<https://doi.org/10.2514/6.2010-8421>). Where the bids and times of two
10//!   different winners point different ways, the receiver forgets its winner and passes the
11//!   sender's claim on, and newer information about an agreed winner is taken without
12//!   rebroadcasting it. Its rules live in [`gnc2010`].
13//! - [`Rules::Infotech2011`]: Table 1 of the same authors' "Asynchronous Decentralized Task
14//!   Allocation for Dynamic Environments", AIAA Infotech@Aerospace 2011
15//!   (<https://doi.org/10.2514/6.2011-1441>), which cites the 2010 paper for its details.
16//!   It takes the newer claim in those cases instead. That table has no rows 21 and 27,
17//!   which fall to the default. Its rules live in this module.
18//!
19//! The rule numbers below are each table's row numbers. Unlike CBBA's table, each claim
20//! carries the time its bid was made (`t`), and every rule also decides what to rebroadcast,
21//! so agents only pass on what is new.
22//!
23//! Choices the papers leave open:
24//!
25//! - Two times count as equal when they differ by less than `epsilon` (`ε_t` in the
26//!   papers, which call it small but give no value). When a time is both later and equal
27//!   within `epsilon`, as rows 4 and 5 can be, the equality rule wins: the information is
28//!   the same.
29//! - What time a reset leaves: see [`Rules`].
30//!
31//! One deliberate deviation, which [`Exchange::third_party_ties`] turns off: with equal
32//! bids for two different winners, row 9 and row 27 (the missing row 27 in 2011) leave the
33//! receiver's claim, on both sides, so two neighbors that disagree about a tie answer each
34//! other forever. The 2010 paper expects the prospective winners to break such ties once
35//! they hear of them; with integer scores, ties are common enough that this did not settle
36//! in simulation. Here the smaller winner takes the tie, as rows 2 and 18 already do, and
37//! the rows report as 9 and 27.
38
39use crate::id::AgentId;
40
41/// One task's entry: who wins it, with what bid, and when that bid was made.
42#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
43pub struct Claim {
44    /// The believed winner; `None` when there is no known bid.
45    pub winner: Option<AgentId>,
46
47    /// The winning bid; zero when there is no winner.
48    pub bid: u64,
49
50    /// When the bid was made or last confirmed, or when the task was last reset, in ticks of
51    /// a clock all agents share.
52    pub time: u64,
53}
54
55impl Claim {
56    /// No known winner, as of time zero.
57    pub const NONE: Self = Self {
58        winner: None,
59        bid: 0,
60        time: 0,
61    };
62
63    /// No known winner, as of `time`.
64    #[must_use]
65    pub const fn empty_at(time: u64) -> Self {
66        Self {
67            winner: None,
68            bid: 0,
69            time,
70        }
71    }
72}
73
74/// How the receiver changes its own entry.
75#[derive(Clone, Copy, Debug, PartialEq, Eq)]
76pub enum Change {
77    /// Take the sender's claim.
78    Update,
79
80    /// Keep its own claim, but stamp it with the current time: it is the winner, and
81    /// confirms the bid is still active.
82    UpdateTime,
83
84    /// Take the sender's claim, and when it is not newer than the receiver's, stamp it
85    /// `epsilon` after the receiver's time; the thesis's "update†", as inferred.
86    UpdatePast,
87
88    /// Keep its own claim, and when it is not newer than the sender's, stamp it `epsilon`
89    /// after the sender's time; the thesis's "update‡" and "leave‡", as inferred.
90    KeepPast,
91
92    /// Forget the winner.
93    Reset,
94
95    /// Keep its own claim.
96    Leave,
97}
98
99/// What the receiver broadcasts in response.
100#[derive(Clone, Copy, Debug, PartialEq, Eq)]
101pub enum Broadcast {
102    /// Nothing.
103    Nothing,
104
105    /// Its own entry, after the change.
106    Own,
107
108    /// The sender's claim, as received.
109    Sender,
110
111    /// An empty bid, stamped with the current time.
112    EmptyNow,
113}
114
115/// The outcome of one table lookup.
116#[derive(Clone, Copy, Debug, PartialEq, Eq)]
117pub struct Decision {
118    /// How the receiver changes its entry.
119    pub change: Change,
120
121    /// What it broadcasts.
122    pub broadcast: Broadcast,
123
124    /// The table row that applied; zero for the default, leave and rebroadcast.
125    pub row: u8,
126}
127
128/// Which published decision table an agent follows.
129#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
130#[non_exhaustive]
131pub enum Rules {
132    /// Table 1 of the 2010 GNC paper, the original ACBBA. A reset forgets the winner and
133    /// the time, as an agent that never heard of the task, so that only the sender's claim,
134    /// which it passes on, travels further: with time zero, row 33 never spreads the reset
135    /// as if it were a released bid, and rows 12 and 30 still take any later claim. The
136    /// paper only says the winner and bid are cleared. Stamping the reset with the current
137    /// time instead gives the same outcomes in the comparison matrix.
138    Gnc2010,
139
140    /// Table 1 of the 2011 Infotech@Aerospace paper. A reset forgets the winner and stamps
141    /// the entry with the current time, the same empty bid that rows 14 and 16 broadcast.
142    /// The default: of the three, it settles most often in simulation.
143    #[default]
144    Infotech2011,
145
146    /// Table 4.1 of Johnson's thesis, "Decentralized Task Allocation for Dynamic
147    /// Environments" (MIT, 2012): the 2011 table with times pushed past the losing claim's
148    /// in rows 1, 3, 13, 15, 17, 19, 31 and 32 (2011 numbering), as [`thesis2012`]
149    /// describes. A reset works as in 2011.
150    Thesis2012,
151}
152
153/// The two sides of one exchange.
154#[derive(Clone, Copy, Debug)]
155pub struct Exchange {
156    /// The agent receiving the message.
157    pub receiver: AgentId,
158
159    /// The agent that sent it.
160    pub sender: AgentId,
161
162    /// Times closer than this count as equal; `ε_t` in the paper.
163    pub epsilon: u64,
164
165    /// The table to follow.
166    pub rules: Rules,
167
168    /// Whether a receiver that is neither of two tied winners breaks the tie for the
169    /// smaller one, in rows 9 and 27; the published tables leave both claims as they are.
170    pub third_party_ties: bool,
171}
172
173/// Update and rebroadcast the sender's information.
174const fn update(row: u8) -> Decision {
175    Decision {
176        change: Change::Update,
177        broadcast: Broadcast::Sender,
178        row,
179    }
180}
181
182/// Leave, and rebroadcast own information.
183const fn leave(row: u8) -> Decision {
184    Decision {
185        change: Change::Leave,
186        broadcast: Broadcast::Own,
187        row,
188    }
189}
190
191/// Update, and broadcast nothing.
192const fn update_quietly(row: u8) -> Decision {
193    Decision {
194        change: Change::Update,
195        broadcast: Broadcast::Nothing,
196        row,
197    }
198}
199
200/// Forget the winner, and rebroadcast the sender's information.
201const fn reset(row: u8) -> Decision {
202    Decision {
203        change: Change::Reset,
204        broadcast: Broadcast::Sender,
205        row,
206    }
207}
208
209/// Leave, and broadcast nothing.
210const fn quiet(row: u8) -> Decision {
211    Decision {
212        change: Change::Leave,
213        broadcast: Broadcast::Nothing,
214        row,
215    }
216}
217
218/// The default: leave and rebroadcast own information.
219const DEFAULT: Decision = leave(0);
220
221impl Exchange {
222    /// The decision for one task, given the sender's claim `sent` and the receiver's claim
223    /// `held`.
224    #[must_use]
225    pub fn decide(&self, sent: Claim, held: Claim) -> Decision {
226        match self.rules {
227            Rules::Gnc2010 => self.decide_gnc2010(sent, held),
228            Rules::Infotech2011 => self.decide_infotech2011(sent, held),
229            Rules::Thesis2012 => thesis2012::decide(self.decide_infotech2011(sent, held)),
230        }
231    }
232
233    /// The decision under the 2011 table.
234    fn decide_infotech2011(&self, sent: Claim, held: Claim) -> Decision {
235        match sent.winner {
236            Some(winner) if winner == self.sender => self.sender_claims_itself(sent, held),
237            Some(winner) if winner == self.receiver => self.sender_claims_receiver(sent, held),
238            Some(winner) => self.sender_claims_other(winner, sent, held),
239            None => self.sender_claims_nobody(sent, held),
240        }
241    }
242
243    /// Whether the two times are equal within `epsilon`.
244    const fn same_time(&self, sent: Claim, held: Claim) -> bool {
245        sent.time.abs_diff(held.time) < self.epsilon
246    }
247
248    /// Rows 1 to 12: the sender believes it wins the task itself.
249    fn sender_claims_itself(&self, sent: Claim, held: Claim) -> Decision {
250        match held.winner {
251            Some(winner) if winner == self.receiver => receiver_contested(sent, held, [1, 2, 3]),
252            Some(winner) if winner == self.sender => self.newer_wins(sent, held, [4, 5, 6], false),
253            Some(_) => self.bids_and_times(sent, held, [7, 8, 9, 10, 11], true),
254            None => update(12),
255        }
256    }
257
258    /// Rows 13 to 16: the sender believes the receiver wins the task.
259    fn sender_claims_receiver(&self, sent: Claim, held: Claim) -> Decision {
260        match held.winner {
261            Some(winner) if winner == self.receiver => {
262                if self.same_time(sent, held) {
263                    quiet(13)
264                } else {
265                    DEFAULT
266                }
267            }
268            Some(winner) if winner == self.sender => Decision {
269                change: Change::Reset,
270                broadcast: Broadcast::EmptyNow,
271                row: 14,
272            },
273            Some(_) => leave(15),
274            None => Decision {
275                change: Change::Leave,
276                broadcast: Broadcast::EmptyNow,
277                row: 16,
278            },
279        }
280    }
281
282    /// Rows 17 to 30: the sender believes a third agent `winner` wins the task.
283    fn sender_claims_other(&self, winner: AgentId, sent: Claim, held: Claim) -> Decision {
284        match held.winner {
285            Some(held_winner) if held_winner == self.receiver => {
286                receiver_contested(sent, held, [17, 18, 19])
287            }
288            Some(held_winner) if held_winner == self.sender => update(20),
289            Some(held_winner) if held_winner == winner => {
290                self.newer_wins(sent, held, [22, 23, 24], true)
291            }
292            Some(_) => self.bids_and_times(sent, held, [25, 26, 27, 28, 29], false),
293            None => update(30),
294        }
295    }
296
297    /// Rows 31 to 34: the sender knows of no winner.
298    fn sender_claims_nobody(&self, sent: Claim, held: Claim) -> Decision {
299        match held.winner {
300            Some(winner) if winner == self.receiver => leave(31),
301            Some(winner) if winner == self.sender => update(32),
302            Some(_) if sent.time > held.time => update(33),
303            Some(_) => DEFAULT,
304            None => quiet(34),
305        }
306    }
307
308    /// Rows 4 to 6 and 22 to 24: both sides name the same winner, so the newer information
309    /// wins. `rows` are the rows for newer, equal and older; with `older_rebroadcasts`, an
310    /// older claim is answered with the receiver's own.
311    fn newer_wins(
312        &self,
313        sent: Claim,
314        held: Claim,
315        rows: [u8; 3],
316        older_rebroadcasts: bool,
317    ) -> Decision {
318        let [newer, equal, older] = rows;
319
320        if self.same_time(sent, held) {
321            quiet(equal)
322        } else if sent.time > held.time {
323            update(newer)
324        } else if older_rebroadcasts {
325            leave(older)
326        } else {
327            quiet(older)
328        }
329    }
330}
331
332/// Rows 1 to 3 and 17 to 19: the receiver believes it wins the task, the sender disagrees.
333/// A higher bid, or an equal bid by a smaller agent, wins; a lower one makes the receiver
334/// confirm its own bid with the current time.
335fn receiver_contested(sent: Claim, held: Claim, rows: [u8; 3]) -> Decision {
336    let [higher, tie, lower] = rows;
337
338    if sent.bid > held.bid {
339        update(higher)
340    } else if sent.bid == held.bid && sent.winner < held.winner {
341        update(tie)
342    } else if sent.bid < held.bid {
343        Decision {
344            change: Change::UpdateTime,
345            broadcast: Broadcast::Own,
346            row: lower,
347        }
348    } else {
349        DEFAULT
350    }
351}
352
353impl Exchange {
354    /// Rows 7 to 11 and 25 to 29 of the 2011 table: sender and receiver name two different
355    /// winners, neither of them the receiver, so bids and times decide. `rows` are the rows
356    /// for a higher and not older bid, a lower and not newer bid, an equal bid, a lower but
357    /// newer bid, and a higher but older bid. With `higher_older_updates`, a higher but
358    /// older bid updates (row 11), otherwise it is answered (row 29).
359    fn bids_and_times(
360        &self,
361        sent: Claim,
362        held: Claim,
363        rows: [u8; 5],
364        higher_older_updates: bool,
365    ) -> Decision {
366        let [higher_newer, lower_older, equal, lower_newer, higher_older] = rows;
367
368        if sent.bid > held.bid && sent.time >= held.time {
369            update(higher_newer)
370        } else if sent.bid < held.bid && sent.time <= held.time {
371            leave(lower_older)
372        } else if sent.bid == held.bid {
373            self.tie(sent, held, equal)
374        } else if sent.bid < held.bid {
375            update(lower_newer)
376        } else if higher_older_updates {
377            update(higher_older)
378        } else {
379            leave(higher_older)
380        }
381    }
382
383    /// Row 9 or 27, `row`: equal bids for two different winners, neither of them the
384    /// receiver. The published tables leave and rebroadcast; with
385    /// [`third_party_ties`](Self::third_party_ties), the smaller winner takes the tie.
386    fn tie(&self, sent: Claim, held: Claim, row: u8) -> Decision {
387        if self.third_party_ties && sent.winner < held.winner {
388            update(row)
389        } else {
390            leave(row)
391        }
392    }
393}
394
395mod gnc2010;
396mod thesis2012;
397
398/// Unit tests, one per row of the 2011 table.
399#[cfg(test)]
400mod tests;