Skip to main content

task_allocation/centralized/
greedy.rs

1//! Sequential greedy assignment: one task at a time, to the agent that values it most.
2
3use crate::assignment::Assignment;
4use crate::id::{AgentId, TaskId};
5use crate::valuation::{Insertion, Valuation};
6
7/// The sequential greedy assignment of tasks `0..tasks` to `agents`, each holding at most
8/// `capacity` tasks.
9///
10/// Each round considers every agent with room left and every unassigned task, and gives the
11/// task with the highest marginal gain to its agent, at its best insertion. Ties go to the
12/// smaller agent, then to the smaller task. Tasks with gain zero are never assigned. The
13/// result is conflict-free.
14///
15/// Each round asks every agent about every unassigned task:
16/// `O(tasks² × agents)` calls to [`Valuation::best_insertion`] overall.
17///
18/// # Examples
19///
20/// ```
21/// use task_allocation::{AgentId, TaskId, Valuation, centralized::sequential_greedy};
22///
23/// /// Values every task at `reward`, for at most one task.
24/// struct Single { reward: [u64; 2] }
25///
26/// impl Valuation<TaskId> for Single {
27///     fn marginal_score(&self, path: &[TaskId], task: &TaskId) -> Option<u64> {
28///         path.is_empty().then_some(self.reward[task.index()])
29///     }
30/// }
31///
32/// let agents = [Single { reward: [5, 4] }, Single { reward: [6, 1] }];
33/// let assignment = sequential_greedy(&agents, 2, 1);
34///
35/// assert_eq!(assignment.holder(TaskId(0)), Some(AgentId(1)));
36/// assert_eq!(assignment.holder(TaskId(1)), Some(AgentId(0)));
37/// assert_eq!(assignment.value(), 10);
38/// ```
39#[must_use]
40pub fn sequential_greedy<V>(agents: &[V], tasks: usize, capacity: usize) -> Assignment
41where
42    V: Valuation<TaskId>,
43{
44    let mut assignment = Assignment::empty(agents.len());
45
46    while let Some((agent, task, insertion)) = best_remaining(agents, &assignment, tasks, capacity)
47    {
48        assignment.bundle_mut(agent).insert(task, insertion);
49    }
50
51    assignment
52}
53
54/// The most valuable assignment of one more task: the agent, the task and its insertion;
55/// `None` when no agent with room left gains from any unassigned task.
56fn best_remaining<V>(
57    agents: &[V],
58    assignment: &Assignment,
59    tasks: usize,
60    capacity: usize,
61) -> Option<(AgentId, TaskId, Insertion)>
62where
63    V: Valuation<TaskId>,
64{
65    let mut best: Option<(AgentId, TaskId, Insertion)> = None;
66    for (index, valuation) in agents.iter().enumerate() {
67        let agent = AgentId(index);
68        let bundle = assignment.bundle(agent);
69        if bundle.len() >= capacity {
70            continue;
71        }
72
73        let unassigned = (0..tasks)
74            .map(TaskId)
75            .filter(|&task| assignment.holder(task).is_none());
76        for task in unassigned {
77            let Some(insertion) = valuation.best_insertion(bundle.path(), &task) else {
78                continue;
79            };
80
81            let floor = best.map_or(0, |(_, _, current)| current.gain);
82            if insertion.gain > floor {
83                best = Some((agent, task, insertion));
84            }
85        }
86    }
87
88    best
89}