use coopgame::auto::AutoNucleolus;
use coopgame::game::{Coalition, ExplicitGame};
use coopgame::games::bankruptcy::BankruptcyGame;
use coopgame::games::voting::WeightedVotingGame;
use coopgame::solution::{Guarantee, Property, Solution};
fn main() -> coopgame::Result<()> {
let game = ExplicitGame::from_lex(&[0.0, 0.0, 0.0, 4.0, 6.0, 8.0, 12.0])?;
println!("== 3 人ゲーム ==");
for players in [&[0, 1][..], &[0, 2], &[1, 2], &[0, 1, 2]] {
let coalition = Coalition::from_players(players);
println!("v({coalition}) = {}", game.value(coalition));
}
let solution = game.nucleolus_auto()?;
print_solution(&solution);
assert!(close(solution.allocation(), &[2.0, 4.0, 6.0], 1e-6));
assert_eq!(solution.method(), "sequential-lp");
assert_eq!(solution.guarantee(), Guarantee::Exact);
println!("== 破産問題 ==");
let bankruptcy = BankruptcyGame::new(200.0, vec![100.0, 200.0, 300.0])?;
let solution = bankruptcy.nucleolus_auto()?;
print_solution(&solution);
assert!(close(solution.allocation(), &[50.0, 75.0, 75.0], 1e-6));
assert_eq!(solution.method(), "talmud-rule");
assert_eq!(
solution.guarantee(),
Guarantee::Proven(Property::Bankruptcy)
);
println!("== 重み付き投票ゲーム ==");
let voting = WeightedVotingGame::new(vec![35, 20, 15, 15, 15], 51)?;
let solution = voting.nucleolus_auto()?;
print_solution(&solution);
assert!(close(
solution.allocation(),
&[0.375, 0.25, 0.125, 0.125, 0.125],
1e-6
));
assert_eq!(solution.method(), "oracle-constraint-generation");
assert_eq!(solution.guarantee(), Guarantee::Exact);
let table = ExplicitGame::tabulate(&voting)?;
assert!(close(
solution.allocation(),
table.nucleolus_auto()?.allocation(),
1e-6
));
Ok(())
}
fn print_solution(solution: &Solution) {
println!("仁: {:.4?}", solution.allocation());
println!("手法: {}", solution.method());
println!(
"保証: {} (仮定に依存しない: {})",
solution.guarantee(),
solution.guarantee().is_reliable()
);
println!();
}
fn close(a: &[f64], b: &[f64], tolerance: f64) -> bool {
a.len() == b.len() && a.iter().zip(b).all(|(x, y)| (x - y).abs() < tolerance)
}