game-theory-core 0.1.0

Exact game-theoretic solvers. No I/O, no async, no protocol.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
//! Iterated deletion of dominated strategies (Bonanno §1.2, §1.5, §5.4).
//!
//! Strict deletion is order-independent — any elimination order reaches the
//! same reduced game. Weak deletion is not, so its result is reported as one
//! valid reduction rather than the reduction.
//!
//! On cardinal games, strict deletion also tests dominance by *mixed*
//! strategies (§5.4): a pure strategy can be strictly dominated by a mixture
//! while no single pure strategy dominates it. That test is an LP feasibility
//! check per candidate, so pure dominance is tried first and the LP only runs
//! when it finds nothing.

use crate::exact::{solve_lp, LpProblem, LpSolution};
use crate::game::{PayoffKind, PlayerId, Profile, Rational, StrategyId, ValidStrategicGame};
use crate::limits;
use num_traits::{One, Zero};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum DominanceMode {
    Strict,
    Weak,
}

/// What beat the eliminated strategy.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Dominator {
    Pure(StrategyId),
    /// Probabilities over the player's full strategy list, zero off the
    /// support, summing to one.
    Mixed(Vec<Rational>),
}

/// One deletion, in the round it happened.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct EliminationStep {
    pub round: usize,
    pub player: PlayerId,
    pub eliminated: StrategyId,
    pub dominated_by: Dominator,
    pub mode: DominanceMode,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DominanceResult {
    /// Surviving strategies per player, ascending.
    pub surviving: Vec<Vec<StrategyId>>,
    pub steps: Vec<EliminationStep>,
    /// `Some` when exactly one strategy survives for every player.
    pub unique_profile: Option<Profile>,
    /// True for weak deletion: a different elimination order may give a
    /// different surviving set.
    pub order_dependent: bool,
    /// True when dominance by mixed strategies was tested (Bonanno §5.4).
    /// False for ordinal payoffs, for weak mode, and when the reduced game
    /// exceeded `limits::MAX_MIXED_DOMINANCE_PROFILES` — the result is then
    /// pure dominance only, and says so rather than implying completeness.
    pub mixed_dominance_checked: bool,
}

pub fn solve_dominance(game: &ValidStrategicGame, mode: DominanceMode) -> DominanceResult {
    let n = game.n_players();
    let mut surviving: Vec<Vec<StrategyId>> = (0..n)
        .map(|p| (0..game.n_strategies(p)).collect())
        .collect();
    let mut steps = Vec::new();
    let mut round = 0usize;

    // Expected utility over ordinal ranks is meaningless, and mixed *weak*
    // dominance is not defined in this version.
    let mut mixed_enabled =
        matches!(mode, DominanceMode::Strict) && game.payoff_kind() == PayoffKind::Cardinal;
    let mut mixed_ran = false;

    loop {
        round += 1;
        let mut eliminated_this_round = false;

        for p in 0..n {
            // Recomputed each pass: a deletion can create new dominance.
            let found = find_dominated(game, &surviving, p, mode)
                .map(|(victim, dominator)| (victim, Dominator::Pure(dominator)))
                .or_else(|| {
                    if !mixed_enabled {
                        return None;
                    }
                    if others_profile_count(&surviving, p) > limits::MAX_MIXED_DOMINANCE_PROFILES {
                        mixed_enabled = false;
                        return None;
                    }
                    mixed_ran = true;
                    surviving[p].clone().into_iter().find_map(|candidate| {
                        strictly_dominated_by_mixture(game, &surviving, p, candidate)
                            .map(|mix| (candidate, Dominator::Mixed(mix)))
                    })
                });

            if let Some((victim, dominated_by)) = found {
                surviving[p].retain(|&s| s != victim);
                steps.push(EliminationStep {
                    round,
                    player: p,
                    eliminated: victim,
                    dominated_by,
                    mode,
                });
                eliminated_this_round = true;
            }
        }

        if !eliminated_this_round {
            break;
        }
    }

    let unique_profile = if surviving.iter().all(|s| s.len() == 1) {
        Some(surviving.iter().map(|s| s[0]).collect())
    } else {
        None
    };

    DominanceResult {
        surviving,
        steps,
        unique_profile,
        order_dependent: matches!(mode, DominanceMode::Weak),
        mixed_dominance_checked: mixed_ran,
    }
}

