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
use crate::{CardDeck, CardDrawSequence, Probability, PROBABILITY_ONE, PROBABILITY_ZERO};
use itertools::Itertools;
use std::collections::BTreeMap;
use std::{
    fmt::{Display, Write},
    hash::Hash,
};

/// Prefix used for graphviz ids
const GRAPHVIZ_PREFIX: &str = "_";

/// A representation of a card drawing process.
///
/// # Type Parameters
/// - `C`: The type of a single card
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CardDrawTree<C>
where
    C: Eq + Hash + Ord,
{
    probability: Probability,
    probability_in_tree: Probability,
    nodes: BTreeMap<C, CardDrawTree<C>>,
}

impl<C> Default for CardDrawTree<C>
where
    C: Eq + Hash + Ord + Clone,
{
    fn default() -> Self {
        Self::new()
    }
}

impl<C> Display for CardDrawTree<C>
where
    C: Eq + Hash + Ord + Clone + Display,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            self.nodes
                .iter()
                .map(|x| format!(
                    "* {} ({})\n{}",
                    x.0,
                    x.1.probability_in_tree,
                    x.1.to_string()
                        .lines()
                        .map(|x| format!("\t{x}"))
                        .collect::<Vec<String>>()
                        .join("\n")
                ))
                .collect::<Vec<String>>()
                .join("\n")
        )
    }
}

