use proptest::prelude::*;
use task_allocation::bwtc::{Agent, Bid};
use task_allocation::sim::{Network, Simulation};
use task_allocation::{AgentId, TaskId, Valuation};
use crate::acbba::lossless;
use crate::bundle::gains_replay;
use crate::cbba::{MAX_TEAM, SETTLE_TICKS};
use crate::support::{self, Instance, instance_with};
fn team(instance: &Instance) -> Vec<Agent<support::Agent>> {
let size = instance.agents.len();
instance
.agents
.iter()
.cloned()
.enumerate()
.map(|(id, valuation)| {
Agent::new(
AgentId(id),
size,
instance.tasks,
instance.capacity,
valuation,
)
})
.collect()
}
fn run(instance: &Instance, network: Network) -> Option<Simulation<Agent<support::Agent>>> {
let mut simulation = Simulation::new(team(instance), network);
simulation.run_until_settled(SETTLE_TICKS)?;
Some(simulation)
}
pub fn stable(
simulation: &Simulation<Agent<support::Agent>>,
instance: &Instance,
) -> Result<(), TestCaseError> {
let assignment = simulation.assignment();
prop_assert!(assignment.is_conflict_free());
for agent in simulation.agents() {
for task in (0..instance.tasks).map(TaskId) {
let held = assignment.holder(task).map(|holder| {
let gain = assignment
.bundle(holder)
.entries()
.find(|(held, _)| **held == task)
.map_or(0, |(_, gain)| gain);
Bid {
bidder: holder,
gain,
}
});
prop_assert_eq!(agent.best(task), held);
}
let bundle = agent.bundle();
prop_assert!(bundle.len() <= instance.capacity);
gains_replay(agent.valuation(), bundle)?;
if bundle.len() < instance.capacity {
for task in (0..instance.tasks)
.map(TaskId)
.filter(|task| !bundle.contains(task))
{
let Some(insertion) = agent.valuation().best_insertion(bundle.path(), &task) else {
continue;
};
let mine = Bid {
bidder: agent.id(),
gain: insertion.gain,
};
let beats =
insertion.gain > 0 && agent.best(task).is_none_or(|best| mine.outbids(best));
prop_assert!(!beats, "{:?} could outbid on {:?}", agent.id(), task);
}
}
}
Ok(())
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(256))]
#[test]
fn lossless_runs_settle(
(instance, network) in instance_with(MAX_TEAM).prop_flat_map(|instance| {
let agents = instance.agents.len();
(Just(instance), lossless(agents))
}),
) {
let Some(simulation) = run(&instance, network) else {
return Err(TestCaseError::fail("did not settle"));
};
stable(&simulation, &instance)?;
let stats = simulation.stats();
prop_assert!(stats.useful <= stats.delivered);
if instance.agents.len() > 1 && simulation.assignment().value() > 0 {
prop_assert!(stats.useful > 0, "{:?}", stats);
}
}
}
fn coverage(covers: Vec<u16>, weights: Vec<u64>, allowed: u32) -> support::Agent {
support::Agent::Coverage(support::Coverage {
covers,
weights,
allowed,
})
}
fn courier(start: u64, sites: &[(u64, u64, u64)], rewards: Vec<u64>) -> support::Agent {
let sites = sites
.iter()
.map(|&(position, duration, deadline)| support::Site {
position,
duration,
deadline,
})
.collect();
support::Agent::Courier(task_allocation::BestInsertion(support::Courier {
start,
sites,
rewards,
}))
}
#[test]
fn discarded_bids_come_back_when_the_best_is_dropped() {
let instance = Instance {
agents: vec![
coverage(
vec![0, 0, 0, 155, 26],
vec![19, 19, 16, 1, 19, 6, 16, 2],
555_907_097,
),
coverage(
vec![198, 204, 72, 25, 197],
vec![4, 13, 9, 7, 18, 17, 17, 19],
1_761_490_529,
),
coverage(
vec![46, 169, 90, 175, 149],
vec![12, 10, 17, 16, 4, 9, 5, 15],
4_143_711_347,
),
coverage(
vec![72, 52, 118, 186, 102],
vec![3, 1, 5, 6, 19, 12, 11, 4],
4_041_653_145,
),
courier(
16,
&[
(39, 14, 187),
(50, 14, 178),
(32, 8, 231),
(37, 14, 113),
(5, 1, 243),
],
vec![13, 10, 29, 23, 3],
),
coverage(
vec![102, 60, 73, 70, 12],
vec![7, 7, 16, 15, 8, 5, 18, 12],
1_198_303_813,
),
],
tasks: 5,
capacity: 5,
};
let network = Network {
seed: 1_074_557_508_531_120_924,
max_delay: 2,
..Network::default()
};
let simulation = run(&instance, network).expect("settles");
stable(&simulation, &instance).expect("stable");
}
#[test]
fn runner_up_bids_are_announced_with_the_drop() {
let instance = Instance {
agents: vec![
coverage(
vec![65, 225, 134, 168, 88, 47],
vec![3, 5, 9, 3, 2, 8, 15, 19],
4_055_615_673,
),
coverage(
vec![54, 61, 181, 186, 12, 114],
vec![3, 10, 6, 18, 5, 19, 11, 16],
2_491_619_579,
),
courier(
63,
&[
(38, 1, 58),
(56, 4, 30),
(79, 18, 285),
(13, 2, 233),
(27, 17, 277),
(33, 4, 155),
],
vec![11, 5, 27, 17, 14, 8],
),
],
tasks: 6,
capacity: 5,
};
let network = Network {
seed: 7_548_525_644_882_666_869,
max_delay: 2,
topology: task_allocation::sim::Topology::Links(vec![
(AgentId(0), AgentId(1)),
(AgentId(1), AgentId(2)),
]),
..Network::default()
};
let simulation = run(&instance, network).expect("settles");
stable(&simulation, &instance).expect("stable");
}
#[test]
#[ignore = "an experiment; run on demand"]
fn matrix_cases_settle() {
use proptest::strategy::ValueTree;
use proptest::test_runner::TestRunner;
let cases: usize = std::env::var("ACBBA_CASES")
.ok()
.and_then(|cases| cases.parse().ok())
.unwrap_or(500);
let strategy = instance_with(MAX_TEAM).prop_flat_map(|instance| {
let agents = instance.agents.len();
(Just(instance), lossless(agents))
});
let mut runner = TestRunner::deterministic();
for case in 0..cases {
let (instance, network) = strategy.new_tree(&mut runner).expect("generates").current();
let simulation =
run(&instance, network).unwrap_or_else(|| panic!("case {case} did not settle"));
stable(&simulation, &instance).unwrap_or_else(|error| panic!("case {case}: {error}"));
}
}