use task_allocation::AgentId;
use task_allocation::acbba::{Agent, Rules};
use task_allocation::sim::Topology;
use super::{Bounds, State, explore};
use crate::explore::cbba::instances;
use crate::support::{self, Instance};
const CLOSE_LIMIT: u32 = 200;
fn team(instance: &Instance, rules: Rules) -> Vec<Agent<support::Agent>> {
instance
.agents
.iter()
.cloned()
.enumerate()
.map(|(id, valuation)| {
Agent::new(AgentId(id), instance.tasks, instance.capacity, valuation).with_rules(rules)
})
.collect()
}
fn stall(state: &State<Agent<support::Agent>>, topology: &Topology) -> Option<String> {
let mut closing = state.clone();
if closing.close(topology, CLOSE_LIMIT).is_some() {
return None;
}
for agent in 0..closing.agents.len() {
closing.act(agent, topology);
}
let verdict = if closing.in_flight.is_empty() {
"falls quiet without agreeing"
} else {
"never falls quiet"
};
Some(format!(
"{verdict}\nschedule:\n {}\nagents after {CLOSE_LIMIT} closing rounds: {:#?}",
state.trace.join("\n "),
closing.agents
))
}
type Shape = (usize, usize, Topology, u32, usize);
fn shapes() -> Vec<Shape> {
vec![
(2, 2, Topology::Complete, 2, 40),
(2, 3, Topology::Complete, 2, 40),
]
}
#[test]
#[ignore = "a search; run with --ignored"]
fn smallest_stall() {
for (agents, tasks, topology, acts, count) in shapes() {
let bounds = Bounds { acts, losses: 0 };
for (index, instance) in instances(agents, tasks, count).iter().enumerate() {
let start = State::new(team(instance, Rules::Infotech2011));
let found = explore(start, &topology, bounds, |state| {
stall(state, &topology).map_or(Ok(()), Err)
});
if let Err(stall) = found {
println!(
"{agents} agents, {tasks} tasks, {topology:?}, instance {index}: {instance:?}\n{stall}"
);
return;
}
}
println!("{agents} agents, {tasks} tasks, {topology:?}, {acts} rounds each: no stall");
}
}