Skip to main content

task_allocation/cbba/
dynamic.rs

1//! Changing a running team: new tasks, finished tasks, and agents that leave.
2//!
3//! Every change keeps the bundles that are not affected. Agents bid on a new task with the
4//! room they have left, so with full bundles it waits for room, or for a [`Replan`] that
5//! releases some. Every agent has to apply the same change, in the same order, so that task
6//! identifiers stay the same everywhere; meanwhile, messages are merged over the tasks both
7//! sides know.
8//!
9//! [`Replan`]: super::Replan
10
11use super::{Agent, Claim};
12use crate::id::{AgentId, TaskId};
13use crate::valuation::Valuation;
14
15impl<V: Valuation<TaskId>> Agent<V> {
16    /// Adds a task after the existing ones and returns its identifier; the next bundle phase
17    /// may bid on it. Existing bundles stay as they are.
18    pub fn add_task(&mut self) -> TaskId {
19        let task = TaskId(self.claims.len());
20        self.claims.push(Claim::NONE);
21        self.retired.push(false);
22        self.tasks.push(task);
23
24        task
25    }
26
27    /// Retires `task`, because it is done or cancelled: nobody wins it any more, and this
28    /// agent never bids on it again. If this agent holds it, it gives it up together with the
29    /// tasks it added after it, whose gains assumed it. Returns whether this agent held it.
30    ///
31    /// # Panics
32    ///
33    /// Panics when `task` is not one of this agent's tasks.
34    pub fn retire_task(&mut self, task: TaskId) -> bool {
35        let held = self.give_up(task);
36        self.retired[task.index()] = true;
37        self.claims[task.index()] = Claim::NONE;
38        self.tasks.retain(|&candidate| candidate != task);
39
40        held
41    }
42
43    /// Records that `agent` left the team: its claims are void, now and when relayed later,
44    /// and its messages are ignored. Its tasks become free to bid on.
45    ///
46    /// # Panics
47    ///
48    /// Panics when `agent` is not part of the team.
49    pub fn forget_agent(&mut self, agent: AgentId) {
50        self.departed[agent.index()] = true;
51        for claim in &mut self.claims {
52            if claim.winner == Some(agent) {
53                *claim = Claim::NONE;
54            }
55        }
56    }
57
58    /// Makes this agent leave the team: it gives up its whole bundle and bids no more. The
59    /// others should [forget](Agent::forget_agent) it.
60    pub fn leave(&mut self) {
61        self.release_own(0);
62        self.capacity = 0;
63        self.left = true;
64    }
65}
66
67impl<V> Agent<V> {
68    /// Whether this agent left the team.
69    #[must_use]
70    pub const fn has_left(&self) -> bool {
71        self.left
72    }
73
74    /// Whether this agent was told that `agent` left the team.
75    ///
76    /// # Panics
77    ///
78    /// Panics when `agent` is not part of the team.
79    #[must_use]
80    pub fn has_departed(&self, agent: AgentId) -> bool {
81        self.departed[agent.index()]
82    }
83}