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
//! Extensive -> strategic conversion. A strategy is a *complete contingent
//! plan*: one action at every decision node of the player, including nodes
//! their own earlier choices make unreachable (Bonanno ยง2.3). This is the most
//! common modelling error in dynamic games, and the strategic form built here
//! feeds the backward-induction/Nash cross-check that fails loudly if the
//! enumeration is wrong.

use crate::error::GtError;
use crate::game::{
    Node, NodeId, Outcome, PlayerId, StrategicGame, StrategyId, ValidExtensiveGame,
    ValidStrategicGame,
};
use num_traits::ToPrimitive;

/// Convert a perfect-information extensive game to its strategic (normal) form.
///
/// Each player's strategy set is the cartesian product of the action sets at
/// that player's decision nodes, taken in ascending node order with the **last
/// node varying fastest**. A player with no decision nodes gets one strategy,
/// the empty plan.
///
/// Returns [`GtError::ImperfectInformationUnsupported`] on any non-singleton
/// information set, and `GameTooLarge` (via [`ValidStrategicGame::validate`])
/// if the enumerated strategy or profile counts exceed the strategic-form
/// limits.
pub fn to_strategic(game: &ValidExtensiveGame) -> Result<ValidStrategicGame, GtError> {
    game.require_perfect_information()?;
    let n_players = game.n_players();

    let decision_nodes: Vec<Vec<NodeId>> =
        (0..n_players).map(|p| game.decision_nodes_of(p)).collect();
    let action_counts: Vec<Vec<usize>> = decision_nodes
        .iter()
        .map(|nodes| nodes.iter().map(|&id| n_actions(game, id)).collect())
        .collect();

    let mut strategies: Vec<Vec<String>> = Vec::with_capacity(n_players);
    for p in 0..n_players {
        let labels: Vec<String> = cartesian_indices(&action_counts[p])
            .iter()
            .map(|choice| plan_label(game, &decision_nodes[p], choice))
            .collect();
        // A player with no decision nodes has exactly one strategy: the empty
        // plan, which `cartesian_indices` yields as a single empty tuple.
        strategies.push(if labels.len() == 1 && labels[0].is_empty() {
            vec!["(no move)".into()]
        } else {
            labels
        });
    }

    // For every profile (one strategy per player), play the tree to a terminal.
    let strategy_counts: Vec<usize> = strategies.iter().map(Vec::len).collect();
    let mut outcomes = Vec::new();
    for profile in cartesian_indices(&strategy_counts) {
        let choice_per_player: Vec<Vec<usize>> = (0..n_players)
            .map(|p| unrank(profile[p], &action_counts[p]))
            .collect();
        let terminal = play(game, &decision_nodes, &choice_per_player);
        let payoffs = game
            .terminal_payoffs(terminal)
            .iter()
            .map(|r| {
                r.to_f64()
                    .expect("terminal payoff came from an f64, so it round-trips")
            })
            .collect();
        outcomes.push(Outcome { profile, payoffs });
    }

    ValidStrategicGame::validate(StrategicGame {
        players: game.players().to_vec(),
        strategies,
        outcomes,
        payoff_kind: game.payoff_kind(),
    })
}

/// The strategic-form strategy index for a per-player plan โ€” a `(node, action)`
/// list covering every decision node of `player`. Inverts the enumeration
/// [`to_strategic`] uses, so a tree solution can be located in the converted
/// game.
///
/// Panics if `plan` omits one of the player's decision nodes.
pub fn plan_to_strategy_index(
    game: &ValidExtensiveGame,
    player: PlayerId,
    plan: &[(NodeId, StrategyId)],
) -> usize {
    let nodes = game.decision_nodes_of(player);
    if nodes.is_empty() {
        return 0; // the single empty plan
    }
    let counts: Vec<usize> = nodes.iter().map(|&id| n_actions(game, id)).collect();
    let choice: Vec<usize> = nodes
        .iter()
        .map(|&id| {
            plan.iter()
                .find(|(n, _)| *n == id)
                .map(|(_, a)| *a)
                .expect("a plan must fix an action at every decision node of the player")
        })
        .collect();
    rank(&choice, &counts)
}

