mod acbba;
mod bwtc;
mod cbba;
mod ccbba;
use std::collections::HashSet;
use std::collections::hash_map::DefaultHasher;
use std::fmt::Debug;
use std::hash::{Hash, Hasher};
use std::rc::Rc;
use task_allocation::AgentId;
use task_allocation::sim::{Participant, Topology};
#[derive(Clone, Copy, Debug)]
pub struct Bounds {
pub acts: u32,
pub losses: u32,
}
#[derive(Debug)]
pub struct State<A: Participant> {
pub agents: Vec<A>,
pub outboxes: Vec<Vec<A::Message>>,
pub in_flight: Vec<(AgentId, Rc<A::Message>)>,
acts: Vec<u32>,
losses: u32,
now: u64,
pub trace: Vec<String>,
}
#[derive(Clone, Copy, Debug, Default)]
pub struct Report {
pub states: usize,
pub revisits: usize,
}
impl<A> Clone for State<A>
where
A: Participant + Clone,
A::Message: Clone,
{
fn clone(&self) -> Self {
Self {
agents: self.agents.clone(),
outboxes: self.outboxes.clone(),
in_flight: self.in_flight.clone(),
acts: self.acts.clone(),
losses: self.losses,
now: self.now,
trace: self.trace.clone(),
}
}
}
impl<A> State<A>
where
A: Participant + Clone + Debug,
A::Message: Clone + Debug,
{
pub fn new(agents: Vec<A>) -> Self {
let team = agents.len();
Self {
agents,
outboxes: (0..team).map(|_| Vec::new()).collect(),
in_flight: Vec::new(),
acts: vec![0; team],
losses: 0,
now: 0,
trace: Vec::new(),
}
}
pub fn act(&mut self, agent: usize, topology: &Topology) {
let now = self.now;
self.agents[agent].act(now, &mut self.outboxes[agent]);
self.acts[agent] += 1;
self.now += 1;
let sender = AgentId(agent);
for message in self.outboxes[agent].drain(..) {
let message = Rc::new(message);
for receiver in (0..self.agents.len()).map(AgentId) {
if receiver != sender && topology.connects(sender, receiver) {
self.in_flight.push((receiver, Rc::clone(&message)));
}
}
}
}
pub fn deliver(&mut self, index: usize) {
let (to, message) = self.in_flight.remove(index);
let now = self.now;
self.agents[to.index()].receive(now, &message, &mut self.outboxes[to.index()]);
self.now += 1;
}
fn lose(&mut self, index: usize) {
self.in_flight.remove(index);
self.losses += 1;
}
fn key(&self) -> u128 {
let mut copies: Vec<String> = self
.in_flight
.iter()
.map(|copy| format!("{copy:?}"))
.collect();
copies.sort_unstable();
let rendered = format!(
"{:?}|{:?}|{copies:?}|{:?}|{}",
self.agents, self.outboxes, self.acts, self.losses
);
let half = |salt: u8| {
let mut hasher = DefaultHasher::new();
salt.hash(&mut hasher);
rendered.hash(&mut hasher);
hasher.finish()
};
(u128::from(half(0)) << 64) | u128::from(half(1))
}
fn successors(&self, topology: &Topology, bounds: Bounds) -> Vec<Self> {
let mut next = Vec::new();
for agent in 0..self.agents.len() {
if self.acts[agent] < bounds.acts {
let mut state = self.clone();
state.act(agent, topology);
state.trace.push(format!("agent {agent} runs a round"));
next.push(state);
}
}
for index in 0..self.in_flight.len() {
let (to, message) = &self.in_flight[index];
let copy = format!("{message:?} to agent {}", to.index());
let mut delivered = self.clone();
delivered.deliver(index);
delivered.trace.push(format!("deliver {copy}"));
next.push(delivered);
if self.losses < bounds.losses {
let mut lost = self.clone();
lost.lose(index);
lost.trace.push(format!("lose {copy}"));
next.push(lost);
}
}
next
}
pub fn close(&mut self, topology: &Topology, limit: u32) -> Option<u32> {
for round in 0..=limit {
while !self.in_flight.is_empty() {
self.deliver(0);
}
if A::is_settled(&self.agents, &[]) {
return Some(round);
}
for agent in 0..self.agents.len() {
self.act(agent, topology);
}
}
None
}
}
pub fn explore<A>(
start: State<A>,
topology: &Topology,
bounds: Bounds,
mut visit: impl FnMut(&State<A>) -> Result<(), String>,
) -> Result<Report, String>
where
A: Participant + Clone + Debug,
A::Message: Clone + Debug,
{
let mut seen = HashSet::new();
let mut report = Report::default();
let mut stack = vec![start];
while let Some(state) = stack.pop() {
if !seen.insert(state.key()) {
report.revisits += 1;
continue;
}
report.states += 1;
visit(&state)?;
stack.extend(state.successors(topology, bounds));
}
Ok(report)
}