use crate::error::{Error, Result};
use crate::game::{Coalition, ExplicitGame, PlayerSet, SetFunction};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Domain {
Preimputation,
Imputation,
}
pub(crate) fn coalition_sums(x: &[f64]) -> Vec<f64> {
let size = 1usize << x.len();
let mut sums = vec![0.0; size];
for mask in 1..size {
let lowest = mask.trailing_zeros() as usize;
sums[mask] = sums[mask & (mask - 1)] + x[lowest];
}
sums
}
pub(crate) fn excess_table(game: &ExplicitGame, x: &[f64]) -> Vec<f64> {
debug_assert_eq!(x.len(), game.players());
let mut excess = coalition_sums(x);
for (e, value) in excess.iter_mut().zip(game.values()) {
*e = value - *e;
}
excess
}
pub(crate) fn feasibility_violation<G: SetFunction + ?Sized>(
game: &G,
x: &[f64],
domain: Domain,
tolerance: f64,
) -> Option<String> {
let n = game.players();
let total: f64 = x.iter().sum();
let grand = game.value(&PlayerSet::grand(n));
if (total - grand).abs() > tolerance {
return Some(format!(
"効率性を満たさない: x(N) = {total}, v(N) = {grand}"
));
}
if domain == Domain::Imputation {
for (i, xi) in x.iter().enumerate() {
let lower = game.value(&PlayerSet::singleton(n, i));
if *xi < lower - tolerance {
return Some(format!(
"個人合理性を満たさない: x_{i} = {xi} < v({{{i}}}) = {lower}"
));
}
}
}
None
}
pub(crate) fn check_imputation_set<G: SetFunction + ?Sized>(
game: &G,
domain: Domain,
tolerance: f64,
) -> Result<()> {
let n = game.players();
if domain == Domain::Imputation {
let lower: f64 = (0..n)
.map(|i| game.value(&PlayerSet::singleton(n, i)))
.sum();
if lower > game.value(&PlayerSet::grand(n)) + tolerance {
return Err(Error::EmptyImputationSet);
}
}
Ok(())
}
pub(crate) fn check_blocks(
game: &ExplicitGame,
blocks: &[Coalition],
domain: Domain,
tolerance: f64,
) -> Result<()> {
if domain == Domain::Imputation {
for &block in blocks {
let lower: f64 = block
.members()
.map(|i| game.value(Coalition::singleton(i)))
.sum();
if lower > game.value(block) + tolerance {
return Err(Error::EmptyImputationSet);
}
}
}
Ok(())
}