impl<C> CardDrawTree<C>
where
    C: Eq + Hash + Ord + Clone,
{
    /// Creates a new empty tree.
    ///
    /// # Example
    ///
    /// ```
    /// use stochasta::CardDrawTree;
    ///
    /// let tree: CardDrawTree<i32> = CardDrawTree::new();
    /// assert!(tree.is_empty());
    /// ```
    #[must_use]
    pub fn new() -> Self {
        Self {
            probability: PROBABILITY_ONE,
            probability_in_tree: PROBABILITY_ONE,
            nodes: BTreeMap::new(),
        }
    }

    /// Creates a new empty tree node.
    #[must_use]
    fn new_node(probability: Probability, parent_probability: Probability) -> Self {
        Self {
            probability,
            probability_in_tree: parent_probability * probability,
            nodes: BTreeMap::<C, CardDrawTree<C>>::new(),
        }
    }

    /// Creates a tree with one level from the given card deck.
    ///
    /// # Example
    ///
    /// ```
    /// use stochasta::{CardDeck, CardDrawTree};
    ///
    /// let deck = CardDeck::from(vec!["heads", "tails"]);
    /// let tree = CardDrawTree::create_from(&deck);
    /// ```
    #[must_use]
    pub fn create_from(card_deck: &CardDeck<C>) -> Self {
        let mut tree = Self::new_node(PROBABILITY_ONE, PROBABILITY_ONE);
        for (card, probability) in card_deck.probabilities() {
            tree.nodes
                .insert(card.clone(), Self::new_node(probability, PROBABILITY_ONE));
        }
        tree
    }

    /// Creates a new tree with the number of `draws` with an unshrinking stack.
    ///
    /// The stack won't shrink from drawing cards;
    /// instead every drawn card is put back to the stack.
    ///
    /// For a shrinking deck, see [`Self::shrinking()`].
    #[must_use]
    pub fn without_shrinking(card_deck: &CardDeck<C>, draws: u32) -> Self {
        Self::without_shrinking_root_probability(card_deck, draws, PROBABILITY_ONE, PROBABILITY_ONE)
    }

    /// Creates a new tree with the number of `draws` with a shrinking stack.
    ///
    /// The stack will shrink from drawing cards;
    /// once a card is drawn it is no longer part of the stack.
    ///
    /// For a non-shrinking deck, see [`Self::without_shrinking()`].
    #[must_use]
    pub fn shrinking(card_deck: &CardDeck<C>, draws: u32) -> Self {
        Self::shrinking_root_probability(card_deck, draws, PROBABILITY_ONE, PROBABILITY_ONE)
    }

    fn without_shrinking_root_probability(
        card_deck: &CardDeck<C>,
        draws: u32,
        probability: Probability,
        parent_probability: Probability,
    ) -> Self {
        let mut tree = Self::new_node(probability, parent_probability);
        if 0 < draws {
            for (card, card_probability) in card_deck.probabilities() {
                tree.nodes.insert(
                    card.clone(),
                    Self::without_shrinking_root_probability(
                        card_deck,
                        draws - 1,
                        card_probability,
                        tree.probability_in_tree,
                    ),
                );
            }
        }
        tree
    }

    fn shrinking_root_probability(
        card_deck: &CardDeck<C>,
        draws: u32,
        probability: Probability,
        parent_probability: Probability,
    ) -> Self {
        let mut tree = Self::new_node(probability, parent_probability);
        if 0 < draws {
            for (card, card_probability) in card_deck.probabilities() {
                let new_stack = card_deck.draw(card.clone());
                tree.nodes.insert(
                    card.clone(),
                    Self::shrinking_root_probability(
                        &new_stack,
                        draws - 1,
                        card_probability,
                        tree.probability_in_tree,
                    ),
                );
            }
        }
        tree
    }

    /// Returns the probability of a certain sequence in the tree.
    ///
    /// The order is important as well as the position - the first entry will be searched among the
    /// root nodes.
    ///
    /// # Example
    ///
    /// ```
    /// use stochasta::{CardDeck, CardDrawTree, Probability, PROBABILITY_ONE, PROBABILITY_ZERO};
    ///
    /// let coin = CardDeck::from(vec!["H", "T"]);
    /// let tree = CardDrawTree::without_shrinking(&coin, 2);
    ///
    /// assert_eq!(tree.probability_of(&[]), PROBABILITY_ONE);
    /// assert_eq!(tree.probability_of(&["H"]), Probability::new(1, 2));
    /// assert_eq!(tree.probability_of(&["H", "H"]), Probability::new(1, 4));
    /// // 3x heads is impossible when only throwing 2x
    /// assert_eq!(tree.probability_of(&["H", "H", "H"]), PROBABILITY_ZERO);
    /// ```
    #[must_use]
    pub fn probability_of(&self, sequence: &[C]) -> Probability {
        if sequence.is_empty() {
            PROBABILITY_ONE
        } else if let Some(node) = self.nodes.get(&sequence[0]) {
            node.probability * node.probability_of(&sequence[1..])
        } else {
            PROBABILITY_ZERO
        }
    }

    /// Returns `true` if the tree has no nodes.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.nodes.is_empty()
    }

    /// Returns all paths.
    ///
    /// # Example
    ///
    /// ```
    /// use stochasta::{CardDeck, CardDrawTree, CardDrawSequence, Probability};
    ///
    /// let coin = CardDeck::from(vec!["H", "T"]);
    /// let tree = CardDrawTree::without_shrinking(&coin, 2);
    ///
    /// let result = tree.paths();
    /// let one_quarter = Probability::new(1, 4);
    ///
    /// assert_eq!(result.len(), 4);
    /// assert!(result.contains(&CardDrawSequence::new(vec!["H", "H"], one_quarter)));
    /// assert!(result.contains(&CardDrawSequence::new(vec!["H", "T"], one_quarter)));
    /// assert!(result.contains(&CardDrawSequence::new(vec!["T", "H"], one_quarter)));
    /// assert!(result.contains(&CardDrawSequence::new(vec!["T", "T"], one_quarter)));
    /// ```
    #[must_use]
    pub fn paths(&self) -> Vec<CardDrawSequence<C>> {
        self.create_paths(&[])
    }

    fn create_paths(&self, sequence: &[C]) -> Vec<CardDrawSequence<C>> {
        let mut result = Vec::new();

        if self.is_empty() {
            result.push(CardDrawSequence::new(
                Vec::from(sequence),
                self.probability_in_tree,
            ));
        } else {
            for (card, tree) in &self.nodes {
                let mut s = Vec::new();
                s.extend(sequence.iter().cloned());
                s.push(card.clone());
                result.extend(tree.create_paths(&s));
            }
        }

        result
    }
}

