native_solver/solver/
discount.rs1#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
18#[non_exhaustive]
19pub enum SolverAlgorithm {
20 Cfr,
22 CfrPlus,
28 #[default]
32 Dcfr,
33}
34
35#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
37pub enum UpdateSchedule {
38 #[default]
40 Simultaneous,
41 Alternating,
47}
48
49#[derive(Debug, Clone, Copy)]
51pub(crate) enum IterationUpdate {
52 Cfr,
53 CfrPlus,
54 Dcfr { positive: f64, strategy: f64 },
55}
56
57impl SolverAlgorithm {
58 pub(crate) fn iteration(self, t: u64) -> IterationUpdate {
59 assert!(t > 0, "CFR iteration numbers are one-based");
60 match self {
61 Self::Cfr => IterationUpdate::Cfr,
62 Self::CfrPlus => IterationUpdate::CfrPlus,
63 Self::Dcfr => {
64 let t = t as f64;
65 let positive_power = t.powf(1.5);
66 IterationUpdate::Dcfr {
67 positive: positive_power / (positive_power + 1.0),
68 strategy: (t / (t + 1.0)).powi(2),
69 }
70 }
71 }
72 }
73}
74
75impl IterationUpdate {
76 pub(crate) fn regret(self, previous: f64, delta: f64) -> f64 {
80 let accumulated = previous + delta;
81 match self {
82 Self::Cfr => accumulated,
83 Self::CfrPlus => accumulated.max(0.0),
84 Self::Dcfr { positive, .. } => {
85 accumulated * if accumulated > 0.0 { positive } else { 0.5 }
86 }
87 }
88 }
89
90 pub(crate) fn strategy_sum(self, accumulated: f64) -> f64 {
94 match self {
95 Self::Dcfr { strategy, .. } => accumulated * strategy,
96 Self::Cfr | Self::CfrPlus => accumulated,
97 }
98 }
99}
100
101#[cfg(test)]
102mod tests {
103 use super::*;
104
105 #[test]
106 fn dcfr_first_iteration_adds_before_discounting() {
107 let update = SolverAlgorithm::Dcfr.iteration(1);
108 assert_eq!(update.regret(0.0, 8.0), 4.0);
109 assert_eq!(update.regret(0.0, -8.0), -4.0);
110 assert_eq!(update.strategy_sum(8.0), 2.0);
111 }
112
113 #[test]
114 fn dcfr_uses_post_addition_sign_and_published_coefficients() {
115 let update = SolverAlgorithm::Dcfr.iteration(4);
116 assert_eq!(update.regret(-6.0, 15.0), 8.0);
118 assert_eq!(update.regret(6.0, -14.0), -4.0);
119 assert_eq!(update.regret(3.0, -3.0), 0.0);
120 assert!((update.strategy_sum(25.0) - 16.0).abs() < 1e-14);
121 }
122
123 #[test]
124 fn dcfr_multiple_updates_match_independent_scalar_recurrence() {
125 let mut regret = 0.0;
126 let mut strategy_sum = 0.0;
127 let mut expected_regret = 0.0;
128 let mut weighted_strategy_sum = 0.0;
129 for (i, (delta, contribution)) in [(8.0, 0.25), (-10.0, 0.0), (9.0, 0.75), (-2.0, 1.0)]
130 .into_iter()
131 .enumerate()
132 {
133 let t = (i + 1) as f64;
134 let accumulated = expected_regret + delta;
135 expected_regret = accumulated
136 * if accumulated > 0.0 {
137 (t * t.sqrt()) / (t * t.sqrt() + 1.0)
138 } else {
139 0.5
140 };
141 weighted_strategy_sum += t * t * contribution;
142 let update = SolverAlgorithm::Dcfr.iteration(i as u64 + 1);
143 regret = update.regret(regret, delta);
144 strategy_sum = update.strategy_sum(strategy_sum + contribution);
145 assert!((regret - expected_regret).abs() < 1e-14);
146 assert!((strategy_sum - weighted_strategy_sum / (t + 1.0).powi(2)).abs() < 1e-14);
147 }
148 }
149
150 #[test]
151 fn cfr_and_cfr_plus_keep_undiscounted_uniform_sums() {
152 assert_eq!(SolverAlgorithm::default(), SolverAlgorithm::Dcfr);
153 for t in [1, 2, 100, u64::MAX] {
154 let vanilla = SolverAlgorithm::Cfr.iteration(t);
155 let plus = SolverAlgorithm::CfrPlus.iteration(t);
156 assert_eq!(vanilla.regret(2.0, -5.0), -3.0);
157 assert_eq!(plus.regret(2.0, -5.0), 0.0);
158 assert_eq!(vanilla.strategy_sum(7.0), 7.0);
159 assert_eq!(plus.strategy_sum(7.0), 7.0);
160 }
161 }
162
163 #[test]
164 fn dcfr_late_iteration_coefficients_remain_finite() {
165 let update = SolverAlgorithm::Dcfr.iteration(u64::MAX);
166 assert!(update.regret(0.0, 1.0).is_finite());
167 assert_eq!(update.regret(0.0, -1.0), -0.5);
168 assert!(update.strategy_sum(1.0).is_finite());
169 }
170
171 #[test]
172 #[should_panic(expected = "CFR iteration numbers are one-based")]
173 fn zero_iteration_is_rejected() {
174 SolverAlgorithm::Dcfr.iteration(0);
175 }
176}