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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//! How agents value tasks: marginal gains given the tasks they already hold.
//!
//! An agent's *path* is its tasks in the order it will execute them. A [`Valuation`] scores
//! what one more task adds to that path. Allocation algorithms build bundles by repeatedly
//! adding the task with the highest marginal gain, so the gains of a bundle add up to its
//! value:
//!
//! ```text
//! value(path) = sum of the gains recorded while the path was built
//! ```
//!
//! Two ways to write a valuation:
//!
//! - implement [`Valuation::marginal_score`] directly, when a new task always goes at the
//! end of the path (or when the valuation decides on its own where it goes and only the
//! gain matters to the caller);
//! - implement [`PathScore`] for a whole path and wrap it in [`BestInsertion`], which tries
//! every insertion position and keeps the best.
//!
//! Consensus algorithms such as CBBA come with performance guarantees only when gains
//! *diminish*: a task never becomes more valuable to an agent because the agent took on more
//! work. [`check_diminishing`] tests that on a small task set.
pub use ;
use Vec;
/// An agent's valuation of tasks, used to place bids.
///
/// # Contract
///
/// - Higher scores are better. A score is a marginal gain: how much the agent's value grows
/// when it adds `task` to `path`.
/// - `None` means the agent cannot take the task on top of `path`, for example because it
/// would miss a deadline or exceed a capacity. An agent places no bid on such a task.
/// - [`best_insertion`](Valuation::best_insertion) and
/// [`marginal_score`](Valuation::marginal_score) agree: the insertion's gain is the
/// marginal score.
/// - Valuations are deterministic: the same path and task always give the same score.
/// Agents that disagree on scores cannot agree on winners.
///
/// # Examples
///
/// A rover that collects a fixed reward for each task it finishes before the task's
/// deadline, working through its path in order:
///
/// ```
/// use task_allocation::Valuation;
///
/// struct Task { duration: u64, deadline: u64, reward: u64 }
/// struct Rover;
///
/// impl Valuation<Task> for Rover {
/// fn marginal_score(&self, path: &[Task], task: &Task) -> Option<u64> {
/// let busy_until: u64 = path.iter().map(|held| held.duration).sum();
///
/// (busy_until + task.duration <= task.deadline).then_some(task.reward)
/// }
/// }
///
/// let late = Task { duration: 50, deadline: 60, reward: 4 };
/// let early = Task { duration: 20, deadline: 40, reward: 9 };
///
/// assert_eq!(Rover.marginal_score(&[], &early), Some(9));
/// assert_eq!(Rover.marginal_score(&[late], &early), None);
/// ```
/// Where a task goes in an agent's path, and what it adds there.
/// The value of a whole path: the tasks an agent holds, in execution order.
///
/// Wrap a path score in [`BestInsertion`] to get a [`Valuation`] that inserts each new task
/// where it adds the most.
/// A [`Valuation`] that tries every insertion position of a [`PathScore`] and keeps the best.
///
/// The marginal gain is the difference between the path's value with and without the new
/// task. A task that lowers the path's value scores zero, which never wins a bid. Of equally
/// good positions the earliest wins.
///
/// Each call scores `path.len() + 1` candidate paths through
/// [`PathScore::insertion_scores`], which allocates one scratch path unless the path score
/// overrides it.
///
/// # Examples
///
/// ```
/// use task_allocation::{BestInsertion, Insertion, PathScore, Valuation};
///
/// /// Visits points on a line, starting at 0: 50 per point, minus the distance traveled.
/// struct Walker;
///
/// impl PathScore<i64> for Walker {
/// fn path_score(&self, path: &[i64]) -> Option<u64> {
/// let mut at: i64 = 0;
/// let mut traveled = 0;
///
/// for &point in path {
/// traveled += at.abs_diff(point);
/// at = point;
/// }
///
/// (50 * path.len() as u64).checked_sub(traveled)
/// }
/// }
///
/// let walker = BestInsertion(Walker);
///
/// // Visiting 5 on the way to 10 adds no travel, so it gains the full 50.
/// assert_eq!(
/// walker.best_insertion(&[10], &5),
/// Some(Insertion { position: 0, gain: 50 })
/// );
/// ```
;
/// The better of two insertions: the higher gain, then the earlier position.
/// Unit tests for the valuation traits.