impl<C> CardDrawTree<C>
where
    C: Eq + Hash + Ord + Display,
{
    /// Creates a [Graphviz](https://www.graphviz.org/)-graph from the decision tree.
    ///
    /// # Example
    ///
    /// For a more interesting graph this example covers an oddly weighted coin where *heads* is
    /// twice as likely to be thrown as *tails*.
    ///
    /// ```
    /// use stochasta::{CardDeck, CardDrawTree};
    ///
    /// let odd_coin = CardDeck::from(vec!["heads", "heads", "tails"]);
    /// let tree = CardDrawTree::without_shrinking(&odd_coin, 2);
    /// let output = r#"digraph {
    /// _root[label="", shape="circle"];
    /// _root->_heads_2[label="2/3"];
    /// _heads_2[label="heads (2/3)"];
    /// _heads_2->_heads_3[label="2/3"];
    /// _heads_3[label="heads (4/9)"];
    /// _heads_2->_tails_4[label="1/3"];
    /// _tails_4[label="tails (2/9)"];
    /// _root->_tails_5[label="1/3"];
    /// _tails_5[label="tails (1/3)"];
    /// _tails_5->_heads_6[label="2/3"];
    /// _heads_6[label="heads (2/9)"];
    /// _tails_5->_tails_7[label="1/3"];
    /// _tails_7[label="tails (1/9)"];
    /// }"#;
    /// assert_eq!(tree.to_graphviz(), output);
    /// ```
    ///
    /// # ASCII Visualisation
    ///
    /// This will result in the following graph (here sideways for better visualisation):
    ///
    /// ```plain
    ///                         2/3
    ///                       +-----[ heads (4/9) ]
    ///       2/3            /
    ///     +-----[ heads (2/3) ]
    ///    /                 \  1/3
    ///   /                   +-----[ tails (2/9) ]
    ///  /
    /// O
    ///  \                      2/3
    ///   \                   +-----[ heads (2/9) ]
    ///    \  1/3            /
    ///     +-----[ tails (1/3) ]
    ///                      \  1/3
    ///                       +-----[ tails (1/9) ]
    ///```
    ///
    /// # Output
    ///
    /// - the paths have the probability from their parent node
    /// - the cards have additionally the total probability to reach it from the root node in
    ///   brackets
    #[must_use]
    pub fn to_graphviz(&self) -> String {
        let mut result = String::new();

        let root = "root";

        write!(
            result,
            "digraph {{\n{prefix}{root}[label=\"\", shape=\"circle\"];\n",
            prefix = GRAPHVIZ_PREFIX,
            root = root.replace(' ', "")
        )
        .expect("Not written");

        let (subtree, _) = self.to_graphviz_iter(root, 1);
        result.push_str(&subtree);

        result.push('}');
        result
    }

    fn to_graphviz_sub(&self, root: &str, card: &str, id: u32) -> (String, u32) {
        let mut result = String::new();
        let new_root = format!("{card}_{id}");

        write!(
            result,
            "{prefix}{root_id}->{prefix}{node_id}[label=\"{prob_edge}\"];\n\
             {prefix}{node_id}[label=\"{node_label} ({prob_node})\"];\n",
            prefix = GRAPHVIZ_PREFIX,
            root_id = root.replace(' ', ""),
            node_id = new_root.replace(' ', ""),
            node_label = card,
            prob_edge = self.probability,
            prob_node = self.probability_in_tree
        )
        .expect("Not written");

        let (subtree, new_id) = self.to_graphviz_iter(&new_root, id);
        result.push_str(&subtree);

        (result, new_id)
    }

    fn to_graphviz_iter(&self, root: &str, id: u32) -> (String, u32) {
        let mut result = String::new();
        let mut new_id = id;
        for (card, subtree) in self.nodes.iter().sorted_by_key(|&(c, _)| c) {
            let (graphviz, last_id) = subtree.to_graphviz_sub(root, &card.to_string(), new_id + 1);
            new_id = last_id;
            result.push_str(&graphviz);
        }
        (result, new_id)
    }
}

#[cfg(test)]
mod tests {
    use num_rational::Ratio;

    use super::*;

    #[test]
    fn probability_of_empty() {
        let tree: CardDrawTree<i32> = CardDrawTree::default();
        assert_eq!(tree.probability_of(&[]), PROBABILITY_ONE);
    }

