Skip to main content

native_solver/solver/
discount.rs

1//! Game-independent regret and average-strategy update policies.
2//!
3//! DCFR follows Brown and Sandholm (AAAI 2019), "Solving
4//! Imperfect-Information Games via Discounted Regret Minimization",
5//! §Regret Discounting: <https://doi.org/10.1609/aaai.v33i01.33011829>.
6//! After the one-based iteration `t`, add this iteration's contributions,
7//! then discount accumulated positive regret by `t^1.5 / (t^1.5 + 1)`,
8//! negative regret by `1/2`, and accumulated strategy by `(t/(t+1))^2`.
9//! The regret sign is tested **after** adding the instantaneous regret.
10//! This end-of-iteration convention gives strategy contribution `s` weight
11//! `s^2 / (T+1)^2` after `T` iterations, hence relative weights `s^2`.
12
13/// The regret and average-strategy update rule; independent of the game tree.
14///
15/// The player-update order is chosen separately with [`UpdateSchedule`].
16/// [`Dcfr`](Self::Dcfr), the default, usually converges fastest.
17#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
18#[non_exhaustive]
19pub enum SolverAlgorithm {
20    /// Vanilla regret matching and uniformly weighted average strategies.
21    Cfr,
22    /// Regret matching+ (accumulated regrets clipped at zero) with uniformly
23    /// weighted average strategies and no averaging delay.
24    ///
25    /// This is not the linearly weighted CFR+ of the original paper, nor the
26    /// quadratically weighted CFR+ baseline used in the DCFR paper.
27    CfrPlus,
28    /// Discounted CFR with fixed `(alpha, beta, gamma) = (1.5, 0, 2)`.
29    ///
30    /// Negative accumulated regrets are discounted, not clipped to zero.
31    #[default]
32    Dcfr,
33}
34
35/// Player update order, independent of regret discounting and averaging.
36#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
37pub enum UpdateSchedule {
38    /// Both players update against the same pre-iteration strategy profile.
39    #[default]
40    Simultaneous,
41    /// One iteration updates OOP, then IP against the updated OOP strategy.
42    ///
43    /// Each player's regrets and average strategy are updated exactly once.
44    /// This uses two full traversals, each averaging by the updating player's
45    /// own reach.
46    Alternating,
47}
48
49/// Coefficients are computed once per global iteration, not per infoset.
50#[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    /// Apply exactly one infoset/action update after its regret contributions
77    /// have been reduced. CFR+ clips at zero only after the whole delta is
78    /// added.
79    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    /// Called once after **all** this iteration's reach-weighted strategy
91    /// contributions to a bucket/action have been added. Discounting per
92    /// combo would incorrectly discount large buckets more often.
93    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        // 4^1.5 = 8, beta=0 => 1/2, (4/5)^2 = 0.64.
117        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}