1use alloc::vec::Vec;
5use core::fmt;
6
7use crate::graph::{Graph, Vertex};
8
9pub type AgentId = usize;
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
14pub struct Agent {
15 pub start: Vertex,
17 pub goal: Vertex,
19}
20
21#[derive(Debug, Clone, PartialEq, Eq)]
26pub struct Instance<G> {
27 graph: G,
29 agents: Vec<Agent>,
31}
32
33impl<G: Graph> Instance<G> {
34 pub fn new(graph: G, agents: Vec<Agent>) -> Result<Self, InstanceError> {
43 let count = graph.vertex_count();
44 for (agent, a) in agents.iter().enumerate() {
45 if a.start >= count || a.goal >= count {
46 return Err(InstanceError::OutOfRange { agent });
47 }
48
49 if let Some(other) = agents[..agent].iter().position(|b| b.start == a.start) {
50 return Err(InstanceError::SharedStart(other, agent));
51 }
52
53 if let Some(other) = agents[..agent].iter().position(|b| b.goal == a.goal) {
54 return Err(InstanceError::SharedGoal(other, agent));
55 }
56 }
57
58 Ok(Self { graph, agents })
59 }
60
61 #[must_use]
63 pub fn graph(&self) -> &G {
64 &self.graph
65 }
66
67 #[must_use]
69 pub fn agents(&self) -> &[Agent] {
70 &self.agents
71 }
72}
73
74#[derive(Debug, Clone, Copy, PartialEq, Eq)]
76pub enum InstanceError {
77 OutOfRange {
79 agent: AgentId,
81 },
82 SharedStart(AgentId, AgentId),
84 SharedGoal(AgentId, AgentId),
86}
87
88impl fmt::Display for InstanceError {
89 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
90 match self {
91 Self::OutOfRange { agent } => {
92 write!(f, "agent {agent} starts or ends outside the graph")
93 }
94 Self::SharedStart(a, b) => write!(f, "agents {a} and {b} share a start"),
95 Self::SharedGoal(a, b) => write!(f, "agents {a} and {b} share a goal"),
96 }
97 }
98}
99
100impl core::error::Error for InstanceError {}