fn n_actions(game: &ValidExtensiveGame, id: NodeId) -> usize {
    match game.node(id) {
        Node::Decision { actions, .. } => actions.len(),
        Node::Terminal { .. } => {
            unreachable!("only decision nodes carry action counts")
        }
    }
}

/// Play the tree from the root, following each player's chosen action at every
/// node they own, until a terminal is reached. Validation guarantees a finite
/// tree, so this terminates.
fn play(
    game: &ValidExtensiveGame,
    decision_nodes: &[Vec<NodeId>],
    choice_per_player: &[Vec<usize>],
) -> NodeId {
    let mut id = game.root();
    loop {
        match game.node(id) {
            Node::Terminal { .. } => return id,
            Node::Decision { player, actions } => {
                let slot = decision_nodes[*player]
                    .iter()
                    .position(|&n| n == id)
                    .expect("a decision node is listed among its own player's nodes");
                id = actions[choice_per_player[*player][slot]].1;
            }
        }
    }
}

/// Mixed-radix enumeration: every index tuple for the given per-position
/// counts, last position varying fastest. An empty `counts` yields one empty
/// tuple.
fn cartesian_indices(counts: &[usize]) -> Vec<Vec<usize>> {
    let mut out = vec![vec![]];
    for &c in counts {
        let mut next = Vec::with_capacity(out.len() * c);
        for prefix in &out {
            for a in 0..c {
                let mut row = prefix.clone();
                row.push(a);
                next.push(row);
            }
        }
        out = next;
    }
    out
}

/// Mixed-radix rank/unrank, last position varying fastest โ€” the inverse pair
/// that moves between an action-per-node tuple and a flat strategy index.
fn rank(choice: &[usize], counts: &[usize]) -> usize {
    let mut idx = 0;
    for (c, r) in choice.iter().zip(counts) {
        idx = idx * r + c;
    }
    idx
}

fn unrank(mut idx: usize, counts: &[usize]) -> Vec<usize> {
    let mut out = vec![0usize; counts.len()];
    for i in (0..counts.len()).rev() {
        out[i] = idx % counts[i];
        idx /= counts[i];
    }
    out
}