    #[test]
    fn probability_of_coin() {
        let coin = CardDeck::from(vec!["heads", "tails"]);
        let tree = CardDrawTree::create_from(&coin);

        assert_eq!(tree.probability_of(&["heads"]), Probability::new(1, 2));
        assert_eq!(tree.probability_of(&["tails"]), Probability::new(1, 2));
        assert_eq!(tree.probability_of(&["side"]), PROBABILITY_ZERO);
        assert_eq!(tree.probability_of(&["heads", "tails"]), PROBABILITY_ZERO);
    }

    #[test]
    fn to_graphviz_empty() {
        let deck = CardDeck::<String>::new();
        let tree = CardDrawTree::without_shrinking(&deck, 1);
        let output = r#"digraph {
_root[label="", shape="circle"];
}"#;
        assert_eq!(tree.to_graphviz(), output);
    }

    #[test]
    fn to_graphviz_number() {
        let odd_coin = CardDeck::from(vec![7, 42]);
        let tree = CardDrawTree::without_shrinking(&odd_coin, 1);
        let output = r#"digraph {
_root[label="", shape="circle"];
_root->_7_2[label="1/2"];
_7_2[label="7 (1/2)"];
_root->_42_3[label="1/2"];
_42_3[label="42 (1/2)"];
}"#;
        assert_eq!(output, tree.to_graphviz());
    }

    #[test]
    fn shrinking_empty() {
        let deck: CardDeck<i32> = CardDeck::new();
        let tree = CardDrawTree::shrinking(&deck, 1);
        assert!(tree.is_empty());
    }

    #[test]
    fn shrinking_multiple_draws() {
        let deck = CardDeck::from(vec![1, 2, 3]);
        let tree = CardDrawTree::shrinking(&deck, 3);
        assert_eq!(tree.probability_of(&[1, 2, 1]), PROBABILITY_ZERO);
        assert_eq!(tree.probability_of(&[1, 2, 2]), PROBABILITY_ZERO);
        assert_eq!(tree.probability_of(&[1, 2, 3]), Probability::new(1, 6));
    }

    #[test]
    fn without_shrinking_multiple_draws() {
        let deck = CardDeck::from(vec![1, 2, 3]);
        let tree = CardDrawTree::without_shrinking(&deck, 3);
        assert_eq!(tree.probability_of(&[1, 2, 1]), Probability::new(1, 27));
        assert_eq!(tree.probability_of(&[1, 2, 2]), Probability::new(1, 27));
        assert_eq!(tree.probability_of(&[1, 2, 3]), Probability::new(1, 27));
    }

    #[test]
    fn shrinking_sum_resolves_to_one() {
        let deck = CardDeck::from(vec![1, 2, 3]);
        let tree = CardDrawTree::shrinking(&deck, 3);

        assert_eq!(
            tree.paths()
                .iter()
                .map(|x| x.probability().ratio())
                .sum::<Ratio<_>>(),
            Ratio::new(1, 1)
        );
    }

    #[test]
    fn without_shrinking_sum_resolves_to_one() {
        let deck = CardDeck::from(vec![1, 2, 3]);
        let tree = CardDrawTree::without_shrinking(&deck, 3);

        assert_eq!(
            tree.paths()
                .iter()
                .map(|x| x.probability().ratio())
                .sum::<Ratio<_>>(),
            Ratio::new(1, 1)
        );
    }

    #[test]
    fn tree_to_string() {
        let deck = CardDeck::from(vec![1, 2, 3]);
        let tree = CardDrawTree::shrinking(&deck, 3);
        assert_eq!(
            r"* 1 (1/3)
	* 2 (1/6)
		* 3 (1/6)
	* 3 (1/6)
		* 2 (1/6)
* 2 (1/3)
	* 1 (1/6)
		* 3 (1/6)
	* 3 (1/6)
		* 1 (1/6)
* 3 (1/3)
	* 1 (1/6)
		* 2 (1/6)
	* 2 (1/6)
		* 1 (1/6)",
            tree.to_string()
        );
    }
}