/// How many joint profiles the other players still have.
fn others_profile_count(surviving: &[Vec<StrategyId>], player: PlayerId) -> usize {
    surviving
        .iter()
        .enumerate()
        .filter(|(p, _)| *p != player)
        .map(|(_, s)| s.len())
        .product()
}

/// Is `candidate` strictly dominated by some mixture over `player`'s other
/// surviving strategies, against every surviving opponent profile?
///
/// Returns the dominating mixture as a full-length distribution over
/// `player`'s strategies (zero off the support), or `None` if no mixture
/// dominates. Bonanno §5.4 (p. 201): this is strictly stronger than pure
/// dominance.
///
/// The feasibility question — is there a mixture beating `candidate` against
/// every opponent profile? — is turned into an optimization by maximizing the
/// margin `eps`, so a strictly positive optimum is exactly strict dominance.
pub fn strictly_dominated_by_mixture(
    game: &ValidStrategicGame,
    surviving: &[Vec<StrategyId>],
    player: PlayerId,
    candidate: StrategyId,
) -> Option<Vec<Rational>> {
    let mixers: Vec<StrategyId> = surviving[player]
        .iter()
        .copied()
        .filter(|&s| s != candidate)
        .collect();
    if mixers.is_empty() {
        return None;
    }

    let opponents = others_profiles(surviving, player);
    let m = mixers.len();
    let j = opponents.len();
    // Columns: y_0..y_{m-1}, eps, slack_0..slack_{j-1}.
    let cols = m + 1 + j;
    let eps_col = m;

    let mut constraints: Vec<Vec<Rational>> = Vec::with_capacity(j + 1);
    let mut rhs: Vec<Rational> = Vec::with_capacity(j + 1);

    for (index, others) in opponents.iter().enumerate() {
        let mut row = vec![Rational::zero(); cols];
        for (k, &mixer) in mixers.iter().enumerate() {
            let mut profile = others.clone();
            profile[player] = mixer;
            row[k] = game.payoff(&profile, player).clone();
        }
        row[eps_col] = -Rational::one();
        row[m + 1 + index] = -Rational::one();
        constraints.push(row);

        let mut profile = others.clone();
        profile[player] = candidate;
        rhs.push(game.payoff(&profile, player).clone());
    }

    // The mixture is a probability distribution.
    let mut sum_row = vec![Rational::zero(); cols];
    for entry in sum_row.iter_mut().take(m) {
        *entry = Rational::one();
    }
    constraints.push(sum_row);
    rhs.push(Rational::one());

    let mut objective = vec![Rational::zero(); cols];
    objective[eps_col] = Rational::one();

    match solve_lp(LpProblem {
        objective,
        constraints,
        rhs,
    }) {
        LpSolution::Optimal { x, value } if value > Rational::zero() => {
            let mut probs = vec![Rational::zero(); game.n_strategies(player)];
            for (k, &mixer) in mixers.iter().enumerate() {
                probs[mixer] = x[k].clone();
            }
            Some(probs)
        }
        // Zero margin means no *strict* domination; infeasible and unbounded
        // cannot occur for this program (the mixture lies in a simplex and the
        // payoffs are finite), and are treated as "not dominated" rather than
        // silently retried.
        _ => None,
    }
}

/// First `(dominated, dominator)` pair found for `player` in the reduced game.
fn find_dominated(
    game: &ValidStrategicGame,
    surviving: &[Vec<StrategyId>],
    player: PlayerId,
    mode: DominanceMode,
) -> Option<(StrategyId, StrategyId)> {
    let own = &surviving[player];
    for &candidate in own {
        for &alternative in own {
            if candidate == alternative {
                continue;
            }
            if dominates(game, surviving, player, alternative, candidate, mode) {
                return Some((candidate, alternative));
            }
        }
    }
    None
}

