use alloc::vec;
use alloc::vec::Vec;
use super::{Network, Partition, Simulation, Topology};
use crate::cbba::Agent;
use crate::centralized::sequential_greedy;
use crate::id::{AgentId, TaskId};
use crate::valuation::Valuation;
#[derive(Clone, Copy)]
struct Skewed(usize);
impl Valuation<TaskId> for Skewed {
fn marginal_score(&self, bundle: &[TaskId], task: &TaskId) -> Option<u64> {
let task = u64::try_from(task.index()).ok()?;
let held = u64::try_from(bundle.len()).ok()?;
let base = if self.0 == 0 { 10 + task } else { 20 - task };
base.checked_sub(2 * held)
}
}
fn team(agents: usize, tasks: usize, capacity: usize) -> Vec<Agent<Skewed>> {
(0..agents)
.map(|id| Agent::new(AgentId(id), agents, tasks, capacity, Skewed(id)))
.collect()
}
#[test]
fn reliable_network_reaches_the_greedy_assignment() {
let mut simulation = Simulation::new(team(3, 6, 2), Network::default());
assert!(simulation.run_until_settled(100).is_some());
let valuations: Vec<Skewed> = (0..3).map(Skewed).collect();
assert_eq!(
simulation.assignment(),
sequential_greedy(&valuations, 6, 2)
);
}
#[test]
fn runs_are_reproducible() {
let network = Network {
seed: 11,
max_delay: 4,
loss_per_mille: 300,
topology: Topology::Complete,
partitions: Vec::new(),
};
let mut first = Simulation::new(team(3, 6, 2), network.clone());
let mut second = Simulation::new(team(3, 6, 2), network);
assert_eq!(
first.run_until_settled(1000),
second.run_until_settled(1000)
);
assert_eq!(first.stats(), second.stats());
assert!(first.stats().lost > 0);
}
#[test]
fn partitions_block_until_they_heal() {
let network = Network {
partitions: vec![Partition {
from: 0,
until: 50,
groups: vec![0, 1, 1],
}],
..Network::default()
};
let mut simulation = Simulation::new(team(3, 6, 2), network);
assert_eq!(simulation.run_until_settled(50), None);
assert!(simulation.stats().blocked > 0);
assert!(simulation.run_until_settled(100).is_some());
assert!(simulation.tick() > 50);
}
#[test]
fn partition_membership() {
let partition = Partition {
from: 5,
until: 10,
groups: vec![0, 0],
};
assert!(!partition.separates(5, AgentId(0), AgentId(1)));
assert!(partition.separates(5, AgentId(0), AgentId(2)));
assert!(!partition.separates(10, AgentId(0), AgentId(2)));
assert!(!partition.separates(4, AgentId(0), AgentId(2)));
}
#[test]
#[should_panic(expected = "in order")]
fn agents_must_be_in_order() {
let mut agents = team(2, 1, 1);
agents.swap(0, 1);
let _ = Simulation::new(agents, Network::default());
}
#[test]
fn empty_team_is_settled() {
let simulation: Simulation<Agent<Skewed>> = Simulation::new(Vec::new(), Network::default());
assert!(simulation.is_settled());
assert_eq!(simulation.assignment().agents(), 0);
}
#[test]
fn line_topology_relays() {
let line = (1..4)
.map(|agent| (AgentId(agent - 1), AgentId(agent)))
.collect();
let network = Network {
topology: Topology::Links(line),
..Network::default()
};
let mut simulation = Simulation::new(team(4, 6, 2), network);
assert!(simulation.run_until_settled(200).is_some());
assert!(simulation.assignment().is_conflict_free());
assert!(simulation.stats().blocked > 0);
assert!(Topology::Links(vec![(AgentId(0), AgentId(1))]).connects(AgentId(1), AgentId(0)));
assert!(!Topology::Links(vec![]).connects(AgentId(1), AgentId(0)));
}
#[test]
fn fresh_team_is_not_settled() {
let simulation = Simulation::new(team(3, 6, 2), Network::default());
assert!(!simulation.is_settled());
assert!(simulation.agents().iter().all(Agent::can_bid));
}
#[test]
fn simulations_clone() {
let mut original = Simulation::new(team(3, 6, 2), Network::default());
original.step();
let mut copy = original.clone();
copy.step();
assert_eq!(original.tick() + 1, copy.tick());
}