use crate::error::{Error, Result};
use crate::game::scale::positive_scale;
use crate::game::{Coalition, ExplicitGame};
pub fn utopia_payoffs(game: &ExplicitGame) -> Vec<f64> {
let n = game.players();
let total = game.value(game.grand());
(0..n)
.map(|i| total - game.value(Coalition::singleton(i).complement(n)))
.collect()
}
pub fn minimal_rights(game: &ExplicitGame) -> Vec<f64> {
let n = game.players();
let utopia = utopia_payoffs(game);
let mut rights = vec![f64::NEG_INFINITY; n];
for mask in 1..1u64 << n {
let coalition = Coalition(mask);
let value = game.value(coalition);
let utopia_sum: f64 = coalition.members().map(|j| utopia[j]).sum();
for i in coalition.members() {
let remainder = value - (utopia_sum - utopia[i]);
if remainder > rights[i] {
rights[i] = remainder;
}
}
}
rights
}
fn tolerance(game: &ExplicitGame) -> f64 {
1e-9 * positive_scale(game.max_abs_value())
}
pub fn tau_value(game: &ExplicitGame) -> Result<Vec<f64>> {
let utopia = utopia_payoffs(game);
let rights = minimal_rights(game);
let tol = tolerance(game);
let total = game.value(game.grand());
if let Some(i) = (0..game.players()).find(|&i| rights[i] > utopia[i] + tol) {
return Err(Error::InvalidArgument(format!(
"準平衡でない: プレイヤー {i} の最小の権利 {} が理想の支払い {} を超える",
rights[i], utopia[i]
)));
}
let (low, high): (f64, f64) = (rights.iter().sum(), utopia.iter().sum());
if total < low - tol || total > high + tol {
return Err(Error::InvalidArgument(format!(
"準平衡でない: v(N) = {total} が最小の権利の和 {low} と理想の支払いの和 {high} の間にない"
)));
}
if high - low <= tol {
return Ok(rights);
}
let alpha = (total - low) / (high - low);
Ok(rights
.iter()
.zip(&utopia)
.map(|(m, big_m)| m + alpha * (big_m - m))
.collect())
}
pub fn gately_point(game: &ExplicitGame) -> Result<Vec<f64>> {
let utopia = utopia_payoffs(game);
let singles = game.singleton_values();
let total = game.value(game.grand());
let tol = tolerance(game);
let surplus = total - singles.iter().sum::<f64>();
if surplus <= tol {
return Err(Error::InvalidArgument(
"v(N) <= sum v({i}) (本質的でない) ため Gately 点が定まらない".into(),
));
}
let gaps: Vec<f64> = utopia.iter().zip(&singles).map(|(m, s)| m - s).collect();
let spread: f64 = gaps.iter().sum();
let mixed = gaps.iter().any(|&g| g > tol) && gaps.iter().any(|&g| g < -tol);
if mixed || spread.abs() <= tol {
return Err(Error::InvalidArgument(
"M_i - v({i}) の符号が人によって異なるか全員 0 のため、Gately 点が配分として定まらない"
.into(),
));
}
let t = surplus / spread;
Ok(singles
.iter()
.zip(&utopia)
.map(|(s, m)| s + t * (m - s))
.collect())
}
#[cfg(test)]
mod tests {
use super::*;
fn assert_close(actual: &[f64], expected: &[f64]) {
assert_eq!(actual.len(), expected.len());
for (a, e) in actual.iter().zip(expected) {
assert!((a - e).abs() < 1e-9, "{actual:?} != {expected:?}");
}
}
#[test]
fn glove_game_tau_value_gives_everything_to_the_scarce_player() {
let game = ExplicitGame::from_lex(&[0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0]).unwrap();
assert_close(&utopia_payoffs(&game), &[1.0, 0.0, 0.0]);
assert_close(&minimal_rights(&game), &[1.0, 0.0, 0.0]);
assert_close(&tau_value(&game).unwrap(), &[1.0, 0.0, 0.0]);
}
#[test]
fn three_player_tau_value_by_hand() {
let game = ExplicitGame::from_lex(&[0.0, 0.0, 0.0, 4.0, 6.0, 8.0, 12.0]).unwrap();
assert_close(&minimal_rights(&game), &[0.0, 0.0, 2.0]);
assert_close(&tau_value(&game).unwrap(), &[2.5, 3.75, 5.75]);
}
#[test]
fn tau_value_rejects_games_that_are_not_quasi_balanced() {
let game = ExplicitGame::from_lex(&[0.0, 0.0, 0.0, 80.0, 80.0, 80.0, 90.0]).unwrap();
assert!(tau_value(&game).is_err());
}
#[test]
fn gately_point_matches_coopgame_examples() {
let game = ExplicitGame::from_lex(&[0.0, 0.0, 0.0, 4.0, 0.0, 3.0, 6.0]).unwrap();
assert_close(
&gately_point(&game).unwrap(),
&[18.0 / 11.0, 36.0 / 11.0, 12.0 / 11.0],
);
let gately =
ExplicitGame::from_lex(&[0.0, 0.0, 0.0, 1170.0, 770.0, 210.0, 1530.0]).unwrap();
let x = gately_point(&gately).unwrap();
for (a, e) in x.iter().zip([827.7049, 476.5574, 225.7377]) {
assert!((a - e).abs() < 1e-4, "{x:?}");
}
}
#[test]
fn gately_point_rejects_mixed_signs() {
let game = ExplicitGame::from_lex(&[0.0, 0.0, 0.0, 1.0, 1.0, 4.0, 3.0]).unwrap();
assert!(gately_point(&game).is_err());
}
#[test]
fn tau_value_matches_coopgame_example() {
let game = ExplicitGame::from_lex(&[0.0, 0.0, 0.0, 1.0, 2.0, 1.0, 3.0]).unwrap();
assert_close(&tau_value(&game).unwrap(), &[1.2, 0.6, 1.2]);
}
#[test]
fn gately_point_equalizes_propensity_to_disrupt() {
let game = ExplicitGame::from_lex(&[1.0, 0.0, 2.0, 4.0, 6.0, 8.0, 12.0]).unwrap();
let x = gately_point(&game).unwrap();
assert!((x.iter().sum::<f64>() - 12.0).abs() < 1e-9);
let propensity: Vec<f64> = (0..3)
.map(|i| {
let others = Coalition::singleton(i).complement(3);
let x_others = others.sum(&x);
(x_others - game.value(others)) / (x[i] - game.value(Coalition::singleton(i)))
})
.collect();
assert!((propensity[0] - propensity[1]).abs() < 1e-9);
assert!((propensity[1] - propensity[2]).abs() < 1e-9);
}
}