/// Does `better` dominate `worse` for `player`, over the surviving profiles
/// of the other players?
fn dominates(
    game: &ValidStrategicGame,
    surviving: &[Vec<StrategyId>],
    player: PlayerId,
    better: StrategyId,
    worse: StrategyId,
    mode: DominanceMode,
) -> bool {
    let mut strict_somewhere = false;

    for others in others_profiles(surviving, player) {
        let mut with_better = others.clone();
        let mut with_worse = others;
        with_better[player] = better;
        with_worse[player] = worse;

        let u_better = game.payoff(&with_better, player);
        let u_worse = game.payoff(&with_worse, player);

        match mode {
            DominanceMode::Strict => {
                if u_better <= u_worse {
                    return false;
                }
            }
            DominanceMode::Weak => {
                if u_better < u_worse {
                    return false;
                }
                if u_better > u_worse {
                    strict_somewhere = true;
                }
            }
        }
    }

    match mode {
        DominanceMode::Strict => true,
        // Weak dominance needs at least one strictly better case; without it
        // the two strategies are payoff-equivalent and neither dominates.
        DominanceMode::Weak => strict_somewhere,
    }
}

/// Every surviving profile, with `player`'s own slot left at a placeholder for
/// the caller to overwrite.
fn others_profiles(surviving: &[Vec<StrategyId>], player: PlayerId) -> Vec<Profile> {
    let n = surviving.len();
    let mut out = vec![vec![0usize; n]];

    for p in 0..n {
        if p == player {
            continue;
        }
        let mut next = Vec::with_capacity(out.len() * surviving[p].len());
        for base in &out {
            for &s in &surviving[p] {
                let mut extended = base.clone();
                extended[p] = s;
                next.push(extended);
            }
        }
        out = next;
    }

    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::game::{MatrixForm, PayoffKind, Rational, StrategicGame, ValidStrategicGame};
    use num_traits::{One, Zero};

    fn game(rows: Vec<&str>, cols: Vec<&str>, m: Vec<Vec<[f64; 2]>>) -> ValidStrategicGame {
        let form = MatrixForm {
            players: ["Row".into(), "Col".into()],
            row_strategies: rows.into_iter().map(String::from).collect(),
            col_strategies: cols.into_iter().map(String::from).collect(),
            payoff_matrix: m,
            payoff_kind: PayoffKind::Cardinal,
        };
        ValidStrategicGame::validate(StrategicGame::try_from(form).expect("converts"))
            .expect("valid")
    }

    /// Prisoner's Dilemma: Defect strictly dominates Cooperate for both.
    fn prisoners_dilemma() -> ValidStrategicGame {
        game(
            vec!["Cooperate", "Defect"],
            vec!["Cooperate", "Defect"],
            vec![vec![[3.0, 3.0], [0.0, 4.0]], vec![[4.0, 0.0], [1.0, 1.0]]],
        )
    }

    #[test]
    fn strict_dominance_solves_the_prisoners_dilemma_to_a_single_profile() {
        let result = solve_dominance(&prisoners_dilemma(), DominanceMode::Strict);
        assert_eq!(result.surviving, vec![vec![1], vec![1]]);
        assert_eq!(result.unique_profile, Some(vec![1, 1]));
        assert_eq!(result.steps.len(), 2, "one elimination per player");
        assert!(!result.order_dependent);
    }

    #[test]
    fn the_trace_names_the_dominating_strategy() {
        let result = solve_dominance(&prisoners_dilemma(), DominanceMode::Strict);
        let first = &result.steps[0];
        assert_eq!(first.eliminated, 0, "Cooperate is eliminated");
        assert_eq!(first.dominated_by, Dominator::Pure(1), "by Defect");
        assert_eq!(first.round, 1);
    }

    #[test]
    fn a_game_with_no_dominated_strategies_eliminates_nothing() {
        // Matching Pennies: no dominance in either direction.
        let mp = game(
            vec!["Heads", "Tails"],
            vec!["Heads", "Tails"],
            vec![
                vec![[1.0, -1.0], [-1.0, 1.0]],
                vec![[-1.0, 1.0], [1.0, -1.0]],
            ],
        );
        let result = solve_dominance(&mp, DominanceMode::Strict);
        assert!(result.steps.is_empty());
        assert_eq!(result.surviving, vec![vec![0, 1], vec![0, 1]]);
        assert_eq!(result.unique_profile, None);
    }

    #[test]
    fn elimination_iterates_a_strategy_dominated_only_after_an_earlier_deletion() {
        // Row: T dominates nothing initially; after Col's R goes, T beats B.
        // Col: L strictly dominates R (3>2, 3>1). Then for Row against L: T=2 < B=3?
        // Construct so that round 2 has a Row elimination.
        let g = game(
            vec!["T", "B"],
            vec!["L", "R"],
            vec![vec![[2.0, 3.0], [4.0, 2.0]], vec![[3.0, 3.0], [1.0, 1.0]]],
        );
        let result = solve_dominance(&g, DominanceMode::Strict);
        // Col: L gives 3,3 vs R giving 2,1 — L strictly dominates R.
        assert!(result
            .steps
            .iter()
            .any(|s| s.player == 1 && s.eliminated == 1));
        // With R gone, Row compares T=2 against B=3 — B strictly dominates T.
        assert!(result
            .steps
            .iter()
            .any(|s| s.player == 0 && s.eliminated == 0 && s.round >= 2));
        assert_eq!(result.unique_profile, Some(vec![1, 0]));
    }

    #[test]
    fn weak_dominance_is_flagged_as_order_dependent() {
        let g = game(
            vec!["T", "B"],
            vec!["L", "R"],
            vec![vec![[1.0, 1.0], [1.0, 0.0]], vec![[0.0, 1.0], [1.0, 1.0]]],
        );
        let result = solve_dominance(&g, DominanceMode::Weak);
        assert!(
            result.order_dependent,
            "iterated weak deletion can depend on elimination order"
        );
    }

    #[test]
    fn weak_dominance_eliminates_where_strict_dominance_does_not() {
        // T weakly dominates B: equal against L, strictly better against R.
        let g = game(
            vec!["T", "B"],
            vec!["L", "R"],
            vec![vec![[1.0, 0.0], [2.0, 0.0]], vec![[1.0, 0.0], [0.0, 0.0]]],
        );
        let strict = solve_dominance(&g, DominanceMode::Strict);
        assert!(strict.steps.is_empty(), "no strict dominance here");

        let weak = solve_dominance(&g, DominanceMode::Weak);
        assert!(weak
            .steps
            .iter()
            .any(|s| s.player == 0 && s.eliminated == 1));
    }

    /// Row's C is beaten by no pure strategy — A wins in the left column, B in
    /// the right — but the even mixture of A and B gives 3/2 in both columns,
    /// against C's 1. This is the §5.4 structure (concept at p. 201); the
    /// payoff numbers here were constructed for this crate.
    fn mixed_dominance_game() -> ValidStrategicGame {
        game(
            vec!["A", "B", "C"],
            vec!["L", "R"],
            vec![
                vec![[3.0, 0.0], [0.0, 0.0]],
                vec![[0.0, 0.0], [3.0, 0.0]],
                vec![[1.0, 0.0], [1.0, 0.0]],
            ],
        )
    }

    #[test]
    fn no_pure_strategy_dominates_the_candidate() {
        let g = mixed_dominance_game();
        let surviving = vec![vec![0, 1, 2], vec![0, 1]];
        assert!(
            !dominates(&g, &surviving, 0, 0, 2, DominanceMode::Strict),
            "A does not dominate C: A gets 0 against R, C gets 1"
        );
        assert!(
            !dominates(&g, &surviving, 0, 1, 2, DominanceMode::Strict),
            "B does not dominate C: B gets 0 against L, C gets 1"
        );
    }

    #[test]
    fn a_mixture_of_a_and_b_strictly_dominates_c() {
        let g = mixed_dominance_game();
        let surviving = vec![vec![0, 1, 2], vec![0, 1]];
        let mix = strictly_dominated_by_mixture(&g, &surviving, 0, 2).expect("C is dominated");
        assert_eq!(
            mix.len(),
            3,
            "full-length distribution over Row's strategies"
        );
        assert!(mix[2].is_zero(), "the candidate carries no weight");
        let total: Rational = mix.iter().sum();
        assert_eq!(total, Rational::one(), "probabilities sum to one");
        // Independently re-check the dominance the LP claims to have found.
        for col in 0..2 {
            let mixed_payoff: Rational =
                (0..3).map(|row| &mix[row] * g.payoff(&[row, col], 0)).sum();
            assert!(
                mixed_payoff > *g.payoff(&[2, col], 0),
                "mixture must beat C in column {col}"
            );
        }
    }

    #[test]
    fn a_strategy_that_is_not_dominated_by_any_mixture_is_reported_as_such() {
        // Matching Pennies: nothing is dominated, purely or mixed.
        let mp = game(
            vec!["Heads", "Tails"],
            vec!["Heads", "Tails"],
            vec![
                vec![[1.0, -1.0], [-1.0, 1.0]],
                vec![[-1.0, 1.0], [1.0, -1.0]],
            ],
        );
        let surviving = vec![vec![0, 1], vec![0, 1]];
        assert!(strictly_dominated_by_mixture(&mp, &surviving, 0, 0).is_none());
        assert!(strictly_dominated_by_mixture(&mp, &surviving, 0, 1).is_none());
    }

    #[test]
    fn iterated_deletion_uses_mixed_dominance_and_records_the_mixture() {
        let result = solve_dominance(&mixed_dominance_game(), DominanceMode::Strict);
        assert!(result.mixed_dominance_checked);
        let step = result
            .steps
            .iter()
            .find(|s| s.player == 0 && s.eliminated == 2)
            .expect("C is eliminated");
        match &step.dominated_by {
            Dominator::Mixed(probs) => {
                assert_eq!(probs.len(), 3);
                assert!(probs[2].is_zero());
            }
            Dominator::Pure(s) => panic!("expected a mixture, got pure strategy {s}"),
        }
    }

    #[test]
    fn pure_dominance_is_still_reported_as_pure() {
        let result = solve_dominance(&prisoners_dilemma(), DominanceMode::Strict);
        assert_eq!(result.steps[0].dominated_by, Dominator::Pure(1));
    }

    #[test]
    fn ordinal_games_get_pure_dominance_only() {
        // Expected utility over ranks is meaningless, so the mixed check is
        // not run and the result says so.
        let form = MatrixForm {
            players: ["Row".into(), "Col".into()],
            row_strategies: vec!["A".into(), "B".into(), "C".into()],
            col_strategies: vec!["L".into(), "R".into()],
            payoff_matrix: vec![
                vec![[3.0, 0.0], [0.0, 0.0]],
                vec![[0.0, 0.0], [3.0, 0.0]],
                vec![[1.0, 0.0], [1.0, 0.0]],
            ],
            payoff_kind: PayoffKind::Ordinal,
        };
        let g = ValidStrategicGame::validate(StrategicGame::try_from(form).unwrap()).unwrap();
        let result = solve_dominance(&g, DominanceMode::Strict);
        assert!(!result.mixed_dominance_checked);
        assert!(
            result.steps.is_empty(),
            "C survives: no pure strategy dominates it"
        );
    }

    #[test]
    fn weak_mode_does_not_run_the_mixed_check() {
        // Weak dominance by mixtures is not defined in this version; the flag
        // must not claim a check that did not happen.
        let result = solve_dominance(&mixed_dominance_game(), DominanceMode::Weak);
        assert!(!result.mixed_dominance_checked);
    }

    #[test]
    fn dominance_works_on_ordinal_payoffs() {
        // Dominance is an ordinal notion — no expectation is taken.
        let form = MatrixForm {
            players: ["Row".into(), "Col".into()],
            row_strategies: vec!["C".into(), "D".into()],
            col_strategies: vec!["C".into(), "D".into()],
            payoff_matrix: vec![vec![[3.0, 3.0], [0.0, 4.0]], vec![[4.0, 0.0], [1.0, 1.0]]],
            payoff_kind: PayoffKind::Ordinal,
        };
        let g = ValidStrategicGame::validate(StrategicGame::try_from(form).unwrap()).unwrap();
        let result = solve_dominance(&g, DominanceMode::Strict);
        assert_eq!(result.unique_profile, Some(vec![1, 1]));
    }
}