1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//! The outcome of an allocation: one bundle per agent.
use alloc::vec::Vec;
use crate::bundle::Bundle;
use crate::id::{AgentId, TaskId};
/// Which agent holds which tasks: one [`Bundle`] per agent, indexed by [`AgentId`].
///
/// An assignment is *conflict-free* when no task is in two bundles. Every algorithm in this
/// crate returns conflict-free assignments; consensus algorithms reach one once their agents
/// agree.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Assignment {
/// The bundle of each agent, indexed by agent.
bundles: Vec<Bundle<TaskId>>,
}
impl Assignment {
/// An assignment from one bundle per agent, in agent order.
#[must_use]
pub const fn from_bundles(bundles: Vec<Bundle<TaskId>>) -> Self {
Self { bundles }
}
/// An assignment of `agents` empty bundles.
#[must_use]
pub fn empty(agents: usize) -> Self {
Self::from_bundles((0..agents).map(|_| Bundle::new()).collect())
}
/// The number of agents.
#[must_use]
pub fn agents(&self) -> usize {
self.bundles.len()
}
/// The bundle of `agent`.
///
/// # Panics
///
/// Panics when `agent` is not part of the assignment.
#[must_use]
pub fn bundle(&self, agent: AgentId) -> &Bundle<TaskId> {
&self.bundles[agent.index()]
}
/// The bundle of `agent`, to change it.
///
/// # Panics
///
/// Panics when `agent` is not part of the assignment.
pub fn bundle_mut(&mut self, agent: AgentId) -> &mut Bundle<TaskId> {
&mut self.bundles[agent.index()]
}
/// The bundles, in agent order.
#[must_use]
pub fn bundles(&self) -> &[Bundle<TaskId>] {
&self.bundles
}
/// The agent holding `task`; `None` when no agent holds it. When several do, the one with
/// the smallest identifier.
#[must_use]
pub fn holder(&self, task: TaskId) -> Option<AgentId> {
self.bundles
.iter()
.position(|bundle| bundle.contains(&task))
.map(AgentId)
}
/// The total value: the sum of the bundles' values, saturating at [`u64::MAX`].
#[must_use]
pub fn value(&self) -> u64 {
self.bundles
.iter()
.fold(0, |total, bundle| total.saturating_add(bundle.value()))
}
/// Whether every task is held by at most one agent.
#[must_use]
pub fn is_conflict_free(&self) -> bool {
let mut seen: Vec<TaskId> = self
.bundles
.iter()
.flat_map(Bundle::tasks)
.copied()
.collect();
let held = seen.len();
seen.sort_unstable();
seen.dedup();
seen.len() == held
}
}
/// Unit tests for [`Assignment`].
#[cfg(test)]
mod tests {
use super::Assignment;
use crate::id::{AgentId, TaskId};
use crate::valuation::Insertion;
/// Records `task` in the bundle of `agent` with gain `gain`, appended to the path.
fn give(assignment: &mut Assignment, agent: usize, task: usize, gain: u64) {
let bundle = assignment.bundle_mut(AgentId(agent));
let position = bundle.len();
bundle.insert(TaskId(task), Insertion { position, gain });
}
/// Holders, values and conflicts follow the bundles.
#[test]
fn reports_holders_and_value() {
let mut assignment = Assignment::empty(2);
give(&mut assignment, 0, 1, 4);
give(&mut assignment, 1, 0, 3);
assert_eq!(assignment.agents(), 2);
assert_eq!(assignment.bundles().len(), 2);
assert_eq!(assignment.holder(TaskId(1)), Some(AgentId(0)));
assert_eq!(assignment.holder(TaskId(2)), None);
assert_eq!(assignment.value(), 7);
assert!(assignment.is_conflict_free());
give(&mut assignment, 1, 1, 2);
assert!(!assignment.is_conflict_free());
}
}