task_allocation/auction/sequential.rs
1//! Sequential single-item auctions.
2
3use alloc::vec;
4
5use crate::assignment::Assignment;
6use crate::id::{AgentId, TaskId};
7use crate::valuation::{Insertion, Valuation};
8
9/// Auctions the tasks one at a time, in the order `order` lists them, among `agents` that
10/// hold at most `capacity` tasks each.
11///
12/// For each task, every agent with room left bids its marginal gain on its current path,
13/// and the highest positive bid wins; ties go to the smaller agent. The winner inserts the
14/// task at its best position. A task nobody bids on stays unassigned. Tasks listed twice
15/// are auctioned only the first time, whether or not anyone bid; identifiers of `tasks` or
16/// more are ignored.
17///
18/// Each task asks every agent once: `order.len() × agents` calls to
19/// [`Valuation::best_insertion`].
20///
21/// # Examples
22///
23/// ```
24/// use task_allocation::{AgentId, TaskId, Valuation, auction::sequential};
25///
26/// /// Values each task at a fixed reward, for at most one task.
27/// struct Single([u64; 2]);
28///
29/// impl Valuation<TaskId> for Single {
30/// fn marginal_score(&self, path: &[TaskId], task: &TaskId) -> Option<u64> {
31/// path.is_empty().then_some(self.0[task.index()])
32/// }
33/// }
34///
35/// let agents = [Single([5, 0]), Single([6, 5])];
36///
37/// // Auctioned first, task 0 goes to agent 1, which then has no room for task 1.
38/// let first = sequential(&agents, 2, 1, [TaskId(0), TaskId(1)]);
39/// assert_eq!(first.value(), 6);
40///
41/// // The other way round, both tasks find a taker.
42/// let second = sequential(&agents, 2, 1, [TaskId(1), TaskId(0)]);
43/// assert_eq!(second.holder(TaskId(0)), Some(AgentId(0)));
44/// assert_eq!(second.value(), 10);
45/// ```
46#[must_use]
47pub fn sequential<V, I>(agents: &[V], tasks: usize, capacity: usize, order: I) -> Assignment
48where
49 V: Valuation<TaskId>,
50 I: IntoIterator<Item = TaskId>,
51{
52 let mut assignment = Assignment::empty(agents.len());
53 let mut auctioned = vec![false; tasks];
54
55 for task in order {
56 let Some(done) = auctioned.get_mut(task.index()) else {
57 continue;
58 };
59
60 if core::mem::replace(done, true) {
61 continue;
62 }
63
64 if let Some((winner, insertion)) = highest_bid(agents, &assignment, capacity, task) {
65 assignment.bundle_mut(winner).insert(task, insertion);
66 }
67 }
68
69 assignment
70}
71
72/// The agent with the highest positive bid on `task`, among those with room left, with
73/// its insertion; ties go to the smaller agent.
74fn highest_bid<V>(
75 agents: &[V],
76 assignment: &Assignment,
77 capacity: usize,
78 task: TaskId,
79) -> Option<(AgentId, Insertion)>
80where
81 V: Valuation<TaskId>,
82{
83 let mut best: Option<(AgentId, Insertion)> = None;
84 for (index, valuation) in agents.iter().enumerate() {
85 let bundle = assignment.bundle(AgentId(index));
86 if bundle.len() >= capacity {
87 continue;
88 }
89
90 let Some(insertion) = valuation.best_insertion(bundle.path(), &task) else {
91 continue;
92 };
93
94 let floor = best.map_or(0, |(_, current)| current.gain);
95 if insertion.gain > floor {
96 best = Some((AgentId(index), insertion));
97 }
98 }
99
100 best
101}