fn plan_label(game: &ValidExtensiveGame, nodes: &[NodeId], choice: &[usize]) -> String {
    nodes
        .iter()
        .zip(choice)
        .map(|(&id, &a)| match game.node(id) {
            Node::Decision { actions, .. } => format!("n{id}={}", actions[a].0),
            Node::Terminal { .. } => unreachable!("only decision nodes are enumerated"),
        })
        .collect::<Vec<_>>()
        .join(",")
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::game::{ExtensiveGame, Node, PayoffKind, Player, Rational, ValidExtensiveGame};
    use crate::solve::pure_nash::solve_pure_nash;

    fn r(n: i64) -> Rational {
        Rational::from_integer(n.into())
    }

    fn entry_deterrence() -> ValidExtensiveGame {
        ValidExtensiveGame::validate(ExtensiveGame {
            players: vec![
                Player {
                    id: 0,
                    name: "Entrant".into(),
                },
                Player {
                    id: 1,
                    name: "Incumbent".into(),
                },
            ],
            root: 0,
            nodes: vec![
                Node::Decision {
                    player: 0,
                    actions: vec![("In".into(), 1), ("Out".into(), 2)],
                },
                Node::Decision {
                    player: 1,
                    actions: vec![("Fight".into(), 3), ("Accommodate".into(), 4)],
                },
                Node::Terminal {
                    payoffs: vec![0.0, 2.0],
                }, // Out
                Node::Terminal {
                    payoffs: vec![-1.0, -1.0],
                }, // In, Fight
                Node::Terminal {
                    payoffs: vec![1.0, 1.0],
                }, // In, Accommodate
            ],
            information_sets: vec![vec![0], vec![1]],
            payoff_kind: PayoffKind::Cardinal,
        })
        .expect("valid")
    }

    #[test]
    fn each_player_has_the_expected_strategy_count() {
        let strategic = to_strategic(&entry_deterrence()).expect("converts");
        // Entrant has one decision node (2 actions); Incumbent one (2 actions).
        assert_eq!(strategic.n_strategies(0), 2);
        assert_eq!(strategic.n_strategies(1), 2);
    }

    #[test]
    fn incumbent_has_a_strategy_even_when_unreached() {
        // When Entrant plays Out (strategy 1), the Incumbent's node is unreached,
        // but each Incumbent strategy still fixes an action there, so the payoff
        // is the same across Incumbent strategies: (0, 2).
        let strategic = to_strategic(&entry_deterrence()).expect("converts");
        let out_fight = strategic.payoffs_at(&[1, 0]).to_vec();
        let out_accom = strategic.payoffs_at(&[1, 1]).to_vec();
        assert_eq!(
            out_fight, out_accom,
            "Out ends the game regardless of the Incumbent's plan"
        );
        assert_eq!(out_fight, vec![r(0), r(2)]);
    }

    #[test]
    fn on_path_payoffs_match_the_tree() {
        let strategic = to_strategic(&entry_deterrence()).expect("converts");
        // In (0), Fight (0) -> terminal 3 -> (-1,-1).
        assert_eq!(strategic.payoffs_at(&[0, 0]), &[r(-1), r(-1)]);
        // In (0), Accommodate (1) -> terminal 4 -> (1,1).
        assert_eq!(strategic.payoffs_at(&[0, 1]), &[r(1), r(1)]);
    }

    #[test]
    fn converted_game_has_the_expected_pure_nash_equilibria() {
        // (In, Accommodate) and (Out, Fight) are both pure Nash of the
        // strategic form; only the first is subgame perfect. (Out, Fight)
        // survives only because the Incumbent's node is never reached, so the
        // non-credible threat to Fight costs nothing โ€” that is the whole point
        // of subgame perfection, and Task 5 checks it.
        let strategic = to_strategic(&entry_deterrence()).expect("converts");
        let nash = solve_pure_nash(&strategic);
        assert!(nash.equilibria.contains(&vec![0, 1]));
        assert!(nash.equilibria.contains(&vec![1, 0]));
    }

    #[test]
    fn strategy_labels_name_the_node_and_action() {
        let strategic = to_strategic(&entry_deterrence()).expect("converts");
        assert_eq!(strategic.strategy_name(1, 0), "n1=Fight");
        assert_eq!(strategic.strategy_name(1, 1), "n1=Accommodate");
    }

    #[test]
    fn a_player_with_two_decision_nodes_gets_the_product_of_action_counts() {
        // Player 0 moves at the root and again after action A; player 1 never moves.
        let g = ValidExtensiveGame::validate(ExtensiveGame {
            players: vec![
                Player {
                    id: 0,
                    name: "Solo".into(),
                },
                Player {
                    id: 1,
                    name: "Idle".into(),
                },
            ],
            root: 0,
            nodes: vec![
                Node::Decision {
                    player: 0,
                    actions: vec![("A".into(), 1), ("B".into(), 2)],
                },
                Node::Decision {
                    player: 0,
                    actions: vec![("L".into(), 3), ("R".into(), 4)],
                },
                Node::Terminal {
                    payoffs: vec![9.0, 0.0],
                },
                Node::Terminal {
                    payoffs: vec![1.0, 0.0],
                },
                Node::Terminal {
                    payoffs: vec![2.0, 0.0],
                },
            ],
            information_sets: vec![vec![0], vec![1]],
            payoff_kind: PayoffKind::Cardinal,
        })
        .expect("valid");
        let strategic = to_strategic(&g).expect("converts");
        assert_eq!(strategic.n_strategies(0), 4); // {A,B} x {L,R}
        assert_eq!(strategic.n_strategies(1), 1); // idle player: one empty plan

        // Strategy order is mixed-radix with the LAST node varying fastest:
        // 0 = (A,L), 1 = (A,R), 2 = (B,L), 3 = (B,R).
        // (A,L) -> node 1 -> L -> terminal 3 -> payoff 1.
        assert_eq!(strategic.payoff(&[0, 0], 0), &r(1));
        // (A,R) -> node 1 -> R -> terminal 4 -> payoff 2.
        assert_eq!(strategic.payoff(&[1, 0], 0), &r(2));
        // (B,*) -> terminal 2 -> payoff 9, regardless of the unreached node 1.
        assert_eq!(strategic.payoff(&[2, 0], 0), &r(9));
        assert_eq!(strategic.payoff(&[3, 0], 0), &r(9));
    }

    #[test]
    fn plan_index_round_trips_with_enumeration_order() {
        let g = entry_deterrence();
        // Incumbent (player 1) node 1, action 1 (Accommodate) -> strategy index 1.
        assert_eq!(plan_to_strategy_index(&g, 1, &[(1, 1)]), 1);
        assert_eq!(plan_to_strategy_index(&g, 1, &[(1, 0)]), 0);
    }

    #[test]
    fn plan_index_matches_the_generated_strategy_labels() {
        // Exhaustive check that `plan_to_strategy_index` inverts the same
        // enumeration `to_strategic` used, for a player with two nodes.
        let g = ValidExtensiveGame::validate(ExtensiveGame {
            players: vec![
                Player {
                    id: 0,
                    name: "Solo".into(),
                },
                Player {
                    id: 1,
                    name: "Idle".into(),
                },
            ],
            root: 0,
            nodes: vec![
                Node::Decision {
                    player: 0,
                    actions: vec![("A".into(), 1), ("B".into(), 2)],
                },
                Node::Decision {
                    player: 0,
                    actions: vec![("L".into(), 3), ("R".into(), 4)],
                },
                Node::Terminal {
                    payoffs: vec![9.0, 0.0],
                },
                Node::Terminal {
                    payoffs: vec![1.0, 0.0],
                },
                Node::Terminal {
                    payoffs: vec![2.0, 0.0],
                },
            ],
            information_sets: vec![vec![0], vec![1]],
            payoff_kind: PayoffKind::Cardinal,
        })
        .expect("valid");
        let strategic = to_strategic(&g).expect("converts");
        for a0 in 0..2 {
            for a1 in 0..2 {
                let idx = plan_to_strategy_index(&g, 0, &[(0, a0), (1, a1)]);
                let label = strategic.strategy_name(0, idx);
                let expected = format!("n0={},n1={}", ["A", "B"][a0], ["L", "R"][a1]);
                assert_eq!(label, expected, "index {idx} mislabelled");
            }
        }
    }

    #[test]
    fn an_idle_players_plan_is_index_zero() {
        let g = ValidExtensiveGame::validate(ExtensiveGame {
            players: vec![
                Player {
                    id: 0,
                    name: "Solo".into(),
                },
                Player {
                    id: 1,
                    name: "Idle".into(),
                },
            ],
            root: 0,
            nodes: vec![
                Node::Decision {
                    player: 0,
                    actions: vec![("A".into(), 1)],
                },
                Node::Terminal {
                    payoffs: vec![1.0, 2.0],
                },
            ],
            information_sets: vec![vec![0]],
            payoff_kind: PayoffKind::Cardinal,
        })
        .expect("valid");
        assert_eq!(plan_to_strategy_index(&g, 1, &[]), 0);
    }

    #[test]
    fn imperfect_information_is_rejected() {
        let g = ValidExtensiveGame::validate(ExtensiveGame {
            players: vec![
                Player {
                    id: 0,
                    name: "A".into(),
                },
                Player {
                    id: 1,
                    name: "B".into(),
                },
            ],
            root: 0,
            nodes: vec![
                Node::Decision {
                    player: 0,
                    actions: vec![("A".into(), 1), ("B".into(), 2)],
                },
                Node::Decision {
                    player: 1,
                    actions: vec![("L".into(), 3), ("R".into(), 4)],
                },
                Node::Decision {
                    player: 1,
                    actions: vec![("L".into(), 5), ("R".into(), 6)],
                },
                Node::Terminal {
                    payoffs: vec![0.0, 0.0],
                },
                Node::Terminal {
                    payoffs: vec![0.0, 0.0],
                },
                Node::Terminal {
                    payoffs: vec![0.0, 0.0],
                },
                Node::Terminal {
                    payoffs: vec![0.0, 0.0],
                },
            ],
            information_sets: vec![vec![0], vec![1, 2]],
            payoff_kind: PayoffKind::Cardinal,
        })
        .expect("structurally valid");
        assert!(matches!(
            to_strategic(&g),
            Err(GtError::ImperfectInformationUnsupported { .. })
        ));
    }
}