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
//! Exact game-theoretic computation.
//!
//! No I/O, no async, no protocol handling. A caller-supplied [`StrategicGame`]
//! passes through [`ValidStrategicGame::validate`] once; every solver takes
//! that type, so an unchecked game cannot reach a solver.
//!
//! Payoffs cross the API as `f64` but are stored and computed as exact
//! rationals, so there is no tolerance anywhere and answers match published
//! textbook solutions exactly.
//!
//! ```
//! use game_theory_core::game::{MatrixForm, PayoffKind, StrategicGame, ValidStrategicGame};
//! use game_theory_core::solve::pure_nash::solve_pure_nash;
//!
//! let matrix = MatrixForm {
//! players: ["Row".into(), "Col".into()],
//! row_strategies: vec!["Cooperate".into(), "Defect".into()],
//! col_strategies: vec!["Cooperate".into(), "Defect".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::Cardinal,
//! };
//!
//! let game = ValidStrategicGame::validate(StrategicGame::try_from(matrix)?)?;
//! let result = solve_pure_nash(&game);
//!
//! // Both players defect: the unique equilibrium, and worse for both than
//! // mutual cooperation.
//! assert_eq!(result.equilibria, vec![vec![1, 1]]);
//! # Ok::<(), game_theory_core::GtError>(())
//! ```
//!
//! Extensive-form (game-tree) games take the same route — validate once, then
//! solve. Backward induction returns every subgame-perfect equilibrium:
//!
//! ```
//! use game_theory_core::game::Player;
//! use game_theory_core::{solve_backward_induction, ExtensiveGame, Node, PayoffKind, ValidExtensiveGame};
//!
//! // The Entrant moves first; if it enters, the Incumbent chooses whether to
//! // fight. Fighting hurts the Incumbent too, so the threat is not credible.
//! let game = 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] },
//! Node::Terminal { payoffs: vec![-1.0, -1.0] },
//! Node::Terminal { payoffs: vec![1.0, 1.0] },
//! ],
//! information_sets: vec![vec![0], vec![1]],
//! payoff_kind: PayoffKind::Cardinal,
//! };
//!
//! let game = ValidExtensiveGame::validate(game)?;
//! let result = solve_backward_induction(&game)?;
//!
//! // Unique equilibrium: the Entrant enters and the Incumbent accommodates.
//! assert_eq!(result.solutions.len(), 1);
//! assert_eq!(result.solutions[0].path, vec![(0, 0), (1, 1)]);
//! # Ok::<(), game_theory_core::GtError>(())
//! ```
//!
//! Repeating a stage game changes what is sustainable. Grim trigger with Nash
//! reversion gives the exact patience cooperation requires:
//!
//! ```
//! use game_theory_core::{
//! analyze_repeated_game, MatrixForm, PayoffKind, Punishment, Rational,
//! StrategicGame, ValidStrategicGame,
//! };
//!
//! let matrix = MatrixForm {
//! players: ["Row".into(), "Col".into()],
//! row_strategies: vec!["Cooperate".into(), "Defect".into()],
//! col_strategies: vec!["Cooperate".into(), "Defect".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::Cardinal,
//! };
//! let game = ValidStrategicGame::validate(StrategicGame::try_from(matrix)?)?;
//!
//! let report = analyze_repeated_game(&game, &[0, 0], Punishment::GrimTrigger, None)?;
//!
//! // Mutual cooperation is sustainable exactly when the discount factor
//! // reaches 1/3 — an exact fraction, not a rounded decimal.
//! assert_eq!(
//! report.critical_discount_factor,
//! Some(Rational::new(1.into(), 3.into()))
//! );
//! # Ok::<(), game_theory_core::GtError>(())
//! ```
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;